-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
432 lines (389 loc) · 12.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package types
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
const (
LCPMessageVersion = 1
LCPMessageTypeUpdateClient = 1
LCPMessageTypeState = 2
)
const (
LCPMessageContextTypeEmpty = 0
LCPMessageContextTypeTrustingPeriod = 1
)
var (
commitmentProofABI, _ = abi.NewType("tuple", "struct CommitmentProof", []abi.ArgumentMarshaling{
{Name: "message", Type: "bytes"},
{Name: "signer", Type: "address"},
{Name: "signature", Type: "bytes"},
})
headeredMessageABI, _ = abi.NewType("tuple", "struct HeaderedMessage", []abi.ArgumentMarshaling{
{Name: "header", Type: "bytes32"},
{Name: "message", Type: "bytes"},
})
updateClientMessageABI, _ = abi.NewType("tuple", "struct UpdateClientMessage", []abi.ArgumentMarshaling{
{Name: "prev_height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "prev_state_id", Type: "bytes32"},
{Name: "post_height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "post_state_id", Type: "bytes32"},
{Name: "timestamp", Type: "uint128"},
{Name: "context", Type: "bytes"},
{Name: "emitted_states", Type: "tuple[]", Components: []abi.ArgumentMarshaling{
{Name: "height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "state", Type: "bytes"},
}},
})
headeredMessageContextABI, _ = abi.NewType("tuple", "struct HeaderedMessageContext", []abi.ArgumentMarshaling{
{Name: "header", Type: "bytes32"},
{Name: "context_bytes", Type: "bytes"},
})
trustingPeriodContextABI, _ = abi.NewType("tuple", "struct TrustingPeriodValidationContext", []abi.ArgumentMarshaling{
{Name: "timestamps", Type: "bytes32"},
{Name: "params", Type: "bytes32"},
})
verifyMembershipMessageABI, _ = abi.NewType("tuple", "struct VerifyMembershipMessage", []abi.ArgumentMarshaling{
{Name: "prefix", Type: "bytes"},
{Name: "path", Type: "bytes"},
{Name: "value", Type: "bytes32"},
{Name: "height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "state_id", Type: "bytes32"},
})
)
type StateID [32]byte
func (id StateID) EqualBytes(bz []byte) bool {
return bytes.Equal(id[:], bz)
}
type ELCUpdateClientMessage struct {
PrevHeight *clienttypes.Height
PrevStateID *StateID
PostHeight clienttypes.Height
PostStateID StateID
Timestamp *big.Int
Context ValidationContext
EmittedStates []EmittedState
}
type EmittedState struct {
Height clienttypes.Height
State codectypes.Any
}
// ValidationContext is the interface of validation context.
type ValidationContext interface {
Validate(time.Time) error
}
// EmptyValidationContext is the validation context for a commitment that does not require any validation.
type EmptyValidationContext struct{}
var _ ValidationContext = EmptyValidationContext{}
func (EmptyValidationContext) Validate(time.Time) error {
return nil
}
// TrustingPeriodValidationContext is the commitment context for a commitment that requires the current time to be within the trusting period.
type TrustingPeriodValidationContext struct {
UntrustedHeaderTimestamp time.Time
TrustedStateTimestamp time.Time
TrustingPeriod big.Int
ClockDrift big.Int
}
func DecodeTrustingPeriodValidationContext(timestamps, params [32]byte) *TrustingPeriodValidationContext {
// 0-15: untrusted_header_timestamp
// 16-31: trusted_state_timestamp
untrustedHeaderTimestamp := timestampNanosBytesToTime(timestamps[:16])
trustedStateTimestamp := timestampNanosBytesToTime(timestamps[16:32])
// 0-15: trusting_period
// 16-31: clock_drift
trustingPeriod := uint128BytesToBigInt(params[:16])
clockDrift := uint128BytesToBigInt(params[16:32])
return &TrustingPeriodValidationContext{
UntrustedHeaderTimestamp: untrustedHeaderTimestamp,
TrustedStateTimestamp: trustedStateTimestamp,
TrustingPeriod: trustingPeriod,
ClockDrift: clockDrift,
}
}
func uint128BytesToBigInt(bz []byte) big.Int {
if len(bz) != 16 {
panic("invalid length")
}
var durationNanos big.Int
durationNanos.SetBytes(bz)
return durationNanos
}
func timestampNanosBytesToTime(bz []byte) time.Time {
if len(bz) != 16 {
panic("invalid length")
}
var (
timestampNanos big.Int
secs big.Int
nanos big.Int
)
timestampNanos.SetBytes(bz)
secs.Div(×tampNanos, big.NewInt(1e9))
nanos.Mod(×tampNanos, big.NewInt(1e9))
return time.Unix(secs.Int64(), nanos.Int64())
}
var _ ValidationContext = TrustingPeriodValidationContext{}
func timeToBigInt(t time.Time) big.Int {
var (
secs big.Int
nanos big.Int
)
secs.SetInt64(t.Unix())
secs.Mul(&secs, big.NewInt(1e9))
nanos.SetInt64(int64(t.Nanosecond()))
secs.Add(&secs, &nanos)
return secs
}
func (c TrustingPeriodValidationContext) Validate(now time.Time) error {
currentTimestamp := timeToBigInt(now)
trustedStateTimestamp := timeToBigInt(c.TrustedStateTimestamp)
untrustedHeaderTimestamp := timeToBigInt(c.UntrustedHeaderTimestamp)
var (
trustingPeriodEnd big.Int
driftedCurrentTimestamp big.Int
)
trustingPeriodEnd.Add(&trustedStateTimestamp, &c.TrustingPeriod)
driftedCurrentTimestamp.Add(¤tTimestamp, &c.ClockDrift)
// ensure current timestamp is within trusting period
if currentTimestamp.Cmp(&trustingPeriodEnd) > 0 {
return fmt.Errorf("current time is after trusting period end: trusting_period_end=%v current=%v trusted_state_timestamp=%v trusting_period=%v", trustingPeriodEnd, now, c.TrustedStateTimestamp, c.TrustingPeriod)
}
// ensure header's timestamp indicates past
if untrustedHeaderTimestamp.Cmp(&driftedCurrentTimestamp) > 0 {
return fmt.Errorf("untrusted header timestamp is after current time: untrusted_header_timestamp=%v current=%v clock_drift=%v", c.UntrustedHeaderTimestamp, driftedCurrentTimestamp, c.ClockDrift)
}
return nil
}
type ELCVerifyMembershipMessage struct {
Prefix []byte
Path []byte
Value [32]byte
Height clienttypes.Height
StateID StateID
}
type CommitmentProof struct {
Message []byte
Signer common.Address
Signature []byte
}
func (p CommitmentProof) GetELCMessage() (*HeaderedELCMessage, error) {
return EthABIDecodeHeaderedMessage(p.Message)
}
type HeaderedELCMessage struct {
Version uint16
Type uint16
Message []byte
}
func (c HeaderedELCMessage) GetUpdateClientMessage() (*ELCUpdateClientMessage, error) {
if c.Version != LCPMessageVersion {
return nil, fmt.Errorf("unexpected commitment version: expected=%v actual=%v", LCPMessageVersion, c.Version)
}
if c.Type != LCPMessageTypeUpdateClient {
return nil, fmt.Errorf("unexpected commitment type: expected=%v actual=%v", LCPMessageTypeUpdateClient, c.Type)
}
return EthABIDecodeUpdateClientMessage(c.Message)
}
func (c HeaderedELCMessage) GetVerifyMembershipMessage() (*ELCVerifyMembershipMessage, error) {
if c.Version != LCPMessageVersion {
return nil, fmt.Errorf("unexpected commitment version: expected=%v actual=%v", LCPMessageVersion, c.Version)
}
if c.Type != LCPMessageTypeState {
return nil, fmt.Errorf("unexpected commitment type: expected=%v actual=%v", LCPMessageTypeState, c.Type)
}
return EthABIDecodeVerifyMembershipMessage(c.Message)
}
func EthABIEncodeCommitmentProof(p *CommitmentProof) ([]byte, error) {
packer := abi.Arguments{
{Type: commitmentProofABI},
}
return packer.Pack(p)
}
func EthABIDecodeCommitmentProof(bz []byte) (*CommitmentProof, error) {
unpacker := abi.Arguments{
{Type: commitmentProofABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := CommitmentProof(v[0].(struct {
Message []byte `json:"message"`
Signer common.Address `json:"signer"`
Signature []byte `json:"signature"`
}))
return &p, nil
}
func EthABIDecodeHeaderedMessage(bz []byte) (*HeaderedELCMessage, error) {
unpacker := abi.Arguments{
{Type: headeredMessageABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
Header [32]byte `json:"header"`
Message []byte `json:"message"`
})
// Header format:
// MSB first
// 0-1: version
// 2-3: message type
// 4-31: reserved
version := binary.BigEndian.Uint16(p.Header[:2])
messageType := binary.BigEndian.Uint16(p.Header[2:4])
return &HeaderedELCMessage{
Version: version,
Type: messageType,
Message: p.Message,
}, nil
}
func EthABIDecodeUpdateClientMessage(bz []byte) (*ELCUpdateClientMessage, error) {
unpacker := abi.Arguments{
{Type: updateClientMessageABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
PrevHeight struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"prev_height"`
PrevStateId [32]byte `json:"prev_state_id"`
PostHeight struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"post_height"`
PostStateId [32]byte `json:"post_state_id"`
Timestamp *big.Int `json:"timestamp"`
Context []byte `json:"context"`
EmittedStates []struct {
Height struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"height"`
State []byte `json:"state"`
} `json:"emitted_states"`
})
cctx, err := EthABIDecodeValidationContext(p.Context)
if err != nil {
return nil, err
}
c := &ELCUpdateClientMessage{
PostStateID: p.PostStateId,
PostHeight: clienttypes.Height{RevisionNumber: p.PostHeight.RevisionNumber, RevisionHeight: p.PostHeight.RevisionHeight},
Timestamp: p.Timestamp,
Context: cctx,
}
if p.PrevStateId != [32]byte{} {
prev := StateID(p.PrevStateId)
c.PrevStateID = &prev
}
if p.PrevHeight.RevisionNumber != 0 || p.PrevHeight.RevisionHeight != 0 {
c.PrevHeight = &clienttypes.Height{RevisionNumber: p.PrevHeight.RevisionNumber, RevisionHeight: p.PrevHeight.RevisionHeight}
}
for _, emitted := range p.EmittedStates {
var anyState codectypes.Any
if err := anyState.Unmarshal(emitted.State); err != nil {
return nil, err
}
c.EmittedStates = append(c.EmittedStates, EmittedState{
Height: clienttypes.Height{RevisionNumber: emitted.Height.RevisionNumber, RevisionHeight: emitted.Height.RevisionHeight},
State: anyState,
})
}
return c, nil
}
func EthABIDecodeValidationContext(bz []byte) (ValidationContext, error) {
unpacker := abi.Arguments{
{Type: headeredMessageContextABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
Header [32]byte `json:"header"`
ContextBytes []byte `json:"context_bytes"`
})
// Header format:
// MSB first
// 0-1: type
// 2-31: reserved
contextType := binary.BigEndian.Uint16(p.Header[:2])
switch contextType {
case LCPMessageContextTypeEmpty:
if len(p.ContextBytes) != 0 {
return nil, fmt.Errorf("unexpected context bytes for empty commitment context: %X", p.ContextBytes)
}
return &EmptyValidationContext{}, nil
case LCPMessageContextTypeTrustingPeriod:
return EthABIDecodeTrustingPeriodValidationContext(p.ContextBytes)
default:
return nil, fmt.Errorf("unexpected commitment context type: %v", contextType)
}
}
func EthABIDecodeTrustingPeriodValidationContext(bz []byte) (*TrustingPeriodValidationContext, error) {
if len(bz) != 64 {
return nil, fmt.Errorf("unexpected length of trusting period commitment context: %d", len(bz))
}
unpacker := abi.Arguments{
{Type: trustingPeriodContextABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
Timestamps [32]byte `json:"timestamps"`
Params [32]byte `json:"params"`
})
return DecodeTrustingPeriodValidationContext(p.Timestamps, p.Params), nil
}
func EthABIDecodeVerifyMembershipMessage(bz []byte) (*ELCVerifyMembershipMessage, error) {
unpacker := abi.Arguments{
{Type: verifyMembershipMessageABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
Prefix []byte `json:"prefix"`
Path []byte `json:"path"`
Value [32]byte `json:"value"`
Height struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"height"`
StateId [32]byte `json:"state_id"`
})
return &ELCVerifyMembershipMessage{
Prefix: p.Prefix,
Path: p.Path,
Value: p.Value,
Height: clienttypes.Height{RevisionNumber: p.Height.RevisionNumber, RevisionHeight: p.Height.RevisionHeight},
StateID: StateID(p.StateId),
}, nil
}