Skip to content

Commit bdff326

Browse files
author
Seulgi Kim
committed
Add more information to RLP error
1 parent 364672c commit bdff326

31 files changed

+552
-156
lines changed

core/src/block.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,26 @@ impl Block {
5454

5555
impl Decodable for Block {
5656
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
57-
if rlp.as_raw().len() != rlp.payload_info()?.total() {
58-
return Err(DecoderError::RlpIsTooBig)
57+
let got = rlp.as_raw().len();
58+
let expected = rlp.payload_info()?.total();
59+
if got > expected {
60+
return Err(DecoderError::RlpIsTooBig {
61+
expected,
62+
got,
63+
})
5964
}
65+
if got < expected {
66+
return Err(DecoderError::RlpIsTooShort {
67+
expected,
68+
got,
69+
})
70+
}
71+
let item_count = rlp.item_count()?;
6072
if rlp.item_count()? != 2 {
61-
return Err(DecoderError::RlpIncorrectListLen)
73+
return Err(DecoderError::RlpIncorrectListLen {
74+
expected: 2,
75+
got: item_count,
76+
})
6277
}
6378
Ok(Block {
6479
header: rlp.val_at(0)?,

core/src/consensus/epoch.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ impl Encodable for Transition {
6565

6666
impl Decodable for Transition {
6767
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
68-
if rlp.item_count()? != 3 {
69-
return Err(DecoderError::RlpInvalidLength)
68+
let item_count = rlp.item_count()?;
69+
if item_count != 3 {
70+
return Err(DecoderError::RlpInvalidLength {
71+
expected: 3,
72+
got: item_count,
73+
})
7074
}
7175
Ok(Transition {
7276
block_hash: rlp.val_at(0)?,

core/src/consensus/tendermint/message.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,23 @@ impl Decodable for TendermintMessage {
163163
let id = rlp.val_at(0)?;
164164
Ok(match id {
165165
MESSAGE_ID_CONSENSUS_MESSAGE => {
166-
if rlp.item_count()? != 2 {
167-
return Err(DecoderError::RlpIncorrectListLen)
166+
let item_count = rlp.item_count()?;
167+
if item_count != 2 {
168+
return Err(DecoderError::RlpIncorrectListLen {
169+
got: item_count,
170+
expected: 2,
171+
})
168172
}
169173
let bytes = rlp.at(1)?;
170174
TendermintMessage::ConsensusMessage(bytes.as_val()?)
171175
}
172176
MESSAGE_ID_PROPOSAL_BLOCK => {
173-
if rlp.item_count()? != 3 {
174-
return Err(DecoderError::RlpIncorrectListLen)
177+
let item_count = rlp.item_count()?;
178+
if item_count != 3 {
179+
return Err(DecoderError::RlpIncorrectListLen {
180+
got: item_count,
181+
expected: 3,
182+
})
175183
}
176184
let signature = rlp.at(1)?;
177185
let message = rlp.at(2)?;
@@ -181,8 +189,12 @@ impl Decodable for TendermintMessage {
181189
}
182190
}
183191
MESSAGE_ID_STEP_STATE => {
184-
if rlp.item_count()? != 5 {
185-
return Err(DecoderError::RlpIncorrectListLen)
192+
let item_count = rlp.item_count()?;
193+
if item_count != 5 {
194+
return Err(DecoderError::RlpIncorrectListLen {
195+
got: item_count,
196+
expected: 5,
197+
})
186198
}
187199
let vote_step = rlp.at(1)?.as_val()?;
188200
let proposal = rlp.at(2)?.as_val()?;
@@ -196,8 +208,12 @@ impl Decodable for TendermintMessage {
196208
}
197209
}
198210
MESSAGE_ID_REQUEST_MESSAGE => {
199-
if rlp.item_count()? != 3 {
200-
return Err(DecoderError::RlpIncorrectListLen)
211+
let item_count = rlp.item_count()?;
212+
if item_count != 3 {
213+
return Err(DecoderError::RlpIncorrectListLen {
214+
got: item_count,
215+
expected: 3,
216+
})
201217
}
202218
let vote_step = rlp.at(1)?.as_val()?;
203219
let requested_votes = rlp.at(2)?.as_val()?;
@@ -207,8 +223,12 @@ impl Decodable for TendermintMessage {
207223
}
208224
}
209225
MESSAGE_ID_REQUEST_PROPOSAL => {
210-
if rlp.item_count()? != 3 {
211-
return Err(DecoderError::RlpIncorrectListLen)
226+
let item_count = rlp.item_count()?;
227+
if item_count != 3 {
228+
return Err(DecoderError::RlpIncorrectListLen {
229+
got: item_count,
230+
expected: 3,
231+
})
212232
}
213233
let height = rlp.at(1)?.as_val()?;
214234
let view = rlp.at(2)?.as_val()?;

core/src/consensus/tendermint/stake/actions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ impl Decodable for Action {
4343
let tag = rlp.val_at(0)?;
4444
match tag {
4545
ACTION_TAG_TRANSFER_CCS => {
46-
if rlp.item_count()? != 3 {
47-
return Err(DecoderError::RlpInvalidLength)
46+
let item_count = rlp.item_count()?;
47+
if item_count != 3 {
48+
return Err(DecoderError::RlpInvalidLength {
49+
expected: 3,
50+
got: item_count,
51+
})
4852
}
4953
Ok(Action::TransferCCS {
5054
address: rlp.val_at(1)?,

core/src/consensus/tendermint/types.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,18 @@ impl Encodable for BitSet {
249249
impl Decodable for BitSet {
250250
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
251251
rlp.decoder().decode_value(|bytes| {
252-
if bytes.len() > BITSET_SIZE {
253-
Err(DecoderError::RlpIsTooBig)
254-
} else if bytes.len() < BITSET_SIZE {
255-
Err(DecoderError::RlpIsTooShort)
252+
let expected = BITSET_SIZE;
253+
let got = bytes.len();
254+
if got > expected {
255+
Err(DecoderError::RlpIsTooBig {
256+
expected,
257+
got,
258+
})
259+
} else if got < expected {
260+
Err(DecoderError::RlpIsTooShort {
261+
expected,
262+
got,
263+
})
256264
} else {
257265
let mut bit_set = BitSet::new();
258266
bit_set.0.copy_from_slice(bytes);

core/src/transaction.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ impl From<UnverifiedTransaction> for Transaction {
5454

5555
impl rlp::Decodable for UnverifiedTransaction {
5656
fn decode(d: &UntrustedRlp) -> Result<Self, DecoderError> {
57-
if d.item_count()? != 5 {
58-
return Err(DecoderError::RlpIncorrectListLen)
57+
let item_count = d.item_count()?;
58+
if item_count != 5 {
59+
return Err(DecoderError::RlpIncorrectListLen {
60+
expected: 5,
61+
got: item_count,
62+
})
5963
}
6064
let hash = blake256(d.as_raw());
6165
Ok(UnverifiedTransaction {

key/src/network.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ impl Encodable for NetworkId {
6666
impl Decodable for NetworkId {
6767
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
6868
let network_id = String::decode(rlp)?;
69-
network_id.parse().map_err(|_| DecoderError::RlpInvalidLength)
69+
if network_id.len() != 2 {
70+
return Err(DecoderError::RlpInvalidLength {
71+
expected: 2,
72+
got: network_id.len(),
73+
})
74+
}
75+
Ok(network_id.parse().expect("The length of network id is already checked"))
7076
}
7177
}
7278

network/src/addr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ impl Decodable for SocketAddr {
201201
Ok(SocketAddr::v4(ip0, ip1, ip2, ip3, port))
202202
}
203203
17 => unimplemented!(),
204-
_ => Err(DecoderError::RlpIncorrectListLen),
204+
got => Err(DecoderError::RlpIncorrectListLen {
205+
expected: 5,
206+
got,
207+
}),
205208
}
206209
}
207210
}

network/src/p2p/message/extension.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ impl Encodable for Message {
120120

121121
impl Decodable for Message {
122122
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
123-
if rlp.item_count()? != 5 {
124-
return Err(DecoderError::RlpInvalidLength)
123+
let item_count = rlp.item_count()?;
124+
if item_count != 5 {
125+
return Err(DecoderError::RlpInvalidLength {
126+
expected: 5,
127+
got: item_count,
128+
})
125129
}
126130
let version: Version = rlp.val_at(0)?;
127131
let protocol_id: ProtocolId = rlp.val_at(1)?;

network/src/p2p/message/handshake.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,25 @@ impl Decodable for Message {
8787
let protocol_id: ProtocolId = rlp.val_at(1)?;
8888
match protocol_id {
8989
SYNC_ID => {
90-
if rlp.item_count()? != 3 {
91-
return Err(DecoderError::RlpIncorrectListLen)
90+
let item_count = rlp.item_count()?;
91+
if item_count != 3 {
92+
return Err(DecoderError::RlpIncorrectListLen {
93+
got: item_count,
94+
expected: 3,
95+
})
9296
}
9397
Ok(Message::Sync {
9498
version,
9599
port: rlp.val_at(2)?,
96100
})
97101
}
98102
ACK_ID => {
99-
if rlp.item_count()? != 2 {
100-
return Err(DecoderError::RlpIncorrectListLen)
103+
let item_count = rlp.item_count()?;
104+
if item_count != 2 {
105+
return Err(DecoderError::RlpIncorrectListLen {
106+
got: item_count,
107+
expected: 2,
108+
})
101109
}
102110
Ok(Message::Ack(version))
103111
}

network/src/p2p/message/negotiation.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,13 @@ impl Decodable for Message {
156156
},
157157
_ => return Err(DecoderError::Custom("invalid protocol id")),
158158
};
159-
if rlp.item_count()? != message.item_count() {
160-
return Err(DecoderError::RlpInvalidLength)
159+
let item_count = rlp.item_count()?;
160+
let expected = message.item_count();
161+
if item_count != expected {
162+
return Err(DecoderError::RlpInvalidLength {
163+
expected,
164+
got: item_count,
165+
})
161166
}
162167
Ok(message)
163168
}

network/src/socket/message.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,13 @@ impl Decodable for Message {
149149
}
150150
_ => return Err(DecoderError::Custom("Invalid protocol id")),
151151
};
152-
if message.item_count() != rlp.item_count()? {
153-
return Err(DecoderError::RlpInvalidLength)
152+
let item_count = rlp.item_count()?;
153+
let expected = message.item_count();
154+
if item_count != expected {
155+
return Err(DecoderError::RlpInvalidLength {
156+
expected,
157+
got: item_count,
158+
})
154159
}
155160
Ok(message)
156161
}

state/src/item/account.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ impl Encodable for Account {
130130

131131
impl Decodable for Account {
132132
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
133-
if rlp.item_count()? != 4 {
134-
return Err(DecoderError::RlpInvalidLength)
133+
let item_count = rlp.item_count()?;
134+
if item_count != 4 {
135+
return Err(DecoderError::RlpInvalidLength {
136+
expected: 4,
137+
got: item_count,
138+
})
135139
}
136140

137141
let prefix = rlp.val_at::<u8>(0)?;

state/src/item/asset.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ impl Encodable for OwnedAsset {
129129

130130
impl Decodable for OwnedAsset {
131131
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
132+
let item_count = rlp.item_count()?;
132133
if rlp.item_count()? != 6 {
133-
return Err(DecoderError::RlpInvalidLength)
134+
return Err(DecoderError::RlpInvalidLength {
135+
expected: 6,
136+
got: item_count,
137+
})
134138
}
135139

136140
let prefix = rlp.val_at::<u8>(0)?;

state/src/item/asset_scheme.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ impl Encodable for AssetScheme {
161161

162162
impl Decodable for AssetScheme {
163163
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
164-
if rlp.item_count()? != 7 {
165-
return Err(DecoderError::RlpInvalidLength)
164+
let item_count = rlp.item_count()?;
165+
if item_count != 7 {
166+
return Err(DecoderError::RlpInvalidLength {
167+
got: item_count,
168+
expected: 7,
169+
})
166170
}
167171

168172
let prefix = rlp.val_at::<u8>(0)?;

state/src/item/metadata.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ impl Encodable for Metadata {
9595

9696
impl Decodable for Metadata {
9797
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
98-
if rlp.item_count()? != 4 {
99-
return Err(DecoderError::RlpInvalidLength)
98+
let item_count = rlp.item_count()?;
99+
if item_count != 4 {
100+
return Err(DecoderError::RlpInvalidLength {
101+
got: item_count,
102+
expected: 4,
103+
})
100104
}
101105
let prefix = rlp.val_at::<u8>(0)?;
102106
if PREFIX != prefix {

state/src/item/regular_account.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ impl Encodable for RegularAccount {
6565

6666
impl Decodable for RegularAccount {
6767
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
68-
if rlp.item_count()? != 2 {
69-
return Err(DecoderError::RlpInvalidLength)
68+
let item_count = rlp.item_count()?;
69+
if item_count != 2 {
70+
return Err(DecoderError::RlpInvalidLength {
71+
got: item_count,
72+
expected: 2,
73+
})
7074
}
7175
let prefix = rlp.val_at::<u8>(0)?;
7276
if PREFIX != prefix {

state/src/item/shard.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ impl Encodable for Shard {
8989

9090
impl Decodable for Shard {
9191
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
92-
if rlp.item_count()? != 4 {
93-
return Err(DecoderError::RlpInvalidLength)
92+
let item_count = rlp.item_count()?;
93+
if item_count != 4 {
94+
return Err(DecoderError::RlpInvalidLength {
95+
expected: 4,
96+
got: item_count,
97+
})
9498
}
9599
let prefix = rlp.val_at::<u8>(0)?;
96100
if PREFIX != prefix {

state/src/item/text.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ impl Encodable for Text {
7676

7777
impl Decodable for Text {
7878
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
79-
if rlp.item_count()? != 3 {
80-
return Err(DecoderError::RlpInvalidLength)
79+
let item_count = rlp.item_count()?;
80+
if item_count != 3 {
81+
return Err(DecoderError::RlpInvalidLength {
82+
got: item_count,
83+
expected: 3,
84+
})
8185
}
8286
let prefix = rlp.val_at::<u8>(0)?;
8387
if PREFIX != prefix {

0 commit comments

Comments
 (0)