Skip to content

Commit be35914

Browse files
remagpieforiequal0
authored andcommitted
Update snapshot message format
1 parent f4af598 commit be35914

File tree

6 files changed

+32
-158
lines changed

6 files changed

+32
-158
lines changed

spec/Block-Synchronization-Extension.md

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,17 @@ Request corresponding bodies for each hash.
5353
* Restriction:
5454
* MUST include at least one item
5555

56-
57-
### GetStateHead
58-
59-
```
60-
GetStateHead(block_hash)
61-
```
62-
63-
Request corresponding state head for block of `block_hash`.
64-
65-
* Identifier: 0x06
66-
* Restriction: Block number of requested block MUST be multiple of 214.
67-
68-
6956
### GetStateChunk
7057

7158
```
72-
GetStateChunk(block_hash, tree_root)
59+
GetStateChunk(block_hash, [...chunk_roots])
7360
```
7461

75-
Request entire subtree starting from `tree_root`.
62+
Request corresponding snapshot chunk for each `chunk_root`.
7663

77-
* Identifier: 0x08
64+
* Identifier: 0x0a
7865
* Restriction:
79-
* Block number of requested block MUST be multiple of 214.
80-
* `tree_root` MUST be included in requested block’s state trie.
81-
* Depth of `tree_root` inside state trie MUST be equal to 2. (Depth of state root is 0)
66+
* All values in `[...chunk_roots]` MUST be included in requested block’s state trie.
8267

8368

8469
## Response messages
@@ -113,30 +98,15 @@ Response to `GetBodies` message. Snappy algorithm is used to compress content.
11398
* If received body is zero-length array, it means either body value is [], or sender doesn’t have body for requested hash
11499

115100

116-
### StateHead
117-
118-
```
119-
StateHead(compressed((key_0, value_0), …) | [])
120-
```
121-
122-
Response to `GetStateHead` message. Key and value included in this messages are raw value stored in state trie. Snappy algorithm is used for compression of content.
123-
124-
* Identifier: 0x07
125-
* Restriction:
126-
* State root of requested block MUST be included
127-
* For all nodes with depth of less than 2 included in this message, all of its child MUST also be included.
128-
* Content MUST be empty array if sender didn’t have requested data
129-
130-
131101
### StateChunk
132102
```
133-
StateChunk(compressed((key_0, value_0), …) | [])
103+
StateChunk([compressed([terminal_0, …] | []), ...])
134104
```
135105

136-
Response to `GetStateChunk` message. Details of message is same as `StateHead` message.
106+
Response to `GetStateChunk` message. Snappy algorithm is used for compression of content.
137107

138-
* Identifier: 0x09
108+
* Identifier: 0x0b
139109
* Restriction:
140-
* Node corresponding to tree_root in request MUST be included
141-
* Every nodes included in message MUST have all of its child in same message.
142-
* Content MUST be empty array if sender didn’t have requested data
110+
* Number and order of chunks included in this message MUST be equal to request information.
111+
* Node corresponding to `chunk_root` in request MUST be included
112+
* If sender doesn’t have a chunk for the requested hash, corresponding chunk MUST be compressed([]), not omitted.

sync/src/block/extension.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ const SYNC_EXPIRE_TOKEN_END: TimerToken = SYNC_EXPIRE_TOKEN_BEGIN + SYNC_EXPIRE_
4949
const SYNC_TIMER_INTERVAL: u64 = 1000;
5050
const SYNC_EXPIRE_REQUEST_INTERVAL: u64 = 15000;
5151

52-
const SNAPSHOT_PERIOD: u64 = (1 << 14);
53-
5452
#[derive(Debug, PartialEq)]
5553
pub struct TokenInfo {
5654
node_id: NodeId,
@@ -563,11 +561,9 @@ impl Extension {
563561
ctrace!(SYNC, "Received body request from {}", from);
564562
self.create_bodies_response(hashes)
565563
}
566-
RequestMessage::StateHead(hash) => self.create_state_head_response(hash),
567-
RequestMessage::StateChunk {
568-
block_hash,
569-
tree_root,
570-
} => self.create_state_chunk_response(block_hash, tree_root),
564+
RequestMessage::StateChunk(block_hash, chunk_root) => {
565+
self.create_state_chunk_response(block_hash, chunk_root)
566+
}
571567
};
572568

