Skip to content

Commit

Permalink
Run onchain reconciliation when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Mar 6, 2024
1 parent e9fc54e commit 4b3c2bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
34 changes: 19 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions oracle/onchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,22 +1456,22 @@ 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 {
log.Fatal("Duplicate deposit address found: ", address)
}
claimedMap[address] = claimed
}
return claimedMap
return claimedMap, nil
}

func (o *Onchain) UpdateContractMerkleRoot(slot uint64, newMerkleRoot string) error {
Expand Down

0 comments on commit 4b3c2bf

Please sign in to comment.