Skip to content

Commit

Permalink
Make state sync request size configurable (#614)
Browse files Browse the repository at this point in the history
* Make state sync request size configurable

* finish passing the parameter

* set default to 256
  • Loading branch information
darioush committed Apr 12, 2023
1 parent c33961f commit 157e9b9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
5 changes: 4 additions & 1 deletion plugin/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const (
// time assumptions:
// - normal bootstrap processing time: ~14 blocks / second
// - state sync time: ~6 hrs.
defaultStateSyncMinBlocks = 300_000
defaultStateSyncMinBlocks = 300_000
defaultStateSyncRequestSize = 256 // the number of key/values to ask peers for per request
)

var (
Expand Down Expand Up @@ -186,6 +187,7 @@ type Config struct {
StateSyncIDs string `json:"state-sync-ids"`
StateSyncCommitInterval uint64 `json:"state-sync-commit-interval"`
StateSyncMinBlocks uint64 `json:"state-sync-min-blocks"`
StateSyncRequestSize uint16 `json:"state-sync-request-size"`

// SkipUpgradeCheck disables checking that upgrades must take place before the last
// accepted block. Skipping this check is useful when a node operator does not update
Expand Down Expand Up @@ -263,6 +265,7 @@ func (c *Config) SetDefaults() {
c.StateSyncServerTrieCache = defaultStateSyncServerTrieCache
c.StateSyncCommitInterval = defaultSyncableCommitInterval
c.StateSyncMinBlocks = defaultStateSyncMinBlocks
c.StateSyncRequestSize = defaultStateSyncRequestSize
c.AllowUnprotectedTxHashes = defaultAllowUnprotectedTxHashes
c.AcceptedCacheSize = defaultAcceptedCacheSize
}
Expand Down
4 changes: 3 additions & 1 deletion plugin/evm/syncervm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type stateSyncClientConfig struct {
// Specifies the number of blocks behind the latest state summary that the chain must be
// in order to prefer performing state sync over falling back to the normal bootstrapping
// algorithm.
stateSyncMinBlocks uint64
stateSyncMinBlocks uint64
stateSyncRequestSize uint16 // number of key/value pairs to ask peers for per request

lastAcceptedHeight uint64

Expand Down Expand Up @@ -283,6 +284,7 @@ func (client *stateSyncerClient) syncStateTrie(ctx context.Context) error {
DB: client.chaindb,
MaxOutstandingCodeHashes: statesync.DefaultMaxOutstandingCodeHashes,
NumCodeFetchingWorkers: statesync.DefaultNumCodeFetchingWorkers,
RequestSize: client.stateSyncRequestSize,
})
if err != nil {
return err
Expand Down
19 changes: 10 additions & 9 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,16 @@ func (vm *VM) initializeStateSyncClient(lastAcceptedHeight uint64) error {
BlockParser: vm,
},
),
enabled: vm.config.StateSyncEnabled,
skipResume: vm.config.StateSyncSkipResume,
stateSyncMinBlocks: vm.config.StateSyncMinBlocks,
lastAcceptedHeight: lastAcceptedHeight, // TODO clean up how this is passed around
chaindb: vm.chaindb,
metadataDB: vm.metadataDB,
acceptedBlockDB: vm.acceptedBlockDB,
db: vm.db,
toEngine: vm.toEngine,
enabled: vm.config.StateSyncEnabled,
skipResume: vm.config.StateSyncSkipResume,
stateSyncMinBlocks: vm.config.StateSyncMinBlocks,
stateSyncRequestSize: vm.config.StateSyncRequestSize,
lastAcceptedHeight: lastAcceptedHeight, // TODO clean up how this is passed around
chaindb: vm.chaindb,
metadataDB: vm.metadataDB,
acceptedBlockDB: vm.acceptedBlockDB,
db: vm.db,
toEngine: vm.toEngine,
})

// If StateSync is disabled, clear any ongoing summary so that we will not attempt to resume
Expand Down
2 changes: 1 addition & 1 deletion sync/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func TestGetLeafsRetries(t *testing.T) {
Root: root,
Start: bytes.Repeat([]byte{0x00}, common.HashLength),
End: bytes.Repeat([]byte{0xff}, common.HashLength),
Limit: defaultLeafRequestLimit,
Limit: 1024,
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down
20 changes: 10 additions & 10 deletions sync/client/leaf_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ var (
errFailedToFetchLeafs = errors.New("failed to fetch leafs")
)

const defaultLeafRequestLimit = 1024

// LeafSyncTask represents a complete task to be completed by the leaf syncer.
// Note: each LeafSyncTask is processed on its own goroutine and there will
// not be concurrent calls to the callback methods. Implementations should return
Expand All @@ -38,9 +36,10 @@ type LeafSyncTask interface {
}

type CallbackLeafSyncer struct {
client LeafClient
done chan error
tasks <-chan LeafSyncTask
client LeafClient
done chan error
tasks <-chan LeafSyncTask
requestSize uint16
}

type LeafClient interface {
Expand All @@ -50,11 +49,12 @@ type LeafClient interface {
}

// NewCallbackLeafSyncer creates a new syncer object to perform leaf sync of tries.
func NewCallbackLeafSyncer(client LeafClient, tasks <-chan LeafSyncTask) *CallbackLeafSyncer {
func NewCallbackLeafSyncer(client LeafClient, tasks <-chan LeafSyncTask, requestSize uint16) *CallbackLeafSyncer {
return &CallbackLeafSyncer{
client: client,
done: make(chan error),
tasks: tasks,
client: client,
done: make(chan error),
tasks: tasks,
requestSize: requestSize,
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func (c *CallbackLeafSyncer) syncTask(ctx context.Context, task LeafSyncTask) er
Root: root,
Account: task.Account(),
Start: start,
Limit: defaultLeafRequestLimit,
Limit: c.requestSize,
})
if err != nil {
return fmt.Errorf("%s: %w", errFailedToFetchLeafs, err)
Expand Down
7 changes: 4 additions & 3 deletions sync/statesync/state_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type StateSyncerConfig struct {
Client syncclient.Client
DB ethdb.Database
BatchSize int
MaxOutstandingCodeHashes int // Maximum number of code hashes in the code syncer queue
NumCodeFetchingWorkers int // Number of code syncing threads
MaxOutstandingCodeHashes int // Maximum number of code hashes in the code syncer queue
NumCodeFetchingWorkers int // Number of code syncing threads
RequestSize uint16 // Number of leafs to request from a peer at a time
}

// stateSync keeps the state of the entire state sync operation.
Expand Down Expand Up @@ -82,7 +83,7 @@ func NewStateSyncer(config *StateSyncerConfig) (*stateSync, error) {
mainTrieDone: make(chan struct{}),
done: make(chan error, 1),
}
ss.syncer = syncclient.NewCallbackLeafSyncer(config.Client, ss.segments)
ss.syncer = syncclient.NewCallbackLeafSyncer(config.Client, ss.segments, config.RequestSize)
ss.codeSyncer = newCodeSyncer(CodeSyncerConfig{
DB: config.DB,
Client: config.Client,
Expand Down
1 change: 1 addition & 0 deletions sync/statesync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func testSync(t *testing.T, test syncTest) {
BatchSize: 1000, // Use a lower batch size in order to get test coverage of batches being written early.
NumCodeFetchingWorkers: DefaultNumCodeFetchingWorkers,
MaxOutstandingCodeHashes: DefaultMaxOutstandingCodeHashes,
RequestSize: 1024,
})
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 157e9b9

Please sign in to comment.