Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aurora block hashchain #705

Closed
wants to merge 85 commits into from
Closed
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
096067e
Comments
Casuso Feb 16, 2023
5572a74
hashchain.rs file
Casuso Feb 16, 2023
0d08ffe
Direct parameters for hashing
Casuso Feb 16, 2023
8e064c2
Adding more comments
Casuso Feb 16, 2023
50ee8dc
More comments
Casuso Feb 16, 2023
6f0f155
Merkle tree tests
Casuso Feb 16, 2023
92385dc
Clippy
Casuso Feb 16, 2023
0c184e7
Use RawH256 instead of H256
Casuso Feb 16, 2023
4894c0b
Borsh
Casuso Feb 16, 2023
ef60aa4
Using RawH256 instead of H256
Casuso Feb 16, 2023
6c034d6
Block Hashchain Computer tests
Casuso Feb 17, 2023
a7d5a1b
Misspellings
Casuso Feb 17, 2023
f917b56
Cargo fmt
Casuso Feb 17, 2023
06b59be
Including chain id on hashchain and using standard contrac account id…
Casuso Feb 21, 2023
650aa3f
Blockchain Hashchain
Casuso Feb 22, 2023
ef73aad
Blockchain Hashchain tests
Casuso Feb 23, 2023
e7ad969
Simplifiying add_block_tx
Casuso Feb 23, 2023
eb4efd5
Storage get and set
Casuso Feb 23, 2023
c457b75
function update_hashchain
Casuso Feb 23, 2023
d97a376
lib.rs
Casuso Feb 24, 2023
1f41ac1
Format
Casuso Feb 24, 2023
68a0356
Fixing the build
Casuso Feb 27, 2023
860361e
Fixes
Casuso Feb 27, 2023
512c8bf
Solving comments. Fix for when get_previous_block_hashchain is called…
Casuso Feb 27, 2023
f28eb84
Revert "Fixes"
Casuso Feb 27, 2023
6d70f96
Revert "Solving comments. Fix for when get_previous_block_hashchain i…
Casuso Feb 27, 2023
aaa1630
Adding fixes again
Casuso Feb 27, 2023
bfd4555
Adding hashchain logic to entry point methods of Aurora block txs.
Casuso Feb 27, 2023
c949515
Using &[] instead of &[0; 0]
Casuso Feb 28, 2023
f487f74
Returnin tuple <height, hashchain> for get_previous_block_hashchain
Casuso Feb 28, 2023
f0b9158
Bringing InnerTransactionKind from borealis-engine-lib
Casuso Mar 14, 2023
45a969d
pub mod hashchain so it can be consumed from the standalone
Casuso Mar 15, 2023
72de295
Merge and fix conflicts: submit_with_args and set_owner
Casuso Mar 15, 2023
f6f308c
V1 Adding hashchain logic to standalone
Casuso Mar 16, 2023
4603df2
V1 Fixed
Casuso Mar 16, 2023
b09ee43
Revert "V1 Fixed"
Casuso Mar 17, 2023
9e60481
Revert "V1 Adding hashchain logic to standalone"
Casuso Mar 17, 2023
e2f8af8
V2 Adding hashchain to the standalone.
Casuso Mar 17, 2023
6edf400
Hashchain for and methods.
Casuso Mar 17, 2023
4b67211
Format
Casuso Mar 17, 2023
d070a43
Removing inner hashs in favor of lenght or nothing.
Casuso Mar 21, 2023
90fd38a
Fix test
Casuso Mar 21, 2023
90cc57e
Adding hashchain output to refund_on_error
Casuso Mar 22, 2023
a419adc
Adding Bloom implementation from Refiner.
Casuso Mar 23, 2023
ee7ffa1
Bloom fixes and integration
Casuso Mar 24, 2023
282c4d1
Standalone state matching 1
Casuso Apr 5, 2023
1b39802
merge with develop
Casuso Apr 6, 2023
9a90289
Fix build affter merging with develop.
Casuso Apr 6, 2023
24286b3
Engine top level methods changes to respond correctly to errors.
Casuso Apr 25, 2023
73f3db2
Merge with develop
Casuso Apr 26, 2023
156ee85
Printing and Logging hashchain from Standalone and Engine (wasm)
Casuso Apr 27, 2023
3d6b387
Hashchain activation-deactivation feature for testing. Fixen tests. A…
Casuso May 10, 2023
7701d62
Fix Cargo file
Casuso May 10, 2023
310b660
Merge and fix tests
Casuso May 11, 2023
9f009fc
Clippy fixes
Casuso May 12, 2023
ace7699
Merge with develop and fix problems.
Casuso May 12, 2023
379767d
Clippy and Format
Casuso May 12, 2023
1a76cf7
Clippy
Casuso May 12, 2023
18d3161
Merge develop
Casuso Jun 13, 2023
32dd4e1
Fix tests build and tests
Casuso Jun 13, 2023
6f1fcdf
Format
Casuso Jun 14, 2023
61ea643
Clippy and other errors.
Casuso Jun 14, 2023
33f3768
Format
Casuso Jun 14, 2023
5687d31
Start hashchain mechanism on the contract.
Casuso Jun 28, 2023
7610999
Starting hashchain mechanism.
Casuso Jul 5, 2023
edb02b8
Cancel hashchain mechanism.
Casuso Jul 5, 2023
31c76c3
Clippy
Casuso Jul 5, 2023
ea2e3f6
Merge branch 'develop' into aurora_block_hashchain
Casuso Jul 5, 2023
4c7c039
Fix merge.
Casuso Jul 5, 2023
5f7f19c
Ignore function_name on udeps.
Casuso Jul 5, 2023
26e7d54
Merge and fix build.
Casuso Jul 13, 2023
5f514a5
Adding Hashchain to Pause and Resume.
Casuso Jul 13, 2023
eeacbd5
Adding resume logic in start hashchain.
Casuso Jul 13, 2023
0d220e8
Comment.
Casuso Jul 13, 2023
eef8c46
Adding resume check in pause test.
Casuso Jul 14, 2023
735b40f
Remove borsh direct dependency and use aurora-engine-types borsh.
Casuso Jul 18, 2023
2d13411
Remove contract_account_id and method_name from hashchain computation.
Casuso Jul 18, 2023
692ddb5
Revert "Remove contract_account_id and method_name from hashchain com…
Casuso Jul 24, 2023
47f375c
Merge with develop.
Casuso Jul 26, 2023
547c1f3
Fix build.
Casuso Jul 26, 2023
e5d26b8
Format
Casuso Jul 26, 2023
8615c40
Adding hashchain to relayer_key methods.
Casuso Jul 26, 2023
0a53470
Solving some PR comments.
Casuso Jul 27, 2023
5b86749
Clippy and Format.
Casuso Jul 27, 2023
5480f08
Merge branch 'develop' into aurora_block_hashchain
Casuso Jul 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env/mainnet.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CARGO_FEATURES_BUILD="mainnet"
CARGO_FEATURES_BUILD_TEST="mainnet,integration-test"
CARGO_FEATURES_TEST="mainnet-test"
CARGO_FEATURES_TEST="mainnet-test,integration-test"
RUSTC_FLAGS_BUILD="-C link-arg=-s"
NEAR_EVM_ACCOUNT="aurora"
WASM_FILE="aurora-mainnet.wasm"
Expand Down
2 changes: 1 addition & 1 deletion .env/testnet.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CARGO_FEATURES_BUILD="testnet"
CARGO_FEATURES_BUILD_TEST="testnet,integration-test"
CARGO_FEATURES_TEST="testnet-test"
CARGO_FEATURES_TEST="testnet-test,integration-test"
RUSTC_FLAGS_BUILD="-C link-arg=-s"
NEAR_EVM_ACCOUNT="aurora"
WASM_FILE="aurora-testnet.wasm"
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ evm = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.0-
evm-core = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.0-aurora", default-features = false, features = ["std"] }
evm-gasometer = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.0-aurora", default-features = false, features = ["std", "tracing"] }
evm-runtime = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.0-aurora", default-features = false, features = ["std", "tracing"] }
fixed-hash = { version = "0.8.0", default-features = false}
git2 = "0.17"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
ibig = { version = "0.3", default-features = false, features = ["num-traits"] }
impl-serde = { version = "0.4.0", default-features = false}
libsecp256k1 = { version = "0.7", default-features = false }
near-crypto = "0.16"
near-primitives = "0.16"
Expand All @@ -54,6 +56,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.24.1", features = [ "derive" ] }
tempfile = "3"
test-case = "3.1"
walrus = "0.20"
Expand Down
2 changes: 2 additions & 0 deletions engine-standalone-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ aurora-engine-modexp = { workspace = true, features = ["std"] }
aurora-engine-precompiles = { workspace = true, features = ["std"] }
aurora-engine-sdk = { workspace = true, features = ["std"] }
aurora-engine-transactions = { workspace = true, features = ["std"] }
borsh.workspace = true
evm-core.workspace = true
hex = { workspace = true, features = ["std"] }
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
161 changes: 154 additions & 7 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use aurora_engine::bloom::{self, Bloom};
use aurora_engine::hashchain::{
self, blockchain_hashchain_error::BlockchainHashchainError, BlockchainHashchain,
};
use aurora_engine::parameters::SubmitArgs;
use aurora_engine::pausables::{
EnginePrecompilesPauser, PausedPrecompilesManager, PrecompileFlags,
Expand All @@ -7,6 +11,7 @@ use aurora_engine_modexp::ModExpAlgorithm;
use aurora_engine_sdk::env::{self, Env, DEFAULT_PREPAID_GAS};
use aurora_engine_types::{
account_id::AccountId,
borsh::BorshSerialize,
parameters::PromiseWithCallbackArgs,
types::{Address, Yocto},
H256,
Expand All @@ -18,6 +23,8 @@ use crate::engine_state::EngineStateAccess;
use crate::{BlockMetadata, Diff, Storage};
use types::{Message, TransactionKind, TransactionMessage};

use self::types::InnerTransactionKind;

pub fn consume_message<M: ModExpAlgorithm + 'static>(
storage: &mut Storage,
message: Message,
Expand Down Expand Up @@ -51,7 +58,7 @@ pub fn consume_message<M: ModExpAlgorithm + 'static>(
transaction_message.as_ref(),
block_height,
&block_metadata,
engine_account_id,
&engine_account_id,
io,
)
})
Expand Down Expand Up @@ -87,7 +94,7 @@ pub fn execute_transaction_message<M: ModExpAlgorithm + 'static>(
&transaction_message,
block_height,
&block_metadata,
engine_account_id,
&engine_account_id,
io,
)
});
Expand All @@ -105,8 +112,8 @@ fn execute_transaction<'db, M: ModExpAlgorithm + 'static>(
transaction_message: &TransactionMessage,
block_height: u64,
block_metadata: &BlockMetadata,
engine_account_id: AccountId,
io: EngineStateAccess<'db, 'db, 'db>,
engine_account_id: &AccountId,
mut io: EngineStateAccess<'db, 'db, 'db>,
) -> (
H256,
Diff,
Expand All @@ -117,7 +124,7 @@ fn execute_transaction<'db, M: ModExpAlgorithm + 'static>(
let relayer_address =
aurora_engine_sdk::types::near_account_to_evm_address(predecessor_account_id.as_bytes());
let near_receipt_id = transaction_message.near_receipt_id;
let current_account_id = engine_account_id;
let current_account_id = engine_account_id.clone();
Casuso marked this conversation as resolved.
Show resolved Hide resolved
let env = env::Fixed {
signer_account_id,
current_account_id,
Expand All @@ -129,7 +136,7 @@ fn execute_transaction<'db, M: ModExpAlgorithm + 'static>(
prepaid_gas: DEFAULT_PREPAID_GAS,
};

let (tx_hash, result) = match &transaction_message.transaction {
let (tx_hash, mut result) = match &transaction_message.transaction {
TransactionKind::Submit(tx) => {
// We can ignore promises in the standalone engine because it processes each receipt separately
// and it is fed a stream of receipts (it does not schedule them)
Expand Down Expand Up @@ -193,6 +200,17 @@ fn execute_transaction<'db, M: ModExpAlgorithm + 'static>(
}
};

if let Ok(option_result) = &result {
if let Err(e) = update_hashchain(
&mut io,
block_height,
&transaction_message.transaction,
option_result,
) {
result = Err(e);
}
}

let diff = io.get_transaction_diff();

(tx_hash, diff, result)
Expand Down Expand Up @@ -469,13 +487,127 @@ fn non_submit_execute<'db, M: ModExpAlgorithm + 'static>(
prev.is_paused = false;
state::set_state(&mut io, &prev)?;

None
}
TransactionKind::StartHashchain(args) => {
let mut prev = state::get_state(&io)?;
let block_height = env.block_height;

let mut blockchain_hashchain = BlockchainHashchain::new(
prev.chain_id,
env.current_account_id.as_bytes().to_vec(),
args.block_height + 1,
args.block_hashchain,
);

// move hashchain from the args state to the current state
if block_height > blockchain_hashchain.get_current_block_height() {
blockchain_hashchain.move_to_block(block_height)?;
}

hashchain::storage::set_state(&mut io, &blockchain_hashchain)?;
prev.is_paused = false;
state::set_state(&mut io, &prev)?;

None
}
};

Ok(result)
}

fn update_hashchain<'db>(
io: &mut EngineStateAccess<'db, 'db, 'db>,
block_height: u64,
transaction: &TransactionKind,
result: &Option<TransactionExecutionResult>,
) -> Result<(), error::Error> {
let hashchain_state = hashchain::storage::get_state(io);

if matches!(hashchain_state, Err(BlockchainHashchainError::NotFound)) {
return Ok(());
}

let mut blockchain_hashchain = hashchain_state?;

let method_name = InnerTransactionKind::from(transaction).to_string();
let input = get_input(transaction)?;
let (output, log_bloom) = get_output_and_log_bloom(result)?;

if block_height > blockchain_hashchain.get_current_block_height() {
blockchain_hashchain.move_to_block(block_height)?;
}

blockchain_hashchain.add_block_tx(block_height, &method_name, &input, &output, &log_bloom)?;

Ok(hashchain::storage::set_state(io, &blockchain_hashchain)?)
}

fn get_input(transaction: &TransactionKind) -> Result<Vec<u8>, error::Error> {
match transaction {
TransactionKind::Submit(tx) => Ok(tx.into()),
TransactionKind::SubmitWithArgs(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::Call(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::PausePrecompiles(args) | TransactionKind::ResumePrecompiles(args) => {
args.try_to_vec().map_err(Into::into)
}
TransactionKind::Deploy(input) => Ok(input.clone()),
TransactionKind::DeployErc20(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::FtOnTransfer(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::Deposit(raw_proof) => Ok(raw_proof.clone()),
TransactionKind::FtTransferCall(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::FinishDeposit(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::ResolveTransfer(args, _) => args.try_to_vec().map_err(Into::into),
TransactionKind::FtTransfer(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::Withdraw(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::StorageDeposit(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::StorageUnregister(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::StorageWithdraw(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::SetOwner(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::SetPausedFlags(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::RegisterRelayer(evm_address) => Ok(evm_address.as_bytes().to_vec()),
TransactionKind::RefundOnError(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::SetConnectorData(args) | TransactionKind::NewConnector(args) => {
args.try_to_vec().map_err(Into::into)
}
TransactionKind::NewEngine(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::FactoryUpdate(bytecode) => Ok(bytecode.clone()),
TransactionKind::FactoryUpdateAddressVersion(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::FactorySetWNearAddress(address) => Ok(address.as_bytes().to_vec()),
TransactionKind::FundXccSubAccound(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::SetUpgradeDelayBlocks(args) => args.try_to_vec().map_err(Into::into),
TransactionKind::PauseContract
| TransactionKind::ResumeContract
| TransactionKind::Unknown => Ok(vec![]),
TransactionKind::StartHashchain(args) => args.try_to_vec().map_err(Into::into),
}
}

#[allow(clippy::option_if_let_else)]
fn get_output_and_log_bloom(
result: &Option<TransactionExecutionResult>,
) -> Result<(Vec<u8>, Bloom), error::Error> {
match result {
None => Ok((vec![], Bloom::default())),
Some(execution_result) => match execution_result {
TransactionExecutionResult::Promise(_) => Ok((vec![], Bloom::default())),
TransactionExecutionResult::DeployErc20(address) => {
Ok((address.as_bytes().try_to_vec().unwrap(), Bloom::default()))
}
TransactionExecutionResult::Submit(submit) => match submit {
Err(e) => Err(e.clone().into()),
Ok(submit_result) => {
let result_output = submit_result.try_to_vec();
match result_output {
Err(e) => Err(e.into()),
Ok(output) => Ok((output, bloom::get_logs_bloom(&submit_result.logs))),
}
}
},
},
}
}

#[derive(Debug)]
pub enum ConsumeMessageOutcome {
BlockAdded,
Expand All @@ -499,7 +631,8 @@ pub enum TransactionExecutionResult {
}

pub mod error {
use aurora_engine::{connector, engine, fungible_token, state, xcc};
use aurora_engine::{connector, engine, fungible_token, hashchain, state, xcc};
use std::io;

#[derive(Debug)]
pub enum Error {
Expand All @@ -516,6 +649,8 @@ pub mod error {
ConnectorInit(connector::error::InitContractError),
ConnectorStorage(connector::error::StorageReadError),
FundXccError(xcc::FundXccError),
BlockchainHashchain(hashchain::blockchain_hashchain_error::BlockchainHashchainError),
IO(io::Error),
}

impl From<state::EngineStateError> for Error {
Expand Down Expand Up @@ -595,4 +730,16 @@ pub mod error {
Self::FundXccError(e)
}
}

impl From<hashchain::blockchain_hashchain_error::BlockchainHashchainError> for Error {
fn from(e: hashchain::blockchain_hashchain_error::BlockchainHashchainError) -> Self {
Self::BlockchainHashchain(e)
}
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Self::IO(e)
}
}
}
Loading
Loading