From 3bb3d1850479f1bd89cbfbce1158c9f13ca2ce66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Mon, 13 Sep 2021 15:45:02 -0500 Subject: [PATCH] add test to read EVM contract from ReadBuf --- chain-impl-mockchain/src/smartcontract/mod.rs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/chain-impl-mockchain/src/smartcontract/mod.rs b/chain-impl-mockchain/src/smartcontract/mod.rs index 31d252eff..e48b289ca 100644 --- a/chain-impl-mockchain/src/smartcontract/mod.rs +++ b/chain-impl-mockchain/src/smartcontract/mod.rs @@ -55,3 +55,56 @@ impl Payload for Contract { todo!(); } } + +#[cfg(test)] +mod tests { + use super::*; + use typed_bytes::ByteBuilder; + + #[cfg(feature = "evm")] + #[test] + fn test_readable_evm_contract() { + use chain_core::mempack::ReadBuf; + use typed_bytes::ByteArray; + + let from = AccountAddress::random(); + let to = None; + let gas: Gas = 10000.into(); + let gas_price: GasPrice = 2000.into(); + let value = None; + let data = None; + + let contract_type = 0; // Contract::EVM = 0 + let has_to = 0; + let has_gas = 1; + let has_gas_price = 1; + let has_value = 0; + let has_data = 0; + + let bb: ByteArray = ByteBuilder::new() + .u8(contract_type) + .bytes(from.as_fixed_bytes()) + .u8(has_to) + .u8(has_gas) + .bytes(&<[u8; 32]>::from(gas)) + .u8(has_gas_price) + .bytes(&<[u8; 32]>::from(gas_price)) + .u8(has_value) + .u8(has_data) + .finalize(); + + let mut readbuf = ReadBuf::from(bb.as_slice()); + let contract = Contract::read(&mut readbuf).unwrap(); + + let expected = Contract::EVM { + from, + to, + gas: Some(gas), + gas_price: Some(gas_price), + value, + data, + }; + + assert_eq!(contract, expected); + } +}