Skip to content

Commit

Permalink
boolean checks added
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Oct 12, 2021
1 parent ec95c0f commit 4109ddb
Showing 1 changed file with 129 additions and 67 deletions.
196 changes: 129 additions & 67 deletions chain-impl-mockchain/src/config.rs
Expand Up @@ -848,66 +848,68 @@ impl ConfigParamVariant for CommitteeId {
#[cfg(feature = "evm")]
impl ConfigParamVariant for EvmConfigParams {
fn to_payload(&self) -> Vec<u8> {
let cfg = &self.config;
let refund_sstore_clears = cfg.refund_sstore_clears.try_into().unwrap();
let bb: ByteBuilder<EvmConfigParams> = ByteBuilder::new()
.u64(self.config.gas_ext_code)
.u64(self.config.gas_ext_code_hash)
.u64(self.config.gas_sstore_set)
.u64(self.config.gas_sstore_reset)
.u64(self.config.refund_sstore_clears.try_into().unwrap())
.u64(self.config.gas_balance)
.u64(self.config.gas_sload)
.u64(self.config.gas_suicide)
.u64(self.config.gas_suicide_new_account)
.u64(self.config.gas_call)
.u64(self.config.gas_expbyte)
.u64(self.config.gas_transaction_create)
.u64(self.config.gas_transaction_call)
.u64(self.config.gas_transaction_zero_data)
.u64(self.config.gas_transaction_non_zero_data)
.u8(self.config.sstore_gas_metering as u8)
.u8(self.config.sstore_revert_under_stipend as u8)
.u8(self.config.err_on_call_with_more_gas as u8)
.u8(self.config.call_l64_after_gas as u8)
.u8(self.config.empty_considered_exists as u8)
.u8(self.config.create_increase_nonce as u8)
.u64(self.config.stack_limit as u64)
.u64(self.config.memory_limit as u64)
.u64(self.config.call_stack_limit as u64);
let bb = if let Some(limit) = self.config.create_contract_limit {
.u64(cfg.gas_ext_code)
.u64(cfg.gas_ext_code_hash)
.u64(cfg.gas_sstore_set)
.u64(cfg.gas_sstore_reset)
.u64(refund_sstore_clears)
.u64(cfg.gas_balance)
.u64(cfg.gas_sload)
.u64(cfg.gas_suicide)
.u64(cfg.gas_suicide_new_account)
.u64(cfg.gas_call)
.u64(cfg.gas_expbyte)
.u64(cfg.gas_transaction_create)
.u64(cfg.gas_transaction_call)
.u64(cfg.gas_transaction_zero_data)
.u64(cfg.gas_transaction_non_zero_data)
.u8(cfg.sstore_gas_metering as u8)
.u8(cfg.sstore_revert_under_stipend as u8)
.u8(cfg.err_on_call_with_more_gas as u8)
.u8(cfg.call_l64_after_gas as u8)
.u8(cfg.empty_considered_exists as u8)
.u8(cfg.create_increase_nonce as u8)
.u64(cfg.stack_limit as u64)
.u64(cfg.memory_limit as u64)
.u64(cfg.call_stack_limit as u64);
let bb = if let Some(limit) = cfg.create_contract_limit {
bb.u8(1).u64(limit as u64)
} else {
bb.u8(0)
};
let bb = bb
.u64(self.config.call_stipend as u64)
.u8(self.config.has_delegate_call as u8)
.u8(self.config.has_create2 as u8)
.u8(self.config.has_revert as u8)
.u8(self.config.has_return_data as u8)
.u8(self.config.has_bitwise_shifting as u8)
.u8(self.config.has_chain_id as u8)
.u8(self.config.has_self_balance as u8)
.u8(self.config.has_ext_code_hash as u8)
.u8(self.config.estimate as u8);
.u64(cfg.call_stipend as u64)
.u8(cfg.has_delegate_call as u8)
.u8(cfg.has_create2 as u8)
.u8(cfg.has_revert as u8)
.u8(cfg.has_return_data as u8)
.u8(cfg.has_bitwise_shifting as u8)
.u8(cfg.has_chain_id as u8)
.u8(cfg.has_self_balance as u8)
.u8(cfg.has_ext_code_hash as u8)
.u8(cfg.estimate as u8);
let env = &self.environment;
let bb = bb
.bytes(&<[u8; 32]>::from(self.environment.gas_price))
.bytes(self.environment.origin.as_fixed_bytes())
.bytes(&<[u8; 32]>::from(self.environment.chain_id));
let bb = if self.environment.block_hashes.is_empty() {
.bytes(&<[u8; 32]>::from(env.gas_price))
.bytes(env.origin.as_fixed_bytes())
.bytes(&<[u8; 32]>::from(env.chain_id));
let bb = if env.block_hashes.is_empty() {
bb.u64(0)
} else {
let bb = bb.u64(self.environment.block_hashes.len().try_into().unwrap());
self.environment
.block_hashes
let bb = bb.u64(env.block_hashes.len().try_into().unwrap());
env.block_hashes
.iter()
.fold(bb, |b, h| b.bytes(h.as_fixed_bytes()))
};
let bb = bb
.bytes(&<[u8; 32]>::from(self.environment.block_number))
.bytes(self.environment.block_coinbase.as_fixed_bytes())
.bytes(&<[u8; 32]>::from(self.environment.block_timestamp))
.bytes(&<[u8; 32]>::from(self.environment.block_difficulty))
.bytes(&<[u8; 32]>::from(self.environment.block_gas_limit));
.bytes(&<[u8; 32]>::from(env.block_number))
.bytes(env.block_coinbase.as_fixed_bytes())
.bytes(&<[u8; 32]>::from(env.block_timestamp))
.bytes(&<[u8; 32]>::from(env.block_difficulty))
.bytes(&<[u8; 32]>::from(env.block_gas_limit));
bb.finalize_as_vec()
}

Expand All @@ -918,7 +920,7 @@ impl ConfigParamVariant for EvmConfigParams {
let gas_ext_code_hash = rb.get_u64()?;
let gas_sstore_set = rb.get_u64()?;
let gas_sstore_reset = rb.get_u64()?;
let refund_sstore_clears: i64 = rb.get_u64()?.try_into().unwrap();
let refund_sstore_clears = rb.get_u64()?.try_into().map_err(|_| Error::SizeInvalid)?;
let gas_balance = rb.get_u64()?;
let gas_sload = rb.get_u64()?;
let gas_suicide = rb.get_u64()?;
Expand All @@ -929,32 +931,92 @@ impl ConfigParamVariant for EvmConfigParams {
let gas_transaction_call = rb.get_u64()?;
let gas_transaction_zero_data = rb.get_u64()?;
let gas_transaction_non_zero_data = rb.get_u64()?;
let sstore_gas_metering = rb.get_u8()? != 0;
let sstore_revert_under_stipend = rb.get_u8()? != 0;
let err_on_call_with_more_gas = rb.get_u8()? != 0;
let call_l64_after_gas = rb.get_u8()? != 0;
let empty_considered_exists = rb.get_u8()? != 0;
let create_increase_nonce = rb.get_u8()? != 0;
let sstore_gas_metering = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let sstore_revert_under_stipend = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let err_on_call_with_more_gas = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let call_l64_after_gas = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let empty_considered_exists = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let create_increase_nonce = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let stack_limit = rb.get_u64()? as usize;
let memory_limit = rb.get_u64()? as usize;
let call_stack_limit = rb.get_u64()? as usize;

// Check if create contract limit is set
let create_contract_limit = if rb.get_u8()? != 0 {
Some(rb.get_u64()? as usize)
} else {
None
let create_contract_limit = match rb.get_u8()? {
0 => None,
1 => Some(rb.get_u64()? as usize),
_ => return Err(Error::BoolInvalid),
};
let call_stipend = rb.get_u64()?;
let has_delegate_call = rb.get_u8()? != 0;
let has_create2 = rb.get_u8()? != 0;
let has_revert = rb.get_u8()? != 0;
let has_return_data = rb.get_u8()? != 0;
let has_bitwise_shifting = rb.get_u8()? != 0;
let has_chain_id = rb.get_u8()? != 0;
let has_self_balance = rb.get_u8()? != 0;
let has_ext_code_hash = rb.get_u8()? != 0;
let estimate = rb.get_u8()? != 0;
let has_delegate_call = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_create2 = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_revert = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_return_data = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_bitwise_shifting = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_chain_id = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_self_balance = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let has_ext_code_hash = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};
let estimate = match rb.get_u8()? {
0 => false,
1 => true,
_ => return Err(Error::BoolInvalid),
};

let config = Config {
gas_ext_code,
Expand Down

0 comments on commit 4109ddb

Please sign in to comment.