@@ -6,8 +6,10 @@ package chain
66import (
77 "encoding/json"
88 "fmt"
9+ "time"
910
1011 "github.com/ava-labs/avalanchego/database"
12+ "github.com/ava-labs/avalanchego/database/versiondb"
1113 "github.com/ava-labs/avalanchego/utils/units"
1214 "github.com/ethereum/go-ethereum/common"
1315 "github.com/ethereum/go-ethereum/crypto"
@@ -118,36 +120,44 @@ func (g *Genesis) Verify() error {
118120 return nil
119121}
120122
121- func (g * Genesis ) Load (db database.KeyValueWriter , airdropData []byte ) error {
123+ func (g * Genesis ) Load (db database.Database , airdropData []byte ) error {
124+ start := time .Now ()
125+ defer func () {
126+ log .Debug ("loaded genesis allocations" , "t" , time .Since (start ))
127+ }()
128+
129+ vdb := versiondb .New (db )
122130 if len (g .AirdropHash ) > 0 {
123131 h := common .BytesToHash (crypto .Keccak256 (airdropData )).Hex ()
124132 if g .AirdropHash != h {
125133 return fmt .Errorf ("expected standard allocation %s but got %s" , g .AirdropHash , h )
126134 }
127135
128- standardAllocation := []* Airdrop {}
129- if err := json .Unmarshal (airdropData , & standardAllocation ); err != nil {
136+ airdrop := []* Airdrop {}
137+ if err := json .Unmarshal (airdropData , & airdrop ); err != nil {
130138 return err
131139 }
132140
133- for _ , alloc := range standardAllocation {
134- if err := SetBalance (db , alloc .Address , g .AirdropUnits ); err != nil {
141+ for _ , alloc := range airdrop {
142+ if err := SetBalance (vdb , alloc .Address , g .AirdropUnits ); err != nil {
135143 return fmt .Errorf ("%w: addr=%s, bal=%d" , err , alloc .Address , g .AirdropUnits )
136144 }
137145 }
138146 log .Debug (
139147 "applied airdrop allocation" ,
140- "hash" , h , "addrs" , len (standardAllocation ), "balance" , g .AirdropUnits ,
148+ "hash" , h , "addrs" , len (airdrop ), "balance" , g .AirdropUnits ,
141149 )
142150 }
143151
144152 // Do custom allocation last in case an address shows up in standard
145153 // allocation
146154 for _ , alloc := range g .CustomAllocation {
147- if err := SetBalance (db , alloc .Address , alloc .Balance ); err != nil {
155+ if err := SetBalance (vdb , alloc .Address , alloc .Balance ); err != nil {
148156 return fmt .Errorf ("%w: addr=%s, bal=%d" , err , alloc .Address , alloc .Balance )
149157 }
150158 log .Debug ("applied custom allocation" , "addr" , alloc .Address , "balance" , alloc .Balance )
151159 }
152- return nil
160+
161+ // Commit as a batch to improve speed
162+ return vdb .Commit ()
153163}
0 commit comments