-
Notifications
You must be signed in to change notification settings - Fork 40
Check NoArchiveError when sending block notifications #408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jenikd
wants to merge
3
commits into
main
Choose a base branch
from
jenikd/fix_feeder_logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,178 @@ | ||
package gossip | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Fantom-foundation/go-opera/evmcore" | ||
"github.com/Fantom-foundation/go-opera/gossip/evmstore" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestServiceFeed_SubscribeNewBlock(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
store := NewMockArchiveBlockHeightSource(ctrl) | ||
|
||
store.EXPECT().GetArchiveBlockHeight().Return(uint64(12), false, nil).AnyTimes() | ||
|
||
feed := ServiceFeed{} | ||
feed.Start(store) | ||
|
||
consumer := make(chan evmcore.ChainHeadNotify, 1) | ||
feed.SubscribeNewBlock(consumer) | ||
|
||
// There should be no signal delivered until there is a notification. | ||
select { | ||
case <-consumer: | ||
t.Fatal("expected no notification to be sent") | ||
case <-time.After(100 * time.Millisecond): | ||
// all good | ||
} | ||
|
||
feed.notifyAboutNewBlock(&evmcore.EvmBlock{ | ||
EvmHeader: evmcore.EvmHeader{ | ||
Number: big.NewInt(12), | ||
}, | ||
}, nil) | ||
|
||
// The notification should be delivered. | ||
select { | ||
case notification := <-consumer: | ||
if notification.Block.Number.Cmp(big.NewInt(12)) != 0 { | ||
t.Fatalf("expected block number 12, got %d", notification.Block.Number) | ||
} | ||
case <-time.After(100 * time.Millisecond): | ||
t.Fatal("expected notification to be sent") | ||
} | ||
|
||
feed.Stop() | ||
} | ||
|
||
func TestServiceFeed_BlocksInOrder(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
store := NewMockArchiveBlockHeightSource(ctrl) | ||
|
||
var startBlocknumber uint64 = 5 | ||
mockBlockNumber := startBlocknumber | ||
expectedBlockNumber := startBlocknumber + 1 | ||
|
||
// Increment expected block height | ||
store.EXPECT().GetArchiveBlockHeight().DoAndReturn(func() (uint64, bool, error) { | ||
mockBlockNumber++ | ||
return mockBlockNumber, false, nil | ||
}).AnyTimes() | ||
|
||
feed := ServiceFeed{} | ||
feed.Start(store) | ||
|
||
consumer := make(chan evmcore.ChainHeadNotify, 5) | ||
feed.SubscribeNewBlock(consumer) | ||
|
||
// Emit blocks | ||
blockNumbers := []int64{8, 6, 7, 10, 9} | ||
for _, blockNumber := range blockNumbers { | ||
feed.notifyAboutNewBlock(&evmcore.EvmBlock{ | ||
EvmHeader: evmcore.EvmHeader{ | ||
Number: big.NewInt(blockNumber), | ||
}, | ||
}, nil) | ||
} | ||
|
||
// The notification should be delivered in correct order | ||
for expectedBlockNumber <= startBlocknumber+uint64(len(blockNumbers)) { | ||
select { | ||
case notification := <-consumer: | ||
if notification.Block.Number.Cmp(big.NewInt(int64(expectedBlockNumber))) != 0 { | ||
t.Fatalf("expected block number %d, got %d", expectedBlockNumber, notification.Block.Number) | ||
} | ||
expectedBlockNumber++ | ||
|
||
case <-time.After(3 * time.Second): | ||
t.Fatal("expected notification should be already received") | ||
} | ||
} | ||
|
||
feed.Stop() | ||
} | ||
|
||
type expectedBlockNotification struct { | ||
blockNumber uint64 | ||
} | ||
|
||
func TestServiceFeed_ArchiveState(t *testing.T) { | ||
|
||
tests := map[string]struct { | ||
blockHeight uint64 | ||
emptyArchive bool | ||
err error | ||
expectedNotification *expectedBlockNotification | ||
}{ | ||
"empty archive": { | ||
blockHeight: 0, | ||
emptyArchive: true, | ||
err: nil, | ||
expectedNotification: nil, | ||
}, | ||
"non-empty archive": { | ||
blockHeight: 12, | ||
emptyArchive: false, | ||
err: nil, | ||
expectedNotification: &expectedBlockNotification{blockNumber: 12}, | ||
}, | ||
"non-existing archive": { | ||
blockHeight: 12, | ||
emptyArchive: true, | ||
err: evmstore.NoArchiveError, | ||
expectedNotification: &expectedBlockNotification{blockNumber: 12}, | ||
}, | ||
"different archive error": { | ||
blockHeight: 12, | ||
emptyArchive: false, | ||
err: fmt.Errorf("some other error"), | ||
expectedNotification: nil, | ||
}, | ||
} | ||
|
||
for testName, test := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
|
||
ctrl := gomock.NewController(t) | ||
store := NewMockArchiveBlockHeightSource(ctrl) | ||
|
||
store.EXPECT().GetArchiveBlockHeight().Return(test.blockHeight, test.emptyArchive, test.err).AnyTimes() | ||
|
||
feed := ServiceFeed{} | ||
feed.Start(store) | ||
|
||
consumer := make(chan evmcore.ChainHeadNotify, 1) | ||
feed.SubscribeNewBlock(consumer) | ||
|
||
feed.notifyAboutNewBlock(&evmcore.EvmBlock{ | ||
EvmHeader: evmcore.EvmHeader{ | ||
Number: big.NewInt(int64(test.blockHeight)), | ||
}, | ||
}, nil) | ||
|
||
// The notification should be delivered. | ||
select { | ||
case notification := <-consumer: | ||
if test.expectedNotification == nil { | ||
t.Fatal("expected notification to be sent") | ||
} else { | ||
if notification.Block.Number.Cmp(big.NewInt(int64(test.expectedNotification.blockNumber))) != 0 { | ||
t.Fatalf("expected block number %d, got %d", test.expectedNotification.blockNumber, notification.Block.Number) | ||
} | ||
} | ||
// no notification should be received | ||
case <-time.After(100 * time.Millisecond): | ||
if test.expectedNotification != nil { | ||
t.Fatal("expected no notification to be sent") | ||
} | ||
} | ||
|
||
feed.Stop() | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.