Skip to content

Commit

Permalink
init port near-store
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Jan 19, 2022
1 parent 3eaf113 commit 9cc176c
Show file tree
Hide file tree
Showing 43 changed files with 10,286 additions and 783 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ jobs:
run: |
yarn blockchain:ci
- name: Check Contract SDK Compile
run: |
yarn contract-sdk:test
- name: Check Enclave Compile
run: |
yarn enclave:ci
- name: Check Mock-Enclave VM Compile
run: |
yarn mock-enclave:test
- name: Check Contract SDK Compile
run: |
yarn contract-sdk:test
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ exclude = [
'mock-enclave/src/skw-vm-engine',
'mock-enclave/src/skw-vm-engine-cli',
'mock-enclave/src/near-test-contracts',
'mock-enclave/src/skw-vm-runtime',
'mock-enclave/src/skw-vm-store',
'skw-contract-sdk/skw-contract-sdk',
'skw-contract-sdk/skw-sdk-macros',
'skw-contract-sdk/skw-sdk-sim',
Expand Down
24 changes: 24 additions & 0 deletions mock-enclave/src/skw-vm-primitives/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,30 @@ pub enum InvalidAccessKeyError {
DepositWithFunctionCall,
}


/// Internal
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageError {
/// Key-value db internal failure
StorageInternalError,
/// Storage is PartialStorage and requested a missing trie node
TrieNodeMissing,
/// Either invalid state or key-value db is corrupted.
/// For PartialStorage it cannot be corrupted.
/// Error message is unreliable and for debugging purposes only. It's also probably ok to
/// panic in every place that produces this error.
/// We can check if db is corrupted by verifying everything in the state trie.
StorageInconsistentState(String),
}

impl std::fmt::Display for StorageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.write_str(&format!("{:?}", self))
}
}

impl std::error::Error for StorageError {}

impl Display for InvalidAccessKeyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Expand Down
226 changes: 113 additions & 113 deletions mock-enclave/src/skw-vm-runtime/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ impl<'a> External for RuntimeExt<'a> {
Ok(new_receipt_index)
}

fn append_action_create_account(&mut self, receipt_index: u64) -> ExtResult<()> {
self.append_action(receipt_index, Action::CreateAccount(CreateAccountAction {}));
Ok(())
}
// fn append_action_create_account(&mut self, receipt_index: u64) -> ExtResult<()> {
// self.append_action(receipt_index, Action::CreateAccount(CreateAccountAction {}));
// Ok(())
// }

