This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstate_size.go
More file actions
400 lines (339 loc) · 13.1 KB
/
state_size.go
File metadata and controls
400 lines (339 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package operations
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/cosmos/iavl"
"github.com/sei-protocol/sei-db/common/logger"
"github.com/sei-protocol/sei-db/sc/memiavl"
"github.com/sei-protocol/sei-db/tools/utils"
"github.com/spf13/cobra"
)
func StateSizeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "state-size",
Short: "Print analytical results for state size",
Run: executeStateSize,
}
cmd.PersistentFlags().StringP("db-dir", "d", "", "Database Directory")
cmd.PersistentFlags().Int64("height", 0, "Block Height")
cmd.PersistentFlags().StringP("module", "m", "", "Module to export. Default to export all")
// DynamoDB export flags
cmd.PersistentFlags().Bool("export-dynamodb", false, "Export results to DynamoDB instead of printing")
cmd.PersistentFlags().String("dynamodb-table", "state_size_analysis", "DynamoDB table name")
cmd.PersistentFlags().String("aws-region", "us-east-2", "AWS region for DynamoDB")
return cmd
}
const (
deletionLogInterval = 5000
deletionChunkSize = 10000
)
func executeStateSize(cmd *cobra.Command, _ []string) {
module, _ := cmd.Flags().GetString("module")
dbDir, _ := cmd.Flags().GetString("db-dir")
height, _ := cmd.Flags().GetInt64("height")
exportDynamoDB, _ := cmd.Flags().GetBool("export-dynamodb")
dynamoDBTable, _ := cmd.Flags().GetString("dynamodb-table")
awsRegion, _ := cmd.Flags().GetString("aws-region")
if dbDir == "" {
panic("Must provide database dir")
}
opts := memiavl.Options{
Dir: dbDir,
ZeroCopy: true,
CreateIfMissing: false,
}
db, err := memiavl.OpenDB(logger.NewNopLogger(), height, opts)
if err != nil {
panic(err)
}
defer db.Close()
// Get the actual height of the opened database
actualHeight := db.Version()
fmt.Printf("Finished opening db at height %d (requested: %d), calculating state size for module: %s\n", actualHeight, height, module)
// First, collect all the data by scanning the trees
moduleResults, err := collectAllModuleData(module, db)
if err != nil {
panic(err)
}
// Then process the results based on the flag
if exportDynamoDB {
fmt.Printf("Exporting results to DynamoDB table: %s\n", dynamoDBTable)
err = exportResultsToDynamoDB(moduleResults, actualHeight, dynamoDBTable, awsRegion)
if err != nil {
panic(err)
}
fmt.Println("Successfully exported to DynamoDB!")
} else {
printResultsToConsole(moduleResults)
}
}
// collectModuleStats collects all the statistics for a module and records zeroed entries for deletion.
func collectModuleStats(tree *memiavl.Tree, moduleName string) (*ModuleResult, []*iavl.KVPair, error) {
result := &ModuleResult{
ModuleName: moduleName,
PrefixSizes: make(map[string]*utils.PrefixSize),
ContractSizes: make(map[string]*utils.ContractSizeEntry),
}
deletedCount := 0
var deletionPairs []*iavl.KVPair
// Scan the tree to collect statistics
tree.ScanPostOrder(func(node memiavl.Node) bool {
if node.IsLeaf() {
result.TotalNumKeys++
keySize := len(node.Key())
valueSize := len(node.Value())
result.TotalKeySize += uint64(keySize)
result.TotalValueSize += uint64(valueSize)
result.TotalSize += uint64(keySize + valueSize)
prefixKey := fmt.Sprintf("%X", node.Key())
prefix := prefixKey[:2]
if _, exists := result.PrefixSizes[moduleName]; !exists {
result.PrefixSizes[moduleName] = &utils.PrefixSize{}
}
result.PrefixSizes[moduleName].KeySize += uint64(keySize)
result.PrefixSizes[moduleName].ValueSize += uint64(valueSize)
result.PrefixSizes[moduleName].TotalSize += uint64(keySize + valueSize)
result.PrefixSizes[moduleName].KeyCount++
// Handle EVM contract analysis
if moduleName == "evm" && prefix == "03" {
result.TotalEVM03Entries++
if isAllZero(node.Value()) {
result.ZeroedEVM03Entries++
result.ZeroedEVM03KeyBytes += uint64(keySize)
result.ZeroedEVM03ValueBytes += uint64(valueSize)
deletedCount++
currentCount := deletedCount
keyCopy := append([]byte(nil), node.Key()...)
if currentCount%deletionLogInterval == 0 {
fmt.Printf("Found zeroed EVM 0x03 entry #%d; preparing deletion for key %X\n", currentCount, keyCopy)
fmt.Printf("Deleting zeroed EVM 0x03 entry #%d with key %X\n", currentCount, keyCopy)
}
deletionPairs = append(deletionPairs, &iavl.KVPair{Key: keyCopy, Delete: true})
}
addr := prefixKey[2:42]
if _, exists := result.ContractSizes[addr]; !exists {
result.ContractSizes[addr] = &utils.ContractSizeEntry{Address: addr}
}
entry := result.ContractSizes[addr]
entry.TotalSize += uint64(len(node.Key()) + len(node.Value()))
entry.KeyCount++
}
if result.TotalNumKeys%1000000 == 0 {
fmt.Printf("Scanned %d keys for module %s\n", result.TotalNumKeys, moduleName)
}
}
return true
})
// Limit to top 100 contracts by total size
result.ContractSizes = limitToTopContracts(result.ContractSizes, 100)
return result, deletionPairs, nil
}
// limitToTopContracts keeps only the top N contracts by total size
func limitToTopContracts(contracts map[string]*utils.ContractSizeEntry, limit int) map[string]*utils.ContractSizeEntry {
if len(contracts) <= limit {
return contracts
}
// Convert to slice for sorting
var contractSlice []utils.ContractSizeEntry
for _, contract := range contracts {
contractSlice = append(contractSlice, *contract)
}
// Sort by total size in descending order
sort.Slice(contractSlice, func(i, j int) bool {
return contractSlice[i].TotalSize > contractSlice[j].TotalSize
})
// Keep only top N
result := make(map[string]*utils.ContractSizeEntry)
for i := 0; i < limit; i++ {
contract := contractSlice[i]
result[contract.Address] = &contract
}
return result
}
// ModuleResult holds the complete analysis results for a single module
type ModuleResult struct {
ModuleName string
TotalNumKeys uint64
TotalKeySize uint64
TotalValueSize uint64
TotalSize uint64
PrefixSizes map[string]*utils.PrefixSize
ContractSizes map[string]*utils.ContractSizeEntry
// EVM-specific statistics for 0x03 storage prefix
TotalEVM03Entries uint64
ZeroedEVM03Entries uint64
ZeroedEVM03KeyBytes uint64
ZeroedEVM03ValueBytes uint64
}
// collectAllModuleData scans all modules, collects statistics, and persists deletions.
func collectAllModuleData(module string, db *memiavl.DB) (map[string]*ModuleResult, error) {
modules := []string{}
if module == "" {
modules = AllModules
} else {
modules = append(modules, module)
}
moduleResults := make(map[string]*ModuleResult)
deletionsByModule := make(map[string][]*iavl.KVPair)
for _, moduleName := range modules {
tree := db.TreeByName(moduleName)
if tree == nil {
fmt.Printf("Tree does not exist for module %s, skipping...\n", moduleName)
continue
}
fmt.Printf("Analyzing module: %s\n", moduleName)
// Collect statistics directly into ModuleResult
result, deletions, err := collectModuleStats(tree, moduleName)
if err != nil {
return nil, fmt.Errorf("collect module stats for %s: %w", moduleName, err)
}
// Store in memory (result is already a ModuleResult)
moduleResults[moduleName] = result
fmt.Printf("Collected stats for module %s: %d keys, %d total size\n",
moduleName, result.TotalNumKeys, result.TotalSize)
if len(deletions) > 0 {
deletionsByModule[moduleName] = deletions
}
}
if len(deletionsByModule) > 0 {
moduleNames := make([]string, 0, len(deletionsByModule))
for name := range deletionsByModule {
moduleNames = append(moduleNames, name)
}
sort.Strings(moduleNames)
processed := 0
for _, moduleName := range moduleNames {
pairs := deletionsByModule[moduleName]
sort.Slice(pairs, func(i, j int) bool { // attempt to avoid "out of order" error by sorting the keys
return bytes.Compare(pairs[i].Key, pairs[j].Key) < 0
})
total := len(pairs)
for chunkStart := 0; chunkStart < total; chunkStart += deletionChunkSize {
chunkEnd := chunkStart + deletionChunkSize
if chunkEnd > total {
chunkEnd = total
}
chunk := pairs[chunkStart:chunkEnd]
if err := db.ApplyChangeSet(moduleName, iavl.ChangeSet{Pairs: chunk}); err != nil {
return nil, fmt.Errorf("apply change set for %s: %w", moduleName, err)
}
if _, err := db.Commit(); err != nil {
panic(err)
}
for _, pair := range chunk {
processed++
if processed%deletionLogInterval == 0 {
fmt.Printf("Deleted zeroed EVM 0x03 entry #%d with key %X\n", processed, pair.Key)
}
}
fmt.Printf("Committed deletion chunk of %d zeroed EVM 0x03 entries for module %s (%d/%d processed)\n",
len(chunk), moduleName, chunkEnd, total)
}
}
}
return moduleResults, nil
}
// exportResultsToDynamoDB exports the collected results to DynamoDB
func exportResultsToDynamoDB(moduleResults map[string]*ModuleResult, height int64, tableName, awsRegion string) error {
// Initialize DynamoDB client
dynamoClient, err := utils.NewDynamoDBClient(tableName, awsRegion)
if err != nil {
return fmt.Errorf("failed to create DynamoDB client: %w", err)
}
var analyses []*utils.StateSizeAnalysis
for _, result := range moduleResults {
// Create analysis object directly from raw data
analysis := createStateSizeAnalysis(height, result.ModuleName, result)
analyses = append(analyses, analysis)
}
// Export all analyses to DynamoDB
if err := dynamoClient.ExportMultipleAnalyses(analyses); err != nil {
return fmt.Errorf("failed to export analyses to DynamoDB: %w", err)
}
metadataTableName := tableName + "_metadata"
_, err = dynamoClient.UpdateLatestHeightIfGreater(metadataTableName, height)
return err
}
// printResultsToConsole prints the collected results to console
func printResultsToConsole(moduleResults map[string]*ModuleResult) {
for moduleName, result := range moduleResults {
fmt.Printf("Module %s total numKeys:%d, total keySize:%d, total valueSize:%d, totalSize: %d \n",
result.ModuleName, result.TotalNumKeys, result.TotalKeySize, result.TotalValueSize, result.TotalSize)
fmt.Println("prefix sizes: ", result.PrefixSizes)
fmt.Println("module name: ", moduleName)
fmt.Println("Prefix Sizes[moduleName]: ", result.PrefixSizes[moduleName])
prefixKeyResult, _ := json.MarshalIndent(result.PrefixSizes[moduleName].KeySize, "", " ")
fmt.Printf("Module %s prefix key size breakdown (bytes): %s \n", result.ModuleName, prefixKeyResult)
prefixValueResult, _ := json.MarshalIndent(result.PrefixSizes[moduleName].ValueSize, "", " ")
fmt.Printf("Module %s prefix value size breakdown (bytes): %s \n", result.ModuleName, prefixValueResult)
totalSizeResult, _ := json.MarshalIndent(result.PrefixSizes[moduleName].TotalSize, "", " ")
fmt.Printf("Module %s prefix total size breakdown (bytes): %s \n", result.ModuleName, totalSizeResult)
numKeysResult, _ := json.MarshalIndent(result.PrefixSizes[moduleName].KeyCount, "", " ")
fmt.Printf("Module %s prefix num of keys breakdown: %s \n", result.ModuleName, numKeysResult)
// EVM-only: zeroed-entry statistics for 0x03 storage
if moduleName == "evm" {
var pct float64
if result.TotalEVM03Entries > 0 {
pct = float64(result.ZeroedEVM03Entries) / float64(result.TotalEVM03Entries) * 100
}
fmt.Printf("EVM 0x03 entries: total=%d, zeroed=%d (%.2f%%), zeroed_key_bytes=%d, zeroed_value_bytes=%d\n",
result.TotalEVM03Entries,
result.ZeroedEVM03Entries,
pct,
result.ZeroedEVM03KeyBytes,
result.ZeroedEVM03ValueBytes,
)
}
// Display top contracts (already limited to top 100)
fmt.Printf("\nDetailed breakdown for 0x03 prefix (top %d contracts by total size):\n", len(result.ContractSizes))
fmt.Printf("%-42s %15s %10s\n", "Contract Address", "Total Size", "Key Count")
fmt.Printf("%s\n", strings.Repeat("-", 70))
// Convert to slice for display
var contractSlice []utils.ContractSizeEntry
for _, entry := range result.ContractSizes {
contractSlice = append(contractSlice, *entry)
}
// Sort by total size in descending order for display
sort.Slice(contractSlice, func(i, j int) bool {
return contractSlice[i].TotalSize > contractSlice[j].TotalSize
})
for _, contract := range contractSlice {
fmt.Printf("0x%-40s %15d %10d\n",
contract.Address,
contract.TotalSize,
contract.KeyCount)
}
}
}
// createStateSizeAnalysis creates a new StateSizeAnalysis from ModuleResult
func createStateSizeAnalysis(blockHeight int64, moduleName string, result *ModuleResult) *utils.StateSizeAnalysis {
// Convert raw data to JSON strings for DynamoDB storage
prefixJSON, _ := json.Marshal(result.PrefixSizes)
var contractSlice []utils.ContractSizeEntry
for _, contract := range result.ContractSizes {
contractSlice = append(contractSlice, *contract)
}
contractJSON, _ := json.Marshal(contractSlice)
return &utils.StateSizeAnalysis{
BlockHeight: blockHeight,
ModuleName: moduleName,
TotalNumKeys: result.TotalNumKeys,
TotalKeySize: result.TotalKeySize,
TotalValueSize: result.TotalValueSize,
TotalSize: result.TotalSize,
PrefixBreakdown: string(prefixJSON),
ContractBreakdown: string(contractJSON),
}
}
// isAllZero returns true if the provided byte slice is empty or consists entirely of zero bytes.
func isAllZero(b []byte) bool {
for _, by := range b {
if by != 0x00 {
return false
}
}
return true
}