Skip to content

Commit

Permalink
Refactored executor.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mj52951 committed Jan 5, 2024
1 parent 5fc1eb3 commit 84b7f02
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
49 changes: 23 additions & 26 deletions chains/evm/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ 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/message"
"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 +38,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 +50,7 @@ type Executor struct {
comm comm.Communication
fetcher signing.SaveDataFetcher
bridge BridgeContract
mh MessageHandler
mh *message.MessageHandler
exitLock *sync.RWMutex
transactionMaxGas uint64
}
Expand All @@ -63,7 +59,7 @@ func NewExecutor(
host host.Host,
comm comm.Communication,
coordinator *tss.Coordinator,
mh MessageHandler,
mh *message.MessageHandler,
bridgeContract BridgeContract,
fetcher signing.SaveDataFetcher,
exitLock *sync.RWMutex,
Expand All @@ -82,11 +78,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,21 +178,22 @@ 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 {
prop := &chains.TransferProposal{
Source: prop.Source,
Destination: prop.Destination,
Data: prop.Data.(chains.TransferProposalData),
Type: prop.Type,
}

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

var propGasLimit uint64
l, ok := evmProposal.Metadata.Data["gasLimit"]
l, ok := evmProposal.Data.(TransferMessageData).Metadata["gasLimit"]
if ok {
propGasLimit = l.(uint64)
} else {
Expand All @@ -216,7 +213,7 @@ 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)
Expand Down Expand Up @@ -245,7 +242,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
7 changes: 7 additions & 0 deletions chains/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func NewProposal(source, destination uint8, depositNonce uint64, resourceId type
}
}

type TransferProposal struct {
Source uint8
Destination uint8
Data TransferProposalData
Type proposal.ProposalType
}

type TransferProposalData struct {
DepositNonce uint64
ResourceId [32]byte
Expand Down

0 comments on commit 84b7f02

Please sign in to comment.