@@ -6,19 +6,19 @@ use eyre::Ok;
6
6
use frieda:: api:: commit;
7
7
use rand:: { thread_rng, RngCore } ;
8
8
use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
9
- use rs_merkle:: { algorithms:: Sha256 , Hasher , MerkleTree } ;
9
+ use rs_merkle:: { algorithms:: Sha256 , MerkleTree } ;
10
10
use tracing:: { error, info} ;
11
11
12
- use crate :: blob:: BLOB_SIZE ;
13
12
use crate :: malachite_types:: { address:: Address , signing:: PrivateKey } ;
13
+ use crate :: transactions:: Transaction ;
14
14
use crate :: { blob:: Blob , error:: BlockError , header:: Header } ;
15
15
16
16
#[ derive( Debug , Encode , Decode , Default ) ]
17
17
pub struct Block {
18
18
/// Block Header.
19
19
header : Header ,
20
20
/// list of blobs in this block.
21
- blobs : [ Blob ; 4 ] ,
21
+ transactions : Vec < Transaction > ,
22
22
}
23
23
24
24
impl Block {
@@ -28,50 +28,54 @@ impl Block {
28
28
timestamp : u64 ,
29
29
parent_hash : [ u8 ; 32 ] ,
30
30
proposer_address : Address ,
31
- blobs : [ Blob ; 4 ] ,
31
+ transactions : Vec < Transaction > ,
32
32
) -> Self {
33
- let leaves: Vec < [ u8 ; 32 ] > = blobs. iter ( ) . map ( |blob| Sha256 :: hash ( blob. data ( ) ) ) . collect ( ) ;
33
+ let tx_commitment = if transactions. is_empty ( ) {
34
+ [ 0 ; 32 ]
35
+ } else if transactions. len ( ) == 1 {
36
+ transactions[ 0 ] . hash ( )
37
+ } else {
38
+ let leaves: Vec < [ u8 ; 32 ] > = transactions. iter ( ) . map ( |tx| tx. hash ( ) ) . collect ( ) ;
34
39
35
- let merkle_tree = MerkleTree :: < Sha256 > :: from_leaves ( & leaves) ;
40
+ let merkle_tree = MerkleTree :: < Sha256 > :: from_leaves ( & leaves) ;
36
41
37
- let data_hash = merkle_tree. root ( ) . unwrap ( ) ;
38
- let da_commitment = blobs
42
+ merkle_tree. root ( ) . unwrap ( )
43
+ } ;
44
+
45
+ let da_commitment = transactions
39
46
. par_iter ( )
40
- . map ( |blob| commit ( blob. data ( ) , 4 ) )
41
- . collect :: < Vec < [ u8 ; 32 ] > > ( )
42
- . try_into ( )
43
- . unwrap ( ) ;
47
+ . flat_map ( |tx| tx. data ( ) )
48
+ . map ( |data| commit ( data. data ( ) , 4 ) )
49
+ . collect :: < Vec < [ u8 ; 32 ] > > ( ) ;
44
50
let header = Header :: new (
45
51
block_number,
46
52
timestamp,
47
- data_hash ,
53
+ tx_commitment ,
48
54
proposer_address,
49
- da_commitment,
55
+ da_commitment. try_into ( ) . unwrap_or_default ( ) ,
50
56
parent_hash,
51
57
) ;
52
- Self { header, blobs }
58
+ Self {
59
+ header,
60
+ transactions,
61
+ }
53
62
}
54
63
pub fn parent_hash ( & self ) -> [ u8 ; 32 ] {
55
64
self . header . parent_hash ( )
56
65
}
66
+ pub fn blobs ( & self ) -> Vec < Blob > {
67
+ self . transactions
68
+ . iter ( )
69
+ . flat_map ( |tx| tx. data ( ) . clone ( ) )
70
+ . collect :: < Vec < _ > > ( )
71
+ }
57
72
58
73
pub fn hash ( & self ) -> [ u8 ; 32 ] {
59
74
self . header . block_hash ( )
60
75
}
61
76
62
77
pub fn genesis ( ) -> Self {
63
- Self :: new (
64
- 0 ,
65
- 69420 ,
66
- [ 0 ; 32 ] ,
67
- Address :: default ( ) ,
68
- [
69
- Blob :: new ( [ 0 ; BLOB_SIZE ] ) ,
70
- Blob :: new ( [ 0 ; BLOB_SIZE ] ) ,
71
- Blob :: new ( [ 0 ; BLOB_SIZE ] ) ,
72
- Blob :: new ( [ 0 ; BLOB_SIZE ] ) ,
73
- ] ,
74
- )
78
+ Self :: new ( 0 , 69420 , [ 0 ; 32 ] , Address :: default ( ) , vec ! [ ] )
75
79
}
76
80
pub fn to_bytes ( & self ) -> eyre:: Result < Bytes > {
77
81
let bytes = bincode:: encode_to_vec ( self , standard ( ) ) ?;
@@ -111,17 +115,17 @@ impl Block {
111
115
) ;
112
116
return Ok ( false ) ;
113
117
}
114
- let expected = self . blob_tree_root ( ) ?;
115
- let actual = self . header . data_hash ;
118
+ let expected = self . tx_tree_root ( ) ?;
119
+ let actual = self . header . tx_commitment ;
116
120
if expected != actual {
117
121
error ! (
118
- "Data hash mismatch: expected {:?}, got {:?}" ,
122
+ "tx commitment mismatch: expected {:?}, got {:?}" ,
119
123
expected, actual
120
124
) ;
121
125
return Ok ( false ) ;
122
126
}
123
127
let expected_commitments = self
124
- . blobs
128
+ . blobs ( )
125
129
. par_iter ( )
126
130
. map ( |blob| commit ( blob. data ( ) , 4 ) )
127
131
. collect :: < Vec < [ u8 ; 32 ] > > ( ) ;
@@ -147,32 +151,19 @@ impl Block {
147
151
Ok ( true )
148
152
}
149
153
150
- /// populate the empty fields in `Header`
151
- pub fn populate ( & mut self ) -> eyre:: Result < ( ) > {
152
- // Set the `data_hash` if not present
153
- let blob_tree_root = self . blob_tree_root ( ) ?;
154
- if self . header . data_hash . is_empty ( ) {
155
- self . header . data_hash = blob_tree_root;
156
- } else if self . header . data_hash != blob_tree_root {
157
- return Err ( BlockError :: DataHashMismatch ( blob_tree_root, self . header . data_hash ) . into ( ) ) ;
158
- }
159
-
160
- println ! ( "Header population success!" ) ;
161
-
162
- Ok ( ( ) )
163
- }
164
-
165
154
/// Merklize the raw blob data
166
- pub fn blob_tree_root ( & self ) -> eyre:: Result < [ u8 ; 32 ] > {
167
- let leaves: Vec < [ u8 ; 32 ] > = self
168
- . blobs
169
- . iter ( )
170
- . map ( |blob| Sha256 :: hash ( blob. data ( ) ) )
171
- . collect ( ) ;
155
+ pub fn tx_tree_root ( & self ) -> eyre:: Result < [ u8 ; 32 ] > {
156
+ if self . transactions . is_empty ( ) {
157
+ Ok ( [ 0 ; 32 ] )
158
+ } else if self . transactions . len ( ) == 1 {
159
+ Ok ( self . transactions [ 0 ] . hash ( ) )
160
+ } else {
161
+ let leaves: Vec < [ u8 ; 32 ] > = self . transactions . iter ( ) . map ( |tx| tx. hash ( ) ) . collect ( ) ;
172
162
173
- let merkle_tree = MerkleTree :: < Sha256 > :: from_leaves ( & leaves) ;
163
+ let merkle_tree = MerkleTree :: < Sha256 > :: from_leaves ( & leaves) ;
174
164
175
- merkle_tree. root ( ) . ok_or ( BlockError :: MerkleTreeError . into ( ) )
165
+ merkle_tree. root ( ) . ok_or ( BlockError :: MerkleTreeError . into ( ) )
166
+ }
176
167
}
177
168
}
178
169
@@ -196,12 +187,7 @@ mod tests {
196
187
Utc :: now ( ) . timestamp ( ) as u64 ,
197
188
prev_block. hash ( ) ,
198
189
mock_make_validator ( ) ,
199
- [
200
- Blob :: random ( ) ,
201
- Blob :: random ( ) ,
202
- Blob :: random ( ) ,
203
- Blob :: random ( ) ,
204
- ] ,
190
+ vec ! [ Transaction :: random( ) ] ,
205
191
) ;
206
192
assert ! ( block. is_valid( 1 , & prev_block) . unwrap( ) ) ;
207
193
}
0 commit comments