Skip to content

Commit

Permalink
feat: send step only for filled epochs (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Feb 9, 2024
1 parent 5d6a09c commit bc2f287
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 16 deletions.
5 changes: 2 additions & 3 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (

type EVMConfig struct {
config.BaseNetworkConfig
BeaconEndpoint string `required:"true" split_words:"true"`
Router string
BeaconEndpoint string `required:"true" split_words:"true"`
Router string `required:"true"`
Spectre string `required:"true"`
Spec string `default:"mainnet"`
MaxGasPrice int64 `default:"500000000000" split_words:"true"`
BlockInterval uint64 `default:"32" split_words:"true"`
GasMultiplier float64 `default:"1" split_words:"true"`
GasIncreasePercentage int64 `default:"15" split_words:"true"`
RetryInterval uint64 `default:"12" split_words:"true"`
Expand Down
2 changes: 0 additions & 2 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
Router: "router",
Spectre: "spectre",
Spec: "mainnet",
BlockInterval: 32,
GasMultiplier: 1,
GasIncreasePercentage: 15,
MaxGasPrice: 500000000000,
Expand Down Expand Up @@ -96,7 +95,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
Router: "router",
Spectre: "spectre",
Spec: "testnet",
BlockInterval: 10,
GasMultiplier: 1,
GasIncreasePercentage: 20,
MaxGasPrice: 1000,
Expand Down
86 changes: 79 additions & 7 deletions chains/evm/listener/handlers/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ package handlers

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

"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
mapset "github.com/deckarep/golang-set/v2"
ethereumABI "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/rs/zerolog/log"
"github.com/sygmaprotocol/spectre-node/chains/evm/abi"
"github.com/sygmaprotocol/spectre-node/chains/evm/listener/events"
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message"
"github.com/sygmaprotocol/spectre-node/chains/evm/prover"
"github.com/sygmaprotocol/sygma-core/relayer/message"
Expand Down Expand Up @@ -46,10 +49,11 @@ type StepEventHandler struct {
prover Prover

domainID uint8
domains []uint8
blockInterval uint64
allDomains []uint8
routerABI ethereumABI.ABI
routerAddress common.Address

latestBlock uint64
}

func NewStepEventHandler(
Expand All @@ -60,19 +64,18 @@ func NewStepEventHandler(
routerAddress common.Address,
domainID uint8,
domains []uint8,
blockInterval uint64,
) *StepEventHandler {
routerABI, _ := ethereumABI.JSON(strings.NewReader(abi.RouterABI))
return &StepEventHandler{
eventFetcher: eventFetcher,
blockFetcher: blockFetcher,
prover: prover,
domains: domains,
routerAddress: routerAddress,
routerABI: routerABI,
msgChan: msgChan,
domainID: domainID,
blockInterval: blockInterval,
allDomains: domains,
latestBlock: 0,
}
}

Expand All @@ -82,14 +85,22 @@ func (h *StepEventHandler) HandleEvents(checkpoint *apiv1.Finality) error {
if err != nil {
return err
}
domains, latestBlock, err := h.destinationDomains(args.Update.FinalizedHeader.Header.Slot)
if err != nil {
return err
}
if len(domains) == 0 {
h.latestBlock = latestBlock
log.Debug().Uint8("domainID", h.domainID).Uint64("slot", args.Update.FinalizedHeader.Header.Slot).Msgf("Skipping step...")
return nil
}

log.Info().Uint8("domainID", h.domainID).Uint64("slot", args.Update.FinalizedHeader.Header.Slot).Msgf("Executing sync step")

proof, err := h.prover.StepProof(args)
if err != nil {
return err
}

node, err := args.Update.FinalizedHeader.Execution.GetTree()
if err != nil {
return err
Expand All @@ -99,7 +110,7 @@ func (h *StepEventHandler) HandleEvents(checkpoint *apiv1.Finality) error {
return err
}

for _, destDomain := range h.domains {
for _, destDomain := range domains {
if destDomain == h.domainID {
continue
}
Expand All @@ -118,5 +129,66 @@ func (h *StepEventHandler) HandleEvents(checkpoint *apiv1.Finality) error {
),
}
}
h.latestBlock = latestBlock
return nil
}

func (h *StepEventHandler) destinationDomains(slot uint64) ([]uint8, uint64, error) {
domains := mapset.NewSet[uint8]()
block, err := h.blockFetcher.SignedBeaconBlock(context.Background(), &api.SignedBeaconBlockOpts{
Block: fmt.Sprint(slot),
})
if err != nil {
return domains.ToSlice(), 0, err
}

endBlock := block.Data.Capella.Message.Body.ExecutionPayload.BlockNumber
if h.latestBlock == 0 {
return h.allDomains, endBlock, nil
}

deposits, err := h.fetchDeposits(big.NewInt(int64(h.latestBlock)), big.NewInt(int64(endBlock)))
if err != nil {
return domains.ToSlice(), endBlock, err
}
if len(deposits) == 0 {
return domains.ToSlice(), endBlock, nil
}
for _, deposit := range deposits {
domains.Add(deposit.DestinationDomainID)
}

return domains.ToSlice(), endBlock, nil
}

func (h *StepEventHandler) fetchDeposits(startBlock *big.Int, endBlock *big.Int) ([]*events.Deposit, error) {
logs, err := h.eventFetcher.FetchEventLogs(context.Background(), h.routerAddress, string(events.DepositSig), startBlock, endBlock)
if err != nil {
return nil, err
}

deposits := make([]*events.Deposit, 0)
for _, dl := range logs {
d, err := h.unpackDeposit(dl.Data)
if err != nil {
log.Error().Msgf("Failed unpacking deposit event log: %v", err)
continue
}
d.SenderAddress = common.BytesToAddress(dl.Topics[1].Bytes())

log.Debug().Msgf("Found deposit log in block: %d, TxHash: %s, contractAddress: %s, sender: %s", dl.BlockNumber, dl.TxHash, dl.Address, d.SenderAddress)
deposits = append(deposits, d)
}

return deposits, nil
}

func (h *StepEventHandler) unpackDeposit(data []byte) (*events.Deposit, error) {
var d events.Deposit
err := h.routerABI.UnpackIntoInterface(&d, "Deposit", data)
if err != nil {
return &events.Deposit{}, err
}

return &d, nil
}
Loading

0 comments on commit bc2f287

Please sign in to comment.