/
commitment.go
405 lines (363 loc) · 12 KB
/
commitment.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
package types
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"time"
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 (
LCPCommitmentVersion = 1
LCPCommitmentTypeUpdateClient = 1
LCPCommitmentTypeState = 2
)
const (
LCPCommitmentContextTypeEmpty = 0
LCPCommitmentContextTypeTrustingPeriod = 1
)
var (
commitmentProofABI, _ = abi.NewType("tuple", "struct CommitmentProof", []abi.ArgumentMarshaling{
{Name: "commitment_bytes", Type: "bytes"},
{Name: "signer", Type: "address"},
{Name: "signature", Type: "bytes"},
})
headeredCommitmentABI, _ = abi.NewType("tuple", "struct HeaderedCommitment", []abi.ArgumentMarshaling{
{Name: "header", Type: "bytes32"},
{Name: "commitment", Type: "bytes"},
})
updateClientCommitmentABI, _ = abi.NewType("tuple", "struct UpdateClientCommitment", []abi.ArgumentMarshaling{
{Name: "prev_state_id", Type: "bytes32"},
{Name: "new_state_id", Type: "bytes32"},
{Name: "new_state", Type: "bytes"},
{Name: "prev_height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "new_height", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "revision_number", Type: "uint64"},
{Name: "revision_height", Type: "uint64"},
}},
{Name: "timestamp", Type: "uint128"},
{Name: "context", Type: "bytes"},
})
headeredCommitmentContextABI, _ = abi.NewType("tuple", "struct HeaderedCommitmentContext", []abi.ArgumentMarshaling{
{Name: "header", Type: "bytes32"},
{Name: "context_bytes", Type: "bytes"},
})
trustingPeriodContextABI, _ = abi.NewType("tuple", "struct TrustingPeriodCommitmentContext", []abi.ArgumentMarshaling{
{Name: "timestamps", Type: "bytes32"},
{Name: "params", Type: "bytes32"},
})
stateCommitmentABI, _ = abi.NewType("tuple", "struct StateCommitment", []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 UpdateClientCommitment struct {
PrevStateID *StateID
NewStateID StateID
NewState []byte
PrevHeight *clienttypes.Height
NewHeight clienttypes.Height
Timestamp *big.Int
Context CommitmentContext
}
// CommitmentContext is the interface for the context of a commitment.
type CommitmentContext interface {
Validate(time.Time) error
}
// NoneCommitmentContext is the commitment context for a commitment that does not require any validation.
type NoneCommitmentContext struct{}
var _ CommitmentContext = NoneCommitmentContext{}
func (NoneCommitmentContext) Validate(time.Time) error {
return nil
}
// TrustingPeriodCommitmentContext is the commitment context for a commitment that requires the current time to be within the trusting period.
type TrustingPeriodCommitmentContext struct {
UntrustedHeaderTimestamp time.Time
TrustedStateTimestamp time.Time
TrustingPeriod big.Int
ClockDrift big.Int
}
func DecodeTrustingPeriodCommitmentContext(timestamps, params [32]byte) *TrustingPeriodCommitmentContext {
// 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 &TrustingPeriodCommitmentContext{
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 _ CommitmentContext = TrustingPeriodCommitmentContext{}
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 TrustingPeriodCommitmentContext) 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 StateCommitment struct {
Prefix []byte
Path []byte
Value [32]byte
Height clienttypes.Height
StateID StateID
}
type CommitmentProof struct {
CommitmentBytes []byte
Signer common.Address
Signature []byte
}
func (p CommitmentProof) GetCommitment() (*HeaderedCommitment, error) {
return EthABIDecodeHeaderedCommitment(p.CommitmentBytes)
}
type HeaderedCommitment struct {
Version uint16
Type uint16
Commitment []byte
}
func (c HeaderedCommitment) GetUpdateClientCommitment() (*UpdateClientCommitment, error) {
if c.Version != LCPCommitmentVersion {
return nil, fmt.Errorf("unexpected commitment version: expected=%v actual=%v", LCPCommitmentVersion, c.Version)
}
if c.Type != LCPCommitmentTypeUpdateClient {
return nil, fmt.Errorf("unexpected commitment type: expected=%v actual=%v", LCPCommitmentTypeUpdateClient, c.Type)
}
return EthABIDecodeUpdateClientCommitment(c.Commitment)
}
func (c HeaderedCommitment) GetStateCommitment() (*StateCommitment, error) {
if c.Version != LCPCommitmentVersion {
return nil, fmt.Errorf("unexpected commitment version: expected=%v actual=%v", LCPCommitmentVersion, c.Version)
}
if c.Type != LCPCommitmentTypeState {
return nil, fmt.Errorf("unexpected commitment type: expected=%v actual=%v", LCPCommitmentTypeState, c.Type)
}
return EthABIDecodeStateCommitment(c.Commitment)
}
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 {
CommitmentBytes []byte `json:"commitment_bytes"`
Signer common.Address `json:"signer"`
Signature []byte `json:"signature"`
}))
return &p, nil
}
func EthABIDecodeHeaderedCommitment(bz []byte) (*HeaderedCommitment, error) {
unpacker := abi.Arguments{
{Type: headeredCommitmentABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
Header [32]byte `json:"header"`
Commitment []byte `json:"commitment"`
})
// Header format:
// MSB first
// 0-1: version
// 2-3: commitment type
// 4-31: reserved
version := binary.BigEndian.Uint16(p.Header[:2])
commitmentType := binary.BigEndian.Uint16(p.Header[2:4])
return &HeaderedCommitment{
Version: version,
Type: commitmentType,
Commitment: p.Commitment,
}, nil
}
func EthABIDecodeUpdateClientCommitment(bz []byte) (*UpdateClientCommitment, error) {
unpacker := abi.Arguments{
{Type: updateClientCommitmentABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
}
p := v[0].(struct {
PrevStateId [32]byte `json:"prev_state_id"`
NewStateId [32]byte `json:"new_state_id"`
NewState []byte `json:"new_state"`
PrevHeight struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"prev_height"`
NewHeight struct {
RevisionNumber uint64 `json:"revision_number"`
RevisionHeight uint64 `json:"revision_height"`
} `json:"new_height"`
Timestamp *big.Int `json:"timestamp"`
Context []byte `json:"context"`
})
cctx, err := EthABIDecodeCommitmentContext(p.Context)
if err != nil {
return nil, err
}
c := &UpdateClientCommitment{
NewStateID: p.NewStateId,
NewState: p.NewState,
NewHeight: clienttypes.Height{RevisionNumber: p.NewHeight.RevisionNumber, RevisionHeight: p.NewHeight.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}
}
return c, nil
}
func EthABIDecodeCommitmentContext(bz []byte) (CommitmentContext, error) {
unpacker := abi.Arguments{
{Type: headeredCommitmentContextABI},
}
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 LCPCommitmentContextTypeEmpty:
if len(p.ContextBytes) != 0 {
return nil, fmt.Errorf("unexpected context bytes for empty commitment context: %X", p.ContextBytes)
}
return &NoneCommitmentContext{}, nil
case LCPCommitmentContextTypeTrustingPeriod:
return EthABIDecodeTrustingPeriodCommitmentContext(p.ContextBytes)
default:
return nil, fmt.Errorf("unexpected commitment context type: %v", contextType)
}
}
func EthABIDecodeTrustingPeriodCommitmentContext(bz []byte) (*TrustingPeriodCommitmentContext, 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 DecodeTrustingPeriodCommitmentContext(p.Timestamps, p.Params), nil
}
func EthABIDecodeStateCommitment(bz []byte) (*StateCommitment, error) {
unpacker := abi.Arguments{
{Type: stateCommitmentABI},
}
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 &StateCommitment{
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
}