Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 12, 2024
1 parent 29da517 commit 995c362
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Binary file added localbridgesync/__debug_bin2544723965
Binary file not shown.
13 changes: 12 additions & 1 deletion localbridgesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,41 @@ func newDownloader(
func download(ctx context.Context, d downloaderInterface, fromBlock, syncBlockChunkSize uint64, downloadedCh chan block) {
lastBlock := d.waitForNewBlocks(ctx, 0)
for {
log.Debug("select")
select {
case <-ctx.Done():
log.Debug("closing channel")
close(downloadedCh)
return
default:
}
log.Debug("default")
toBlock := fromBlock + syncBlockChunkSize
if toBlock > lastBlock {
toBlock = lastBlock
}
if fromBlock == toBlock {
log.Debug("1")
if fromBlock >= toBlock {
log.Debug("waitForNewBlocks")
lastBlock = d.waitForNewBlocks(ctx, toBlock)
log.Debug("out waitForNewBlocks")
continue
}
log.Debug("2", fromBlock, toBlock)
blocks := d.getEventsByBlockRange(ctx, fromBlock, toBlock)
for _, b := range blocks {
log.Debug("sending block with events")
downloadedCh <- b
}
log.Debug("3")
if len(blocks) == 0 || blocks[len(blocks)-1].Num < toBlock {
// Indicate the last downloaded block if there are not events on it
log.Debug("sending block without events")
downloadedCh <- block{
blockHeader: d.getBlockHeader(ctx, toBlock),
}
}
log.Debug("4")
fromBlock = toBlock + 1
}
}
Expand Down
47 changes: 43 additions & 4 deletions localbridgesync/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"math/big"
"testing"
"time"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridge"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -262,19 +264,56 @@ func generateClaim(t *testing.T, blockNum uint32, event *abi.Event, isV1 bool) (
}

func TestDownload(t *testing.T) {
downloaderMock := NewDownloaderMock(t)
d := NewDownloaderMock(t)
downloadCh := make(chan block)
ctx := context.Background()
ctx1, cancel := context.WithCancel(ctx)

// TODO: populate mock and iterations
expectedBlocks := []block{}

go download(ctx1, downloaderMock, 0, syncBlockChunck, downloadCh)
// iteratiion 1:
// last block is 1, download that block (no events and wait)
d.On("waitForNewBlocks", mock.Anything, uint64(0)).
Return(uint64(1)).Once()
b1 := block{
blockHeader: blockHeader{
Num: 1,
Hash: common.HexToHash("01"),
},
}
expectedBlocks = append(expectedBlocks, b1)
d.On("getEventsByBlockRange", mock.Anything, uint64(0), uint64(1)).
Return([]block{})
d.On("getBlockHeader", mock.Anything, uint64(1)).
Return(b1.blockHeader)

// iteration 2: wait for next block to be created
d.On("waitForNewBlocks", mock.Anything, uint64(1)).
// After(time.Millisecond * 100).
Return(uint64(2)).Once()

// iteration 3: block 2 has events
b2 := block{
blockHeader: blockHeader{
Num: 2,
Hash: common.HexToHash("02"),
},
}
expectedBlocks = append(expectedBlocks, b2)
d.On("getEventsByBlockRange", mock.Anything, uint64(2), uint64(2)).
Return([]block{b2})

// iteration 4: wait for next block to be created
d.On("waitForNewBlocks", mock.Anything, uint64(2)).
After(time.Millisecond * 100).
Return(uint64(3)).Once()

go download(ctx1, d, 0, syncBlockChunck, downloadCh)
for _, expectedBlock := range expectedBlocks {
actualBlock := <-downloadCh
log.Debugf("block %d received!", actualBlock.Num)
require.Equal(t, expectedBlock, actualBlock)
}
log.Debug("canceling")
cancel()
_, ok := <-downloadCh
require.False(t, ok)
Expand Down

0 comments on commit 995c362

Please sign in to comment.