Skip to content
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
5 changes: 4 additions & 1 deletion pkg/node/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

package node

var ValidatePublicAddress = validatePublicAddress
var (
ValidatePublicAddress = validatePublicAddress
UseEmbeddedSnapshot = useEmbeddedSnapshot
)
15 changes: 9 additions & 6 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,8 @@ func NewBee(
}
)

// When the postage snapshot applies, hand it to the batch service so it
// rebuilds the store from the snapshot during construction; otherwise the
// store is reset only when --resync is requested. Either way the store is
// reset in a single place (batchservice.New), which avoids a second reset
// wiping the snapshot that was just loaded (see #5495).
var batchSnapshot *batchservice.Snapshot
if !o.SkipPostageSnapshot && !batchStoreExists && (networkID == mainnetNetworkID) && beeNodeMode != api.UltraLightMode {
if useEmbeddedSnapshot(o.SkipPostageSnapshot, batchStoreExists, o.Resync, networkID, beeNodeMode) {
batchSnapshot, err = snapshot.New(ctx, logger, archive.Getter{}, b.syncingStopped, postageStampContractAddress, postageStampContractABI, o.BlockTime, postageSyncingStallingTimeout, postageSyncingBackoffTimeout, postageSyncStart)
if err != nil {
// A corrupt snapshot is not fatal: rebuild from the chain instead.
Expand Down Expand Up @@ -1636,3 +1631,11 @@ func batchStoreExists(s storage.StateStorer) (bool, error) {

return hasOne, err
}

// useEmbeddedSnapshot reports whether to rebuild the batch store from the
// embedded snapshot: mainnet, full or light node, and the store will be built
// from scratch (no store yet, or a resync wipes it), unless explicitly skipped.
func useEmbeddedSnapshot(skip, batchStoreExists, resync bool, networkID uint64, mode api.BeeNodeMode) bool {
storeWillRebuild := !batchStoreExists || resync
return !skip && storeWillRebuild && networkID == mainnetNetworkID && mode != api.UltraLightMode
}
41 changes: 41 additions & 0 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package node_test
import (
"testing"

"github.com/ethersphere/bee/v2/pkg/api"
"github.com/ethersphere/bee/v2/pkg/node"
)

Expand Down Expand Up @@ -107,3 +108,43 @@ func TestValidatePublicAddress(t *testing.T) {
})
}
}

func TestUseEmbeddedSnapshot(t *testing.T) {
t.Parallel()

const (
mainnet = uint64(1)
testnet = uint64(10)
)

testCases := []struct {
name string
skip bool
batchStoreExists bool
resync bool
networkID uint64
mode api.BeeNodeMode
want bool
}{
{name: "first boot on mainnet", networkID: mainnet, mode: api.FullMode, want: true},
{name: "existing store, no resync", batchStoreExists: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "resync on a fresh store", resync: true, networkID: mainnet, mode: api.FullMode, want: true},
// New behavior: resync rebuilds from the snapshot even with a store present.
{name: "resync on an existing store uses the snapshot", batchStoreExists: true, resync: true, networkID: mainnet, mode: api.FullMode, want: true},
{name: "resync with skip syncs from the chain", batchStoreExists: true, resync: true, skip: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "skip on first boot", skip: true, networkID: mainnet, mode: api.FullMode, want: false},
{name: "light node uses the snapshot", networkID: mainnet, mode: api.LightMode, want: true},
{name: "ultra-light never uses the snapshot", resync: true, networkID: mainnet, mode: api.UltraLightMode, want: false},
{name: "non-mainnet never uses the snapshot", resync: true, networkID: testnet, mode: api.FullMode, want: false},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := node.UseEmbeddedSnapshot(tc.skip, tc.batchStoreExists, tc.resync, tc.networkID, tc.mode)
if got != tc.want {
t.Fatalf("UseEmbeddedSnapshot = %v, want %v", got, tc.want)
}
})
}
}
Loading