forked from ChainSafe/chainbridge-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backport substrate code from relayer (#9)
- Loading branch information
Showing
19 changed files
with
943 additions
and
734 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.