Skip to content

Commit

Permalink
feat: add index iterator for indexes with large entry count
Browse files Browse the repository at this point in the history
tests: surpress revert tx test as it requires an archive node to simulate
  • Loading branch information
kamikazechaser committed Aug 13, 2024
1 parent 83d6776 commit c46f4c1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
66 changes: 66 additions & 0 deletions index_iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ethutils

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/w3types"
)

type BatchIterator struct {
provider *Provider
index common.Address
entryCount *big.Int
currentIndex int64
batchSize int64
}

const defaultBatchSize = 500

func (p *Provider) NewBatchIterator(ctx context.Context, index common.Address) (*BatchIterator, error) {
var entryCount big.Int

if err := p.Client.CallCtx(
ctx,
eth.CallFunc(index, entryCountFunc).Returns(&entryCount),
); err != nil {
return nil, err
}

return &BatchIterator{
provider: p,
index: index,
entryCount: &entryCount,
currentIndex: 0,
batchSize: defaultBatchSize,
}, nil
}

func (iter *BatchIterator) Next(ctx context.Context) ([]common.Address, error) {
if iter.currentIndex >= iter.entryCount.Int64() {
return nil, nil
}

endIndex := iter.currentIndex + iter.batchSize
if endIndex > iter.entryCount.Int64() {
endIndex = iter.entryCount.Int64()
}

batchSize := endIndex - iter.currentIndex
calls := make([]w3types.RPCCaller, batchSize)
tokenAddresses := make([]common.Address, batchSize)

for i := int64(0); i < batchSize; i++ {
index := iter.currentIndex + i
calls[i] = eth.CallFunc(iter.index, entrySig, new(big.Int).SetInt64(index)).Returns(&tokenAddresses[i])
}

if err := iter.provider.Client.CallCtx(ctx, calls...); err != nil {
return nil, err
}

iter.currentIndex = endIndex
return tokenAddresses, nil
}
29 changes: 29 additions & 0 deletions index_iter_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ethutils

import (
"context"
"testing"

"github.com/lmittmann/w3"
)

func TestIndexIter_Next(t *testing.T) {
p := NewProvider("https://forno.celo.org", CeloMainnet)

ctx := context.Background()
iter, err := p.NewBatchIterator(ctx, w3.A("0xe2CEf4000d6003958c891D251328850f84654eb9"))
if err != nil {
t.Fatal(err)
}

for {
batch, err := iter.Next(ctx)
if err != nil {
t.Error(err)
}
if batch == nil {
break
}
t.Logf("index batch size %d", len(batch))
}
}
2 changes: 2 additions & 0 deletions simulate_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestProvider_SimulateRevertedTx(t *testing.T) {
t.Skip("requires archive node")

p := NewProvider("https://forno.celo.org", CeloMainnet)

type args struct {
Expand Down

0 comments on commit c46f4c1

Please sign in to comment.