-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer): Allow resync flag option to restart processing from bl…
…ock 0 (#266)
- Loading branch information
1 parent
91d2434
commit 6b01cbe
Showing
11 changed files
with
207 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cli | ||
package relayer | ||
|
||
type Mode string | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/relayer/indexer/set_initial_processing_block_by_mode.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package indexer | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/taikochain/taiko-mono/packages/relayer" | ||
) | ||
|
||
func (svc *Service) setInitialProcessingBlockByMode( | ||
ctx context.Context, | ||
mode relayer.Mode, | ||
chainID *big.Int, | ||
) error { | ||
switch mode { | ||
case relayer.SyncMode: | ||
// get most recently processed block height from the DB | ||
latestProcessedBlock, err := svc.blockRepo.GetLatestBlockProcessedForEvent( | ||
eventName, | ||
chainID, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "s.blockRepo.GetLatestBlock()") | ||
} | ||
|
||
svc.processingBlock = latestProcessedBlock | ||
|
||
return nil | ||
case relayer.ResyncMode: | ||
header, err := svc.ethClient.HeaderByNumber(ctx, big.NewInt(0)) | ||
if err != nil { | ||
return errors.Wrap(err, "s.blockRepo.GetLatestBlock()") | ||
} | ||
|
||
svc.processingBlock = &relayer.Block{ | ||
Height: header.Number.Uint64(), | ||
Hash: header.Hash().Hex(), | ||
} | ||
|
||
return nil | ||
default: | ||
return relayer.ErrInvalidMode | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/relayer/indexer/set_initial_processing_block_by_mode_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package indexer | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/taikochain/taiko-mono/packages/relayer" | ||
"github.com/taikochain/taiko-mono/packages/relayer/mock" | ||
) | ||
|
||
func Test_SetInitialProcessingBlockByMode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
mode relayer.Mode | ||
chainID *big.Int | ||
wantErr bool | ||
wantHeight uint64 | ||
}{ | ||
{ | ||
"resync", | ||
relayer.ResyncMode, | ||
mock.MockChainID, | ||
false, | ||
0, | ||
}, | ||
{ | ||
"sync", | ||
relayer.SyncMode, | ||
mock.MockChainID, | ||
false, | ||
mock.LatestBlock.Height, | ||
}, | ||
{ | ||
"sync error getting latest block", | ||
relayer.SyncMode, | ||
big.NewInt(328938), | ||
true, | ||
0, | ||
}, | ||
{ | ||
"invalidMode", | ||
relayer.Mode("fake"), | ||
mock.MockChainID, | ||
true, | ||
0, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
svc := newTestService() | ||
err := svc.setInitialProcessingBlockByMode( | ||
context.Background(), | ||
tt.mode, | ||
tt.chainID, | ||
) | ||
|
||
assert.Equal(t, tt.wantErr, err != nil) | ||
|
||
assert.Equal(t, tt.wantHeight, svc.processingBlock.Height) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mock | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/taikochain/taiko-mono/packages/relayer" | ||
) | ||
|
||
var ( | ||
LatestBlock = &relayer.Block{ | ||
Height: 100, | ||
Hash: "0x", | ||
ChainID: MockChainID.Int64(), | ||
} | ||
) | ||
|
||
type BlockRepository struct { | ||
} | ||
|
||
func (r *BlockRepository) Save(opts relayer.SaveBlockOpts) error { | ||
return nil | ||
} | ||
|
||
func (r *BlockRepository) GetLatestBlockProcessedForEvent(eventName string, chainID *big.Int) (*relayer.Block, error) { | ||
if chainID.Int64() != MockChainID.Int64() { | ||
return nil, errors.New("error getting latest block processed for event") | ||
} | ||
|
||
return LatestBlock, nil | ||
} |
Oops, something went wrong.