From 0ac89c3c0616e88f439864d2e6f26f87c55c423b Mon Sep 17 00:00:00 2001 From: Bruce Riley Date: Mon, 6 Jun 2022 20:17:14 +0000 Subject: [PATCH] Move polygon stuff to a different branch --- node/cmd/guardiand/node.go | 63 ++++++++++------------ node/pkg/ethereum/polygonfin.go | 93 --------------------------------- node/pkg/ethereum/watcher.go | 5 +- 3 files changed, 29 insertions(+), 132 deletions(-) delete mode 100644 node/pkg/ethereum/polygonfin.go diff --git a/node/cmd/guardiand/node.go b/node/cmd/guardiand/node.go index 628f01e8c..b90ff2b2c 100644 --- a/node/cmd/guardiand/node.go +++ b/node/cmd/guardiand/node.go @@ -70,9 +70,8 @@ var ( bscRPC *string bscContract *string - polygonRPC *string - polygonContract *string - polygonCheckpoint *string + polygonRPC *string + polygonContract *string ethRopstenRPC *string ethRopstenContract *string @@ -170,7 +169,6 @@ func init() { polygonRPC = NodeCmd.Flags().String("polygonRPC", "", "Polygon RPC URL") polygonContract = NodeCmd.Flags().String("polygonContract", "", "Polygon contract address") - polygonCheckpoint = NodeCmd.Flags().String("polygonCheckpoint", "", "Polygon checkpoint query URL") ethRopstenRPC = NodeCmd.Flags().String("ethRopstenRPC", "", "Ethereum Ropsten RPC URL") ethRopstenContract = NodeCmd.Flags().String("ethRopstenContract", "", "Ethereum Ropsten contract address") @@ -792,76 +790,71 @@ func runNode(cmd *cobra.Command, args []string) { } if err := supervisor.Run(ctx, "ethwatch", - ethereum.NewEthWatcher(*ethRPC, ethContractAddr, "eth", common.ReadinessEthSyncing, vaa.ChainIDEthereum, lockC, setC, 1, chainObsvReqC[vaa.ChainIDEthereum], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*ethRPC, ethContractAddr, "eth", common.ReadinessEthSyncing, vaa.ChainIDEthereum, lockC, setC, 1, chainObsvReqC[vaa.ChainIDEthereum], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "bscwatch", - ethereum.NewEthWatcher(*bscRPC, bscContractAddr, "bsc", common.ReadinessBSCSyncing, vaa.ChainIDBSC, lockC, nil, 1, chainObsvReqC[vaa.ChainIDBSC], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*bscRPC, bscContractAddr, "bsc", common.ReadinessBSCSyncing, vaa.ChainIDBSC, lockC, nil, 1, chainObsvReqC[vaa.ChainIDBSC], *unsafeDevMode).Run); err != nil { return err } - { - var extraParams []string - var minConfirmations uint64 = 512 - if *polygonCheckpoint != "" { - extraParams = []string{*polygonCheckpoint} - minConfirmations = 1 - } else if *testnetMode { - minConfirmations = 64 - } - if err := supervisor.Run(ctx, "polygonwatch", - ethereum.NewEthWatcher(*polygonRPC, polygonContractAddr, "polygon", common.ReadinessPolygonSyncing, vaa.ChainIDPolygon, lockC, nil, minConfirmations, chainObsvReqC[vaa.ChainIDPolygon], - *unsafeDevMode, extraParams).Run); err != nil { - // Special case: Polygon can fork like PoW Ethereum, and it's not clear what the safe number of blocks is - // - // If we are not querying the checkpoint server, we should hardcode the minimum number of confirmations to 512 regardless of what the smart contract specifies to protect - // developers from accidentally specifying an unsafe number of confirmations. We can remove this restriction as soon - // as specific public guidance exists for Polygon developers. - return err - } + polygonMinConfirmations := uint64(512) + if *testnetMode { + polygonMinConfirmations = 64 + } + + if err := supervisor.Run(ctx, "polygonwatch", + ethereum.NewEthWatcher(*polygonRPC, polygonContractAddr, "polygon", common.ReadinessPolygonSyncing, vaa.ChainIDPolygon, lockC, nil, polygonMinConfirmations, chainObsvReqC[vaa.ChainIDPolygon], + *unsafeDevMode).Run); err != nil { + // Special case: Polygon can fork like PoW Ethereum, and it's not clear what the safe number of blocks is + // + // If we are not querying the checkpoint server, we should hardcode the minimum number of confirmations to 512 regardless of what the smart contract specifies to protect + // developers from accidentally specifying an unsafe number of confirmations. We can remove this restriction as soon + // as specific public guidance exists for Polygon developers. + return err } if err := supervisor.Run(ctx, "avalanchewatch", - ethereum.NewEthWatcher(*avalancheRPC, avalancheContractAddr, "avalanche", common.ReadinessAvalancheSyncing, vaa.ChainIDAvalanche, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAvalanche], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*avalancheRPC, avalancheContractAddr, "avalanche", common.ReadinessAvalancheSyncing, vaa.ChainIDAvalanche, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAvalanche], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "oasiswatch", - ethereum.NewEthWatcher(*oasisRPC, oasisContractAddr, "oasis", common.ReadinessOasisSyncing, vaa.ChainIDOasis, lockC, nil, 1, chainObsvReqC[vaa.ChainIDOasis], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*oasisRPC, oasisContractAddr, "oasis", common.ReadinessOasisSyncing, vaa.ChainIDOasis, lockC, nil, 1, chainObsvReqC[vaa.ChainIDOasis], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "aurorawatch", - ethereum.NewEthWatcher(*auroraRPC, auroraContractAddr, "aurora", common.ReadinessAuroraSyncing, vaa.ChainIDAurora, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAurora], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*auroraRPC, auroraContractAddr, "aurora", common.ReadinessAuroraSyncing, vaa.ChainIDAurora, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAurora], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "fantomwatch", - ethereum.NewEthWatcher(*fantomRPC, fantomContractAddr, "fantom", common.ReadinessFantomSyncing, vaa.ChainIDFantom, lockC, nil, 1, chainObsvReqC[vaa.ChainIDFantom], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*fantomRPC, fantomContractAddr, "fantom", common.ReadinessFantomSyncing, vaa.ChainIDFantom, lockC, nil, 1, chainObsvReqC[vaa.ChainIDFantom], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "karurawatch", - ethereum.NewEthWatcher(*karuraRPC, karuraContractAddr, "karura", common.ReadinessKaruraSyncing, vaa.ChainIDKarura, lockC, nil, 1, chainObsvReqC[vaa.ChainIDKarura], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*karuraRPC, karuraContractAddr, "karura", common.ReadinessKaruraSyncing, vaa.ChainIDKarura, lockC, nil, 1, chainObsvReqC[vaa.ChainIDKarura], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "acalawatch", - ethereum.NewEthWatcher(*acalaRPC, acalaContractAddr, "acala", common.ReadinessAcalaSyncing, vaa.ChainIDAcala, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAcala], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*acalaRPC, acalaContractAddr, "acala", common.ReadinessAcalaSyncing, vaa.ChainIDAcala, lockC, nil, 1, chainObsvReqC[vaa.ChainIDAcala], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "klaytnwatch", - ethereum.NewEthWatcher(*klaytnRPC, klaytnContractAddr, "klaytn", common.ReadinessKlaytnSyncing, vaa.ChainIDKlaytn, lockC, nil, 1, chainObsvReqC[vaa.ChainIDKlaytn], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*klaytnRPC, klaytnContractAddr, "klaytn", common.ReadinessKlaytnSyncing, vaa.ChainIDKlaytn, lockC, nil, 1, chainObsvReqC[vaa.ChainIDKlaytn], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "celowatch", - ethereum.NewEthWatcher(*celoRPC, celoContractAddr, "celo", common.ReadinessCeloSyncing, vaa.ChainIDCelo, lockC, nil, 1, chainObsvReqC[vaa.ChainIDCelo], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*celoRPC, celoContractAddr, "celo", common.ReadinessCeloSyncing, vaa.ChainIDCelo, lockC, nil, 1, chainObsvReqC[vaa.ChainIDCelo], *unsafeDevMode).Run); err != nil { return err } if *testnetMode { if err := supervisor.Run(ctx, "ethropstenwatch", - ethereum.NewEthWatcher(*ethRopstenRPC, ethRopstenContractAddr, "ethropsten", common.ReadinessEthRopstenSyncing, vaa.ChainIDEthereumRopsten, lockC, nil, 1, chainObsvReqC[vaa.ChainIDEthereumRopsten], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*ethRopstenRPC, ethRopstenContractAddr, "ethropsten", common.ReadinessEthRopstenSyncing, vaa.ChainIDEthereumRopsten, lockC, nil, 1, chainObsvReqC[vaa.ChainIDEthereumRopsten], *unsafeDevMode).Run); err != nil { return err } if err := supervisor.Run(ctx, "moonbeamwatch", - ethereum.NewEthWatcher(*moonbeamRPC, moonbeamContractAddr, "moonbeam", common.ReadinessMoonbeamSyncing, vaa.ChainIDMoonbeam, lockC, nil, 1, chainObsvReqC[vaa.ChainIDMoonbeam], *unsafeDevMode, nil).Run); err != nil { + ethereum.NewEthWatcher(*moonbeamRPC, moonbeamContractAddr, "moonbeam", common.ReadinessMoonbeamSyncing, vaa.ChainIDMoonbeam, lockC, nil, 1, chainObsvReqC[vaa.ChainIDMoonbeam], *unsafeDevMode).Run); err != nil { return err } } diff --git a/node/pkg/ethereum/polygonfin.go b/node/pkg/ethereum/polygonfin.go deleted file mode 100644 index 001fc1e27..000000000 --- a/node/pkg/ethereum/polygonfin.go +++ /dev/null @@ -1,93 +0,0 @@ -// This implements the finality check for Polygon. -// -// It can take up to 512 blocks for polygon blocks to be finalized. Rather than wait that long, we will query the checkpoint to see if they are finalized sooner. -// -// TestNet query URL: "https://apis.matic.network/api/v1/mumbai/block-included/" -// MainNet query URL: "https://apis.matic.network/api/v1/matic/block-included/block-number/" - -package ethereum - -import ( - "context" - "encoding/json" - common "github.com/certusone/wormhole/node/pkg/common" - "go.uber.org/zap" - "io/ioutil" - "math/big" - "net/http" -) - -type PolygonFinalizer struct { - Url string - logger *zap.Logger - networkName string - highestCheckpoint big.Int -} - -func UsePolygonFinalizer(extraParams []string) bool { - return len(extraParams) != 0 && extraParams[0] != "" -} - -func (f *PolygonFinalizer) SetLogger(l *zap.Logger, netName string) { - f.logger = l - f.networkName = netName - f.logger.Info("using Polygon specific finality check", zap.String("eth_network", f.networkName), zap.String("query_url", f.Url)) -} - -func (f *PolygonFinalizer) DialContext(ctx context.Context, _rawurl string) (err error) { - return nil -} - -func (f *PolygonFinalizer) IsBlockFinalized(ctx context.Context, block *common.NewBlock) (bool, error) { - if block.Number.Cmp(&f.highestCheckpoint) <= 0 { - return true, nil - } - - url := f.Url + block.Number.String() - response, err := http.Get(url) - if err != nil { - return false, err - } - - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - return false, err - } - - var result map[string]string - json.Unmarshal([]byte(responseData), &result) - - status := result["message"] - if status == "" || status == "No block found" { - f.logger.Info("DEBUG: not finalized", zap.String("eth_network", f.networkName), zap.String("requested_block", block.Number.String())) - return false, nil - } - - if status != "success" { - f.logger.Error("unexpected checkpoint status", zap.String("eth_network", f.networkName), - zap.String("requested_block", block.Number.String()), zap.String("status", status)) - return false, nil - } - - // If we get this far, we know this block is finalized, so we will return true even in the error cases. - - endStr := result["end"] - if endStr == "" { - f.logger.Error("checkpoint reply is missing end", zap.String("eth_network", f.networkName), zap.String("requested_block", block.Number.String())) - return true, nil - } - - end, ok := new(big.Int).SetString(endStr, 10) - if !ok { - f.logger.Error("checkpoint reply contains unexpected end", zap.String("eth_network", f.networkName), - zap.String("requested_block", block.Number.String()), zap.String("end_str", endStr)) - return true, nil - } - - f.highestCheckpoint = *end - - f.logger.Info("checkpoint query returned", zap.String("eth_network", f.networkName), - zap.String("requested_block", block.Number.String()), zap.String("reply", string(responseData))) - - return (block.Number.Cmp(&f.highestCheckpoint) <= 0), nil -} diff --git a/node/pkg/ethereum/watcher.go b/node/pkg/ethereum/watcher.go index 4e1bda380..f0cfdb672 100644 --- a/node/pkg/ethereum/watcher.go +++ b/node/pkg/ethereum/watcher.go @@ -126,8 +126,7 @@ func NewEthWatcher( setEvents chan *common.GuardianSet, minConfirmations uint64, obsvReqC chan *gossipv1.ObservationRequest, - unsafeDevMode bool, - extraParams []string) *Watcher { + unsafeDevMode bool) *Watcher { var ethIntf common.Ethish if chainID == vaa.ChainIDCelo && !unsafeDevMode { @@ -136,8 +135,6 @@ func NewEthWatcher( ethIntf = &celo.CeloImpl{NetworkName: networkName} } else if chainID == vaa.ChainIDMoonbeam && !unsafeDevMode { ethIntf = &PollImpl{BaseEth: EthImpl{NetworkName: networkName}, Finalizer: &MoonbeamFinalizer{}, DelayInMs: 250} - } else if chainID == vaa.ChainIDPolygon && UsePolygonFinalizer(extraParams) { - ethIntf = &PollImpl{BaseEth: EthImpl{NetworkName: networkName}, Finalizer: &PolygonFinalizer{Url: extraParams[0]}, DelayInMs: 5000} } else { ethIntf = &EthImpl{NetworkName: networkName} }