Skip to content

Commit

Permalink
node: eth finalized check
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-gray committed Sep 14, 2022
1 parent e0843bf commit 58186b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 22 additions & 7 deletions node/pkg/ethereum/pollimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ type PollFinalizer interface {
}

type PollImpl struct {
BaseEth EthImpl
Finalizer PollFinalizer
DelayInMs int
logger *zap.Logger
rawClient *ethRpc.Client
BaseEth EthImpl
Finalizer PollFinalizer
DelayInMs int
IsEthPoS bool
hasEthSwitched bool
logger *zap.Logger
rawClient *ethRpc.Client
}

func (e *PollImpl) SetLogger(l *zap.Logger) {
Expand All @@ -47,6 +49,11 @@ func (e *PollImpl) SetLogger(l *zap.Logger) {
}
}

func (e *PollImpl) SetEthSwitched() {
e.hasEthSwitched = true
e.logger.Info("switching from latest to finalized", zap.String("eth_network", e.BaseEth.NetworkName), zap.Int("delay_in_ms", e.DelayInMs))
}

func (e *PollImpl) DialContext(ctx context.Context, rawurl string) (err error) {
timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
Expand Down Expand Up @@ -228,13 +235,16 @@ func (e *PollImpl) getBlock(ctx context.Context, number *big.Int) (*common.NewBl
var numStr string
if number != nil {
numStr = ethHexUtils.EncodeBig(number)
} else if e.hasEthSwitched {
numStr = "finalized"
} else {
numStr = "latest"
}

type Marshaller struct {
Number *ethHexUtils.Big
Hash ethCommon.Hash `json:"hash"`
Number *ethHexUtils.Big
Hash ethCommon.Hash `json:"hash"`
Difficulty *ethHexUtils.Big
}

var m Marshaller
Expand All @@ -250,6 +260,11 @@ func (e *PollImpl) getBlock(ctx context.Context, number *big.Int) (*common.NewBl
)
return nil, fmt.Errorf("failed to unmarshal block: Number is nil")
}
d := big.Int(*m.Difficulty)
if !e.hasEthSwitched && d.Cmp(big.NewInt(0)) == 0 {
e.SetEthSwitched()
return e.getBlock(ctx, number)
}
n := big.Int(*m.Number)
return &common.NewBlock{
Number: &n,
Expand Down
2 changes: 2 additions & 0 deletions node/pkg/ethereum/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func NewEthWatcher(
// When we are running in mainnet or testnet, we need to use the Celo ethereum library rather than go-ethereum.
// However, in devnet, we currently run the standard ETH node for Celo, so we need to use the standard go-ethereum.
ethIntf = &celo.CeloImpl{NetworkName: networkName}
} else if chainID == vaa.ChainIDEthereum && !unsafeDevMode {
ethIntf = &PollImpl{BaseEth: EthImpl{NetworkName: networkName}, DelayInMs: 250, IsEthPoS: true}
} else if chainID == vaa.ChainIDMoonbeam && !unsafeDevMode {
ethIntf = &PollImpl{BaseEth: EthImpl{NetworkName: networkName}, Finalizer: &MoonbeamFinalizer{}, DelayInMs: 250}
} else if chainID == vaa.ChainIDNeon {
Expand Down

0 comments on commit 58186b8

Please sign in to comment.