Skip to content

Commit

Permalink
fix: fix substrate event handling (#152)
Browse files Browse the repository at this point in the history
use go-substrate-client WIP version to fix event handling
  • Loading branch information
tcar121293 authored Apr 3, 2023
1 parent bc3bbb5 commit 1e14379
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 418 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ genmocks:
mockgen -source=./chains/evm/listener/event-handler.go -destination=./chains/evm/listener/mock/listener.go
mockgen -source=./chains/evm/calls/events/listener.go -destination=./chains/evm/calls/events/mock/listener.go
mockgen -source=./chains/substrate/listener/listener.go -destination=./chains/substrate/listener/mock/listener.go
mockgen -source=./chains/substrate/listener/event-handlers.go -destination=./chains/substrate/listener/mock/handlers.go
mockgen -destination=chains/evm/listener/mock/core/listener.go github.com/ChainSafe/chainbridge-core/chains/evm/listener EventListener,DepositHandler
mockgen -source=./topology/topology.go -destination=./topology/mock/topology.go

Expand Down
20 changes: 8 additions & 12 deletions chains/substrate/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"sync"

"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"

"github.com/centrifuge/go-substrate-rpc-client/v4/rpc"
"github.com/centrifuge/go-substrate-rpc-client/v4/rpc/chain"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

"github.com/ChainSafe/sygma-relayer/chains/substrate/events"
)

