Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade go substrate with sygma core #349

Draft
wants to merge 10 commits into
base: release/v2.2.2-rc
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Run nodes
run: docker-compose -f ./example/docker-compose.yml up -d
run: docker compose -f ./example/docker-compose.yml up -d
- name: Run tests
run: make e2e-test
- name: Print logs
Expand Down
33 changes: 11 additions & 22 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package app
import (
"context"
"fmt"
substrateCheckMetadataModeEnabledSignature "github.com/sygmaprotocol/go-substrate-rpc-client/v4/signature"
substrateCheckMetadataModeEnabledClient "github.com/sygmaprotocol/sygma-core/chains/substrate/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -52,9 +52,7 @@ import (
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/monitored"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/transaction"
substrateClient "github.com/sygmaprotocol/sygma-core/chains/substrate/client"
connection "github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
connectionCheckMetadataModeEnabled "github.com/sygmaprotocol/sygma-core/chains/substrate/connection"

"github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
coreSubstrateListener "github.com/sygmaprotocol/sygma-core/chains/substrate/listener"

"github.com/ChainSafe/sygma-relayer/comm/elector"
Expand Down Expand Up @@ -259,30 +257,21 @@ func Run() error {
panic(err)
}

conn, err := connection.NewSubstrateConnection(config.GeneralChainConfig.Endpoint)
if err != nil {
panic(err)
}
keyPair, err := signature.KeyringPairFromSecret(config.GeneralChainConfig.Key, config.SubstrateNetwork)
if err != nil {
panic(err)
}

conn, err := connection.NewSubstrateConnection(config.GeneralChainConfig.Endpoint)
substrateEventRetriever, err := retriever.NewDefaultEventRetriever(state.NewEventProvider(conn.RPC.State), conn.RPC.State)
if err != nil {
panic(err)
}
subClient := substrateClient.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip)
bridgePallet := substratePallet.NewPallet(subClient, nil, false)

// Temporarily differ the substrate chain with CheckMetadataModeEnabled chain and non-CheckMetadataModeEnabled
// chain by the endpoint name without touching the chain configure.
// This will only stay in this temporary feature branch
isCheckMetadataModeEnabled := strings.Contains(config.GeneralChainConfig.Endpoint, "tangle")
if isCheckMetadataModeEnabled {
connCheckMetadataModeEnabled, err := connectionCheckMetadataModeEnabled.NewCheckMetadataModeEnabledConnection(config.GeneralChainConfig.Endpoint)
if err != nil {
panic(err)
}
subCheckMetadataModeEnabledClient := substrateCheckMetadataModeEnabledClient.NewSubstrateCheckMetadataModeEnabledClient(connCheckMetadataModeEnabled, (*substrateCheckMetadataModeEnabledSignature.KeyringPair)(&keyPair), config.ChainID, config.Tip)
bridgePallet = substratePallet.NewPallet(nil, subCheckMetadataModeEnabledClient, true)
}
substrateClient := substrateClient.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip, substrateEventRetriever)
bridgePallet := substratePallet.NewPallet(substrateClient)

log.Info().Str("domain", config.String()).Msgf("Registering substrate domain")

