Skip to content

Commit

Permalink
refactor(execution_deposits_exporter): cache exported deposits by mer…
Browse files Browse the repository at this point in the history
…kleTreeIndex
  • Loading branch information
guybrush committed Oct 10, 2024
1 parent cab4d83 commit e33d9ac
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions backend/pkg/exporter/modules/execution_deposits_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
gethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/go-redis/redis/v8"
lru "github.com/hashicorp/golang-lru/v2"
"golang.org/x/exp/maps"

"github.com/gobitfly/beaconchain/pkg/commons/contracts/deposit_contract"
Expand Down Expand Up @@ -51,6 +52,7 @@ type executionDepositsExporter struct {
CurrentHeadBlock atomic.Uint64
Signer gethtypes.Signer
DepositMethod abi.Method
exportedCache *lru.Cache[string, bool] // cache for exported deposits by merkle-tree-index
}

func NewExecutionDepositsExporter(moduleContext ModuleContext) ModuleInterface {
Expand All @@ -64,6 +66,12 @@ func NewExecutionDepositsExporter(moduleContext ModuleContext) ModuleInterface {
}

func (d *executionDepositsExporter) Init() error {
exportedCache, err := lru.New[string, bool](2048)
if err != nil {
log.Fatal(err, "error creating exportedCache", 0)
}
d.exportedCache = exportedCache

d.Signer = gethtypes.NewCancunSigner(big.NewInt(0).SetUint64(utils.Config.Chain.ClConfig.DepositChainID))

d.LastExportedFinalizedBlockRedisKey = fmt.Sprintf("%d:execution_deposits_exporter:last_exported_finalized_block", utils.Config.Chain.ClConfig.DepositChainID)
Expand Down Expand Up @@ -311,6 +319,11 @@ func (d *executionDepositsExporter) fetchDeposits(fromBlock, toBlock uint64) (de
}

depositLog := depositLogIterator.Event

if d.exportedCache.Contains(hexutil.Encode(depositLog.Index)) {
continue
}

err = utils.VerifyDepositSignature(&phase0.DepositData{
PublicKey: phase0.BLSPubKey(depositLog.Pubkey),
WithdrawalCredentials: depositLog.WithdrawalCredentials,
Expand Down Expand Up @@ -490,6 +503,10 @@ func (d *executionDepositsExporter) saveDeposits(depositsToSave []*types.ELDepos
return fmt.Errorf("error committing db-tx for execution layer deposits: %w", err)
}

for _, dd := range depositsToSave {
d.exportedCache.Add(hexutil.Encode(dd.MerkletreeIndex), true)
}

return nil
}

Expand Down

0 comments on commit e33d9ac

Please sign in to comment.