573569
self.api.send(from, Arc::new(Message::Response(id, response).rlp_bytes()));
@@ -579,21 +575,9 @@ impl Extension {
579575
..
580576
} => true,
581577
RequestMessage::Bodies(hashes) => !hashes.is_empty(),
582-
RequestMessage::StateHead(hash) => match self.client.block_number(&BlockId::Hash(*hash)) {
583-
Some(number) if number % SNAPSHOT_PERIOD == 0 => true,
584-
_ => false,
585-
},
586578
RequestMessage::StateChunk {
587-
block_hash,
588579
..
589-
} => {
590-
let _is_checkpoint = match self.client.block_number(&BlockId::Hash(*block_hash)) {
591-
Some(number) if number % SNAPSHOT_PERIOD == 0 => true,
592-
_ => false,
593-
};
594-
// FIXME: check tree_root
595-
unimplemented!()
596-
}
580+
} => unimplemented!(),
597581
}
598582
}
599583

@@ -631,11 +615,7 @@ impl Extension {
631615
ResponseMessage::Bodies(bodies)
632616
}
633617

634-
fn create_state_head_response(&self, _hash: BlockHash) -> ResponseMessage {
635-
unimplemented!()
636-
}
637-
638-
fn create_state_chunk_response(&self, _hash: BlockHash, _tree_root: H256) -> ResponseMessage {
618+
fn create_state_chunk_response(&self, _hash: BlockHash, _tree_root: Vec<H256>) -> ResponseMessage {
639619
unimplemented!()
640620
}
641621

@@ -676,7 +656,7 @@ impl Extension {
676656
self.on_body_response(hashes, bodies);
677657
self.check_sync_variable();
678658
}
679-
_ => unimplemented!(),
659+
ResponseMessage::StateChunk(..) => unimplemented!(),
680660
}
681661
}
682662
}
@@ -730,7 +710,6 @@ impl Extension {
730710
}
731711
true
732712
}
733-
(RequestMessage::StateHead(..), ResponseMessage::StateHead(..)) => unimplemented!(),
734713
(
735714
RequestMessage::StateChunk {
736715
..

sync/src/block/message/mod.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ const MESSAGE_ID_GET_HEADERS: u8 = 0x02;
2929
const MESSAGE_ID_HEADERS: u8 = 0x03;
3030
const MESSAGE_ID_GET_BODIES: u8 = 0x04;
3131
const MESSAGE_ID_BODIES: u8 = 0x05;
32-
const MESSAGE_ID_GET_STATE_HEAD: u8 = 0x06;
33-
const MESSAGE_ID_STATE_HEAD: u8 = 0x07;
34-
const MESSAGE_ID_GET_STATE_CHUNK: u8 = 0x08;
35-
const MESSAGE_ID_STATE_CHUNK: u8 = 0x09;
32+
const MESSAGE_ID_GET_STATE_CHUNK: u8 = 0x0a;
33+
const MESSAGE_ID_STATE_CHUNK: u8 = 0x0b;
3634

3735
#[derive(Debug, PartialEq)]
3836
pub enum Message {
@@ -114,11 +112,10 @@ impl Decodable for Message {
114112
let request_id = rlp.val_at(1)?;
115113
let message = rlp.at(2)?;
116114
match id {
117-
MESSAGE_ID_GET_HEADERS
118-
| MESSAGE_ID_GET_BODIES
119-
| MESSAGE_ID_GET_STATE_HEAD
120-
| MESSAGE_ID_GET_STATE_CHUNK => Ok(Message::Request(request_id, RequestMessage::decode(id, &message)?)),
121-
MESSAGE_ID_HEADERS | MESSAGE_ID_BODIES | MESSAGE_ID_STATE_HEAD | MESSAGE_ID_STATE_CHUNK => {
115+
MESSAGE_ID_GET_HEADERS | MESSAGE_ID_GET_BODIES | MESSAGE_ID_GET_STATE_CHUNK => {
116+
Ok(Message::Request(request_id, RequestMessage::decode(id, &message)?))
117+
}
118+
MESSAGE_ID_HEADERS | MESSAGE_ID_BODIES | MESSAGE_ID_STATE_CHUNK => {
122119
Ok(Message::Response(request_id, ResponseMessage::decode(id, &message)?))
123120
}
124121
_ => Err(DecoderError::Custom("Unknown message id detected")),
@@ -148,10 +145,4 @@ mod tests {
148145
let request_id = 10;
149146
rlp_encode_and_decode_test!(Message::Request(request_id, RequestMessage::Bodies(vec![])));
150147
}
151-
152-
#[test]
153-
fn request_state_head_rlp() {
154-
let request_id = 10;
155-
rlp_encode_and_decode_test!(Message::Request(request_id, RequestMessage::StateHead(H256::random().into())));
156-
}
157148
}

sync/src/block/message/request.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ pub enum RequestMessage {
2525
max_count: u64,
2626
},
2727
Bodies(Vec<BlockHash>),
28-
StateHead(BlockHash),
29-
StateChunk {
30-
block_hash: BlockHash,
31-
tree_root: H256,
32-
},
28+
StateChunk(BlockHash, Vec<H256>),
3329
}
3430

3531
impl Encodable for RequestMessage {
@@ -46,17 +42,10 @@ impl Encodable for RequestMessage {
4642
RequestMessage::Bodies(hashes) => {
4743
s.append_list(hashes);
4844
}
49-
RequestMessage::StateHead(block_hash) => {
50-
s.begin_list(1);
51-
s.append(block_hash);
52-
}
53-
RequestMessage::StateChunk {
54-
block_hash,
55-
tree_root,
56-
} => {
45+
RequestMessage::StateChunk(block_hash, merkle_roots) => {
5746
s.begin_list(2);
5847
s.append(block_hash);
59-
s.append(tree_root);
48+
s.append_list(merkle_roots);
6049
}
6150
};
6251
}
@@ -69,7 +58,6 @@ impl RequestMessage {
6958
..
7059
} => super::MESSAGE_ID_GET_HEADERS,
7160
RequestMessage::Bodies(..) => super::MESSAGE_ID_GET_BODIES,
72-
RequestMessage::StateHead(..) => super::MESSAGE_ID_GET_STATE_HEAD,
7361
RequestMessage::StateChunk {
7462
..
7563
} => super::MESSAGE_ID_GET_STATE_CHUNK,
@@ -92,16 +80,6 @@ impl RequestMessage {
9280
}
9381
}
9482
super::MESSAGE_ID_GET_BODIES => RequestMessage::Bodies(rlp.as_list()?),
95-
super::MESSAGE_ID_GET_STATE_HEAD => {
96-
let item_count = rlp.item_count()?;
97-
if item_count != 1 {
98-
return Err(DecoderError::RlpIncorrectListLen {
99-
got: item_count,
100-
expected: 1,
101-
})
102-
}
103-
RequestMessage::StateHead(rlp.val_at(0)?)
104-
}
10583
super::MESSAGE_ID_GET_STATE_CHUNK => {
10684
let item_count = rlp.item_count()?;
10785
if item_count != 2 {
@@ -110,10 +88,7 @@ impl RequestMessage {
11088
expected: 2,
11189
})
11290
}
113-
RequestMessage::StateChunk {
114-
block_hash: rlp.val_at(0)?,
115-
tree_root: rlp.val_at(1)?,
116-
}
91+
RequestMessage::StateChunk(rlp.val_at(0)?, rlp.list_at(1)?)
11792
}
11893
_ => return Err(DecoderError::Custom("Unknown message id detected")),
11994
};
@@ -149,18 +124,9 @@ mod tests {
149124
assert_eq!(message, decode_bytes(message.message_id(), message.rlp_bytes().as_ref()));
150125
}
151126

152-
#[test]
153-
fn request_state_head_message_rlp() {
154-
let message = RequestMessage::StateHead(H256::default().into());
155-
assert_eq!(message, decode_bytes(message.message_id(), message.rlp_bytes().as_ref()));
156-
}
157-
158127
#[test]
159128
fn request_state_chunk_message_rlp() {
160-
let message = RequestMessage::StateChunk {
161-
block_hash: H256::default().into(),
162-
tree_root: H256::default(),
163-
};
129+
let message = RequestMessage::StateChunk(H256::default().into(), vec![H256::default()]);
164130
assert_eq!(message, decode_bytes(message.message_id(), message.rlp_bytes().as_ref()));
165131
}
166132
}

sync/src/block/message/response.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use ctypes::Header;
2424
pub enum ResponseMessage {
2525
Headers(Vec<Header>),
2626
Bodies(Vec<Vec<UnverifiedTransaction>>),
27-
StateHead(Vec<u8>),
28-
StateChunk(Vec<u8>),
27+
StateChunk(Vec<Vec<u8>>),
2928
}
3029

3130
impl Encodable for ResponseMessage {
@@ -53,13 +52,8 @@ impl Encodable for ResponseMessage {
5352

5453
s.append(&compressed);
5554
}
56-
ResponseMessage::StateHead(bytes) => {
57-
s.begin_list(1);
58-
s.append(bytes);
59-
}
60-
ResponseMessage::StateChunk(bytes) => {
61-
s.begin_list(1);
62-
s.append(bytes);
55+
ResponseMessage::StateChunk(chunks) => {
56+
s.append_list::<Vec<u8>, Vec<u8>>(chunks);
6357
}
6458
};
6559
}
@@ -72,7 +66,6 @@ impl ResponseMessage {
7266
..
7367
} => super::MESSAGE_ID_HEADERS,
7468
ResponseMessage::Bodies(..) => super::MESSAGE_ID_BODIES,
75-
ResponseMessage::StateHead(..) => super::MESSAGE_ID_STATE_HEAD,
7669
ResponseMessage::StateChunk {
7770
..
7871
} => super::MESSAGE_ID_STATE_CHUNK,
@@ -109,26 +102,7 @@ impl ResponseMessage {
109102
}
110103
ResponseMessage::Bodies(bodies)
111104
}
112-
super::MESSAGE_ID_STATE_HEAD => {
113-
let item_count = rlp.item_count()?;
114-
if item_count != 1 {
115-
return Err(DecoderError::RlpIncorrectListLen {
116-
got: item_count,
117-
expected: 1,
118-
})
119-
}
120-
ResponseMessage::StateHead(rlp.val_at(0)?)
121-
}
122-
super::MESSAGE_ID_STATE_CHUNK => {
123-
let item_count = rlp.item_count()?;
124-
if item_count != 1 {
125-
return Err(DecoderError::RlpIncorrectListLen {
126-
got: item_count,
127-
expected: 1,
128-
})
129-
}
130-
ResponseMessage::StateChunk(rlp.val_at(0)?)
131-
}
105+
super::MESSAGE_ID_STATE_CHUNK => ResponseMessage::StateChunk(rlp.as_list()?),
132106
_ => return Err(DecoderError::Custom("Unknown message id detected")),
133107
};
134108

@@ -184,12 +158,6 @@ mod tests {
184158
assert_eq!(message, decode_bytes(message.message_id(), message.rlp_bytes().as_ref()));
185159
}
186160

187-
#[test]
188-
fn state_head_message_rlp() {
189-
let message = ResponseMessage::StateHead(vec![]);
190-
assert_eq!(message, decode_bytes(message.message_id(), message.rlp_bytes().as_ref()));
191-
}
192-
193161
#[test]
194162
fn state_chunk_message_rlp() {
195163
let message = ResponseMessage::StateChunk(vec![]);

util/merkle/src/snapshot/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
mod chunk;
17+
pub mod chunk;
1818
mod compress;
1919
mod error;
2020
mod ordered_heap;

0 commit comments

Comments
 (0)