Expand All @@ -305,7 +294,7 @@ func Run() error {
panic(err)
}
if startBlock == nil {
head, err := subClient.LatestBlock()
head, err := substrateClient.LatestBlock()
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions chains/substrate/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type BridgePallet interface {
IsProposalExecuted(p *transfer.TransferProposal) (bool, error)
ExecuteProposals(proposals []*transfer.TransferProposal, signature []byte) (types.Hash, *author.ExtrinsicStatusSubscription, error)
ProposalsHash(proposals []*transfer.TransferProposal) ([]byte, error)
TrackExtrinsic(extHash types.Hash, sub *author.ExtrinsicStatusSubscription) error
TrackExtrinsic(extHash types.Hash, sub *author.ExtrinsicStatusSubscription, extrinsicMethod string) error
}

type Executor struct {
Expand Down Expand Up @@ -161,7 +161,7 @@ func (e *Executor) watchExecution(ctx context.Context, cancelExecution context.C
return err
}

return e.bridge.TrackExtrinsic(hash, sub)
return e.bridge.TrackExtrinsic(hash, sub, "SygmaBridge.execute_proposal")
}
case <-ticker.C:
{
Expand Down
3 changes: 2 additions & 1 deletion chains/substrate/listener/event-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package listener

import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/block"
"math/big"

"github.com/ChainSafe/sygma-relayer/chains/substrate/events"
Expand All @@ -17,7 +18,7 @@ import (

type Connection interface {
GetFinalizedHead() (types.Hash, error)
GetBlock(blockHash types.Hash) (*types.SignedBlock, error)
GetBlock(blockHash types.Hash) (*block.SignedBlock, error)
GetBlockHash(blockNumber uint64) (types.Hash, error)
GetBlockEvents(hash types.Hash) ([]*parser.Event, error)
UpdateMetatdata() error
Expand Down
13 changes: 7 additions & 6 deletions chains/substrate/listener/event-handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package listener_test

import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/block"
"math/big"

"github.com/ChainSafe/sygma-relayer/chains/substrate/listener"
Expand Down Expand Up @@ -354,7 +355,7 @@ func (s *RetryHandlerTestSuite) Test_CannotFetchLatestBlock() {

func (s *RetryHandlerTestSuite) Test_EventTooNew() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand All @@ -380,7 +381,7 @@ func (s *RetryHandlerTestSuite) Test_EventTooNew() {

func (s *RetryHandlerTestSuite) Test_FetchingBlockHashFails() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand Down Expand Up @@ -409,7 +410,7 @@ func (s *RetryHandlerTestSuite) Test_FetchingBlockHashFails() {

func (s *RetryHandlerTestSuite) Test_FetchingBlockEventsFails() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand Down Expand Up @@ -438,7 +439,7 @@ func (s *RetryHandlerTestSuite) Test_FetchingBlockEventsFails() {

func (s *RetryHandlerTestSuite) Test_NoEvents() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand Down Expand Up @@ -467,7 +468,7 @@ func (s *RetryHandlerTestSuite) Test_NoEvents() {

func (s *RetryHandlerTestSuite) Test_ValidEvents() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand Down Expand Up @@ -563,7 +564,7 @@ func (s *RetryHandlerTestSuite) Test_ValidEvents() {

func (s *RetryHandlerTestSuite) Test_EventPanics() {
s.mockConn.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil)
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{Block: types.Block{
s.mockConn.EXPECT().GetBlock(gomock.Any()).Return(&block.SignedBlock{Block: block.Block{
Header: types.Header{
Number: types.BlockNumber(uint32(100)),
},
Expand Down
5 changes: 3 additions & 2 deletions chains/substrate/listener/mock/handlers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 9 additions & 38 deletions chains/substrate/pallet/pallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@ type BridgeProposal struct {
}

type Pallet struct {
c *client.SubstrateClient
m *client.SubstrateCheckMetadataModeEnabledClient
isCheckMetadataModeEnabled bool
}

func (p *Pallet) TrackExtrinsic(extHash types.Hash, sub *author.ExtrinsicStatusSubscription) error {
//TODO implement me
return nil
*client.SubstrateClient
}

func NewPallet(
client *client.SubstrateClient,
checkMetadataModeEnabledClient *client.SubstrateCheckMetadataModeEnabledClient,
isCheckMetadataModeEnabled bool,
) *Pallet {
return &Pallet{
client,
checkMetadataModeEnabledClient,
isCheckMetadataModeEnabled,
}
}

Expand All @@ -64,27 +53,15 @@ func (p *Pallet) ExecuteProposals(
})
}

if p.isCheckMetadataModeEnabled {
hash, _, err := p.m.Transact(
"SygmaBridge.execute_proposal",
bridgeProposals,
signature,
)
return types.Hash(hash), nil, err
} else {
return p.c.Transact(
"SygmaBridge.execute_proposal",
bridgeProposals,
signature,
)
}
return p.Transact(
"SygmaBridge.execute_proposal",
bridgeProposals,
signature,
)
}

func (p *Pallet) ProposalsHash(proposals []*transfer.TransferProposal) ([]byte, error) {
if !p.isCheckMetadataModeEnabled {
return chains.ProposalsHash(proposals, p.c.ChainID.Int64(), verifyingContract, bridgeVersion)
}
return chains.ProposalsHash(proposals, p.m.ChainID.Int64(), verifyingContract, bridgeVersion)
return chains.ProposalsHash(proposals, p.ChainID.Int64(), verifyingContract, bridgeVersion)
}

func (p *Pallet) IsProposalExecuted(prop *transfer.TransferProposal) (bool, error) {
Expand All @@ -94,16 +71,10 @@ func (p *Pallet) IsProposalExecuted(prop *transfer.TransferProposal) (bool, erro
Str("resourceID", hexutil.Encode(prop.Data.ResourceId[:])).
Msg("Getting is proposal executed")
var res bool
if !p.isCheckMetadataModeEnabled {
err := p.c.Conn.Call(&res, "sygma_isProposalExecuted", prop.Data.DepositNonce, prop.Source)
if err != nil {
return false, err
}
return res, nil
}
err := p.m.Conn.Call(&res, "sygma_isProposalExecuted", prop.Data.DepositNonce, prop.Source)
err := p.Conn.Call(&res, "sygma_isProposalExecuted", prop.Data.DepositNonce, prop.Source)
if err != nil {
return false, err
}

return res, nil
}
14 changes: 10 additions & 4 deletions e2e/substrate/substrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package substrate_test
import (
"context"
"encoding/binary"

"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
substrateTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/ethereum/go-ethereum/core/types"
evmClient "github.com/sygmaprotocol/sygma-core/chains/evm/client"
Expand Down Expand Up @@ -52,7 +53,11 @@ func Test_EVMSubstrate(t *testing.T) {
if err != nil {
panic(err)
}
substrateClient := client.NewSubstrateClient(substrateConnection, &substrate.SubstratePK, big.NewInt(5), 0)
retriever, err := retriever.NewDefaultEventRetriever(state.NewEventProvider(substrateConnection.RPC.State), substrateConnection.RPC.State)
if err != nil {
panic(err)
}
substrateClient := client.NewSubstrateClient(substrateConnection, &substrate.SubstratePK, big.NewInt(5), 0, retriever)

var assetId uint32 = 2000
assetIdSerialized := make([]byte, 4)
Expand Down Expand Up @@ -139,10 +144,11 @@ func (s *IntegrationTestSuite) Test_Erc20Deposit_Substrate_to_EVM() {
destBalanceBefore, err := erc20Contract.GetBalance(s.evmClient.From())
s.Nil(err)

extHash, sub, err := s.substrateClient.Transact("SygmaBridge.deposit", substrate.USDCAsset{}, substrate.Destination{})
extrinsicMethod := "SygmaBridge.deposit"
extHash, sub, err := s.substrateClient.Transact(extrinsicMethod, substrate.USDCAsset{}, substrate.Destination{})
s.Nil(err)

err = s.substrateClient.TrackExtrinsic(extHash, sub)
err = s.substrateClient.TrackExtrinsic(extHash, sub, extrinsicMethod)
s.Nil(err)

err = evm.WaitForProposalExecuted(s.evmClient, s.evmConfig.BridgeAddr)
Expand Down
34 changes: 12 additions & 22 deletions example/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package app
import (
"context"
"fmt"
substrateCheckMetadataModeEnabledSignature "github.com/sygmaprotocol/go-substrate-rpc-client/v4/signature"
substrateCheckMetadataModeEnabledClient "github.com/sygmaprotocol/sygma-core/chains/substrate/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -26,8 +25,7 @@ import (
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/transaction"
coreSubstrate "github.com/sygmaprotocol/sygma-core/chains/substrate"
substrateClient "github.com/sygmaprotocol/sygma-core/chains/substrate/client"
connection "github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
connectionCheckMetadataModeEnabled "github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
"github.com/sygmaprotocol/sygma-core/chains/substrate/connection"
coreSubstrateListener "github.com/sygmaprotocol/sygma-core/chains/substrate/listener"
"github.com/sygmaprotocol/sygma-core/crypto/secp256k1"
"github.com/sygmaprotocol/sygma-core/observability"
Expand Down Expand Up @@ -216,33 +214,25 @@ func Run() error {
panic(err)
}

keyPair, err := signature.KeyringPairFromSecret(config.GeneralChainConfig.Key, config.SubstrateNetwork)
conn, err := connection.NewSubstrateConnection(config.GeneralChainConfig.Endpoint)
if err != nil {
panic(err)
}

conn, err := connection.NewSubstrateConnection(config.GeneralChainConfig.Endpoint)
keyPair, err := signature.KeyringPairFromSecret(config.GeneralChainConfig.Key, config.SubstrateNetwork)
if err != nil {
panic(err)
}
subClient := substrateClient.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip)
bridgePallet := substratePallet.NewPallet(subClient, nil, false)

// Temporarily differ the substrate chain with CheckMetadataModeEnabled chain and non-CheckMetadataModeEnabled
// chain by the endpoint name without touching the chain configure.
// This will only stay in this temporary feature branch
isCheckMetadataModeEnabled := strings.Contains(config.GeneralChainConfig.Endpoint, "tangle")
if isCheckMetadataModeEnabled {
connCheckMetadataModeEnabled, err := connectionCheckMetadataModeEnabled.NewCheckMetadataModeEnabledConnection(config.GeneralChainConfig.Endpoint)
if err != nil {
panic(err)
}
subCheckMetadataModeEnabledClient := substrateCheckMetadataModeEnabledClient.NewSubstrateCheckMetadataModeEnabledClient(connCheckMetadataModeEnabled, (*substrateCheckMetadataModeEnabledSignature.KeyringPair)(&keyPair), config.ChainID, config.Tip)
bridgePallet = substratePallet.NewPallet(nil, subCheckMetadataModeEnabledClient, true)
}

log.Info().Str("domain", config.String()).Msgf("Registering substrate domain")

substrateEventRetriever, err := retriever.NewDefaultEventRetriever(state.NewEventProvider(conn.RPC.State), conn.RPC.State)
if err != nil {
panic(err)
}
substrateClient := substrateClient.NewSubstrateClient(conn, &keyPair, config.ChainID, config.Tip, substrateEventRetriever)
bridgePallet := substratePallet.NewPallet(substrateClient)

l := log.With().Str("chain", fmt.Sprintf("%v", config.GeneralChainConfig.Name)).Uint8("domainID", *config.GeneralChainConfig.Id)
depositHandler := substrateListener.NewSubstrateDepositHandler()
depositHandler.RegisterDepositHandler(transfer.FungibleTransfer, substrateListener.FungibleTransferHandler)
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/binance-chain/tss-lib v0.0.0-00010101000000-000000000000
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.2-0.20240917092335-6cd32ee625ca
github.com/creasty/defaults v1.6.0
github.com/deckarep/golang-set/v2 v2.1.0
github.com/ethereum/go-ethereum v1.13.4
Expand All @@ -20,8 +20,7 @@ require (
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.8.4
github.com/sygmaprotocol/go-substrate-rpc-client/v4 v4.2.2-0.20240813185906-0395c914c6f8
github.com/sygmaprotocol/sygma-core v0.0.0-20240813191424-fff897579766
github.com/sygmaprotocol/sygma-core v0.0.0-20240918140014-445b2f2c6bc1
github.com/taurusgroup/multi-party-sig v0.6.0-alpha-2021-09-21.0.20230619131919-9c7c6ffd7217
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/metric v1.16.0
Expand Down
Loading
Loading