type Connection struct {
Expand Down Expand Up @@ -71,20 +73,14 @@ func (c *Connection) UpdateMetatdata() error {
return nil
}

func (c *Connection) GetBlockEvents(hash types.Hash) (*events.Events, error) {
meta := c.GetMetadata()
key, err := types.CreateStorageKey(&meta, "System", "Events", nil)
func (c *Connection) GetBlockEvents(hash types.Hash) ([]*parser.Event, error) {
provider := state.NewProvider(c.State)
eventRetriever, err := retriever.NewDefaultEventRetriever(provider)
if err != nil {
return nil, err
}

var raw types.EventRecordsRaw
_, err = c.RPC.State.GetStorage(key, &raw, hash)
if err != nil {
return nil, err
}
evts := &events.Events{}
err = raw.DecodeEventRecords(&meta, evts)
evts, err := eventRetriever.GetEvents(hash)
if err != nil {
return nil, err
}
Expand Down
109 changes: 9 additions & 100 deletions chains/substrate/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,18 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
)

type Events struct {
types.EventRecords
SygmaBridge_Deposit []Deposit
SygmaBasicFeeHandler_FeeSet []FeeSet

SygmaBridge_ProposalExecution []ProposalExecution
SygmaBridge_FailedHandlerExecution []FailedHandlerExecution
SygmaBridge_Retry []Retry
SygmaBridge_BridgePaused []BridgePaused
SygmaBridge_BridgeUnpaused []BridgeUnpaused
SygmaBridge_RegisterDestDomain []RegisterDestDomain
SygmaBridge_UnRegisterDestDomain []UnregisterDestDomain
SygmaFeeHandlerRouter_FeeHandlerSet []FeeHandlerSet
PhragmenElection
}

type Deposit struct {
Phase types.Phase
DestDomainID types.U8
ResourceID types.Bytes32
DepositNonce types.U64
Sender types.AccountID
TransferType [1]byte
CallData []byte
Handler [1]byte
Topics []types.Hash
}

type FeeSet struct {
Phase types.Phase
DomainID types.U8
Asset types.AssetID
Amount types.U128
Topics []types.Hash
}

type ProposalExecution struct {
Phase types.Phase
OriginDomainID types.U8
DepositNonce types.U64
DataHash types.Bytes32
Topics []types.Hash
}

type FailedHandlerExecution struct {
Phase types.Phase
Error []byte
OriginDomainID types.U8
DepositNonce types.U64
Topics []types.Hash
DestDomainID types.U8 `mapstructure:"dest_domain_id"`
ResourceID types.Bytes32 `mapstructure:"resource_id"`
DepositNonce types.U64 `mapstructure:"deposit_nonce"`
TransferType types.U8 `mapstructure:"sygma_traits_TransferType"`
CallData []byte `mapstructure:"deposit_data"`
Handler [1]byte `mapstructure:"handler_response"`
}

type Retry struct {
Phase types.Phase
DepositOnBlockHeight types.U128
DestDomainID types.U8
Sender types.AccountID
Topics []types.Hash
}

type BridgePaused struct {
Phase types.Phase
DestDomainID types.U8
Topics []types.Hash
}

type BridgeUnpaused struct {
Phase types.Phase
DestDomainID types.U8
Topics []types.Hash
}

type RegisterDestDomain struct {
Phase types.Phase
Sender types.AccountID
DomainID types.U8
ChainID types.U256
Topics []types.Hash
DepositOnBlockHeight types.U128 `mapstructure:"deposit_on_block_height"`
DestDomainID types.U8 `mapstructure:"dest_domain_id"`
}

type UnregisterDestDomain struct {
Phase types.Phase
Sender types.AccountID
DomainID types.U8
ChainID types.U256
Topics []types.Hash
}

type FeeHandlerSet struct {
Phase types.Phase
DomainID types.U8
Asset types.AssetID
HandlerType [1]byte
Topics []types.Hash
}

type PhragmenElection struct {
// Substrate default events
PhragmenElection_CandidateSlashed []types.EventElectionsCandidateSlashed
PhragmenElection_ElectionError []types.EventElectionsElectionError
PhragmenElection_EmptyTerm []types.EventElectionsEmptyTerm
PhragmenElection_MemberKicked []types.EventElectionsMemberKicked
PhragmenElection_NewTerm []types.EventElectionsNewTerm
PhragmenElection_Renounced []types.EventElectionsRenounced
PhragmenElection_SeatHolderSlashed []types.EventElectionsSeatHolderSlashed
}
type CodeUpdated struct{}
8 changes: 6 additions & 2 deletions chains/substrate/listener/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type SubstrateDepositHandler struct {
depositHandlers DepositHandlers
}

const (
FungibleTransfer = iota
)

// NewSubstrateDepositHandler creates an instance of SubstrateDepositHandler that contains
// handler functions for processing deposit events
func NewSubstrateDepositHandler() *SubstrateDepositHandler {
Expand All @@ -25,9 +29,9 @@ func NewSubstrateDepositHandler() *SubstrateDepositHandler {
}
}

func (e *SubstrateDepositHandler) HandleDeposit(sourceID uint8, destID types.U8, depositNonce types.U64, resourceID types.Bytes32, calldata []byte, transferType [1]byte) (*message.Message, error) {
func (e *SubstrateDepositHandler) HandleDeposit(sourceID uint8, destID types.U8, depositNonce types.U64, resourceID types.Bytes32, calldata []byte, transferType types.U8) (*message.Message, error) {
var depositType message.TransferType
if transferType[0] == 0 {
if transferType == FungibleTransfer {
depositType = message.FungibleTransfer
} else {
return nil, errors.New("no corresponding deposit handler for this transfer type exists")
Expand Down
20 changes: 4 additions & 16 deletions chains/substrate/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
calldata = append(calldata, recipientLen...)
calldata = append(calldata, types.Bytes(recipient)...)

sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)
depositLog := &events.Deposit{
Phase: types.Phase{},
DestDomainID: types.NewU8(2),
ResourceID: types.Bytes32{1},
DepositNonce: types.NewU64(1),
Sender: *sender,
TransferType: [1]byte{0},
TransferType: types.NewU8(0),
CallData: calldata,
Handler: [1]byte{0},
Topics: []types.Hash{},
}

sourceID := uint8(1)
Expand Down Expand Up @@ -81,17 +77,13 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
func (s *Erc20HandlerTestSuite) TestErc20HandleEventIncorrectdeposit_dataLen() {
var calldata []byte

sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)
depositLog := &events.Deposit{
Phase: types.Phase{},
DestDomainID: types.NewU8(2),
ResourceID: types.Bytes32{1},
DepositNonce: types.NewU64(1),
Sender: *sender,
TransferType: [1]byte{0},
TransferType: types.NewU8(0),
CallData: calldata,
Handler: [1]byte{0},
Topics: []types.Hash{},
}

sourceID := uint8(1)
Expand All @@ -111,18 +103,14 @@ func (s *Erc20HandlerTestSuite) TestSuccesfullyRegisterFungibleTransferHandler()
recipientLen, _ := (types.BigIntToIntBytes(big.NewInt(int64(len(recipient))), 32))
calldata = append(calldata, recipientLen...)
calldata = append(calldata, types.Bytes(recipient)...)
sender, _ := types.NewAccountID(substrate.SubstratePK.PublicKey)

d1 := &events.Deposit{
Phase: types.Phase{},
DestDomainID: types.NewU8(2),
ResourceID: types.Bytes32{1},
DepositNonce: types.NewU64(1),
Sender: *sender,
TransferType: [1]byte{0},
TransferType: types.NewU8(0),
CallData: calldata,
Handler: [1]byte{0},
Topics: []types.Hash{},
}

depositHandler := listener.NewSubstrateDepositHandler()
Expand All @@ -133,7 +121,7 @@ func (s *Erc20HandlerTestSuite) TestSuccesfullyRegisterFungibleTransferHandler()
s.NotNil(message1)

// Use unregistered transfer type
message2, err2 := depositHandler.HandleDeposit(1, d1.DestDomainID, d1.DepositNonce, d1.ResourceID, d1.CallData, [1]byte{1})
message2, err2 := depositHandler.HandleDeposit(1, d1.DestDomainID, d1.DepositNonce, d1.ResourceID, d1.CallData, 1)
s.Nil(message2)
s.NotNil(err2)
s.EqualError(err2, errNoCorrespondingDepositHandler.Error())
Expand Down
Loading

0 comments on commit 1e14379

Please sign in to comment.