-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathsender.rs
More file actions
92 lines (84 loc) · 2.91 KB
/
sender.rs
File metadata and controls
92 lines (84 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2026, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md
use crate::transfer::primitives::{write_bytes, write_u32, write_u64, write_u8};
use crate::transfer::{markers, IOResult};
use crate::{GoGlobalState, Inbox, Preimages, ValidationInput};
use std::collections::BTreeMap;
use std::io::Write;
pub fn send_validation_input(writer: &mut impl Write, input: &ValidationInput) -> IOResult<()> {
send_globals(writer, &input.small_globals, &input.large_globals)?;
send_inbox(writer, &input.sequencer_messages)?;
send_inbox(writer, &input.delayed_messages)?;
send_preimages(writer, &input.preimages)?;
send_module_asms(writer, &input.module_asms)?;
finish_sending(writer)?;
writer.write_all(&input.end_parent_chain_block_hash)
}
pub fn send_successful_response(
writer: &mut impl Write,
new_state: &GoGlobalState,
memory_used: u64,
) -> IOResult<()> {
write_u8(writer, markers::SUCCESS)?;
send_globals(
writer,
&[new_state.batch, new_state.pos_in_batch],
&[
new_state.block_hash.0,
new_state.send_root.0,
new_state.mel_state_hash.0,
new_state.mel_msg_hash.0,
],
)?;
write_u64(writer, memory_used)
}
pub fn send_failure_response(writer: &mut impl Write, error_message: &str) -> IOResult<()> {
write_u8(writer, markers::FAILURE)?;
write_bytes(writer, error_message.as_bytes())
}
fn send_globals(
writer: &mut impl Write,
small_globals: &[u64; 2],
large_globals: &[[u8; 32]; 4],
) -> IOResult<()> {
write_u64(writer, small_globals[0])?;
write_u64(writer, small_globals[1])?;
writer.write_all(&large_globals[0])?;
writer.write_all(&large_globals[1])?;
writer.write_all(&large_globals[2])?;
writer.write_all(&large_globals[3])
}
fn send_inbox(writer: &mut impl Write, inbox: &Inbox) -> IOResult<()> {
for (number, data) in inbox {
write_u8(writer, markers::ANOTHER)?;
write_u64(writer, *number)?;
write_bytes(writer, data)?;
}
write_u8(writer, markers::SUCCESS)
}
fn send_preimages(writer: &mut impl Write, preimages: &Preimages) -> IOResult<()> {
write_u32(writer, preimages.len() as u32)?;
for (preimage_type, preimage_map) in preimages {
write_u8(writer, *preimage_type)?;
write_u32(writer, preimage_map.len() as u32)?;
for (hash, preimage) in preimage_map {
writer.write_all(hash)?;
write_bytes(writer, preimage)?;
}
}
Ok(())
}
fn send_module_asms(
writer: &mut impl Write,
module_asms: &BTreeMap<[u8; 32], Vec<u8>>,
) -> IOResult<()> {
write_u32(writer, module_asms.len() as u32)?;
for (hash, asm) in module_asms {
writer.write_all(hash)?;
write_bytes(writer, asm)?;
}
Ok(())
}
fn finish_sending(writer: &mut impl Write) -> IOResult<()> {
write_u8(writer, markers::READY)
}