From 4b3c2bf28caacf1358299c9895df9ea79e3735d4 Mon Sep 17 00:00:00 2001 From: alrevuelta Date: Wed, 6 Mar 2024 18:24:17 +0100 Subject: [PATCH] Run onchain reconciliation when possible --- main.go | 34 +++++++++++++++++++--------------- oracle/onchain.go | 8 ++++---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 10e3c12..e5d6970 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "syscall" "time" + "github.com/avast/retry-go/v4" "github.com/dappnode/mev-sp-oracle/api" "github.com/dappnode/mev-sp-oracle/config" "github.com/dappnode/mev-sp-oracle/constants" @@ -279,25 +280,28 @@ func mainLoop(oracleInstance *oracle.Oracle, onchain *oracle.Onchain, cfg *oracl // From time to time we do onchain reconciliation to ensure our assets match our liablities if time.Now().Unix()-lastReconciliationTime > (ReconciliationEveryHours * 3600) { - // TODO: Temporally commented. Doesn't work well with non archival node - //log.Info("Running onchain reconciliation. Last one was: ", lastReconciliationTime) - //lastReconciliationTime = time.Now().Unix() + log.Info("Running onchain reconciliation. Last one was: ", lastReconciliationTime) // If we are up to date, this is the latest finalized block. Run this only in the last block, otherwise // a non archival node will error "missing trie node". Non archival nodes don't store much before last // finalized block. - //finalizedBlock := big.NewInt(0).SetUint64(oracleInstance.State().LatestProcessedBlock) - //uniqueAddresses := oracleInstance.GetUniqueWithdrawalAddresses() - //poolEthBalanceWei, err := onchain.GetPoolEthBalance(finalizedBlock) - //if err != nil { - // log.Fatal("Could not get pool eth balance for reconciliation: ", err) - //} - - //claimedPerAccount := onchain.GetClaimedPerWithdrawalAddress(uniqueAddresses, finalizedBlock) - //err = oracleInstance.RunOnchainReconciliation(poolEthBalanceWei, claimedPerAccount) - //if err != nil { - // log.Fatal("Reconciliation failed, state was not commited: ", err) - //} + finalizedBlock := big.NewInt(0).SetUint64(oracleInstance.State().LatestProcessedBlock) + uniqueAddresses := oracleInstance.GetUniqueWithdrawalAddresses() + + // If EL is not in archival mode, this wont work in longs periods of non finality. + retryOption := retry.Attempts(1) + poolEthBalanceWei, err1 := onchain.GetPoolEthBalance(finalizedBlock, retryOption) + claimedPerAccount, err2 := onchain.GetClaimedPerWithdrawalAddress(uniqueAddresses, finalizedBlock, retryOption) + if err1 != nil || err2 != nil { + log.Warn("Could not get pool eth balance for reconciliation, normal when no finality: ", err1, err2) + } else { + // If we could fetch the data, run onchain reconciliation + err = oracleInstance.RunOnchainReconciliation(poolEthBalanceWei, claimedPerAccount) + if err != nil { + log.Fatal("Reconciliation failed, state was not commited: ", err) + } + } + lastReconciliationTime = time.Now().Unix() } time.Sleep(1 * time.Minute) diff --git a/oracle/onchain.go b/oracle/onchain.go index c1a2d9c..6d232aa 100644 --- a/oracle/onchain.go +++ b/oracle/onchain.go @@ -1456,14 +1456,14 @@ func (o *Onchain) GetAcceptGovernanceEvents( return events, nil } -func (o *Onchain) GetClaimedPerWithdrawalAddress(addresses []string, finalizedBlock *big.Int) map[string]*big.Int { +func (o *Onchain) GetClaimedPerWithdrawalAddress(addresses []string, finalizedBlock *big.Int, opts ...retry.Option) (map[string]*big.Int, error) { claimedMap := make(map[string]*big.Int) for _, address := range addresses { // Important to use finalized blocks. Otherwise our local oracle view and onchain view may differ - claimed, err := o.GetContractClaimedBalance(address, finalizedBlock) + claimed, err := o.GetContractClaimedBalance(address, finalizedBlock, opts...) if err != nil { - log.Fatal("Could not get claimed balance for deposit address ", address, ": ", err.Error()) + return nil, errors.Wrap(err, "could not get claimed balance") } _, found := claimedMap[address] if found { @@ -1471,7 +1471,7 @@ func (o *Onchain) GetClaimedPerWithdrawalAddress(addresses []string, finalizedBl } claimedMap[address] = claimed } - return claimedMap + return claimedMap, nil } func (o *Onchain) UpdateContractMerkleRoot(slot uint64, newMerkleRoot string) error {