Skip to content

Frankablu/TransactionEngineInterview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Transaction Engine

Usage

cargo run -- full_example.csv > output.csv

cargo run -- large_example.csv > output.csv

Introduction

This is a complete implementation of the transaction engine.

The heart of the transaction engine lives in account.rs and relies on typing and 550 lines of unit tests to verify that it's correct. accounts.rs stores an account per client.

Parsing the input is in transaction_record.rs and uses the standard Rust csv crate with relaxed parsing.

Error are defined using the thiserror crate.

Monetary values use the rust_decimal crate, which enlarges both the range and precision to covers plausible values for fiat currency.

Program structure

So main() does the following:

  1. Reads in the input file using a csv reader with transaction_record.rs (csv format) and transaction.rs (in-memory format).
  2. Applies the transactions using account.rs
  3. Prints the final account state to stdout using accounts.rs

User account state is handled by account.rs and it contains a function per transaction type to apply the transaction to the account.

Tests

Unit tests can be run with:

cargo test

15 tests focuses on applying transactions to accounts.

2 tests focus on testing the whole engine from start to finish, they use BufReader and BufWriter to simulate reading from a file and writing to stdout. These 2 tests live in main.rs.

full_example.csv and large_example.csv are in the repository for manual testing.

Performance

For 120000 transactions, the engine takes ~345 ms on my computer. Correctness has been the main focus so the only optimization is using BufReader/BufWriter which is also used for unit testing the code.

A csv reader is used to stream the input data so it does not need to all be loaded in memory at the same time.

Error handling

Custom error types are defined using the thiserror crate. Faulty transactions result in an error messaging being written to stderr. The engine skips the bad transaction after omitting an error and continues with the next transaction.

Development

No AI usage. Developed in RustRover with Auto Line Complete disabled.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages