node/pkg/ethereum: minimum confirmations for Polygon

commit-id:0d95a63c
This commit is contained in:
Leo 2022-01-10 16:24:31 +01:00 committed by Leopold Schabel
parent ab18f278fe
commit 81d1e821c0
2 changed files with 34 additions and 17 deletions

View File

@ -572,31 +572,38 @@ func runNode(cmd *cobra.Command, args []string) {
}
if err := supervisor.Run(ctx, "ethwatch",
ethereum.NewEthWatcher(*ethRPC, ethContractAddr, "eth", common.ReadinessEthSyncing, vaa.ChainIDEthereum, lockC, setC).Run); err != nil {
ethereum.NewEthWatcher(*ethRPC, ethContractAddr, "eth", common.ReadinessEthSyncing, vaa.ChainIDEthereum, lockC, setC, 1).Run); err != nil {
return err
}
if err := supervisor.Run(ctx, "bscwatch",
ethereum.NewEthWatcher(*bscRPC, bscContractAddr, "bsc", common.ReadinessBSCSyncing, vaa.ChainIDBSC, lockC, nil).Run); err != nil {
ethereum.NewEthWatcher(*bscRPC, bscContractAddr, "bsc", common.ReadinessBSCSyncing, vaa.ChainIDBSC, lockC, nil, 1).Run); err != nil {
return err
}
if err := supervisor.Run(ctx, "polygonwatch",
ethereum.NewEthWatcher(*polygonRPC, polygonContractAddr, "polygon", common.ReadinessPolygonSyncing, vaa.ChainIDPolygon, lockC, nil).Run); err != nil {
ethereum.NewEthWatcher(
*polygonRPC, polygonContractAddr, "polygon", common.ReadinessPolygonSyncing, vaa.ChainIDPolygon, lockC, nil,
// Special case: Polygon can fork like PoW Ethereum, and it's not clear what the safe number of blocks is
//
// Hardcode the minimum number of confirmations to 256 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.
256).Run); err != nil {
return err
}
if err := supervisor.Run(ctx, "avalanchewatch",
ethereum.NewEthWatcher(*avalancheRPC, avalancheContractAddr, "avalanche", common.ReadinessAvalancheSyncing, vaa.ChainIDAvalanche, lockC, nil).Run); err != nil {
ethereum.NewEthWatcher(*avalancheRPC, avalancheContractAddr, "avalanche", common.ReadinessAvalancheSyncing, vaa.ChainIDAvalanche, lockC, nil, 1).Run); err != nil {
return err
}
if err := supervisor.Run(ctx, "oasiswatch",
ethereum.NewEthWatcher(*oasisRPC, oasisContractAddr, "oasis", common.ReadinessOasisSyncing, vaa.ChainIDOasis, lockC, nil).Run); err != nil {
ethereum.NewEthWatcher(*oasisRPC, oasisContractAddr, "oasis", common.ReadinessOasisSyncing, vaa.ChainIDOasis, lockC, nil, 1).Run); err != nil {
return err
}
if *testnetMode {
if err := supervisor.Run(ctx, "ethropstenwatch",
ethereum.NewEthWatcher(*ethRopstenRPC, ethRopstenContractAddr, "ethropsten", common.ReadinessEthRopstenSyncing, vaa.ChainIDEthereumRopsten, lockC, setC).Run); err != nil {
ethereum.NewEthWatcher(*ethRopstenRPC, ethRopstenContractAddr, "ethropsten", common.ReadinessEthRopstenSyncing, vaa.ChainIDEthereumRopsten, lockC, setC, 1).Run); err != nil {
return err
}
}

View File

@ -92,6 +92,9 @@ type (
// 0 is a valid guardian set, so we need a nil value here
currentGuardianSet *uint32
// Minimum number of confirmations to accept, regardless of what the contract specifies.
minConfirmations uint64
}
pendingKey struct {
@ -114,16 +117,18 @@ func NewEthWatcher(
readiness readiness.Component,
chainID vaa.ChainID,
messageEvents chan *common.MessagePublication,
setEvents chan *common.GuardianSet) *Watcher {
setEvents chan *common.GuardianSet,
minConfirmations uint64) *Watcher {
return &Watcher{
url: url,
contract: contract,
networkName: networkName,
readiness: readiness,
chainID: chainID,
msgChan: messageEvents,
setChan: setEvents,
pending: map[pendingKey]*pendingMessage{}}
url: url,
contract: contract,
networkName: networkName,
readiness: readiness,
minConfirmations: minConfirmations,
chainID: chainID,
msgChan: messageEvents,
setChan: setEvents,
pending: map[pendingKey]*pendingMessage{}}
}
func (e *Watcher) Run(ctx context.Context) error {
@ -283,10 +288,15 @@ func (e *Watcher) Run(ctx context.Context) error {
e.pendingMu.Lock()
blockNumberU := ev.Number.Uint64()
for key, pLock := range e.pending {
expectedConfirmations := uint64(pLock.message.ConsistencyLevel)
if expectedConfirmations < e.minConfirmations {
expectedConfirmations = e.minConfirmations
}
// Transaction was dropped and never picked up again
if pLock.height+4*uint64(pLock.message.ConsistencyLevel) <= blockNumberU {
if pLock.height+4*uint64(expectedConfirmations) <= blockNumberU {
logger.Debug("observation timed out",
zap.Stringer("tx", pLock.message.TxHash),
zap.Stringer("blockhash", key.BlockHash),
@ -301,7 +311,7 @@ func (e *Watcher) Run(ctx context.Context) error {
}
// Transaction is now ready
if pLock.height+uint64(pLock.message.ConsistencyLevel) <= ev.Number.Uint64() {
if pLock.height+uint64(expectedConfirmations) <= blockNumberU {
timeout, cancel = context.WithTimeout(ctx, 15*time.Second)
tx, err := c.TransactionReceipt(timeout, pLock.message.TxHash)
cancel()