Skip to content

Commit c6b7533

Browse files
committed
Import from github.com/allinbits/gno@aib/feat/ibc
Setup the required forked gno with: gnolang/gno#4868 tbruyelle/gno#1
0 parents  commit c6b7533

File tree

145 files changed

+13145
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+13145
-0
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
ifdef GNOROOT
3+
# If GNOROOT is already user defined, we need to override it with the
4+
# GNOROOT of the fork.
5+
# This is not required otherwise because the GNOROOT that originated the
6+
# binary is stored in a build flag.
7+
# (see -X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT)
8+
GNOROOT = $(shell go list -f '{{.Module.Dir}}' github.com/gnolang/gno)
9+
endif
10+
11+
gnodev:
12+
go tool gnodev -resolver root=. \
13+
-resolver root=$(shell go tool gno env GNOROOT)/examples
14+
15+
test:
16+
go tool gno test -v ./gno.land/...
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package encoding
2+
3+
import "encoding/binary"
4+
5+
// Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be
6+
// sorted
7+
func Uint64ToBigEndian(i uint64) []byte {
8+
b := make([]byte, 8)
9+
binary.BigEndian.PutUint64(b, i)
10+
return b
11+
}
12+
13+
// BigEndianToUint64 returns an uint64 from big endian encoded bytes. If
14+
// encoding is empty, zero is returned.
15+
func BigEndianToUint64(bz []byte) uint64 {
16+
if len(bz) == 0 {
17+
return 0
18+
}
19+
20+
return binary.BigEndian.Uint64(bz)
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module = "gno.land/p/aib/encoding"
2+
gno = "0.9"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module = "gno.land/p/aib/encoding/proto"
2+
gno = "0.9"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package proto
2+
3+
import (
4+
"encoding/binary"
5+
"time"
6+
7+
"gno.land/p/nt/ufmt"
8+
)
9+
10+
type WireType int
11+
12+
const (
13+
VARINT WireType = 0 // int32, int64, uint32, uint64, sint32, sint64, bool, enum
14+
FIXED64 WireType = 1 // fixed64, sfixed64, double
15+
LEN WireType = 2 // string, bytes, embedded messages, packed repeated fields
16+
FIXED32 WireType = 5 // fixed32, sfixed32, float
17+
)
18+
19+
// AppendVarint appends a varint field.
20+
func AppendVarint(buf []byte, fieldNum int, v uint64) []byte {
21+
if v == 0 {
22+
return buf
23+
}
24+
buf = AppendTag(buf, fieldNum, VARINT)
25+
return binary.AppendUvarint(buf, v)
26+
}
27+
28+
// AppendFixed64 appends a fixed 64-bit field.
29+
func AppendFixed64(buf []byte, fieldNum int, v uint64) []byte {
30+
if v == 0 {
31+
return buf
32+
}
33+
buf = AppendTag(buf, fieldNum, FIXED64)
34+
var b [8]byte
35+
binary.LittleEndian.PutUint64(b[:], v)
36+
return append(buf, b[:]...)
37+
}
38+
39+
// AppendLengthDelimited appends a length-delimited field.
40+
func AppendLengthDelimited(buf []byte, fieldNum int, bz []byte) []byte {
41+
if len(bz) == 0 {
42+
return buf
43+
}
44+
buf = AppendTag(buf, fieldNum, LEN)
45+
buf = binary.AppendUvarint(buf, uint64(len(bz)))
46+
return append(buf, bz...)
47+
}
48+
49+
// AppendTime appends a google.protobuf.Timestamp field.
50+
func AppendTime(buf []byte, fieldNum int, t time.Time) []byte {
51+
var (
52+
_buf []byte
53+
seconds = t.Unix()
54+
nanos = int32(t.Nanosecond())
55+
)
56+
// Field 1: seconds (int64 - varint)
57+
_buf = AppendVarint(_buf, 1, uint64(seconds))
58+
// Field 2: nanos (int32 - varint)
59+
_buf = AppendVarint(_buf, 2, uint64(nanos))
60+
61+
return AppendLengthDelimited(buf, fieldNum, _buf)
62+
}
63+
64+
// AppendTag appends a protobuf tag (field number and wire type)
65+
func AppendTag(buf []byte, fieldNum int, wireType WireType) []byte {
66+
tag := (fieldNum << 3) | int(wireType)
67+
return binary.AppendUvarint(buf, uint64(tag))
68+
}
69+
70+
// DecodeVarint reads a varint from the byte slice and returns the value and new position
71+
func DecodeVarint(buf []byte, pos int) (uint64, int, error) {
72+
var result uint64
73+
var shift uint
74+
for {
75+
if pos >= len(buf) {
76+
return 0, pos, ufmt.Errorf("buffer underflow while reading varint")
77+
}
78+
b := buf[pos]
79+
pos++
80+
result |= uint64(b&0x7F) << shift
81+
if b&0x80 == 0 {
82+
break
83+
}
84+
shift += 7
85+
}
86+
return result, pos, nil
87+
}
88+
89+
// DecodeString reads a string (length-prefixed) from the byte slice
90+
func DecodeString(buf []byte, pos int) (string, int, error) {
91+
length, newPos, err := DecodeVarint(buf, pos)
92+
if err != nil {
93+
return "", newPos, err
94+
}
95+
pos = newPos
96+
97+
if pos+int(length) > len(buf) {
98+
return "", pos, ufmt.Errorf("buffer underflow while reading string")
99+
}
100+
101+
str := string(buf[pos : pos+int(length)])
102+
return str, pos + int(length), nil
103+
}

gno.land/p/aib/ibc/app/app.gno

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package app
2+
3+
import (
4+
"chain/banker"
5+
6+
"gno.land/p/aib/ibc/types"
7+
)
8+
9+
// IBCApp defines an interface that implements all the callbacks that apps must
10+
// define as specified in IBC Protocol V2.
11+
type IBCApp interface {
12+
// OnSendPacket is executed when a packet is being sent from sending chain.
13+
// This callback is provided with the source and destination IDs, the signer,
14+
// the packet sequence and the packet data for this specific application.
15+
OnSendPacket(
16+
banker banker.Banker,
17+
sourceClient string,
18+
destinationClient string,
19+
sequence uint64,
20+
payload types.Payload,
21+
) error
22+
23+
// OnRecvPacket is executed when a packet is received from receiving chain.
24+
OnRecvPacket(
25+
sourceClient string,
26+
destinationClient string,
27+
sequence uint64,
28+
payload types.Payload,
29+
) types.RecvPacketResult
30+
31+
// OnTimeoutPacket is executed when a packet has timed out on the receiving
32+
// chain.
33+
OnTimeoutPacket(
34+
sourceClient string,
35+
destinationClient string,
36+
sequence uint64,
37+
payload types.Payload,
38+
) error
39+
40+
// OnAcknowledgementPacket is executed when a packet gets acknowledged.
41+
OnAcknowledgementPacket(
42+
sourceClient string,
43+
destinationClient string,
44+
sequence uint64,
45+
acknowledgement []byte,
46+
payload types.Payload,
47+
) error
48+
}

gno.land/p/aib/ibc/app/gnomod.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module = "gno.land/p/aib/ibc/app"
2+
gno = "0.9"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module = "gno.land/p/aib/ibc/host"
2+
gno = "0.9"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package host
2+
3+
import "gno.land/p/aib/encoding"
4+
5+
const (
6+
PacketCommitmentBasePrefix = byte(1)
7+
PacketReceiptBasePrefix = byte(2)
8+
PacketAcknowledgementBasePrefix = byte(3)
9+
)
10+
11+
// PacketCommitmentPrefixKey returns the store key prefix under which packet
12+
// commitments for a particular client are stored.
13+
// clientID must be a generated identifier, not provided externally so key
14+
// collisions are not possible.
15+
func PacketCommitmentPrefixKey(clientID string) []byte {
16+
return append([]byte(clientID), PacketCommitmentBasePrefix)
17+
}
18+
19+
// PacketCommitmentKey returns the store key of under which a packet commitment
20+
// is stored.
21+
// clientID must be a generated identifier, not provided externally so key
22+
// collisions are not possible.
23+
func PacketCommitmentKey(clientID string, sequence uint64) []byte {
24+
return append(PacketCommitmentPrefixKey(clientID), encoding.Uint64ToBigEndian(sequence)...)
25+
}
26+
27+
// PacketReceiptPrefixKey returns the store key prefix under which packet
28+
// receipts for a particular channel are stored.
29+
// clientID must be a generated identifier, not provided externally so key
30+
// collisions are not possible.
31+
func PacketReceiptPrefixKey(clientID string) []byte {
32+
return append([]byte(clientID), PacketReceiptBasePrefix)
33+
}
34+
35+
// PacketReceiptKey returns the store key of under which a packet receipt is
36+
// stored.
37+
// clientID must be a generated identifier, not provided externally so key
38+
// collisions are not possible.
39+
func PacketReceiptKey(clientID string, sequence uint64) []byte {
40+
return append(PacketReceiptPrefixKey(clientID), encoding.Uint64ToBigEndian(sequence)...)
41+
}
42+
43+
// PacketAcknowledgementPrefixKey returns the store key prefix under which
44+
// packet acknowledgements for a particular channel are stored. clientID must
45+
// be a generated identifier, not provided externally so key collisions are not
46+
// possible.
47+
func PacketAcknowledgementPrefixKey(clientID string) []byte {
48+
return append([]byte(clientID), PacketAcknowledgementBasePrefix)
49+
}
50+
51+
// PacketAcknowledgementKey returns the store key of under which a packet
52+
// acknowledgement is stored.
53+
// clientID must be a generated identifier, not provided externally so key
54+
// collisions are not possible.
55+
func PacketAcknowledgementKey(clientID string, sequence uint64) []byte {
56+
return append(PacketAcknowledgementPrefixKey(clientID), encoding.Uint64ToBigEndian(sequence)...)
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package host_test
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"gno.land/p/aib/ibc/host"
8+
"gno.land/p/nt/urequire"
9+
)
10+
11+
// TestPacketCommitmentKey is primarily used to document the expected key output
12+
// so that other implementations (such as the IBC Solidity) can replicate the
13+
// same key output. But it is also useful to catch any changes in the keys.
14+
func TestPacketCommitmentKey(t *testing.T) {
15+
actual := hex.EncodeToString(host.PacketCommitmentKey("channel-0", 1))
16+
urequire.Equal(t, "6368616e6e656c2d30010000000000000001", actual)
17+
}
18+
19+
// TestPacketReceiptKey is primarily used to document the expected key output
20+
// so that other implementations (such as the IBC Solidity) can replicate the
21+
// same key output. But it is also useful to catch any changes in the keys.
22+
func TestPacketReceiptKey(t *testing.T) {
23+
actual := hex.EncodeToString(host.PacketReceiptKey("channel-0", 1))
24+
urequire.Equal(t, "6368616e6e656c2d30020000000000000001", actual)
25+
}
26+
27+
// TestPacketAcknowledgementKey is primarily used to document the expected key output
28+
// so that other implementations (such as the IBC Solidity) can replicate the
29+
// same key output. But it is also useful to catch any changes in the keys.
30+
func TestPacketAcknowledgementKey(t *testing.T) {
31+
actual := hex.EncodeToString(host.PacketAcknowledgementKey("channel-0", 1))
32+
urequire.Equal(t, "6368616e6e656c2d30030000000000000001", actual)
33+
}

0 commit comments

Comments
 (0)