Skip to content

Commit

Permalink
constrain Expect.RevertBlock() in test
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 24, 2024
1 parent fe959e2 commit 82bd6a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
8 changes: 7 additions & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/mocks"
junoplugin "github.com/NethermindEth/juno/plugin"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
Expand Down Expand Up @@ -55,7 +56,12 @@ func TestPlugin(t *testing.T) {
require.Equal(t, utils.HexToFelt(t, "0x34e815552e42c5eb5233b99de2d3d7fd396e575df2719bf98e7ed2794494f86"), head.Hash)

// Reorg 2 blocks, then sync 3 blocks
plugin.EXPECT().RevertBlock(gomock.Any(), gomock.Any(), gomock.Any()).Times(2)
su1, block1, err := integGw.StateUpdateWithBlock(context.Background(), uint64(1))
require.NoError(t, err)
su0, block0, err := integGw.StateUpdateWithBlock(context.Background(), uint64(0))
require.NoError(t, err)
plugin.EXPECT().RevertBlock(&junoplugin.BlockAndStateUpdate{block1, su1}, &junoplugin.BlockAndStateUpdate{block0, su0}, gomock.Any())
plugin.EXPECT().RevertBlock(&junoplugin.BlockAndStateUpdate{block0, su0}, &junoplugin.BlockAndStateUpdate{nil, nil}, gomock.Any())
for i := range 3 {
su, block, err := mainGw.StateUpdateWithBlock(context.Background(), uint64(i))
require.NoError(t, err)
Expand Down
36 changes: 27 additions & 9 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sync
import (
"context"
"errors"
"fmt"
"runtime"
"sync/atomic"
"time"
Expand Down Expand Up @@ -188,21 +189,38 @@ func (s *Synchronizer) fetchUnknownClasses(ctx context.Context, stateUpdate *cor
return newClasses, closer()
}

func (s *Synchronizer) handlePluginRevertBlock(block *core.Block, stateUpdate *core.StateUpdate, reverseStateDiff *core.StateDiff) {
func (s *Synchronizer) handlePluginRevertBlock() {
if s.plugin == nil {
return

Check warning on line 194 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L194

Added line #L194 was not covered by tests
}
toBlock, err := s.blockchain.Head()
block, err := s.blockchain.Head()
if err != nil {
s.log.Warnw("Failed to retrieve the reverted blockchain head block for the plugin", "err", err)
return

Check warning on line 199 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L198-L199

Added lines #L198 - L199 were not covered by tests
}

toSU, err := s.blockchain.StateUpdateByNumber(toBlock.Number)
stateUpdate, err := s.blockchain.StateUpdateByNumber(block.Number)
if err != nil {
s.log.Warnw("Failed to retrieve the reverted blockchain head state-update for the plugin", "err", err)
return

Check warning on line 204 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L203-L204

Added lines #L203 - L204 were not covered by tests
}
reverseStateDiff, err := s.blockchain.GetReverseStateDiff()
if err != nil {
s.log.Warnw("Failed to retrieve reverse state diff", "head", block.Number, "hash", block.Hash.ShortString(), "err", err)
}
var toBlock *core.Block
var toSU *core.StateUpdate
if block.Number != 0 {
toBlock, err = s.blockchain.BlockByHash(block.ParentHash)
if err != nil {
s.log.Warnw("Failed to retrieve the parent block for the plugin", "err", err)
return

Check warning on line 216 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L215-L216

Added lines #L215 - L216 were not covered by tests
}
toSU, err = s.blockchain.StateUpdateByNumber(toBlock.Number)
if err != nil {
s.log.Warnw("Failed to retrieve the parent state-update for the plugin", "err", err)
return

Check warning on line 221 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L220-L221

Added lines #L220 - L221 were not covered by tests
}
}
err = (s.plugin).RevertBlock(
&junoplugin.BlockAndStateUpdate{Block: block, StateUpdate: stateUpdate},
&junoplugin.BlockAndStateUpdate{Block: toBlock, StateUpdate: toSU},
Expand Down Expand Up @@ -231,20 +249,20 @@ func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stat
return
}
storeTimer := time.Now()
fmt.Println("Store(blo", block.Number)
err = s.blockchain.Store(block, commitments, stateUpdate, newClasses)
fmt.Println(err)
if err != nil {
if errors.Is(err, blockchain.ErrParentDoesNotMatchHead) {
// revert the head and restart the sync process, hoping that the reorg is not deep
// if the reorg is deeper, we will end up here again and again until we fully revert reorged
// blocks
reverseStateDiff, err := s.blockchain.GetReverseStateDiff()
if err != nil {
s.log.Warnw("Failed to retrieve reverse state diff", "head", block.Number, "hash", block.Hash.ShortString(), "err", err)
}
// Todo: move s.revertHead(block) before this
if s.plugin != nil {
s.handlePluginRevertBlock(block, stateUpdate, reverseStateDiff)
s.handlePluginRevertBlock()
}
s.revertHead(block)

} else {

Check failure on line 266 in sync/sync.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
s.log.Warnw("Failed storing Block", "number", block.Number,
"hash", block.Hash.ShortString(), "err", err)
Expand Down

0 comments on commit 82bd6a8

Please sign in to comment.