-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(engine): Hashchain integration (#831)
## 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
Showing
31 changed files
with
1,224 additions
and
582 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.