-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmessage.go
219 lines (180 loc) · 5.35 KB
/
message.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
package types
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
const (
// MessageBodyOffset is the message body offset.
MessageBodyOffset = MessageHeaderSize
)
// Message is an interface that contains the message.
//
//nolint:interfacebloat
type Message interface {
// Header gets the message header
Header() Header
// BaseMessage is the base message if the flag indicates the type is a base message
BaseMessage() BaseMessage
// Body gets the message body
Body() []byte
// OriginDomain returns the Slip-44 ID
OriginDomain() uint32
// Nonce is the count of all previous messages to the destination
Nonce() uint32
// DestinationDomain is the slip-44 id of the destination
DestinationDomain() uint32
// ToLeaf converts a leaf to a keccac256
ToLeaf() (leaf [32]byte, err error)
// OptimisticSeconds gets the optimistic seconds count
OptimisticSeconds() uint32
}
// messageImpl implements a message. It is used for testutils. Real messages are emitted by the contract.
type messageImpl struct {
header Header
baseMessage BaseMessage
body []byte
}
const headerOffset uint16 = 0
// NewMessageFromBaseMessage creates a generic message given a base message.
func NewMessageFromBaseMessage(header Header, baseMessage BaseMessage) (Message, error) {
if header.Flag() != MessageFlagBase {
return nil, fmt.Errorf("header flag is not base")
}
baseMessageBytes, err := EncodeBaseMessage(baseMessage)
if err != nil {
return nil, fmt.Errorf("could not encode base message: %w", err)
}
return &messageImpl{
header: header,
baseMessage: baseMessage,
body: baseMessageBytes,
}, nil
}
// NewMessageFromManagerMessage creates a generic message given a manager message.
func NewMessageFromManagerMessage(header Header, payload []byte) (Message, error) {
if header.Flag() != MessageFlagManager {
return nil, fmt.Errorf("header flag is not manager")
}
return &messageImpl{
header: header,
baseMessage: nil,
body: payload,
}, nil
}
// NewMessage creates a new message from fields passed in.
// TODO: Set up different initializers. One for BaseMessage and one for ManagerMessage.
func NewMessage(header Header, baseMessage BaseMessage, body []byte) Message {
return &messageImpl{
header: header,
baseMessage: baseMessage,
body: body,
}
}
func (m messageImpl) Header() Header {
return m.header
}
func (m messageImpl) OriginDomain() uint32 {
return m.Header().OriginDomain()
}
func (m messageImpl) Nonce() uint32 {
return m.Header().Nonce()
}
func (m messageImpl) DestinationDomain() uint32 {
return m.Header().DestinationDomain()
}
func (m messageImpl) OptimisticSeconds() uint32 {
return m.Header().OptimisticSeconds()
}
func (m messageImpl) BaseMessage() BaseMessage {
return m.baseMessage
}
func (m messageImpl) Body() []byte {
return m.body
}
// ToLeaf converts a message to an encoded leaf.
func (m messageImpl) ToLeaf() (leaf [32]byte, err error) {
var toHash []byte
if m.Header().Flag() == MessageFlagBase {
leaf, err = m.BaseMessage().Leaf()
if err != nil {
return common.Hash{}, fmt.Errorf("could not get leaf: %w", err)
}
toHash = leaf[:]
} else {
toHash = crypto.Keccak256(m.Body())
}
headerLeaf, err := m.Header().Leaf()
if err != nil {
return common.Hash{}, fmt.Errorf("could not get header leaf: %w", err)
}
rawLeaf := crypto.Keccak256(headerLeaf[:], toHash)
copy(leaf[:], rawLeaf)
return leaf, nil
}
// CommittedMessage is the message that got committed.
type CommittedMessage interface {
// Message is the fully detailed message that was committed
Message() []byte
// Leaf gets a leaf
Leaf() [32]byte
// Encode encodes a message
Encode() ([]byte, error)
}
// NewCommittedMessage creates a new committed message.
func NewCommittedMessage(message []byte) CommittedMessage {
return committedMessageImpl{
message: message,
}
}
// commitedMessageImpl contains the implementation of a committed message.
type committedMessageImpl struct {
committedRoot common.Hash
message []byte
}
// CommittedMessageEncoder is used to export fields for struct encoding.
type CommittedMessageEncoder struct {
CommittedRoot common.Hash
Message []byte
}
// DecodeCommittedMessage decodes a committed message into a struct.
func DecodeCommittedMessage(rawMessage []byte) (CommittedMessage, error) {
dec := gob.NewDecoder(bytes.NewReader(rawMessage))
var msg CommittedMessageEncoder
err := dec.Decode(&msg)
if err != nil {
return nil, fmt.Errorf("could not decode message: %w", err)
}
decoded := committedMessageImpl{
committedRoot: msg.CommittedRoot,
message: msg.Message,
}
return decoded, nil
}
// Encode encodes a committed message.
// Deprecated: will be removed.
func (c committedMessageImpl) Encode() ([]byte, error) {
newCommittedMessage := CommittedMessageEncoder{
CommittedRoot: c.committedRoot,
Message: c.message,
}
var res bytes.Buffer
enc := gob.NewEncoder(&res)
err := enc.Encode(newCommittedMessage)
if err != nil {
return nil, fmt.Errorf("could not encode %T: %w", newCommittedMessage, err)
}
return res.Bytes(), nil
}
// Leaf gets the leaf.
func (c committedMessageImpl) Leaf() (leaf [32]byte) {
rawLeaf := crypto.Keccak256(c.message)
copy(leaf[:], rawLeaf)
return leaf
}
// Message gets the message.
func (c committedMessageImpl) Message() []byte {
return c.message
}