-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat(sync): Implement a subjectiveHead atomic pointer on the node to use #108
Draft
renaynay
wants to merge
10
commits into
celestiaorg:main
Choose a base branch
from
renaynay:fork-following-prevention
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.
+320
−58
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d56342
feat(sync): Implement a subjectiveHead atomic pointer on the node to use
renaynay 7907db7
doc(sync): add doc to explain sbjHead
renaynay b124d06
test(sync/fork): Implement a roundabout fork following test to ensure…
renaynay 5f2140a
better assert
renaynay 9200ba0
test(sync/fork_test): Fork chain at height 50 instead of height 1
renaynay b267d6e
chore(sync): implement ryan suggestions
renaynay 0c1bf29
chore(sync): store zero H value on sbjHead pointer
renaynay 2742b9e
fix(sync): ensure only fatal on verification error
renaynay 6bc2e87
chore(sync/fork_test): rename to eclipsedPeer for clarity
renaynay 91d975a
rename err in dummy header
renaynay 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 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 |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
|
||
"github.com/celestiaorg/go-header" | ||
"github.com/celestiaorg/go-header/headertest" | ||
"github.com/celestiaorg/go-header/local" | ||
"github.com/celestiaorg/go-header/store" | ||
"github.com/celestiaorg/go-header/sync" | ||
) | ||
|
||
// This program is for test purposes only. See TestForkFollowingPrevention | ||
// for further context. | ||
// | ||
// This program runs an instance of a syncer against a modified p2p Exchange | ||
// that is designed to serve it a fork instead of the canonical chain. | ||
func main() { | ||
t := &testing.T{} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
suite := headertest.NewTestSuite(t) | ||
head := suite.Head() | ||
|
||
// set up syncer with a malicious peer as its remote peer | ||
ee := newEclipsedExchange(ctx, t, head) | ||
|
||
localStore := store.NewTestStore(ctx, t, head) | ||
syncer, err := sync.NewSyncer[*headertest.DummyHeader]( | ||
local.NewExchange[*headertest.DummyHeader](ee), | ||
localStore, | ||
headertest.NewDummySubscriber(), | ||
// TrustingPeriod can be set to a nanosecond so even if the head | ||
// given by the trusted peer expires by the time `subjectiveHead` is | ||
// called again, it will still call Head on the `eclipsedPeer` | ||
// which will return the same head as before. | ||
sync.WithTrustingPeriod(time.Nanosecond), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// generate a good canonical chain | ||
canonical := suite.GenDummyHeaders(99) | ||
|
||
// give good headers to the trusted peer in order to return a good subjective head | ||
// to the syncer upon its start | ||
err = ee.appendToTrusted(ctx, canonical...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// generate a fork starting at block height 50 of the canonical chain | ||
fork := canonical[:50] | ||
maliciousSuite := headertest.NewTestSuiteWithHead(t, fork[len(fork)-1]) | ||
// generate 50 blocks on the fork | ||
fork = append(fork, maliciousSuite.GenDummyHeaders(50)...) | ||
// give bad headers to the malicious (eclipsing) peer in order | ||
// to attempt to get syncer to follow a fork | ||
err = ee.appendToEclipsedExchange(ctx, fork...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = ee.trustedPeer.GetByHeight(ctx, 100) | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = ee.eclipsedPeer.GetByHeight(ctx, 100) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logging.Logger("sync") | ||
|
||
err = syncer.Start(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// this sleep is necessary to allow the syncer to trigger a job | ||
// as calling SyncWait prematurely may falsely return without error | ||
// as the syncer has not yet registered a sync job. | ||
//time.Sleep(time.Millisecond * 100) | ||
time.Sleep(time.Millisecond * 500) | ||
syncer.SyncWait(ctx) //nolint:errcheck | ||
} | ||
|
||
// eclipsedExchange is an exchange that can serve a good Head to the syncer | ||
// but attempts to "eclipse" the syncer by serving it a fork as it requests | ||
// headers between its storeHead --> subjectiveHead. | ||
type eclipsedExchange struct { | ||
// trusted peer that serves a good Head to the syncer | ||
trustedPeer header.Store[*headertest.DummyHeader] | ||
// bad peers who attempt to eclipse the syncer and get it to follow a fork | ||
eclipsedPeer header.Store[*headertest.DummyHeader] | ||
} | ||
|
||
func newEclipsedExchange( | ||
ctx context.Context, | ||
t *testing.T, | ||
head *headertest.DummyHeader, | ||
) *eclipsedExchange { | ||
return &eclipsedExchange{ | ||
trustedPeer: store.NewTestStore(ctx, t, head), | ||
eclipsedPeer: store.NewTestStore(ctx, t, head), | ||
} | ||
} | ||
|
||
// Head returns a good header from the trusted peer. | ||
func (e *eclipsedExchange) Head(ctx context.Context, h ...header.HeadOption[*headertest.DummyHeader]) (*headertest.DummyHeader, error) { | ||
return e.trustedPeer.Head(ctx, h...) | ||
} | ||
|
||
// GetVerifiedRange returns a fork from the eclipsed exchange in an attempt to | ||
// eclipse the syncer. | ||
func (e *eclipsedExchange) GetVerifiedRange(ctx context.Context, from *headertest.DummyHeader, amount uint64) ([]*headertest.DummyHeader, error) { | ||
return e.eclipsedPeer.GetVerifiedRange(ctx, from, amount) | ||
} | ||
|
||
func (e *eclipsedExchange) appendToTrusted(ctx context.Context, h ...*headertest.DummyHeader) error { | ||
return e.trustedPeer.Append(ctx, h...) | ||
} | ||
|
||
func (e *eclipsedExchange) appendToEclipsedExchange(ctx context.Context, h ...*headertest.DummyHeader) error { | ||
return e.eclipsedPeer.Append(ctx, h...) | ||
} | ||
|
||
func (e *eclipsedExchange) Get(ctx context.Context, hash header.Hash) (*headertest.DummyHeader, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) GetByHeight(ctx context.Context, u uint64) (*headertest.DummyHeader, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) GetRangeByHeight(ctx context.Context, from, amount uint64) ([]*headertest.DummyHeader, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) Init(ctx context.Context, h *headertest.DummyHeader) error { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) Height() uint64 { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) Has(ctx context.Context, hash header.Hash) (bool, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) HasAt(ctx context.Context, u uint64) bool { | ||
panic("implement me") | ||
} | ||
|
||
func (e *eclipsedExchange) Append(ctx context.Context, h ...*headertest.DummyHeader) error { | ||
panic("implement me") | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be removed?