Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rlp encode and decode of Proposal struct #1485

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions protocol/src/codec/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;
use overlord::Codec;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};

use crate::types::{BlockVersion, Bytes, Proposal, BASE_FEE_PER_GAS, MAX_BLOCK_GAS_LIMIT};
use crate::types::{BlockVersion, Bytes, Proposal, BASE_FEE_PER_GAS};
use crate::{codec::error::CodecError, lazy::CHAIN_ID, ProtocolError};

impl Encodable for BlockVersion {
Expand All @@ -23,7 +23,7 @@ impl Decodable for BlockVersion {

impl Encodable for Proposal {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(11)
s.begin_list(13)
.append(&self.version)
.append(&self.prev_hash)
.append(&self.proposer)
Expand All @@ -32,6 +32,8 @@ impl Encodable for Proposal {
.append(&self.signed_txs_hash)
.append(&self.timestamp)
.append(&self.number)
.append(&self.gas_limit.as_u64())
.append_list(&self.extra_data)
.append(&self.proof)
.append(&self.call_system_script_count)
.append_list(&self.tx_hashes);
Expand All @@ -49,13 +51,13 @@ impl Decodable for Proposal {
signed_txs_hash: r.val_at(5)?,
timestamp: r.val_at(6)?,
number: r.val_at(7)?,
gas_limit: MAX_BLOCK_GAS_LIMIT.into(),
extra_data: Default::default(),
gas_limit: r.val_at::<u64>(8)?.into(),
extra_data: r.list_at(9)?,
base_fee_per_gas: BASE_FEE_PER_GAS.into(),
proof: r.val_at(8)?,
proof: r.val_at(10)?,
chain_id: **CHAIN_ID.load(),
call_system_script_count: r.val_at(9)?,
tx_hashes: r.list_at(10)?,
call_system_script_count: r.val_at(11)?,
tx_hashes: r.list_at(12)?,
})
}
}
Expand All @@ -75,10 +77,15 @@ impl Codec for Proposal {
#[cfg(test)]
mod tests {
use crate::traits::MessageCodec;
use crate::types::{Block, Header, Proof};
use crate::types::{Block, Bytes, ExtraData, Header, Proof, H160, H256};
use rand::random;

use super::*;

fn rand_bytes(len: usize) -> Bytes {
(0..len).map(|_| random::<u8>()).collect::<Vec<_>>().into()
}

#[test]
fn test_version_codec() {
let ver = BlockVersion::V0;
Expand Down Expand Up @@ -116,10 +123,31 @@ mod tests {

#[test]
fn test_proposal_codec() {
let mock_proof = Proof {
number: random(),
round: random(),
block_hash: H256::random(),
signature: rand_bytes(65),
bitmap: rand_bytes(32),
};
let mut proposal = Proposal {
gas_limit: 30000000u64.into(),
base_fee_per_gas: 1337u64.into(),
..Default::default()
version: BlockVersion::V0,
prev_hash: H256::random(),
proposer: H160::random(),
prev_state_root: H256::random(),
transactions_root: H256::random(),
signed_txs_hash: H256::random(),
timestamp: random(),
number: random(),
gas_limit: 30000000u64.into(),
extra_data: vec![ExtraData {
inner: H256::random().0.to_vec().into(),
}],
base_fee_per_gas: 1337u64.into(),
proof: mock_proof,
chain_id: 0,
call_system_script_count: random(),
tx_hashes: vec![H256::random()],
};
let bytes = proposal.encode_msg().unwrap();
let decode: Proposal = Proposal::decode_msg(bytes).unwrap();
Expand Down
Loading