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

feat: backport substrate code from relayer #9

Merged
merged 28 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
32f81c1
Update README
mpetrun5 Sep 28, 2023
721f49b
Rename chainbridge-core module to sygma-core
mpetrun5 Sep 28, 2023
e42d14a
Remove all extra code
mpetrun5 Sep 29, 2023
68615e7
Remove test setup temporarily
mpetrun5 Sep 29, 2023
4a18032
Move package from calls to separate folders
mpetrun5 Sep 29, 2023
33256d4
Fix imports
mpetrun5 Sep 29, 2023
c1372a0
Move to proposal to relayer package
mpetrun5 Sep 29, 2023
6107e75
Fix all naming conflicts in code
mpetrun5 Sep 29, 2023
ccada0b
Move generic types to types package
mpetrun5 Sep 29, 2023
73f7311
Move msg channel to be a global message sending tool
mpetrun5 Sep 29, 2023
15ca617
Add evm config module
mpetrun5 Sep 29, 2023
01c27b9
Remove config module from core library
mpetrun5 Sep 29, 2023
9ad8df7
Move keyring to crypto module
mpetrun5 Sep 29, 2023
f16fd6b
Move modules related to transactions to transactor module
mpetrun5 Sep 29, 2023
9a0f09e
Fix tests and mocks
mpetrun5 Oct 2, 2023
f2f2fbc
Add additional github actions
mpetrun5 Oct 2, 2023
9180b16
Fix linter
mpetrun5 Oct 2, 2023
f9be6f9
Replace mockgen lib
mpetrun5 Oct 2, 2023
14105b6
Backport substrate chain from relayer
mpetrun5 Oct 2, 2023
43ae2e1
Fix dependecies
mpetrun5 Oct 2, 2023
614cac2
Bump go-ethereum
mpetrun5 Oct 2, 2023
176eaa8
Update import
mpetrun5 Oct 2, 2023
47f26ec
Remove keystore and sr25519 support
mpetrun5 Oct 2, 2023
7b14fc0
Hardcode random ethereum private key
mpetrun5 Oct 2, 2023
68efd2c
Rename message handler in tests
mpetrun5 Oct 2, 2023
2b9ebb3
Merge branch 'main' into add-substrate-code
mpetrun5 Oct 4, 2023
3526c83
Regenerate mocks
mpetrun5 Oct 4, 2023
d9a4739
Bump critical dependency
mpetrun5 Oct 4, 2023
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 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
Loading