Skip to content

Commit

Permalink
Feat(standalone): logic for parsing TransactionKind from raw Near data (
Browse files Browse the repository at this point in the history
#810)

## Description

This is the first in a series of PRs that is meant to split up #705 .
The idea is to merge the changes which are made in that PR in logical
chunks until eventually the whole hashchain implementation is in. Doing
the work in smaller pieces will both make it easier to review and
prevent us from needing to maintain large, long-lived feature branches.

This first PR pulls in the transaction transaction parsing logic from
[borealis-engine-lib](https://github.com/aurora-is-near/borealis-engine-lib)
into this repo (in a future PR we will remove the duplicated code from
borealis-engine-lib). The logic is used here to simplify how
transactions are handled in tests because all transactions can
automatically be passed to both the Near runtime (processed by the
Engine as Wasm) and the standalone engine. In particular we remove the
large `if` statement that was starting to get unwieldy.

This work is important both because it makes future tests easier to
write and because it synchronizes the standalone and wasm engine
instances in our tests (this latter point is a prerequisite for properly
testing the hashchain).

Some notes about the PR:

1. I renamed the constant `ORIGIN` to `DEFAULT_AURORA_ACCOUNT_ID`
because I felt the latter is a more descriptive name for what the
constant represents.
2. The standalone engine is now present in `AuroraRunner` by default to
make out testing more robust (for example tests will now automatically
fail if a new state-mutating method is added to the Engine's `lib.rs`
without also being added to the standalone implementation). This means
there are a few places were I need to explicitly remove the standalone
instance where `AuroraRunner` is used as something other than an Engine
instance (modexp and xcc tests).
3. Some tests use view calls to inspect the Engine state and make
assertions. These calls are not present in the standalone because it
only cares about transactions that mutate the state. To make view calls
in `AuroraRunner` it is now required to call `.one_shot()` before the
calls. This essentially tells the testing framework that you are making
a view call so no state modifications will be made and therefore we can
ignore the standalone.
  • Loading branch information
birchmd authored Aug 3, 2023
1 parent 0d0dfb7 commit 2e1f1a0
Show file tree
Hide file tree
Showing 17 changed files with 552 additions and 289 deletions.
43 changes: 33 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ serde = { version = "1", default-features = false, features = ["alloc", "derive"
serde_json = { version = "1", default-features = false, features = ["alloc"] }
sha2 = { version = "0.10", default-features = false }
sha3 = { version = "0.10", default-features = false }
strum = { version = "0.25", features = ["derive"] }
tempfile = "3"
tokio = { version = "1", default-features = false, features = ["macros"] }
test-case = "3.1"
Expand Down
1 change: 1 addition & 0 deletions engine-standalone-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rocksdb.workspace = true
postgres.workspace = true
serde = { workspace = true, features = ["std"] }
serde_json = { workspace = true, features = ["std"] }
strum.workspace = true

[features]
default = ["snappy", "lz4", "zstd", "zlib"]
Expand Down
48 changes: 46 additions & 2 deletions engine-standalone-storage/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{sync::types::TransactionKindTag, TransactionIncluded};
use aurora_engine_types::H256;

use crate::TransactionIncluded;
use std::fmt;

#[derive(Debug, PartialEq, Clone)]
pub enum Error {
Expand All @@ -25,3 +25,47 @@ impl From<std::io::Error> for Error {
Self::Borsh(e.to_string())
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseTransactionKindError {
UnknownMethodName {
name: String,
},
FailedDeserialization {
transaction_kind_tag: TransactionKindTag,
error_message: String,
},
}

impl ParseTransactionKindError {
pub fn failed_deserialization<E: fmt::Debug>(
tag: TransactionKindTag,
error: Option<E>,
) -> Self {
Self::FailedDeserialization {
transaction_kind_tag: tag,
error_message: error.map(|e| format!("{e:?}")).unwrap_or_default(),
}
}
}

impl fmt::Display for ParseTransactionKindError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownMethodName { name } => {
write!(
f,
"Error parsing transaction kind: Unknown method name {name}"
)
}
Self::FailedDeserialization {
transaction_kind_tag,
error_message,
} => {
write!(f, "Error deserializing args for transaction of kind {transaction_kind_tag:?}. Error message: {error_message:?}")
}
}
}
}

impl std::error::Error for ParseTransactionKindError {}
Loading

0 comments on commit 2e1f1a0

Please sign in to comment.