From 3f31b5697e488857d5cab43a741539571eba2341 Mon Sep 17 00:00:00 2001 From: Ash Manning Date: Mon, 25 Nov 2024 21:25:40 +0800 Subject: [PATCH] Use canonical(borsh) serialization when checking block body size --- lib/node/mod.rs | 2 -- lib/state.rs | 8 +++++--- lib/types/transaction.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node/mod.rs b/lib/node/mod.rs index 1b57584..94246d1 100644 --- a/lib/node/mod.rs +++ b/lib/node/mod.rs @@ -46,8 +46,6 @@ pub enum Error { AmountUnderflow(#[from] AmountUnderflowError), #[error("archive error")] Archive(#[from] archive::Error), - #[error("bincode error")] - Bincode(#[from] bincode::Error), #[error("CUSF mainchain proto error")] CusfMainchain(#[from] proto::Error), #[error("heed error")] diff --git a/lib/state.rs b/lib/state.rs index eec8daa..2605517 100644 --- a/lib/state.rs +++ b/lib/state.rs @@ -91,10 +91,10 @@ pub enum Error { AmountOverflow(#[from] AmountOverflowError), #[error(transparent)] AmountUnderflow(#[from] AmountUnderflowError), - #[error("binvode error")] - Bincode(#[from] bincode::Error), #[error("body too large")] BodyTooLarge, + #[error(transparent)] + BorshSerialize(borsh::io::Error), #[error("invalid body: expected merkle root {expected}, but computed {computed}")] InvalidBody { expected: MerkleRoot, @@ -505,7 +505,9 @@ impl State { if body.authorizations.len() > Self::body_sigops_limit(height) { return Err(Error::TooManySigops); } - if bincode::serialize(&body)?.len() > Self::body_size_limit(height) { + let body_size = + borsh::object_length(&body).map_err(Error::BorshSerialize)?; + if body_size > Self::body_size_limit(height) { return Err(Error::BodyTooLarge); } let mut accumulator = self diff --git a/lib/types/transaction.rs b/lib/types/transaction.rs index 86e4e48..a1f2d75 100644 --- a/lib/types/transaction.rs +++ b/lib/types/transaction.rs @@ -421,7 +421,7 @@ pub struct AuthorizedTransaction { pub authorizations: Vec, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(BorshSerialize, Clone, Debug, Deserialize, Serialize)] pub struct Body { pub coinbase: Vec, pub transactions: Vec,