-
Notifications
You must be signed in to change notification settings - Fork 82
/
output.go
222 lines (201 loc) · 6.29 KB
/
output.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package eth
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/kroma-network/kroma/kroma-bindings/bindings"
)
// [Kroma: START]
type PublicInputProof struct {
NextBlock *types.Header `json:"nextBlock"`
NextTransactions types.Transactions `json:"nextTransactions"`
L2ToL1MessagePasserBalance *big.Int `json:"l2ToL1MessagePasserBalance"`
L2ToL1MessagePasserCodeHash common.Hash `json:"l2ToL1MessagePasserCodeHash"`
MerkleProof []hexutil.Bytes `json:"merkleProof"`
}
// [Kroma: END]
type OutputResponse struct {
Version Bytes32 `json:"version"`
OutputRoot Bytes32 `json:"outputRoot"`
BlockRef L2BlockRef `json:"blockRef"`
WithdrawalStorageRoot common.Hash `json:"withdrawalStorageRoot"`
StateRoot common.Hash `json:"stateRoot"`
Status *SyncStatus `json:"syncStatus"`
// [Kroma: START]
NextBlockRef L2BlockRef `json:"nextBlockRef"`
PublicInputProof *PublicInputProof `json:"publicInputProof"`
// [Kroma: END]
}
type SafeHeadResponse struct {
L1Block BlockID `json:"l1Block"`
SafeHead BlockID `json:"safeHead"`
}
var (
ErrBlockIsEmpty = errors.New("block is empty")
ErrInvalidOutput = errors.New("invalid output")
ErrInvalidOutputVersion = errors.New("invalid output version")
OutputVersionV0 = Bytes32{}
)
type Output interface {
// Version returns the version of the L2 output
Version() Bytes32
// Marshal a L2 output into a byte slice for hashing
Marshal() []byte
}
type OutputV0 struct {
StateRoot Bytes32
MessagePasserStorageRoot Bytes32
BlockHash common.Hash
// [Kroma: START]
NextBlockHash common.Hash
// [Kroma: END]
}
func (o *OutputV0) Version() Bytes32 {
return OutputVersionV0
}
func (o *OutputV0) Marshal() []byte {
var buf [160]byte
version := o.Version()
copy(buf[:32], version[:])
copy(buf[32:], o.StateRoot[:])
copy(buf[64:], o.MessagePasserStorageRoot[:])
copy(buf[96:], o.BlockHash[:])
copy(buf[128:], o.NextBlockHash[:])
return buf[:]
}
// OutputRoot returns the keccak256 hash of the marshaled L2 output
func OutputRoot(output Output) Bytes32 {
marshaled := output.Marshal()
return Bytes32(crypto.Keccak256Hash(marshaled))
}
func UnmarshalOutput(data []byte) (Output, error) {
if len(data) < 32 {
return nil, ErrInvalidOutput
}
var ver Bytes32
copy(ver[:], data[:32])
switch ver {
case OutputVersionV0:
return unmarshalOutputV0(data)
default:
return nil, ErrInvalidOutputVersion
}
}
func unmarshalOutputV0(data []byte) (*OutputV0, error) {
if len(data) != 160 {
return nil, ErrInvalidOutput
}
var output OutputV0
// data[:32] is the version
copy(output.StateRoot[:], data[32:64])
copy(output.MessagePasserStorageRoot[:], data[64:96])
copy(output.BlockHash[:], data[96:128])
copy(output.NextBlockHash[:], data[128:160])
return &output, nil
}
func (o *OutputResponse) ToOutputRootProof() bindings.TypesOutputRootProof {
return bindings.TypesOutputRootProof{
Version: o.Version,
StateRoot: o.StateRoot,
MessagePasserStorageRoot: o.WithdrawalStorageRoot,
BlockHash: o.BlockRef.Hash,
NextBlockHash: o.NextBlockRef.Hash,
}
}
func (o *OutputResponse) ToPublicInput() (bindings.TypesPublicInput, error) {
p := o.PublicInputProof
if p.NextBlock == nil {
return bindings.TypesPublicInput{}, ErrBlockIsEmpty
}
var withdrawalsRoot common.Hash
if p.NextBlock.WithdrawalsHash != nil {
withdrawalsRoot = *p.NextBlock.WithdrawalsHash
}
txHashes := make([][32]byte, len(p.NextTransactions))
for i, tx := range p.NextTransactions {
txHashes[i] = tx.Hash()
}
var blobGasUsed, excessBlobGas uint64
if p.NextBlock.BlobGasUsed != nil {
blobGasUsed = *p.NextBlock.BlobGasUsed
}
if p.NextBlock.ExcessBlobGas != nil {
excessBlobGas = *p.NextBlock.ExcessBlobGas
}
var parentBeaconRoot common.Hash
if o.PublicInputProof.NextBlock.ParentBeaconRoot != nil {
parentBeaconRoot = *o.PublicInputProof.NextBlock.ParentBeaconRoot
}
return bindings.TypesPublicInput{
BlockHash: o.NextBlockRef.Hash,
ParentHash: o.BlockRef.Hash,
Timestamp: p.NextBlock.Time,
Number: p.NextBlock.Number.Uint64(),
GasLimit: p.NextBlock.GasLimit,
BaseFee: p.NextBlock.BaseFee,
TransactionsRoot: p.NextBlock.TxHash,
StateRoot: p.NextBlock.Root,
WithdrawalsRoot: withdrawalsRoot,
TxHashes: txHashes,
BlobGasUsed: blobGasUsed,
ExcessBlobGas: excessBlobGas,
ParentBeaconRoot: parentBeaconRoot,
}, nil
}
func (o *OutputResponse) ToBlockHeaderRLP() (bindings.TypesBlockHeaderRLP, error) {
p := o.PublicInputProof
if p.NextBlock == nil {
return bindings.TypesBlockHeaderRLP{}, ErrBlockIsEmpty
}
uncleHash, err := rlp.EncodeToBytes(types.EmptyUncleHash)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
coinbase, err := rlp.EncodeToBytes(p.NextBlock.Coinbase)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
receiptsRoot, err := rlp.EncodeToBytes(p.NextBlock.ReceiptHash)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
logsBloom, err := rlp.EncodeToBytes(p.NextBlock.Bloom)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
difficulty, err := rlp.EncodeToBytes(p.NextBlock.Difficulty)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
gasUsed, err := rlp.EncodeToBytes(p.NextBlock.GasUsed)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
extraData, err := rlp.EncodeToBytes(p.NextBlock.Extra)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
mixHash, err := rlp.EncodeToBytes(p.NextBlock.MixDigest)
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
nonce, err := rlp.EncodeToBytes(types.BlockNonce{})
if err != nil {
return bindings.TypesBlockHeaderRLP{}, err
}
return bindings.TypesBlockHeaderRLP{
UncleHash: uncleHash,
Coinbase: coinbase,
ReceiptsRoot: receiptsRoot,
LogsBloom: logsBloom,
Difficulty: difficulty,
GasUsed: gasUsed,
ExtraData: extraData,
MixHash: mixHash,
Nonce: nonce,
}, nil
}