packet.gno
2.06 Kb · 74 lines
1package types
2
3import "gno.land/p/onbloc/encoding/abi"
4
5type PacketStatus uint8
6
7const (
8 PacketStatusUnknown PacketStatus = 0
9 PacketStatusSuccess PacketStatus = 1
10 PacketStatusFailure PacketStatus = 2
11 PacketStatusAsync PacketStatus = 3
12)
13
14// MustBeZero is a u64 that is always zero. Union packets carry no timeout
15// height (timeouts are expressed via the timestamp), so the ABI-encoded height
16// field is fixed to zero, mirroring the ibc-union-spec MustBeZero type.
17type MustBeZero uint64
18
19func (MustBeZero) String() string { return "0" }
20
21// Packet is a Union IBC packet. Mirroring the ibc-union-spec, the timeout
22// height is fixed to zero (see MustBeZero) and only the timeout timestamp is
23// meaningful.
24type Packet struct {
25 SourceChannelId ChannelId
26 DestinationChannelId ChannelId
27 Data []byte
28 TimeoutHeight MustBeZero
29 TimeoutTimestamp Timestamp
30}
31
32func (p Packet) EncodeABI() ([]byte, error) {
33 schema := abi.Schema{Fields: []abi.Field{
34 {Type: abi.TypeUint32},
35 {Type: abi.TypeUint32},
36 {Type: abi.TypeBytes},
37 {Type: abi.TypeUint64},
38 {Type: abi.TypeUint64},
39 }}
40
41 bz, err := abi.Encode(schema, []any{
42 uint32(p.SourceChannelId),
43 uint32(p.DestinationChannelId),
44 p.Data,
45 uint64(0), // timeout height is always zero (MustBeZero)
46 uint64(p.TimeoutTimestamp),
47 })
48 if err != nil {
49 return nil, err
50 }
51
52 return bz, nil
53}
54
55func NewPacket(sourceChannelId, destinationChannelId ChannelId, data []byte, timeoutTimestamp Timestamp) Packet {
56 return Packet{
57 SourceChannelId: sourceChannelId,
58 DestinationChannelId: destinationChannelId,
59 Data: data,
60 TimeoutHeight: 0,
61 TimeoutTimestamp: timeoutTimestamp,
62 }
63}
64
65// RecvPacketResult is the outcome an app returns from OnRecvPacket: the sync
66// status and, for non-async results, the acknowledgement bytes to commit.
67type RecvPacketResult struct {
68 Status PacketStatus
69 Acknowledgement []byte
70}
71
72func NewRecvPacketResult(status PacketStatus, acknowledgement []byte) RecvPacketResult {
73 return RecvPacketResult{Status: status, Acknowledgement: acknowledgement}
74}