Skip to content

Commit

Permalink
Feat(engine): Hashchain integration (#831)
Browse files Browse the repository at this point in the history
## Description

This PR integrates the hashchain feature into the Aurora Engine contract
(see aurora-is-near/AIPs#8). This change is
fully backwards compatible because by default there is no hashchain
present in the contract state and therefore no hashchain computation is
done.

The method to activate the hashchain is `start_hashchain` requires the
contract to be paused and can only be called by a privileged account (I
chose to use the key manager instead of introducing a new role). These
restrictions are necessary because the hashchain is initialized with a
value based on the Engine's transaction history (to enable validating
Aurora blocks starting from the deployment of the contract).

## Performance / NEAR gas cost considerations

After activating the hashchain there will be a small increase in gas
usage because of the hashchain computation. It's not very much for most
transactions (as seen in the performance regression tests), but the
amount of gas is proportional to the size of the input + output of the
transaction (because we compute a hash from this data).

## Testing

The tests have been changed to enable the hashchain by default.
Therefore all existing tests are running the hashchain code and testing
that the Wasm and Standalone results match (this is important because
the Refiner will be computing hashchain values off-chain using the
Standalone Engine). An additional test has been added for the hashchain
feature itself.

## Additional information

Note: the diff in `contract_methods` is relatively large, but the code
changes are minimal. All that happened is the existing function bodies
were wrapped in `with_hashchain`, which changed the indentation. Viewing
the PR with whitespace changes ignored may make this section easier to
review.
  • Loading branch information
birchmd committed Sep 1, 2023
1 parent c3fc42a commit 71980db
Show file tree
Hide file tree
Showing 31 changed files with 1,224 additions and 582 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false

[workspace.dependencies]
aurora-engine = { path = "engine", default-features = false }
aurora-engine-hashchain = { path = "engine-hashchain", default-features = false }
aurora-engine-precompiles = { path = "engine-precompiles", default-features = false }
aurora-engine-sdk = { path = "engine-sdk", default-features = false }
aurora-engine-transactions = { path = "engine-transactions", default-features = false }
Expand Down Expand Up @@ -36,6 +37,7 @@ evm-core = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.
evm-gasometer = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.3-aurora", default-features = false, features = ["std", "tracing"] }
evm-runtime = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.38.3-aurora", default-features = false, features = ["std", "tracing"] }
fixed-hash = { version = "0.8.0", default-features = false}
function_name = "0.3.0"
git2 = "0.17"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
ibig = { version = "0.3", default-features = false, features = ["num-traits"] }
Expand Down
1 change: 1 addition & 0 deletions engine-hashchain/src/hashchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use aurora_engine_sdk::keccak;
use aurora_engine_types::{
account_id::AccountId,
borsh::{self, maybestd::io, BorshDeserialize, BorshSerialize},
format,
types::RawH256,
Cow, Vec,
};
Expand Down
3 changes: 3 additions & 0 deletions engine-hashchain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod bloom;
pub mod error;
pub mod hashchain;
pub mod merkle;
#[cfg(test)]
mod tests;
pub mod wrapped_io;
116 changes: 116 additions & 0 deletions engine-hashchain/src/wrapped_io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! This module contains `CachedIO`, a light wrapper over any IO instance
//! which will cache the input read and output written by the underlying instance.
//! It has no impact on the storage access functions of the trait.
//! The purpose of this struct is to capture the input and output from the underlying
//! IO instance for the purpose of passing it to `Hashchain::add_block_tx`.

use aurora_engine_sdk::io::{StorageIntermediate, IO};
use aurora_engine_types::Vec;
use core::cell::RefCell;

#[derive(Debug, Clone, Copy)]
pub struct CachedIO<'cache, I> {
inner: I,
cache: &'cache RefCell<IOCache>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WrappedInput<T> {
Input(Vec<u8>),
Wrapped(T),
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct IOCache {
pub input: Vec<u8>,
pub output: Vec<u8>,
}

impl<'cache, I> CachedIO<'cache, I> {
pub fn new(io: I, cache: &'cache RefCell<IOCache>) -> Self {
Self { inner: io, cache }
}
}

impl IOCache {
pub fn set_input(&mut self, value: Vec<u8>) {
self.input = value;
}

pub fn set_output(&mut self, value: Vec<u8>) {
self.output = value;
}
}

impl<T: StorageIntermediate> StorageIntermediate for WrappedInput<T> {
fn len(&self) -> usize {
match self {
Self::Input(bytes) => bytes.len(),
Self::Wrapped(x) => x.len(),
}
}

fn is_empty(&self) -> bool {
match self {
Self::Input(bytes) => bytes.is_empty(),
Self::Wrapped(x) => x.is_empty(),
}
}

fn copy_to_slice(&self, buffer: &mut [u8]) {
match self {
Self::Input(bytes) => buffer.copy_from_slice(bytes),
Self::Wrapped(x) => x.copy_to_slice(buffer),
}
}
}

impl<'cache, I: IO> IO for CachedIO<'cache, I> {
type StorageValue = WrappedInput<I::StorageValue>;

fn read_input(&self) -> Self::StorageValue {
let input = self.inner.read_input().to_vec();
self.cache.borrow_mut().set_input(input.clone());
WrappedInput::Input(input)
}

fn return_output(&mut self, value: &[u8]) {
self.cache.borrow_mut().set_output(value.to_vec());
self.inner.return_output(value);
}

fn read_storage(&self, key: &[u8]) -> Option<Self::StorageValue> {
self.inner.read_storage(key).map(WrappedInput::Wrapped)
}

fn storage_has_key(&self, key: &[u8]) -> bool {
self.inner.storage_has_key(key)
}

fn write_storage(&mut self, key: &[u8], value: &[u8]) -> Option<Self::StorageValue> {
self.inner
.write_storage(key, value)
.map(WrappedInput::Wrapped)
}

fn write_storage_direct(
&mut self,
key: &[u8],
value: Self::StorageValue,
) -> Option<Self::StorageValue> {
match value {
WrappedInput::Wrapped(x) => self
.inner
.write_storage_direct(key, x)
.map(WrappedInput::Wrapped),
WrappedInput::Input(bytes) => self
.inner
.write_storage(key, &bytes)
.map(WrappedInput::Wrapped),
}
}

fn remove_storage(&mut self, key: &[u8]) -> Option<Self::StorageValue> {
self.inner.remove_storage(key).map(WrappedInput::Wrapped)
}
}
2 changes: 1 addition & 1 deletion engine-precompiles/src/promise_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod costs {
/// This cost is always charged for calling this precompile.
pub const PROMISE_RESULT_BASE_COST: EthGas = EthGas::new(111);
/// This is the cost per byte of promise result data.
pub const PROMISE_RESULT_BYTE_COST: EthGas = EthGas::new(1);
pub const PROMISE_RESULT_BYTE_COST: EthGas = EthGas::new(2);
}

pub struct PromiseResult<H> {
Expand Down
20 changes: 16 additions & 4 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ pub fn parse_transaction_kind(
TransactionKindTag::PauseContract => TransactionKind::PauseContract,
TransactionKindTag::ResumeContract => TransactionKind::ResumeContract,
TransactionKindTag::SetKeyManager => {
let args = aurora_engine::parameters::RelayerKeyManagerArgs::try_from_slice(&bytes)
.map_err(f)?;
let args: parameters::RelayerKeyManagerArgs = serde_json::from_slice(bytes.as_slice())
.map_err(|e| {
ParseTransactionKindError::failed_deserialization(tx_kind_tag, Some(e))
})?;
TransactionKind::SetKeyManager(args)
}
TransactionKindTag::AddRelayerKey => {
Expand All @@ -212,6 +214,11 @@ pub fn parse_transaction_kind(
aurora_engine::parameters::RelayerKeyArgs::try_from_slice(&bytes).map_err(f)?;
TransactionKind::RemoveRelayerKey(args)
}
TransactionKindTag::StartHashchain => {
let args =
aurora_engine::parameters::StartHashchainArgs::try_from_slice(&bytes).map_err(f)?;
TransactionKind::StartHashchain(args)
}
TransactionKindTag::Unknown => {
return Err(ParseTransactionKindError::UnknownMethodName {
name: method_name.into(),
Expand Down Expand Up @@ -523,7 +530,7 @@ fn non_submit_execute<I: IO + Copy>(
None
}
TransactionKind::NewEngine(_) => {
contract_methods::admin::new(io)?;
contract_methods::admin::new(io, env)?;

None
}
Expand All @@ -545,7 +552,7 @@ fn non_submit_execute<I: IO + Copy>(
}
TransactionKind::FundXccSubAccound(_) => {
let mut handler = crate::promise::NoScheduler { promise_data };
contract_methods::xcc::fund_xcc_sub_account(&io, env, &mut handler)?;
contract_methods::xcc::fund_xcc_sub_account(io, env, &mut handler)?;

None
}
Expand Down Expand Up @@ -597,6 +604,11 @@ fn non_submit_execute<I: IO + Copy>(
let mut handler = crate::promise::NoScheduler { promise_data };
contract_methods::admin::remove_relayer_key(io, env, &mut handler)?;

None
}
TransactionKind::StartHashchain(_) => {
contract_methods::admin::start_hashchain(io, env)?;

None
}
};
Expand Down
9 changes: 9 additions & 0 deletions engine-standalone-storage/src/sync/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub enum TransactionKind {
AddRelayerKey(parameters::RelayerKeyArgs),
/// Remove the relayer public function call access key
RemoveRelayerKey(parameters::RelayerKeyArgs),
StartHashchain(parameters::StartHashchainArgs),
/// Sentinel kind for cases where a NEAR receipt caused a
/// change in Aurora state, but we failed to parse the Action.
Unknown,
Expand Down Expand Up @@ -372,6 +373,7 @@ impl TransactionKind {
Self::SetKeyManager(_) => Self::no_evm_execution("set_key_manager"),
Self::AddRelayerKey(_) => Self::no_evm_execution("add_relayer_key"),
Self::RemoveRelayerKey(_) => Self::no_evm_execution("remove_relayer_key"),
Self::StartHashchain(_) => Self::no_evm_execution("start_hashchain"),
}
}

Expand Down Expand Up @@ -480,6 +482,8 @@ pub enum TransactionKindTag {
AddRelayerKey,
#[strum(serialize = "remove_relayer_key")]
RemoveRelayerKey,
#[strum(serialize = "start_hashchain")]
StartHashchain,
Unknown,
}

Expand Down Expand Up @@ -527,6 +531,7 @@ impl TransactionKind {
Self::AddRelayerKey(args) | Self::RemoveRelayerKey(args) => {
args.try_to_vec().unwrap_or_default()
}
Self::StartHashchain(args) => args.try_to_vec().unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -569,6 +574,7 @@ impl From<&TransactionKind> for TransactionKindTag {
TransactionKind::SetKeyManager(_) => Self::SetKeyManager,
TransactionKind::AddRelayerKey(_) => Self::AddRelayerKey,
TransactionKind::RemoveRelayerKey(_) => Self::RemoveRelayerKey,
TransactionKind::StartHashchain(_) => Self::StartHashchain,
TransactionKind::Unknown => Self::Unknown,
}
}
Expand Down Expand Up @@ -748,6 +754,7 @@ enum BorshableTransactionKind<'a> {
SetKeyManager(Cow<'a, parameters::RelayerKeyManagerArgs>),
AddRelayerKey(Cow<'a, parameters::RelayerKeyArgs>),
RemoveRelayerKey(Cow<'a, parameters::RelayerKeyArgs>),
StartHashchain(Cow<'a, parameters::StartHashchainArgs>),
}

impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
Expand Down Expand Up @@ -799,6 +806,7 @@ impl<'a> From<&'a TransactionKind> for BorshableTransactionKind<'a> {
TransactionKind::SetKeyManager(x) => Self::SetKeyManager(Cow::Borrowed(x)),
TransactionKind::AddRelayerKey(x) => Self::AddRelayerKey(Cow::Borrowed(x)),
TransactionKind::RemoveRelayerKey(x) => Self::RemoveRelayerKey(Cow::Borrowed(x)),
TransactionKind::StartHashchain(x) => Self::StartHashchain(Cow::Borrowed(x)),
}
}
}
Expand Down Expand Up @@ -871,6 +879,7 @@ impl<'a> TryFrom<BorshableTransactionKind<'a>> for TransactionKind {
BorshableTransactionKind::RemoveRelayerKey(x) => {
Ok(Self::RemoveRelayerKey(x.into_owned()))
}
BorshableTransactionKind::StartHashchain(x) => Ok(Self::StartHashchain(x.into_owned())),
}
}
}
1 change: 1 addition & 0 deletions engine-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ autobenches = false

[dev-dependencies]
aurora-engine = { workspace = true, features = ["std", "tracing", "impl-serde"] }
aurora-engine-hashchain = { workspace = true, features = ["std"] }
aurora-engine-modexp = { workspace = true, features = ["std"] }
aurora-engine-precompiles = { workspace = true, features = ["std"] }
aurora-engine-sdk = { workspace = true, features = ["std"] }
Expand Down
6 changes: 1 addition & 5 deletions engine-tests/src/tests/account_id_precompiles.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::utils::{self, standalone};
use crate::utils;
use aurora_engine::parameters::SubmitResult;

#[test]
fn test_account_id_precompiles() {
let mut signer = utils::Signer::random();
let mut runner = utils::deploy_runner();
let mut standalone = standalone::StandaloneRunner::default();

standalone.init_evm();
runner.standalone_runner = Some(standalone);

let constructor = utils::solidity::ContractConstructor::compile_from_source(
"src/tests/res",
Expand Down
Loading

0 comments on commit 71980db

Please sign in to comment.