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

fix: EVM message handling refactor #242

Merged
merged 7 commits into from
Jan 9, 2024
Merged
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/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# CODEOWNERS: https://help.github.com/articles/about-codeowners/

# Primary repo maintainers
* @P1sar @waymobetta @mpetrun5 @tcar121293 @MakMuftic @nmlinaric @vezenovm @freddyli7
* @mpetrun5 @tcar121293 @MakMuftic @nmlinaric @freddyli7
tss/* @timoth-y
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [1.10.2](https://github.com/sygmaprotocol/sygma-relayer/compare/v1.10.1...v1.10.2) (2023-11-30)


### Miscellaneous

* add version on startup ([#240](https://github.com/sygmaprotocol/sygma-relayer/issues/240)) ([e86c9d9](https://github.com/sygmaprotocol/sygma-relayer/commit/e86c9d9b31cc24bce542f99bab1d774863dab608))
* update codeowners ([#237](https://github.com/sygmaprotocol/sygma-relayer/issues/237)) ([a6be32a](https://github.com/sygmaprotocol/sygma-relayer/commit/a6be32a19dbfe2b856840fd1995fde2dcfb14f91))

## [1.10.1](https://github.com/sygmaprotocol/sygma-relayer/compare/v1.10.0...v1.10.1) (2023-10-19)


Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ADD . /src
WORKDIR /src
RUN cd /src && echo $(ls -1 /src)
RUN go mod download
RUN go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=ignore" -o /bridge .
RUN go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=ignore -X github.com/ChainSafe/sygma-relayer/app.Version=$(sed -n '0,/## \[\([0-9.]*\)\]/s/.*\[\([0-9.]*\)\].*/\1/p' CHANGELOG.md)" -o /bridge .

# final stage
FROM debian:stable-slim
Expand Down
36 changes: 9 additions & 27 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmgaspricer"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmtransaction"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/monitored"
coreExecutor "github.com/ChainSafe/chainbridge-core/chains/evm/executor"
coreListener "github.com/ChainSafe/chainbridge-core/chains/evm/listener"
"github.com/ChainSafe/chainbridge-core/crypto/secp256k1"
"github.com/ChainSafe/chainbridge-core/flags"
Expand Down Expand Up @@ -55,8 +54,12 @@
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

coreMessage "github.com/sygmaprotocol/sygma-core/relayer/message"
)

var Version string