fn append_action_deploy_contract(
&mut self,
Expand Down Expand Up @@ -275,118 +275,118 @@ impl<'a> External for RuntimeExt<'a> {
Ok(())
}

fn append_action_transfer(&mut self, receipt_index: u64, deposit: u128) -> ExtResult<()> {
self.append_action(receipt_index, Action::Transfer(TransferAction { deposit }));
Ok(())
}

fn append_action_stake(
&mut self,
receipt_index: u64,
stake: u128,
public_key: Vec<u8>,
) -> ExtResult<()> {
self.append_action(
receipt_index,
Action::Stake(StakeAction {
stake,
public_key: PublicKey::try_from_slice(&public_key)
.map_err(|_| HostError::InvalidPublicKey)?,
}),
);
Ok(())
}

fn append_action_add_key_with_full_access(
&mut self,
receipt_index: u64,
public_key: Vec<u8>,
nonce: u64,
) -> ExtResult<()> {
self.append_action(
receipt_index,
Action::AddKey(AddKeyAction {
public_key: PublicKey::try_from_slice(&public_key)
.map_err(|_| HostError::InvalidPublicKey)?,
access_key: AccessKey { nonce, permission: AccessKeyPermission::FullAccess },
}),
);
Ok(())
}

fn append_action_add_key_with_function_call(
&mut self,
receipt_index: u64,
public_key: Vec<u8>,
nonce: u64,
allowance: Option<u128>,
receiver_id: AccountId,
method_names: Vec<Vec<u8>>,
) -> ExtResult<()> {
self.append_action(
receipt_index,
Action::AddKey(AddKeyAction {
public_key: PublicKey::try_from_slice(&public_key)
.map_err(|_| HostError::InvalidPublicKey)?,
access_key: AccessKey {
nonce,
permission: AccessKeyPermission::FunctionCall(FunctionCallPermission {
allowance,
receiver_id: receiver_id.into(),
method_names: method_names
.into_iter()
.map(|method_name| {
String::from_utf8(method_name)
.map_err(|_| HostError::InvalidMethodName)
})
.collect::<std::result::Result<Vec<_>, _>>()?,
}),
},
}),
);
Ok(())
}

fn append_action_delete_key(
&mut self,
receipt_index: u64,
public_key: Vec<u8>,
) -> ExtResult<()> {
self.append_action(
receipt_index,
Action::DeleteKey(DeleteKeyAction {
public_key: PublicKey::try_from_slice(&public_key)
.map_err(|_| HostError::InvalidPublicKey)?,
}),
);
Ok(())
}

fn append_action_delete_account(
&mut self,
receipt_index: u64,
beneficiary_id: AccountId,
) -> ExtResult<()> {
self.append_action(
receipt_index,
Action::DeleteAccount(DeleteAccountAction { beneficiary_id }),
);
Ok(())
}
// fn append_action_transfer(&mut self, receipt_index: u64, deposit: u128) -> ExtResult<()> {
// self.append_action(receipt_index, Action::Transfer(TransferAction { deposit }));
// Ok(())
// }

// fn append_action_stake(
// &mut self,
// receipt_index: u64,
// stake: u128,
// public_key: Vec<u8>,
// ) -> ExtResult<()> {
// self.append_action(
// receipt_index,
// Action::Stake(StakeAction {
// stake,
// public_key: PublicKey::try_from_slice(&public_key)
// .map_err(|_| HostError::InvalidPublicKey)?,
// }),
// );
// Ok(())
// }

// fn append_action_add_key_with_full_access(
// &mut self,
// receipt_index: u64,
// public_key: Vec<u8>,
// nonce: u64,
// ) -> ExtResult<()> {
// self.append_action(
// receipt_index,
// Action::AddKey(AddKeyAction {
// public_key: PublicKey::try_from_slice(&public_key)
// .map_err(|_| HostError::InvalidPublicKey)?,
// access_key: AccessKey { nonce, permission: AccessKeyPermission::FullAccess },
// }),
// );
// Ok(())
// }

// fn append_action_add_key_with_function_call(
// &mut self,
// receipt_index: u64,
// public_key: Vec<u8>,
// nonce: u64,
// allowance: Option<u128>,
// receiver_id: AccountId,
// method_names: Vec<Vec<u8>>,
// ) -> ExtResult<()> {
// self.append_action(
// receipt_index,
// Action::AddKey(AddKeyAction {
// public_key: PublicKey::try_from_slice(&public_key)
// .map_err(|_| HostError::InvalidPublicKey)?,
// access_key: AccessKey {
// nonce,
// permission: AccessKeyPermission::FunctionCall(FunctionCallPermission {
// allowance,
// receiver_id: receiver_id.into(),
// method_names: method_names
// .into_iter()
// .map(|method_name| {
// String::from_utf8(method_name)
// .map_err(|_| HostError::InvalidMethodName)
// })
// .collect::<std::result::Result<Vec<_>, _>>()?,
// }),
// },
// }),
// );
// Ok(())
// }

// fn append_action_delete_key(
// &mut self,
// receipt_index: u64,
// public_key: Vec<u8>,
// ) -> ExtResult<()> {
// self.append_action(
// receipt_index,
// Action::DeleteKey(DeleteKeyAction {
// public_key: PublicKey::try_from_slice(&public_key)
// .map_err(|_| HostError::InvalidPublicKey)?,
// }),
// );
// Ok(())
// }

// fn append_action_delete_account(
// &mut self,
// receipt_index: u64,
// beneficiary_id: AccountId,
// ) -> ExtResult<()> {
// self.append_action(
// receipt_index,
// Action::DeleteAccount(DeleteAccountAction { beneficiary_id }),
// );
// Ok(())
// }

fn get_touched_nodes_count(&self) -> u64 {
self.trie_update.trie.counter.get()
}

fn validator_stake(&self, account_id: &AccountId) -> ExtResult<Option<Balance>> {
self.epoch_info_provider
.validator_stake(self.epoch_id, self.prev_block_hash, account_id)
.map_err(|e| ExternalError::ValidatorError(e).into())
}

fn validator_total_stake(&self) -> ExtResult<Balance> {
self.epoch_info_provider
.validator_total_stake(self.epoch_id, self.prev_block_hash)
.map_err(|e| ExternalError::ValidatorError(e).into())
}
// fn validator_stake(&self, account_id: &AccountId) -> ExtResult<Option<Balance>> {
// self.epoch_info_provider
// .validator_stake(self.epoch_id, self.prev_block_hash, account_id)
// .map_err(|e| ExternalError::ValidatorError(e).into())
// }

// fn validator_total_stake(&self) -> ExtResult<Balance> {
// self.epoch_info_provider
// .validator_total_stake(self.epoch_id, self.prev_block_hash)
// .map_err(|e| ExternalError::ValidatorError(e).into())
// }
}
Loading

0 comments on commit 9cc176c

Please sign in to comment.