Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit bf02125

Browse files
jacogrrphmeier
authored andcommitted
Add codec tests (#74)
* Add tests for block & header encoding * Add encoding tests for transaction/unchecked_transaction
1 parent 8629e4a commit bf02125

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

substrate/primitives/src/block.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,30 @@ mod tests {
177177
use codec::Slicable;
178178
use substrate_serializer as ser;
179179

180+
#[test]
181+
fn test_header_encoding() {
182+
let header = Header {
183+
parent_hash: 5.into(),
184+
number: 67,
185+
state_root: 3.into(),
186+
transaction_root: 6.into(),
187+
digest: Digest { logs: vec![Log(vec![1]), Log(vec![2])] },
188+
};
189+
190+
assert_eq!(header.encode(), vec![
191+
// parent_hash
192+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
193+
// number
194+
67, 0, 0, 0, 0, 0, 0, 0,
195+
// state_root
196+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
197+
// transaction_root
198+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
199+
// digest (length, log1, log2)
200+
2, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 2
201+
]);
202+
}
203+
180204
#[test]
181205
fn test_header_serialization() {
182206
let header = Header {
@@ -202,4 +226,27 @@ mod tests {
202226
let v = header.encode();
203227
assert_eq!(Header::decode(&mut &v[..]).unwrap(), header);
204228
}
229+
230+
#[test]
231+
fn test_block_encoding() {
232+
let block = Block {
233+
header: Header::from_block_number(12),
234+
transactions: vec![Transaction(vec!(4))],
235+
};
236+
237+
assert_eq!(block.encode(), vec![
238+
// parent_hash
239+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240+
// number
241+
12, 0, 0, 0, 0, 0, 0, 0,
242+
// state_root
243+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244+
// transaction_root
245+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246+
// digest
247+
0, 0, 0, 0,
248+
// transactions (length, tx...)
249+
1, 0, 0, 0, 1, 0, 0, 0, 4
250+
]);
251+
}
205252
}

substrate/test-runtime/src/transaction.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,30 @@ impl Slicable for Transaction {
5454
}
5555

5656
impl ::codec::NonTrivialSlicable for Transaction {}
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
use keyring::Keyring;
62+
63+
#[test]
64+
fn test_tx_encoding() {
65+
let tx = Transaction {
66+
from: Keyring::Alice.to_raw_public(),
67+
to: Keyring::Bob.to_raw_public(),
68+
amount: 69,
69+
nonce: 33,
70+
};
71+
72+
assert_eq!(tx.encode(), vec![
73+
// from
74+
209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79,
75+
// to
76+
215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185,
77+
// amount
78+
69, 0, 0, 0, 0, 0, 0, 0,
79+
// nonce
80+
33, 0, 0, 0, 0, 0, 0, 0
81+
]);
82+
}
83+
}

substrate/test-runtime/src/unchecked_transaction.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,37 @@ impl Slicable for UncheckedTransaction {
6161
}
6262

6363
impl ::codec::NonTrivialSlicable for UncheckedTransaction {}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
use keyring::Keyring;
69+
use ::Transaction;
70+
71+
#[test]
72+
fn test_unchecked_encoding() {
73+
let tx = Transaction {
74+
from: Keyring::Alice.to_raw_public(),
75+
to: Keyring::Bob.to_raw_public(),
76+
amount: 69,
77+
nonce: 34,
78+
};
79+
let signature = Keyring::from_raw_public(tx.from).unwrap().sign(&tx.encode());
80+
let signed = UncheckedTransaction { tx, signature };
81+
82+
assert_eq!(signed.encode(), vec![
83+
// length
84+
144, 0, 0, 0,
85+
// from
86+
209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79,
87+
// to
88+
215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185,
89+
// amount
90+
69, 0, 0, 0, 0, 0, 0, 0,
91+
// nonce
92+
34, 0, 0, 0, 0, 0, 0, 0,
93+
// signature
94+
207, 69, 156, 55, 7, 227, 202, 3, 114, 111, 43, 46, 227, 38, 39, 122, 245, 69, 195, 117, 190, 154, 89, 76, 134, 91, 251, 230, 31, 221, 1, 194, 144, 34, 33, 58, 220, 154, 205, 135, 224, 52, 248, 198, 12, 17, 96, 53, 110, 160, 194, 10, 9, 60, 40, 133, 57, 112, 151, 200, 105, 198, 245, 10
95+
]);
96+
}
97+
}

0 commit comments

Comments
 (0)