mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Merge pull request #587 from hermeznetwork/feature/legacy-sync2
Update synchronizer.Sync2 to Sync from legacy impl
This commit is contained in:
@@ -517,7 +517,7 @@ func TestCoordinatorStress(t *testing.T) {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
blockData, _, err := syn.Sync2(ctx, nil)
|
blockData, _, err := syn.Sync(ctx, nil)
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ func preloadSync(t *testing.T, ethClient *test.Client, sync *synchronizer.Synchr
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
for {
|
for {
|
||||||
syncBlock, discards, err := sync.Sync2(ctx, nil)
|
syncBlock, discards, err := sync.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
if syncBlock == nil {
|
if syncBlock == nil {
|
||||||
|
|||||||
@@ -558,7 +558,7 @@ func (n *Node) handleReorg(ctx context.Context, stats *synchronizer.Stats, vars
|
|||||||
// TODO(Edu): Consider keeping the `lastBlock` inside synchronizer so that we
|
// TODO(Edu): Consider keeping the `lastBlock` inside synchronizer so that we
|
||||||
// don't have to pass it around.
|
// don't have to pass it around.
|
||||||
func (n *Node) syncLoopFn(ctx context.Context, lastBlock *common.Block) (*common.Block, time.Duration, error) {
|
func (n *Node) syncLoopFn(ctx context.Context, lastBlock *common.Block) (*common.Block, time.Duration, error) {
|
||||||
blockData, discarded, err := n.sync.Sync2(ctx, lastBlock)
|
blockData, discarded, err := n.sync.Sync(ctx, lastBlock)
|
||||||
stats := n.sync.Stats()
|
stats := n.sync.Stats()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// case: error
|
// case: error
|
||||||
|
|||||||
@@ -503,13 +503,13 @@ func (s *Synchronizer) resetIntermediateState() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync2 attems to synchronize an ethereum block starting from lastSavedBlock.
|
// Sync attems to synchronize an ethereum block starting from lastSavedBlock.
|
||||||
// If lastSavedBlock is nil, the lastSavedBlock value is obtained from de DB.
|
// If lastSavedBlock is nil, the lastSavedBlock value is obtained from de DB.
|
||||||
// If a block is synched, it will be returned and also stored in the DB. If a
|
// If a block is synched, it will be returned and also stored in the DB. If a
|
||||||
// reorg is detected, the number of discarded blocks will be returned and no
|
// reorg is detected, the number of discarded blocks will be returned and no
|
||||||
// synchronization will be made.
|
// synchronization will be made.
|
||||||
// TODO: Be smart about locking: only lock during the read/write operations
|
// TODO: Be smart about locking: only lock during the read/write operations
|
||||||
func (s *Synchronizer) Sync2(ctx context.Context,
|
func (s *Synchronizer) Sync(ctx context.Context,
|
||||||
lastSavedBlock *common.Block) (blockData *common.BlockData, discarded *int64, err error) {
|
lastSavedBlock *common.Block) (blockData *common.BlockData, discarded *int64, err error) {
|
||||||
if s.resetStateFailed {
|
if s.resetStateFailed {
|
||||||
if err := s.resetIntermediateState(); err != nil {
|
if err := s.resetIntermediateState(); err != nil {
|
||||||
|
|||||||
@@ -359,7 +359,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
assert.Equal(t, false, stats.Synced())
|
assert.Equal(t, false, stats.Synced())
|
||||||
|
|
||||||
// Test Sync for rollup genesis block
|
// Test Sync for rollup genesis block
|
||||||
syncBlock, discards, err := s.Sync2(ctx, nil)
|
syncBlock, discards, err := s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.NotNil(t, syncBlock)
|
require.NotNil(t, syncBlock)
|
||||||
@@ -382,7 +382,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
assert.Equal(t, int64(1), dbBlocks[1].Num)
|
assert.Equal(t, int64(1), dbBlocks[1].Num)
|
||||||
|
|
||||||
// Sync again and expect no new blocks
|
// Sync again and expect no new blocks
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.Nil(t, syncBlock)
|
require.Nil(t, syncBlock)
|
||||||
@@ -479,7 +479,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
|
|
||||||
// Block 2
|
// Block 2
|
||||||
|
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.NotNil(t, syncBlock)
|
require.NotNil(t, syncBlock)
|
||||||
@@ -496,7 +496,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
|
|
||||||
// Block 3
|
// Block 3
|
||||||
|
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
@@ -520,7 +520,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
client.CtlMineBlock()
|
client.CtlMineBlock()
|
||||||
|
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.NotNil(t, syncBlock)
|
require.NotNil(t, syncBlock)
|
||||||
@@ -571,7 +571,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
|
|
||||||
client.CtlMineBlock()
|
client.CtlMineBlock()
|
||||||
|
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.NotNil(t, syncBlock)
|
require.NotNil(t, syncBlock)
|
||||||
@@ -656,7 +656,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// First sync detects the reorg and discards 4 blocks
|
// First sync detects the reorg and discards 4 blocks
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expetedDiscards := int64(4)
|
expetedDiscards := int64(4)
|
||||||
require.Equal(t, &expetedDiscards, discards)
|
require.Equal(t, &expetedDiscards, discards)
|
||||||
@@ -684,7 +684,7 @@ func TestSyncGeneral(t *testing.T) {
|
|||||||
|
|
||||||
// Sync blocks 2-6
|
// Sync blocks 2-6
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
require.NotNil(t, syncBlock)
|
require.NotNil(t, syncBlock)
|
||||||
@@ -807,7 +807,7 @@ func TestSyncForgerCommitment(t *testing.T) {
|
|||||||
|
|
||||||
// be in sync
|
// be in sync
|
||||||
for {
|
for {
|
||||||
syncBlock, discards, err := s.Sync2(ctx, nil)
|
syncBlock, discards, err := s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
if syncBlock == nil {
|
if syncBlock == nil {
|
||||||
@@ -826,7 +826,7 @@ func TestSyncForgerCommitment(t *testing.T) {
|
|||||||
err = client.CtlAddBlocks([]common.BlockData{block})
|
err = client.CtlAddBlocks([]common.BlockData{block})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
syncBlock, discards, err := s.Sync2(ctx, nil)
|
syncBlock, discards, err := s.Sync(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, discards)
|
require.Nil(t, discards)
|
||||||
if syncBlock == nil {
|
if syncBlock == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user