Skip to content

Commit

Permalink
feat: backport substrate code from relayer (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Oct 5, 2023
1 parent 4e1c654 commit 299cefd
Show file tree
Hide file tree
Showing 19 changed files with 943 additions and 734 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Fork the repository, make changes and open a PR to the `main` branch of the repo
A great way to contribute to the project is to send a detailed report when you encounter an issue. We always appreciate a well-written, thorough bug report, and will thank you for it!

When reporting issues, always include:
- chainbridge-core version
- sygma-core version
- modules used
- logs (don't forget to remove sensitive data)
- tx hashes related to issue (if applicable)
Expand Down
62 changes: 23 additions & 39 deletions chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,57 @@ package evm

import (
"context"
"fmt"
"math/big"

"github.com/ChainSafe/sygma-core/store"
"github.com/ChainSafe/sygma-core/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type EventListener interface {
ListenToEvents(ctx context.Context, startBlock *big.Int, errChan chan<- error)
ListenToEvents(ctx context.Context, startBlock *big.Int)
}

type ProposalExecutor interface {
Execute(message *types.Message) error
Execute(messages []*types.Message) error
}

// EVMChain is struct that aggregates all data required for
type EVMChain struct {
listener EventListener
writer ProposalExecutor
executor ProposalExecutor
blockstore *store.BlockStore

domainID uint8
startBlock *big.Int
freshStart bool
latestBlock bool
domainID uint8
startBlock *big.Int

logger zerolog.Logger
}

func NewEVMChain(listener EventListener, writer ProposalExecutor, blockstore *store.BlockStore, domainID uint8, startBlock *big.Int, latestBlock bool, freshStart bool) *EVMChain {
func NewEVMChain(listener EventListener, executor ProposalExecutor, blockstore *store.BlockStore, domainID uint8, startBlock *big.Int) *EVMChain {
return &EVMChain{
listener: listener,
writer: writer,
blockstore: blockstore,
domainID: domainID,
startBlock: startBlock,
latestBlock: latestBlock,
freshStart: freshStart,
listener: listener,
executor: executor,
blockstore: blockstore,
domainID: domainID,
startBlock: startBlock,
logger: log.With().Uint8("domainID", domainID).Logger(),
}
}

// PollEvents is the goroutine that polls blocks and searches Deposit events in them.
// Events are then sent to eventsChan.
func (c *EVMChain) PollEvents(ctx context.Context, sysErr chan<- error) {
log.Info().Msg("Polling Blocks...")

startBlock, err := c.blockstore.GetStartBlock(
c.domainID,
c.startBlock,
c.latestBlock,
c.freshStart,
)
if err != nil {
sysErr <- fmt.Errorf("error %w on getting last stored block", err)
return
}

go c.listener.ListenToEvents(ctx, startBlock, sysErr)
func (c *EVMChain) PollEvents(ctx context.Context) {
c.logger.Info().Str("startBlock", c.startBlock.String()).Msg("Polling Blocks...")
go c.listener.ListenToEvents(ctx, c.startBlock)
}

func (c *EVMChain) Write(msg []*types.Message) error {
for _, msg := range msg {
go func(msg *types.Message) {
err := c.writer.Execute(msg)
if err != nil {
log.Err(err).Msgf("Failed writing message %v", msg)
}
}(msg)
func (c *EVMChain) Write(msgs []*types.Message) error {
err := c.executor.Execute(msgs)
if err != nil {
c.logger.Err(err).Msgf("error writing messages %+v on network %d", msgs, c.DomainID())
return err
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion chains/evm/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewEVMListener(

// ListenToEvents goes block by block of a network and executes event handlers that are
// configured for the listener.
func (l *EVMListener) ListenToEvents(ctx context.Context, startBlock *big.Int, errChn chan<- error) {
func (l *EVMListener) ListenToEvents(ctx context.Context, startBlock *big.Int) {
endBlock := big.NewInt(0)
for {
select {
Expand Down
4 changes: 2 additions & 2 deletions chains/evm/transactor/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ChainSafe/sygma-core/chains/evm/transactor/gas"
"github.com/ChainSafe/sygma-core/chains/evm/transactor/transaction"
"github.com/ChainSafe/sygma-core/crypto/keystore"
"github.com/ChainSafe/sygma-core/crypto/secp256k1"
"github.com/ChainSafe/sygma-core/mock"

"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -17,7 +17,7 @@ import (
"go.uber.org/mock/gomock"
)

var aliceKp = keystore.TestKeyRing.EthereumKeys[keystore.AliceKey]
var aliceKp, _ = secp256k1.NewKeypairFromString("b1370fac45517e19e27e16a2a31da07b9775d3a3bceb77a78f790f99655aa668")

type EVMTxTestSuite struct {
suite.Suite
Expand Down
64 changes: 64 additions & 0 deletions chains/substrate/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package substrate

import (
"context"
"math/big"

"github.com/ChainSafe/sygma-core/chains/substrate/client"
"github.com/ChainSafe/sygma-core/store"
"github.com/ChainSafe/sygma-core/types"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type BatchProposalExecutor interface {
Execute(msgs []*types.Message) error
}

type SubstrateChain struct {
client *client.SubstrateClient

listener EventListener
executor BatchProposalExecutor

blockstore *store.BlockStore

domainID uint8
startBlock *big.Int

logger zerolog.Logger
}

type EventListener interface {
ListenToEvents(ctx context.Context, startBlock *big.Int)
}

func NewSubstrateChain(client *client.SubstrateClient, listener EventListener, blockstore *store.BlockStore, executor BatchProposalExecutor, domainID uint8, startBlock *big.Int) *SubstrateChain {
return &SubstrateChain{
client: client,
listener: listener,
blockstore: blockstore,
executor: executor,
logger: log.With().Uint8("domainID", domainID).Logger()}
}

// PollEvents is the goroutine that polls blocks and searches Deposit events in them.
// Events are then sent to eventsChan.
func (c *SubstrateChain) PollEvents(ctx context.Context) {
c.logger.Info().Str("startBlock", c.startBlock.String()).Msg("Polling Blocks...")
go c.listener.ListenToEvents(ctx, c.startBlock)
}

func (c *SubstrateChain) Write(msgs []*types.Message) error {
err := c.executor.Execute(msgs)
if err != nil {
c.logger.Err(err).Msgf("error writing messages %+v on network %d", msgs, c.DomainID())
return err
}

return nil
}

func (c *SubstrateChain) DomainID() uint8 {
return c.domainID
}
Loading

0 comments on commit 299cefd

Please sign in to comment.