func Run() error {
var err error

Expand Down Expand Up @@ -155,7 +158,6 @@
{
config, err := evm.NewEVMConfig(chainConfig)
panicOnError(err)

kp, err := secp256k1.NewKeypairFromString(config.GeneralChainConfig.Key)
panicOnError(err)

Expand All @@ -174,30 +176,10 @@
bridgeContract := bridge.NewBridgeContract(client, bridgeAddress, t)

depositHandler := coreListener.NewETHDepositHandler(bridgeContract)
mh := coreExecutor.NewEVMMessageHandler(bridgeContract)
mh := coreMessage.NewMessageHandler()
for _, handler := range config.Handlers {
switch handler.Type {
case "erc20":
{
depositHandler.RegisterDepositHandler(handler.Address, listener.Erc20DepositHandler)
mh.RegisterMessageHandler(handler.Address, coreExecutor.ERC20MessageHandler)
}
case "permissionedGeneric":
{
depositHandler.RegisterDepositHandler(handler.Address, coreListener.GenericDepositHandler)
mh.RegisterMessageHandler(handler.Address, coreExecutor.GenericMessageHandler)
}
case "permissionlessGeneric":
{
depositHandler.RegisterDepositHandler(handler.Address, listener.PermissionlessGenericDepositHandler)
mh.RegisterMessageHandler(handler.Address, executor.PermissionlessGenericMessageHandler)
}
case "erc721":
{
depositHandler.RegisterDepositHandler(handler.Address, coreListener.Erc721DepositHandler)
mh.RegisterMessageHandler(handler.Address, coreExecutor.ERC721MessageHandler)
}
}
depositHandler.RegisterDepositHandler(handler.Address, listener.PermissionlessGenericDepositHandler)
mh.RegisterMessageHandler("transfer", &executor.TransferMessageHandler{})
}
depositListener := coreEvents.NewListener(client)
tssListener := events.NewListener(client)
Expand All @@ -208,10 +190,10 @@
eventHandlers = append(eventHandlers, listener.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, listener.NewRetryEventHandler(l, tssListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations))
evmListener := coreListener.NewEVMListener(client, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
executor := executor.NewExecutor(host, communication, coordinator, mh, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())
executor := executor.NewExecutor(host, communication, coordinator, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())

Check failure on line 193 in app/app.go

View workflow job for this annotation

GitHub Actions / linter-check

cannot use bridgeContract (variable of type *bridge.BridgeContract) as type "github.com/ChainSafe/sygma-relayer/chains/evm/executor".BridgeContract in argument to executor.NewExecutor:

Check failure on line 193 in app/app.go

View workflow job for this annotation

GitHub Actions / linter-check

cannot use bridgeContract (variable of type *bridge.BridgeContract) as type "github.com/ChainSafe/sygma-relayer/chains/evm/executor".BridgeContract in argument to executor.NewExecutor:

Check failure on line 193 in app/app.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

cannot use bridgeContract (variable of type *bridge.BridgeContract) as type "github.com/ChainSafe/sygma-relayer/chains/evm/executor".BridgeContract in argument to executor.NewExecutor:

chain := evm.NewEVMChain(
client, evmListener, executor, blockstore, *config.GeneralChainConfig.Id, config.StartBlock,

Check failure on line 196 in app/app.go

View workflow job for this annotation

GitHub Actions / linter-check

cannot use executor (variable of type *"github.com/ChainSafe/sygma-relayer/chains/evm/executor".Executor) as type "github.com/ChainSafe/sygma-relayer/chains/evm".BatchProposalExecutor in argument to evm.NewEVMChain:

Check failure on line 196 in app/app.go

View workflow job for this annotation

GitHub Actions / linter-check

cannot use executor (variable of type *"github.com/ChainSafe/sygma-relayer/chains/evm/executor".Executor) as type "github.com/ChainSafe/sygma-relayer/chains/evm".BatchProposalExecutor in argument to evm.NewEVMChain:

Check failure on line 196 in app/app.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

cannot use executor (variable of type *"github.com/ChainSafe/sygma-relayer/chains/evm/executor".Executor) as type "github.com/ChainSafe/sygma-relayer/chains/evm".BatchProposalExecutor in argument to evm.NewEVMChain:
config.BlockInterval, config.GeneralChainConfig.FreshStart, config.GeneralChainConfig.LatestBlock,
)

Expand Down Expand Up @@ -276,7 +258,7 @@
syscall.SIGQUIT)

relayerName := viper.GetString("name")
log.Info().Msgf("Started relayer: %s with PID: %s", relayerName, host.ID().Pretty())
log.Info().Msgf("Started relayer: %s with PID: %s. Version: v%s", relayerName, host.ID().Pretty(), Version)

_, err = keyshareStore.GetKeyshare()
if err != nil {
Expand Down
46 changes: 16 additions & 30 deletions chains/evm/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ import (
"github.com/libp2p/go-libp2p/core/host"
"github.com/rs/zerolog/log"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
"github.com/ChainSafe/chainbridge-core/chains/evm/executor/proposal"
"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/sygma-relayer/chains"
"github.com/ChainSafe/sygma-relayer/comm"
"github.com/ChainSafe/sygma-relayer/tss"
"github.com/ChainSafe/sygma-relayer/tss/signing"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor"
"github.com/sygmaprotocol/sygma-core/relayer/proposal"
)

const TRANSFER_GAS_COST = 200000

type Batch struct {
proposals []*chains.Proposal
proposals []*proposal.Proposal
gasLimit uint64
}

Expand All @@ -38,14 +36,10 @@ var (
signingTimeout = 30 * time.Minute
)

type MessageHandler interface {
HandleMessage(m *message.Message) (*proposal.Proposal, error)
}

type BridgeContract interface {
IsProposalExecuted(p *chains.Proposal) (bool, error)
ExecuteProposals(proposals []*chains.Proposal, signature []byte, opts transactor.TransactOptions) (*ethCommon.Hash, error)
ProposalsHash(proposals []*chains.Proposal) ([]byte, error)
IsProposalExecuted(p *proposal.Proposal) (bool, error)
ExecuteProposals(proposals []*proposal.Proposal, signature []byte, opts transactor.TransactOptions) (*ethCommon.Hash, error)
ProposalsHash(proposals []*proposal.Proposal) ([]byte, error)
}

type Executor struct {
Expand All @@ -54,7 +48,6 @@ type Executor struct {
comm comm.Communication
fetcher signing.SaveDataFetcher
bridge BridgeContract
mh MessageHandler
exitLock *sync.RWMutex
transactionMaxGas uint64
}
Expand All @@ -63,7 +56,6 @@ func NewExecutor(
host host.Host,
comm comm.Communication,
coordinator *tss.Coordinator,
mh MessageHandler,
bridgeContract BridgeContract,
fetcher signing.SaveDataFetcher,
exitLock *sync.RWMutex,
Expand All @@ -73,7 +65,6 @@ func NewExecutor(
host: host,
comm: comm,
coordinator: coordinator,
mh: mh,
bridge: bridgeContract,
fetcher: fetcher,
exitLock: exitLock,
Expand All @@ -82,11 +73,11 @@ func NewExecutor(
}

// Execute starts a signing process and executes proposals when signature is generated
func (e *Executor) Execute(msgs []*message.Message) error {
func (e *Executor) Execute(proposals []*proposal.Proposal) error {
e.exitLock.RLock()
defer e.exitLock.RUnlock()

batches, err := e.proposalBatches(msgs)
batches, err := e.proposalBatches(proposals)
if err != nil {
return err
}
Expand Down Expand Up @@ -182,22 +173,17 @@ func (e *Executor) watchExecution(ctx context.Context, cancelExecution context.C
}
}

func (e *Executor) proposalBatches(msgs []*message.Message) ([]*Batch, error) {
func (e *Executor) proposalBatches(proposals []*proposal.Proposal) ([]*Batch, error) {
batches := make([]*Batch, 1)
currentBatch := &Batch{
proposals: make([]*chains.Proposal, 0),
proposals: make([]*proposal.Proposal, 0),
gasLimit: 0,
}
batches[0] = currentBatch

for _, m := range msgs {
prop, err := e.mh.HandleMessage(m)
if err != nil {
return nil, err
}
for _, prop := range proposals {

evmProposal := chains.NewProposal(prop.Source, prop.Destination, prop.DepositNonce, prop.ResourceId, prop.Data, prop.Metadata)
isExecuted, err := e.bridge.IsProposalExecuted(evmProposal)
isExecuted, err := e.bridge.IsProposalExecuted(prop)
if err != nil {
return nil, err
}
Expand All @@ -207,7 +193,7 @@ func (e *Executor) proposalBatches(msgs []*message.Message) ([]*Batch, error) {
}

var propGasLimit uint64
l, ok := evmProposal.Metadata.Data["gasLimit"]
l, ok := prop.Data.(TransferMessageData).Metadata["gasLimit"]
if ok {
propGasLimit = l.(uint64)
} else {
Expand All @@ -216,13 +202,13 @@ func (e *Executor) proposalBatches(msgs []*message.Message) ([]*Batch, error) {
currentBatch.gasLimit += propGasLimit
if currentBatch.gasLimit >= e.transactionMaxGas {
currentBatch = &Batch{
proposals: make([]*chains.Proposal, 0),
proposals: make([]*proposal.Proposal, 0),
gasLimit: 0,
}
batches = append(batches, currentBatch)
}

currentBatch.proposals = append(currentBatch.proposals, evmProposal)
currentBatch.proposals = append(currentBatch.proposals, prop)
}

return batches, nil
Expand All @@ -245,7 +231,7 @@ func (e *Executor) executeBatch(batch *Batch, signatureData *common.SignatureDat
return hash, err
}

func (e *Executor) areProposalsExecuted(proposals []*chains.Proposal, sessionID string) bool {
func (e *Executor) areProposalsExecuted(proposals []*proposal.Proposal, sessionID string) bool {
for _, prop := range proposals {
isExecuted, err := e.bridge.IsProposalExecuted(prop)
if err != nil || !isExecuted {
Expand Down
Loading
Loading