Skip to content

Commit

Permalink
feat: change rlp in executor to fixed-codec (nervosnetwork#29)
Browse files Browse the repository at this point in the history
* change rlp in executor to fixed-codec

* change ContractSer to ProtocolFixedCodec

* fix cigst

* impl schema for bytes
  • Loading branch information
Wenchao Hu authored and yejiayu committed Oct 25, 2019
1 parent 9a77ebf commit 7f737cd
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 505 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion core/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ cita_trie = "2.0"
bytes = "0.4"
lazy_static = "1.4"
derive_more = "0.15"
rlp = "0.4"
rocksdb = "0.12"
hex = "0.3"
16 changes: 9 additions & 7 deletions core/executor/src/adapter/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::rc::Rc;
use bytes::Bytes;
use derive_more::{Display, From};

use protocol::fixed_codec::ProtocolFixedCodec;
use protocol::traits::executor::contract::ContractStateAdapter;
use protocol::traits::executor::{ContractSchema, ContractSer, TrieDB};
use protocol::traits::executor::{ContractSchema, TrieDB};
use protocol::types::MerkleRoot;
use protocol::{ProtocolError, ProtocolErrorKind, ProtocolResult};

Expand Down Expand Up @@ -40,20 +41,20 @@ impl<DB: TrieDB> ContractStateAdapter for GeneralContractStateAdapter<DB> {
&self,
key: &<Schema as ContractSchema>::Key,
) -> ProtocolResult<Option<<Schema as ContractSchema>::Value>> {
let encoded_key = key.encode()?;
let encoded_key = key.encode_fixed()?;

if let Some(value_bytes) = self.cache_map.get(&encoded_key) {
let inst = <_>::decode(value_bytes.clone())?;
let inst = <_>::decode_fixed(value_bytes.clone())?;
return Ok(Some(inst));
}

if let Some(value_bytes) = self.stash_map.get(&encoded_key) {
let inst = <_>::decode(value_bytes.clone())?;
let inst = <_>::decode_fixed(value_bytes.clone())?;
return Ok(Some(inst));
}

if let Some(value_bytes) = self.trie.get(&encoded_key)? {
return Ok(Some(Schema::Value::decode(value_bytes)?));
return Ok(Some(Schema::Value::decode_fixed(value_bytes)?));
}

Ok(None)
Expand All @@ -63,7 +64,7 @@ impl<DB: TrieDB> ContractStateAdapter for GeneralContractStateAdapter<DB> {
&self,
key: &<Schema as ContractSchema>::Key,
) -> ProtocolResult<bool> {
let encoded_key = key.encode()?;
let encoded_key = key.encode_fixed()?;

if self.cache_map.contains_key(&encoded_key) {
return Ok(true);
Expand All @@ -81,7 +82,8 @@ impl<DB: TrieDB> ContractStateAdapter for GeneralContractStateAdapter<DB> {
key: <Schema as ContractSchema>::Key,
value: <Schema as ContractSchema>::Value,
) -> ProtocolResult<()> {
self.cache_map.insert(key.encode()?, value.encode()?);
self.cache_map
.insert(key.encode_fixed()?, value.encode_fixed()?);
Ok(())
}

Expand Down
Loading

0 comments on commit 7f737cd

Please sign in to comment.