Skip to content

Commit 4281689

Browse files
committed
Merge branch 'main' into yiren/universal-acc
2 parents c8f93a5 + 4a62f83 commit 4281689

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

tools/cmd/seidb/operations/state_size.go

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ func StateSizeCmd() *cobra.Command {
2424
return cmd
2525
}
2626

27+
type contractSizeEntry struct {
28+
Address string
29+
KeySize int64
30+
ValueSize int64
31+
TotalSize int64
32+
KeyCount int
33+
}
34+
2735
func executeStateSize(cmd *cobra.Command, _ []string) {
2836
module, _ := cmd.Flags().GetString("module")
2937
dbDir, _ := cmd.Flags().GetString("db-dir")
@@ -42,6 +50,7 @@ func executeStateSize(cmd *cobra.Command, _ []string) {
4250
panic(err)
4351
}
4452
defer db.Close()
53+
fmt.Printf("Finished opening db, calculating state size for module: %s\n", module)
4554
err = PrintStateSize(module, db)
4655
if err != nil {
4756
panic(err)
@@ -56,20 +65,25 @@ func PrintStateSize(module string, db *memiavl.DB) error {
5665
} else {
5766
modules = append(modules, module)
5867
}
68+
// Print top 100 contracts by total size
69+
numToShow := 100
5970

6071
for _, moduleName := range modules {
6172
tree := db.TreeByName(moduleName)
6273
totalNumKeys := 0
6374
totalKeySize := 0
6475
totalValueSize := 0
6576
totalSize := 0
77+
6678
if tree == nil {
6779
fmt.Printf("Tree does not exist for module %s \n", moduleName)
6880
} else {
6981
fmt.Printf("Calculating for module: %s \n", moduleName)
7082
keySizeByPrefix := map[string]int64{}
7183
valueSizeByPrefix := map[string]int64{}
7284
numKeysByPrefix := map[string]int64{}
85+
contractSizes := make(map[string]*contractSizeEntry)
86+
// Scan again to collect per-contract statistics
7387
tree.ScanPostOrder(func(node memiavl.Node) bool {
7488
if node.IsLeaf() {
7589
totalNumKeys++
@@ -78,11 +92,28 @@ func PrintStateSize(module string, db *memiavl.DB) error {
7892
totalKeySize += keySize
7993
totalValueSize += valueSize
8094
totalSize += keySize + valueSize
81-
prefix := fmt.Sprintf("%X", node.Key())
82-
prefix = prefix[:2]
95+
prefixKey := fmt.Sprintf("%X", node.Key())
96+
prefix := prefixKey[:2]
8397
keySizeByPrefix[prefix] += int64(keySize)
8498
valueSizeByPrefix[prefix] += int64(valueSize)
8599
numKeysByPrefix[prefix]++
100+
101+
if module == "evm" && prefix == "03" {
102+
// Extract contract address from key (assuming it follows after "03")
103+
addr := prefixKey[2:42] // Adjust indices based on your key format
104+
if _, exists := contractSizes[addr]; !exists {
105+
contractSizes[addr] = &contractSizeEntry{Address: addr}
106+
}
107+
entry := contractSizes[addr]
108+
entry.KeySize += int64(len(node.Key()))
109+
entry.ValueSize += int64(len(node.Value()))
110+
entry.TotalSize = entry.KeySize + entry.ValueSize
111+
entry.KeyCount++
112+
}
113+
114+
if totalNumKeys%1000000 == 0 {
115+
fmt.Printf("Scanned %d keys for module %s\n", totalNumKeys, moduleName)
116+
}
86117
}
87118
return true
88119
})
@@ -94,66 +125,32 @@ func PrintStateSize(module string, db *memiavl.DB) error {
94125
numKeysResult, _ := json.MarshalIndent(numKeysByPrefix, "", " ")
95126
fmt.Printf("Module %s prefix num of keys breakdown: %s \n", moduleName, numKeysResult)
96127

97-
// Print top 20 contracts by total size
98-
numToShow := 20
99-
if valueSizeByPrefix["03"] > 0 || keySizeByPrefix["03"] > 0 {
100-
type contractSizeEntry struct {
101-
Address string
102-
KeySize int64
103-
ValueSize int64
104-
TotalSize int64
105-
KeyCount int
106-
}
107-
108-
contractSizes := make(map[string]*contractSizeEntry)
109-
110-
// Scan again to collect per-contract statistics
111-
tree.ScanPostOrder(func(node memiavl.Node) bool {
112-
if node.IsLeaf() {
113-
prefix := fmt.Sprintf("%X", node.Key())
114-
if prefix[:2] == "03" {
115-
// Extract contract address from key (assuming it follows after "03")
116-
addr := prefix[2:42] // Adjust indices based on your key format
117-
if _, exists := contractSizes[addr]; !exists {
118-
contractSizes[addr] = &contractSizeEntry{Address: addr}
119-
}
120-
entry := contractSizes[addr]
121-
entry.KeySize += int64(len(node.Key()))
122-
entry.ValueSize += int64(len(node.Value()))
123-
entry.TotalSize = entry.KeySize + entry.ValueSize
124-
entry.KeyCount++
125-
}
126-
}
127-
return true
128-
})
129-
130-
// Convert map to slice
131-
var sortedContracts []contractSizeEntry
132-
for _, entry := range contractSizes {
133-
sortedContracts = append(sortedContracts, *entry)
134-
}
128+
// Convert map to slice
129+
var sortedContracts []contractSizeEntry
130+
for _, entry := range contractSizes {
131+
sortedContracts = append(sortedContracts, *entry)
132+
}
135133

136-
// Sort by total size in descending order
137-
sort.Slice(sortedContracts, func(i, j int) bool {
138-
return sortedContracts[i].TotalSize > sortedContracts[j].TotalSize
139-
})
134+
// Sort by total size in descending order
135+
sort.Slice(sortedContracts, func(i, j int) bool {
136+
return sortedContracts[i].TotalSize > sortedContracts[j].TotalSize
137+
})
140138

141-
fmt.Printf("\nDetailed breakdown for 0x03 prefix (top 20 contracts by total size):\n")
142-
fmt.Printf("%-42s %15s %15s %15s %10s\n", "Contract Address", "Key Size", "Value Size", "Total Size", "Key Count")
143-
fmt.Printf("%s\n", strings.Repeat("-", 100))
139+
fmt.Printf("\nDetailed breakdown for 0x03 prefix (top 100 contracts by total size):\n")
140+
fmt.Printf("%-42s %15s %15s %15s %10s\n", "Contract Address", "Key Size", "Value Size", "Total Size", "Key Count")
141+
fmt.Printf("%s\n", strings.Repeat("-", 100))
144142

145-
if len(sortedContracts) < numToShow {
146-
numToShow = len(sortedContracts)
147-
}
148-
for i := 0; i < numToShow; i++ {
149-
contract := sortedContracts[i]
150-
fmt.Printf("0x%-40s %15d %15d %15d %10d\n",
151-
contract.Address,
152-
contract.KeySize,
153-
contract.ValueSize,
154-
contract.TotalSize,
155-
contract.KeyCount)
156-
}
143+
if len(sortedContracts) < numToShow {
144+
numToShow = len(sortedContracts)
145+
}
146+
for i := 0; i < numToShow; i++ {
147+
contract := sortedContracts[i]
148+
fmt.Printf("0x%-40s %15d %15d %15d %10d\n",
149+
contract.Address,
150+
contract.KeySize,
151+
contract.ValueSize,
152+
contract.TotalSize,
153+
contract.KeyCount)
157154
}
158155
}
159156
}

0 commit comments

Comments
 (0)