forked from nervosnetwork/muta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native-contract): Support for asset creation and transfer. (nerv…
…osnetwork#37) * feat(native-contract): Support for asset creation and transfer. * handle deploy asset. * refactor: Abstract trie database. * Update self root. * Reuse trie * fill invok context. * add tests for state adapter and trie. * add test and fix bug * fix ci
- Loading branch information
Showing
34 changed files
with
1,920 additions
and
163 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
This file was deleted.
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,3 +1,5 @@ | ||
mod contract; | ||
|
||
pub use contract::{GeneralContractStateAdapter, GeneralContractStateAdapterError}; | ||
pub use contract::{ | ||
GeneralContractStateAdapter, GeneralContractStateAdapterError, RcGeneralContractStateAdapter, | ||
}; |
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,59 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
use derive_more::{Display, From}; | ||
use lazy_static::lazy_static; | ||
|
||
use protocol::types::Fee; | ||
use protocol::{ProtocolError, ProtocolErrorKind, ProtocolResult}; | ||
|
||
const NATIVE_BASE_CYCLES: u64 = 10; | ||
|
||
lazy_static! { | ||
static ref CYCLES_TABLE: HashMap<CyclesAction, u64> = { | ||
let mut table = HashMap::new(); | ||
table.insert(CyclesAction::AccountTransfer, NATIVE_BASE_CYCLES * 21); | ||
table.insert(CyclesAction::BankRegister, NATIVE_BASE_CYCLES * 210); | ||
table | ||
}; | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
pub enum CyclesAction { | ||
AccountTransfer, | ||
BankRegister, | ||
} | ||
|
||
pub fn consume_cycles( | ||
action: CyclesAction, | ||
cycles_price: u64, | ||
fee: &mut Fee, | ||
limit: &Fee, | ||
) -> ProtocolResult<()> { | ||
let cycles_used = fee.cycle | ||
+ CYCLES_TABLE | ||
.get(&action) | ||
.unwrap_or_else(|| panic!("cycles action {:?} uninitialized", action)); | ||
let cycles_used = cycles_used * cycles_price; | ||
|
||
if cycles_used > limit.cycle { | ||
return Err(CyclesError::OutOfCycles.into()); | ||
} | ||
|
||
fee.cycle = cycles_used; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Display, From)] | ||
pub enum CyclesError { | ||
#[display(fmt = "out of cycles")] | ||
OutOfCycles, | ||
} | ||
|
||
impl Error for CyclesError {} | ||
|
||
impl From<CyclesError> for ProtocolError { | ||
fn from(err: CyclesError) -> ProtocolError { | ||
ProtocolError::new(ProtocolErrorKind::Executor, Box::new(err)) | ||
} | ||
} |
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.