Skip to content

Commit

Permalink
added coreConnection module
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuwei committed Oct 28, 2023
1 parent af257d5 commit f0d524b
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/indexer/decode/handle/coreConnection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package handle

import (
"github.com/celenium-io/celestia-indexer/internal/storage"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/celenium-io/celestia-indexer/pkg/types"
coreConnection "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
)

// MsgConnectionOpenInit defines the msg sent by an account on Chain A to initialize a connection with Chain B.
func MsgConnectionOpenInit(level types.Level, m *coreConnection.MsgConnectionOpenInit) (storageTypes.MsgType, []storage.AddressWithType, error) {
msgType := storageTypes.MsgConnectionOpenInit
addresses, err := createAddresses(addressesData{
{t: storageTypes.MsgAddressTypeSigner, address: m.Signer},
}, level)
return msgType, addresses, err
}

// MsgConnectionOpenTry defines a msg sent by a Relayer to try to open a connection on Chain B.
func MsgConnectionOpenTry(level types.Level, m *coreConnection.MsgConnectionOpenTry) (storageTypes.MsgType, []storage.AddressWithType, error) {
msgType := storageTypes.MsgConnectionOpenTry
addresses, err := createAddresses(addressesData{
{t: storageTypes.MsgAddressTypeSigner, address: m.Signer},
}, level)
return msgType, addresses, err
}

// MsgConnectionOpenAck defines a msg sent by a Relayer to Chain A to
// acknowledge the change of connection state to TRYOPEN on Chain B.
func MsgConnectionOpenAck(level types.Level, m *coreConnection.MsgConnectionOpenAck) (storageTypes.MsgType, []storage.AddressWithType, error) {
msgType := storageTypes.MsgConnectionOpenAck
addresses, err := createAddresses(addressesData{
{t: storageTypes.MsgAddressTypeSigner, address: m.Signer},
}, level)
return msgType, addresses, err
}

// MsgConnectionOpenConfirm defines a msg sent by a Relayer to Chain B to
// acknowledge the change of connection state to OPEN on Chain A.
func MsgConnectionOpenConfirm(level types.Level, m *coreConnection.MsgConnectionOpenConfirm) (storageTypes.MsgType, []storage.AddressWithType, error) {
msgType := storageTypes.MsgConnectionOpenConfirm
addresses, err := createAddresses(addressesData{
{t: storageTypes.MsgAddressTypeSigner, address: m.Signer},
}, level)
return msgType, addresses, err
}
189 changes: 189 additions & 0 deletions pkg/indexer/decode/handle/test/coreConnection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package handle

