-
Notifications
You must be signed in to change notification settings - Fork 4
/
moonbeamfin.go
45 lines (38 loc) · 1.48 KB
/
moonbeamfin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This implements the finality check for Moonbeam.
//
// Moonbeam can publish blocks before they are marked final. This means we need to sit on the block until a special "is finalized"
// query returns true. The assumption is that every block number will eventually be published and finalized, it's just that the contents
// of the block (and therefore the hash) might change if there is a rollback.
package ethereum
import (
"context"
common "github.com/alephium/wormhole-fork/node/pkg/common"
ethRpc "github.com/ethereum/go-ethereum/rpc"
"go.uber.org/zap"
"time"
)
type MoonbeamFinalizer struct {
logger *zap.Logger
networkName string
client *ethRpc.Client
}
func (f *MoonbeamFinalizer) SetLogger(l *zap.Logger, netName string) {
f.logger = l
f.networkName = netName
f.logger.Info("using Moonbeam specific finality check", zap.String("eth_network", f.networkName))
}
func (f *MoonbeamFinalizer) DialContext(ctx context.Context, rawurl string) (err error) {
timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
f.client, err = ethRpc.DialContext(timeout, rawurl)
return err
}
func (f *MoonbeamFinalizer) IsBlockFinalized(ctx context.Context, block *common.NewBlock) (bool, error) {
var finalized bool
err := f.client.CallContext(ctx, &finalized, "moon_isBlockFinalized", block.Hash.Hex())
if err != nil {
f.logger.Error("failed to check for finality", zap.String("eth_network", f.networkName), zap.Error(err))
return false, err
}
return finalized, nil
}