-
Notifications
You must be signed in to change notification settings - Fork 13
refactor(BUX-313): refactoring of checkpoints checking #241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de5b23a
refactor(BUX-313): refactoring of checkpoints checking
dorzepowski 0a0e808
refactor(BUX-313): change checkpoint name to currentCheckpoint, simplβ¦
dorzepowski 2f7ab0b
refactor(BUX-313): rename Check method
dorzepowski 54edcf8
refactor(BUX-313): fix nil pointer issue
dorzepowski ffd6507
chore(BUX-313): go mod tidy
kuba-4chain 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package peer | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/bitcoin-sv/block-headers-service/domains" | ||
| "github.com/bitcoin-sv/block-headers-service/internal/chaincfg" | ||
| "github.com/bitcoin-sv/block-headers-service/internal/chaincfg/chainhash" | ||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| const notFound = -1 | ||
|
|
||
| type checkpoint struct { | ||
| currentCheckpoint *chaincfg.Checkpoint | ||
| currentIndex int | ||
| finalCheckpoint *chaincfg.Checkpoint | ||
| checkpoints []chaincfg.Checkpoint | ||
| log *zerolog.Logger | ||
| lock sync.RWMutex | ||
| } | ||
|
|
||
| func newCheckpoint(checkpoints []chaincfg.Checkpoint, tipHeight int32, log *zerolog.Logger) *checkpoint { | ||
| logger := log.With().Str("subservice", "checkpoint").Logger() | ||
| ch := &checkpoint{ | ||
| checkpoints: checkpoints, | ||
| log: &logger, | ||
| } | ||
|
|
||
| if len(checkpoints) != 0 { | ||
| ch.finalCheckpoint = &checkpoints[len(checkpoints)-1] | ||
| ch.next(tipHeight) | ||
| } | ||
|
|
||
| return ch | ||
| } | ||
|
|
||
| // Height returns the height of the current checkpoint in a thread safety way. | ||
| func (ch *checkpoint) Height() int32 { | ||
| ch.lock.RLock() | ||
| defer ch.lock.RUnlock() | ||
| if ch.currentCheckpoint == nil { | ||
| return notFound | ||
| } | ||
| return ch.currentCheckpoint.Height | ||
| } | ||
|
|
||
| // Hash returns the hash of the current checkpoint in a thread safety way. | ||
| func (ch *checkpoint) Hash() *chainhash.Hash { | ||
| ch.lock.RLock() | ||
| defer ch.lock.RUnlock() | ||
| if ch.currentCheckpoint == nil { | ||
| return nil | ||
| } | ||
| return ch.currentCheckpoint.Hash | ||
| } | ||
|
|
||
| // LastReached returns true if the last checkpoint has been reached. | ||
| func (ch *checkpoint) LastReached() bool { | ||
| ch.lock.RLock() | ||
| defer ch.lock.RUnlock() | ||
|
|
||
| return ch.currentCheckpoint == nil | ||
| } | ||
|
|
||
| // VerifyAndAdvance checks if the header is valid according to the checkpoint and marks switches to next checkpoint if reached. | ||
| func (ch *checkpoint) VerifyAndAdvance(header *domains.BlockHeader) error { | ||
| ch.lock.Lock() | ||
| defer ch.lock.Unlock() | ||
|
|
||
| if ch.currentCheckpoint == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if header.Height < ch.currentCheckpoint.Height { | ||
| return nil | ||
| } | ||
|
|
||
| if header.Height == ch.currentCheckpoint.Height { | ||
| if header.Hash != *ch.currentCheckpoint.Hash { | ||
| return fmt.Errorf("corresponding checkpoint height does not match, got: %v, exp: %v", header.Height, ch.currentCheckpoint.Height) | ||
| } | ||
|
|
||
| ch.next(header.Height) | ||
| return nil | ||
| } | ||
|
|
||
| return fmt.Errorf("unexpected header above next checkpoint height, got: %v, for checkpoint at height %d", header, ch.currentCheckpoint.Height) | ||
| } | ||
|
|
||
| func (ch *checkpoint) next(height int32) { | ||
| nextCheckpoint, index := ch.findNextCheckpoint(height) | ||
| if nextCheckpoint == nil { | ||
| ch.log.Info().Msgf("Last checkpoint reached at height %d", height) | ||
| } else { | ||
| ch.log.Info(). | ||
| Int32("height", nextCheckpoint.Height). | ||
| Msgf("Setting next checkpoint at height %d", nextCheckpoint.Height) | ||
| } | ||
| ch.currentCheckpoint = nextCheckpoint | ||
| ch.currentIndex = index | ||
| } | ||
|
|
||
| func (ch *checkpoint) findNextCheckpoint(height int32) (nextCheckpoint *chaincfg.Checkpoint, index int) { | ||
| if len(ch.checkpoints) == 0 { | ||
| return nil, notFound | ||
| } | ||
|
|
||
| if height >= ch.finalCheckpoint.Height { | ||
| return nil, notFound | ||
| } | ||
|
|
||
| if ch.currentCheckpoint != nil { | ||
| index = ch.currentIndex + 1 | ||
| return &ch.checkpoints[index], index | ||
chris-4chain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| for i := 0; i < len(ch.checkpoints); i++ { | ||
| if height < ch.checkpoints[i].Height { | ||
| return &ch.checkpoints[i], i | ||
| } | ||
| } | ||
|
|
||
| return nil, notFound | ||
| } | ||
dorzepowski marked this conversation as resolved.
Show resolved
Hide resolved
|
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,215 @@ | ||
| package peer | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/bitcoin-sv/block-headers-service/domains" | ||
| "github.com/bitcoin-sv/block-headers-service/internal/chaincfg" | ||
| "github.com/bitcoin-sv/block-headers-service/internal/tests/fixtures" | ||
| "github.com/rs/zerolog" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCheckpointCreationLastReached(t *testing.T) { | ||
| // setup | ||
| checkpoints := chaincfg.MainNetParams.Checkpoints | ||
| testLogger := zerolog.Nop() | ||
|
|
||
| cases := map[string]struct { | ||
| checkpoints []chaincfg.Checkpoint | ||
| tipHeight int32 | ||
| expectedLastReached bool | ||
| }{ | ||
| "last checkpoint not reached at height 0": { | ||
| checkpoints: checkpoints, | ||
| tipHeight: 0, | ||
| expectedLastReached: false, | ||
| }, | ||
| "last checkpoint not reached at height 810 000": { | ||
| checkpoints: checkpoints, | ||
| tipHeight: 810000, | ||
| expectedLastReached: false, | ||
| }, | ||
| "last checkpoint reached at height 999 999 999": { | ||
| checkpoints: checkpoints, | ||
| tipHeight: 999_999_999, | ||
| expectedLastReached: true, | ||
| }, | ||
| "last checkpoint reached if checkpoints list is empty": { | ||
| checkpoints: make([]chaincfg.Checkpoint, 0), | ||
| tipHeight: 0, | ||
| expectedLastReached: true, | ||
| }, | ||
| } | ||
|
|
||
| for name, params := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| // given: | ||
| chckPoint := newCheckpoint(params.checkpoints, params.tipHeight, &testLogger) | ||
|
|
||
| // expect: | ||
| assert.Equal(t, params.expectedLastReached, chckPoint.LastReached()) | ||
| }) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func TestCheckpointCurrentCheckpoint(t *testing.T) { | ||
| // setup | ||
| testLogger := zerolog.Nop() | ||
| checkpoints := chaincfg.MainNetParams.Checkpoints | ||
| lastCheckpoint := checkpoints[len(checkpoints)-1] | ||
|
|
||
| cases := map[string]struct { | ||
| header *domains.BlockHeader | ||
| expectedLastReached bool | ||
| expectedCheckpointHeight int32 | ||
| }{ | ||
| "check header 11110 should keep first checkpoint": { | ||
| header: &domains.BlockHeader{ | ||
| Height: 11110, | ||
| }, | ||
| expectedCheckpointHeight: checkpoints[0].Height, | ||
| expectedLastReached: false, | ||
| }, | ||
| "check header 11111 should set next checkpoint": { | ||
| header: &domains.BlockHeader{ | ||
| Height: 11111, | ||
| Hash: *fixtures.HashOf("0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"), | ||
| }, | ||
| expectedCheckpointHeight: checkpoints[1].Height, | ||
| expectedLastReached: false, | ||
| }, | ||
| "check header just before last checkpoint should keep last checkpoint as next checkpoint": { | ||
| header: &domains.BlockHeader{ | ||
| Height: lastCheckpoint.Height - 1, | ||
| }, | ||
| expectedCheckpointHeight: lastCheckpoint.Height, | ||
| expectedLastReached: false, | ||
| }, | ||
| "check header at last checkpoint should set last checkpoint as next checkpoint": { | ||
| header: &domains.BlockHeader{ | ||
| Height: lastCheckpoint.Height, | ||
| Hash: *lastCheckpoint.Hash, | ||
| }, | ||
| expectedCheckpointHeight: 0, | ||
| expectedLastReached: true, | ||
| }, | ||
| } | ||
|
|
||
| for name, params := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| // given: | ||
| chckPoint := newCheckpoint(checkpoints, params.header.Height-1, &testLogger) | ||
|
|
||
| // when: | ||
| err := chckPoint.VerifyAndAdvance(params.header) | ||
|
|
||
| // then: | ||
| require.NoError(t, err) | ||
| assert.Equal(t, params.expectedLastReached, chckPoint.LastReached()) | ||
| if !params.expectedLastReached { | ||
| assert.Equal(t, params.expectedCheckpointHeight, chckPoint.Height()) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func TestCheckpointVerificationSuccess(t *testing.T) { | ||
| testLogger := zerolog.Nop() | ||
| checkpoints := chaincfg.MainNetParams.Checkpoints | ||
| lastCheckpoint := checkpoints[len(checkpoints)-1] | ||
| cases := map[string]struct { | ||
| checkpoints []chaincfg.Checkpoint | ||
| header *domains.BlockHeader | ||
| }{ | ||
| "valid header reached nearest checkpoint": { | ||
| checkpoints: checkpoints, | ||
| header: &domains.BlockHeader{ | ||
| Height: 802000, | ||
| Hash: *fixtures.HashOf("000000000000000008f42d72af179115c35561d921f43829341967dcb8adbafd"), | ||
| }, | ||
| }, | ||
| "header below nearest checkpoint": { | ||
| checkpoints: checkpoints, | ||
| header: &domains.BlockHeader{ | ||
| Height: 801999, | ||
| }, | ||
| }, | ||
| "header reached last checkpoint": { | ||
| checkpoints: checkpoints, | ||
| header: &domains.BlockHeader{ | ||
| Height: lastCheckpoint.Height, | ||
| Hash: *lastCheckpoint.Hash, | ||
| }, | ||
| }, | ||
| "header above last checkpoint": { | ||
| checkpoints: checkpoints, | ||
| header: &domains.BlockHeader{ | ||
| Height: lastCheckpoint.Height + 1, | ||
| }, | ||
| }, | ||
| "checkpoint list is empty": { | ||
| checkpoints: make([]chaincfg.Checkpoint, 0), | ||
| header: &domains.BlockHeader{ | ||
| Height: 802000, | ||
| Hash: chaincfg.GenesisHash, | ||
| }, | ||
| }, | ||
| } | ||
| for name, params := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| // given: | ||
| chckPoint := newCheckpoint(params.checkpoints, params.header.Height-1, &testLogger) | ||
|
|
||
| // expect: | ||
| require.NoError(t, chckPoint.VerifyAndAdvance(params.header)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckpointVerificationFailure(t *testing.T) { | ||
| testLogger := zerolog.Nop() | ||
| checkpoints := chaincfg.MainNetParams.Checkpoints | ||
| lastCheckpoint := checkpoints[len(checkpoints)-1] | ||
| cases := map[string]struct { | ||
| checkpoints []chaincfg.Checkpoint | ||
| previousHeight int32 | ||
| header *domains.BlockHeader | ||
| err string | ||
| }{ | ||
| "invalid header at checkpoint": { | ||
| checkpoints: checkpoints, | ||
| previousHeight: 801999, | ||
| header: &domains.BlockHeader{ | ||
| Height: 802000, | ||
| Hash: chaincfg.GenesisHash, | ||
| }, | ||
| err: "corresponding checkpoint height does not match", | ||
| }, | ||
| "unexpected header above the next checkpoint": { | ||
| checkpoints: checkpoints, | ||
| previousHeight: 0, | ||
| header: &domains.BlockHeader{ | ||
| Height: lastCheckpoint.Height, | ||
| Hash: *lastCheckpoint.Hash, | ||
| }, | ||
| err: "unexpected header above next checkpoint height", | ||
| }, | ||
| } | ||
| for name, params := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| // given: | ||
| chckPoint := newCheckpoint(params.checkpoints, params.previousHeight, &testLogger) | ||
|
|
||
| // when: | ||
| err := chckPoint.VerifyAndAdvance(params.header) | ||
|
|
||
| // then: | ||
| require.Error(t, err) | ||
| require.ErrorContains(t, err, params.err) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
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.