import (
"github.com/celenium-io/celestia-indexer/internal/storage"
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
testsuite "github.com/celenium-io/celestia-indexer/internal/test_suite"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode"
coreConnection "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
"github.com/fatih/structs"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDecodeMsg_SuccessOnMsgConnectionOpenInit(t *testing.T) {
msg := &coreConnection.MsgConnectionOpenInit{
Signer: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
}
blob, now := testsuite.EmptyBlock()
position := 0

dm, err := decode.Message(msg, blob.Height, blob.Block.Time, position, storageTypes.StatusSuccess)

addressesExpected := []storage.AddressWithType{
{
Type: storageTypes.MsgAddressTypeSigner,
Address: storage.Address{
Id: 0,
Height: blob.Height,
LastHeight: blob.Height,
Address: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
Hash: []byte{0x94, 0x63, 0x42, 0xc7, 0x73, 0x2f, 0x6, 0x22, 0x37, 0xb6, 0x73, 0xf5, 0x26, 0xf2, 0x67, 0x4c, 0x7d, 0xb, 0xff, 0x98},
Balance: storage.Balance{
Id: 0,
Total: decimal.Zero,
},
},
},
}

msgExpected := storage.Message{
Id: 0,
Height: blob.Height,
Time: now,
Position: 0,
Type: storageTypes.MsgConnectionOpenInit,
TxId: 0,
Data: structs.Map(msg),
Namespace: nil,
Addresses: addressesExpected,
}

assert.NoError(t, err)
assert.Equal(t, int64(0), dm.BlobsSize)
assert.Equal(t, msgExpected, dm.Msg)
assert.Equal(t, addressesExpected, dm.Addresses)
}

func TestDecodeMsg_SuccessOnMsgConnectionOpenTry(t *testing.T) {
msg := &coreConnection.MsgConnectionOpenTry{
Signer: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
}
blob, now := testsuite.EmptyBlock()
position := 0

dm, err := decode.Message(msg, blob.Height, blob.Block.Time, position, storageTypes.StatusSuccess)

addressesExpected := []storage.AddressWithType{
{
Type: storageTypes.MsgAddressTypeSigner,
Address: storage.Address{
Id: 0,
Height: blob.Height,
LastHeight: blob.Height,
Address: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
Hash: []byte{0x94, 0x63, 0x42, 0xc7, 0x73, 0x2f, 0x6, 0x22, 0x37, 0xb6, 0x73, 0xf5, 0x26, 0xf2, 0x67, 0x4c, 0x7d, 0xb, 0xff, 0x98},
Balance: storage.Balance{
Id: 0,
Total: decimal.Zero,
},
},
},
}

msgExpected := storage.Message{
Id: 0,
Height: blob.Height,
Time: now,
Position: 0,
Type: storageTypes.MsgConnectionOpenTry,
TxId: 0,
Data: structs.Map(msg),
Namespace: nil,
Addresses: addressesExpected,
}

assert.NoError(t, err)
assert.Equal(t, int64(0), dm.BlobsSize)
assert.Equal(t, msgExpected, dm.Msg)
assert.Equal(t, addressesExpected, dm.Addresses)
}

func TestDecodeMsg_SuccessOnMsgConnectionOpenAck(t *testing.T) {
msg := &coreConnection.MsgConnectionOpenAck{
Signer: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
}
blob, now := testsuite.EmptyBlock()
position := 0

dm, err := decode.Message(msg, blob.Height, blob.Block.Time, position, storageTypes.StatusSuccess)

addressesExpected := []storage.AddressWithType{
{
Type: storageTypes.MsgAddressTypeSigner,
Address: storage.Address{
Id: 0,
Height: blob.Height,
LastHeight: blob.Height,
Address: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
Hash: []byte{0x94, 0x63, 0x42, 0xc7, 0x73, 0x2f, 0x6, 0x22, 0x37, 0xb6, 0x73, 0xf5, 0x26, 0xf2, 0x67, 0x4c, 0x7d, 0xb, 0xff, 0x98},
Balance: storage.Balance{
Id: 0,
Total: decimal.Zero,
},
},
},
}

msgExpected := storage.Message{
Id: 0,
Height: blob.Height,
Time: now,
Position: 0,
Type: storageTypes.MsgConnectionOpenAck,
TxId: 0,
Data: structs.Map(msg),
Namespace: nil,
Addresses: addressesExpected,
}

assert.NoError(t, err)
assert.Equal(t, int64(0), dm.BlobsSize)
assert.Equal(t, msgExpected, dm.Msg)
assert.Equal(t, addressesExpected, dm.Addresses)
}

func TestDecodeMsg_SuccessOnMsgConnectionOpenConfirm(t *testing.T) {
msg := &coreConnection.MsgConnectionOpenConfirm{
Signer: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
}
blob, now := testsuite.EmptyBlock()
position := 0

dm, err := decode.Message(msg, blob.Height, blob.Block.Time, position, storageTypes.StatusSuccess)

addressesExpected := []storage.AddressWithType{
{
Type: storageTypes.MsgAddressTypeSigner,
Address: storage.Address{
Id: 0,
Height: blob.Height,
LastHeight: blob.Height,
Address: "celestia1j33593mn9urzydakw06jdun8f37shlucmhr8p6",
Hash: []byte{0x94, 0x63, 0x42, 0xc7, 0x73, 0x2f, 0x6, 0x22, 0x37, 0xb6, 0x73, 0xf5, 0x26, 0xf2, 0x67, 0x4c, 0x7d, 0xb, 0xff, 0x98},
Balance: storage.Balance{
Id: 0,
Total: decimal.Zero,
},
},
},
}

msgExpected := storage.Message{
Id: 0,
Height: blob.Height,
Time: now,
Position: 0,
Type: storageTypes.MsgConnectionOpenConfirm,
TxId: 0,
Data: structs.Map(msg),
Namespace: nil,
Addresses: addressesExpected,
}

assert.NoError(t, err)
assert.Equal(t, int64(0), dm.BlobsSize)
assert.Equal(t, msgExpected, dm.Msg)
assert.Equal(t, addressesExpected, dm.Addresses)
}
11 changes: 11 additions & 0 deletions pkg/indexer/decode/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
fee "github.com/cosmos/ibc-go/v6/modules/apps/29-fee/types"
ibcTypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
coreClient "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
coreConnection "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
"time"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -224,6 +225,16 @@ func Message(
case *coreClient.MsgSubmitMisbehaviour:
d.Msg.Type, d.Msg.Addresses, err = handle.MsgSubmitMisbehaviour(height, typedMsg)

// coreConnection module
case *coreConnection.MsgConnectionOpenInit:
d.Msg.Type, d.Msg.Addresses, err = handle.MsgConnectionOpenInit(height, typedMsg)
case *coreConnection.MsgConnectionOpenTry:
d.Msg.Type, d.Msg.Addresses, err = handle.MsgConnectionOpenTry(height, typedMsg)
case *coreConnection.MsgConnectionOpenAck:
d.Msg.Type, d.Msg.Addresses, err = handle.MsgConnectionOpenAck(height, typedMsg)
case *coreConnection.MsgConnectionOpenConfirm:
d.Msg.Type, d.Msg.Addresses, err = handle.MsgConnectionOpenConfirm(height, typedMsg)

default:
log.Err(errors.New("unknown message type")).Msgf("got type %T", msg)
d.Msg.Type = storageTypes.MsgUnknown
Expand Down

0 comments on commit f0d524b

Please sign in to comment.