8
8
"context"
9
9
"errors"
10
10
"fmt"
11
- "math"
12
- "sync"
13
11
"time"
14
12
15
13
"github.com/google/btree"
@@ -25,17 +23,14 @@ import (
25
23
"github.com/ava-labs/avalanchego/database/versiondb"
26
24
"github.com/ava-labs/avalanchego/ids"
27
25
"github.com/ava-labs/avalanchego/snow"
28
- "github.com/ava-labs/avalanchego/snow/choices"
29
26
"github.com/ava-labs/avalanchego/snow/uptime"
30
27
"github.com/ava-labs/avalanchego/snow/validators"
31
28
"github.com/ava-labs/avalanchego/upgrade"
32
29
"github.com/ava-labs/avalanchego/utils/constants"
33
30
"github.com/ava-labs/avalanchego/utils/crypto/bls"
34
31
"github.com/ava-labs/avalanchego/utils/hashing"
35
32
"github.com/ava-labs/avalanchego/utils/iterator"
36
- "github.com/ava-labs/avalanchego/utils/logging"
37
33
"github.com/ava-labs/avalanchego/utils/maybe"
38
- "github.com/ava-labs/avalanchego/utils/timer"
39
34
"github.com/ava-labs/avalanchego/utils/wrappers"
40
35
"github.com/ava-labs/avalanchego/vms/components/avax"
41
36
"github.com/ava-labs/avalanchego/vms/components/gas"
102
97
LastAcceptedKey = []byte ("last accepted" )
103
98
HeightsIndexedKey = []byte ("heights indexed" )
104
99
InitializedKey = []byte ("initialized" )
105
- BlocksReindexedKey = []byte ("blocks reindexed" )
106
100
107
101
emptyL1ValidatorCache = & cache.Empty [ids.ID , maybe.Maybe [L1Validator ]]{}
108
102
)
@@ -221,14 +215,6 @@ type State interface {
221
215
// Discard uncommitted changes to the database.
222
216
Abort ()
223
217
224
- // ReindexBlocks converts any block indices using the legacy storage format
225
- // to the new format. If this database has already updated the indices,
226
- // this function will return immediately, without iterating over the
227
- // database.
228
- //
229
- // TODO: Remove after v1.12.x is activated
230
- ReindexBlocks (lock sync.Locker , log logging.Logger ) error
231
-
232
218
// Commit changes to the base database.
233
219
Commit () error
234
220
@@ -241,16 +227,6 @@ type State interface {
241
227
Close () error
242
228
}
243
229
244
- // Prior to https://github.com/ava-labs/avalanchego/pull/1719, blocks were
245
- // stored as a map from blkID to stateBlk. Nodes synced prior to this PR may
246
- // still have blocks partially stored using this legacy format.
247
- //
248
- // TODO: Remove after v1.12.x is activated
249
- type stateBlk struct {
250
- Bytes []byte `serialize:"true"`
251
- Status choices.Status `serialize:"true"`
252
- }
253
-
254
230
/*
255
231
* VMDB
256
232
* |-. validators
@@ -320,7 +296,6 @@ type stateBlk struct {
320
296
* | '-- timestamp + validationID -> nil
321
297
* '-. singletons
322
298
* |-- initializedKey -> nil
323
- * |-- blocksReindexedKey -> nil
324
299
* |-- timestampKey -> timestamp
325
300
* |-- feeStateKey -> feeState
326
301
* |-- l1ValidatorExcessKey -> l1ValidatorExcess
@@ -2283,7 +2258,7 @@ func (s *state) GetStatelessBlock(blockID ids.ID) (block.Block, error) {
2283
2258
return nil , err
2284
2259
}
2285
2260
2286
- blk , _ , err := parseStoredBlock ( blkBytes )
2261
+ blk , err := block . Parse ( block . GenesisCodec , blkBytes )
2287
2262
if err != nil {
2288
2263
return nil , err
2289
2264
}
@@ -3064,162 +3039,6 @@ func (s *state) writeMetadata() error {
3064
3039
return nil
3065
3040
}
3066
3041
3067
- // Returns the block and whether it is a [stateBlk].
3068
- // Invariant: blkBytes is safe to parse with blocks.GenesisCodec
3069
- //
3070
- // TODO: Remove after v1.12.x is activated
3071
- func parseStoredBlock (blkBytes []byte ) (block.Block , bool , error ) {
3072
- // Attempt to parse as blocks.Block
3073
- blk , err := block .Parse (block .GenesisCodec , blkBytes )
3074
- if err == nil {
3075
- return blk , false , nil
3076
- }
3077
-
3078
- // Fallback to [stateBlk]
3079
- blkState := stateBlk {}
3080
- if _ , err := block .GenesisCodec .Unmarshal (blkBytes , & blkState ); err != nil {
3081
- return nil , false , err
3082
- }
3083
-
3084
- blk , err = block .Parse (block .GenesisCodec , blkState .Bytes )
3085
- return blk , true , err
3086
- }
3087
-
3088
- func (s * state ) ReindexBlocks (lock sync.Locker , log logging.Logger ) error {
3089
- has , err := s .singletonDB .Has (BlocksReindexedKey )
3090
- if err != nil {
3091
- return err
3092
- }
3093
- if has {
3094
- log .Info ("blocks already reindexed" )
3095
- return nil
3096
- }
3097
-
3098
- // It is possible that new blocks are added after grabbing this iterator.
3099
- // New blocks are guaranteed to be persisted in the new format, so we don't
3100
- // need to check them.
3101
- blockIterator := s .blockDB .NewIterator ()
3102
- // Releasing is done using a closure to ensure that updating blockIterator
3103
- // will result in having the most recent iterator released when executing
3104
- // the deferred function.
3105
- defer func () {
3106
- blockIterator .Release ()
3107
- }()
3108
-
3109
- log .Info ("starting block reindexing" )
3110
-
3111
- var (
3112
- startTime = time .Now ()
3113
- lastCommit = startTime
3114
- nextUpdate = startTime .Add (indexLogFrequency )
3115
- numIndicesChecked = 0
3116
- numIndicesUpdated = 0
3117
- )
3118
-
3119
- for blockIterator .Next () {
3120
- valueBytes := blockIterator .Value ()
3121
- blk , isStateBlk , err := parseStoredBlock (valueBytes )
3122
- if err != nil {
3123
- return fmt .Errorf ("failed to parse block: %w" , err )
3124
- }
3125
-
3126
- blkID := blk .ID ()
3127
-
3128
- // This block was previously stored using the legacy format, update the
3129
- // index to remove the usage of stateBlk.
3130
- if isStateBlk {
3131
- blkBytes := blk .Bytes ()
3132
- if err := s .blockDB .Put (blkID [:], blkBytes ); err != nil {
3133
- return fmt .Errorf ("failed to write block: %w" , err )
3134
- }
3135
-
3136
- numIndicesUpdated ++
3137
- }
3138
-
3139
- numIndicesChecked ++
3140
-
3141
- now := time .Now ()
3142
- if now .After (nextUpdate ) {
3143
- nextUpdate = now .Add (indexLogFrequency )
3144
-
3145
- progress := timer .ProgressFromHash (blkID [:])
3146
- eta := timer .EstimateETA (
3147
- startTime ,
3148
- progress ,
3149
- math .MaxUint64 ,
3150
- )
3151
-
3152
- log .Info ("reindexing blocks" ,
3153
- zap .Int ("numIndicesUpdated" , numIndicesUpdated ),
3154
- zap .Int ("numIndicesChecked" , numIndicesChecked ),
3155
- zap .Duration ("eta" , eta ),
3156
- )
3157
- }
3158
-
3159
- if numIndicesChecked % indexIterationLimit == 0 {
3160
- // We must hold the lock during committing to make sure we don't
3161
- // attempt to commit to disk while a block is concurrently being
3162
- // accepted.
3163
- lock .Lock ()
3164
- err := errors .Join (
3165
- s .Commit (),
3166
- blockIterator .Error (),
3167
- )
3168
- lock .Unlock ()
3169
- if err != nil {
3170
- return err
3171
- }
3172
-
3173
- // We release the iterator here to allow the underlying database to
3174
- // clean up deleted state.
3175
- blockIterator .Release ()
3176
-
3177
- // We take the minimum here because it's possible that the node is
3178
- // currently bootstrapping. This would mean that grabbing the lock
3179
- // could take an extremely long period of time; which we should not
3180
- // delay processing for.
3181
- indexDuration := now .Sub (lastCommit )
3182
- sleepDuration := min (
3183
- indexIterationSleepMultiplier * indexDuration ,
3184
- indexIterationSleepCap ,
3185
- )
3186
- time .Sleep (sleepDuration )
3187
-
3188
- // Make sure not to include the sleep duration into the next index
3189
- // duration.
3190
- lastCommit = time .Now ()
3191
-
3192
- blockIterator = s .blockDB .NewIteratorWithStart (blkID [:])
3193
- }
3194
- }
3195
-
3196
- // Ensure we fully iterated over all blocks before writing that indexing has
3197
- // finished.
3198
- //
3199
- // Note: This is needed because a transient read error could cause the
3200
- // iterator to stop early.
3201
- if err := blockIterator .Error (); err != nil {
3202
- return fmt .Errorf ("failed to iterate over historical blocks: %w" , err )
3203
- }
3204
-
3205
- if err := s .singletonDB .Put (BlocksReindexedKey , nil ); err != nil {
3206
- return fmt .Errorf ("failed to put marked blocks as reindexed: %w" , err )
3207
- }
3208
-
3209
- // We must hold the lock during committing to make sure we don't attempt to
3210
- // commit to disk while a block is concurrently being accepted.
3211
- lock .Lock ()
3212
- defer lock .Unlock ()
3213
-
3214
- log .Info ("finished block reindexing" ,
3215
- zap .Int ("numIndicesUpdated" , numIndicesUpdated ),
3216
- zap .Int ("numIndicesChecked" , numIndicesChecked ),
3217
- zap .Duration ("duration" , time .Since (startTime )),
3218
- )
3219
-
3220
- return s .Commit ()
3221
- }
3222
-
3223
3042
func (s * state ) GetUptime (vdrID ids.NodeID ) (time.Duration , time.Time , error ) {
3224
3043
return s .validatorState .GetUptime (vdrID , constants .PrimaryNetworkID )
3225
3044
}
0 commit comments