Skip to content
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

spv: Move validate header chain difficulties to earlier point in startup sync #2297

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/loggers/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
WalletLog = backendLog.Logger("WLLT")
TkbyLog = backendLog.Logger("TKBY")
SyncLog = backendLog.Logger("SYNC")
PeerLog = backendLog.Logger("PEER")
GrpcLog = backendLog.Logger("GRPC")
JsonrpcLog = backendLog.Logger("RPCS")
CmgrLog = backendLog.Logger("CMGR")
Expand Down
3 changes: 2 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
ticketbuyer.UseLogger(loggers.TkbyLog)
chain.UseLogger(loggers.SyncLog)
spv.UseLogger(loggers.SyncLog)
p2p.UseLogger(loggers.SyncLog)
p2p.UseLogger(loggers.PeerLog)
rpcserver.UseLogger(loggers.GrpcLog)
jsonrpc.UseLogger(loggers.JsonrpcLog)
connmgr.UseLogger(loggers.CmgrLog)
Expand All @@ -45,6 +45,7 @@ var subsystemLoggers = map[string]slog.Logger{
"WLLT": loggers.WalletLog,
"TKBY": loggers.TkbyLog,
"SYNC": loggers.SyncLog,
"PEER": loggers.PeerLog,
"GRPC": loggers.GrpcLog,
"RPCS": loggers.JsonrpcLog,
"CMGR": loggers.CmgrLog,
Expand Down
22 changes: 16 additions & 6 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,22 @@ func (s *Syncer) getHeaders(ctx context.Context, rp *p2p.RemotePeer) error {
}
}

// Verify the sidechain that includes the received headers has
// the correct difficulty.
s.sidechainMu.Lock()
fullsc, err := s.sidechains.FullSideChain(nodes)
if err != nil {
s.sidechainMu.Unlock()
return err
}
_, err = s.wallet.ValidateHeaderChainDifficulties(ctx, fullsc, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine for now, if only because ValidateHeaderChainDifficulties, despite using a slice of BlockNodes, only actually checks the headers and doesn't need the cfilters to be populated. Since we have the chance to change the API, we may want to change that to take a slice of headers instead (can be in a future change).

if err != nil {
s.sidechainMu.Unlock()
rp.Disconnect(err)
return err
}
s.sidechainMu.Unlock()

g, ctx := errgroup.WithContext(ctx)
for i := range headers {
i := i
Expand Down Expand Up @@ -1408,12 +1424,6 @@ func (s *Syncer) getHeaders(ctx context.Context, rp *p2p.RemotePeer) error {
continue
}

_, err = s.wallet.ValidateHeaderChainDifficulties(ctx, bestChain, 0)
if err != nil {
s.sidechainMu.Unlock()
return err
}

prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
if err != nil {
s.sidechainMu.Unlock()
Expand Down
Loading