github.com/koko1123/flow-go-1@v0.29.6/cmd/util/ledger/migrations/storage_fees_migration.go (about) 1 package migrations 2 3 import ( 4 fvm "github.com/koko1123/flow-go-1/fvm/environment" 5 "github.com/koko1123/flow-go-1/ledger" 6 "github.com/koko1123/flow-go-1/ledger/common/utils" 7 "github.com/koko1123/flow-go-1/model/flow" 8 ) 9 10 // iterates through registers keeping a map of register sizes 11 // after it has reached the end it add storage used and storage capacity for each address 12 func StorageFeesMigration(payload []ledger.Payload) ([]ledger.Payload, error) { 13 storageUsed := make(map[string]uint64) 14 newPayload := make([]ledger.Payload, len(payload)) 15 16 for i, p := range payload { 17 err := incrementStorageUsed(p, storageUsed) 18 if err != nil { 19 return nil, err 20 } 21 newPayload[i] = p 22 } 23 24 for s, u := range storageUsed { 25 // this is the storage used by the storage_used register we are about to add 26 storageUsedByStorageUsed := fvm.RegisterSize( 27 flow.BytesToAddress([]byte(s)), 28 "storage_used", 29 make([]byte, 8)) 30 u = u + uint64(storageUsedByStorageUsed) 31 32 newPayload = append(newPayload, *ledger.NewPayload( 33 registerIDToKey(flow.RegisterID{ 34 Owner: s, 35 Key: "storage_used", 36 }), 37 utils.Uint64ToBinary(u), 38 )) 39 } 40 return newPayload, nil 41 } 42 43 func incrementStorageUsed(p ledger.Payload, used map[string]uint64) error { 44 k, err := p.Key() 45 if err != nil { 46 return err 47 } 48 id, err := KeyToRegisterID(k) 49 if err != nil { 50 return err 51 } 52 if len([]byte(id.Owner)) != flow.AddressLength { 53 // not an address 54 return nil 55 } 56 if _, ok := used[id.Owner]; !ok { 57 used[id.Owner] = 0 58 } 59 used[id.Owner] = used[id.Owner] + uint64(registerSize(id, p)) 60 return nil 61 } 62 63 func registerSize(id flow.RegisterID, p ledger.Payload) int { 64 address := flow.BytesToAddress([]byte(id.Owner)) 65 key := id.Key 66 return fvm.RegisterSize(address, key, p.Value()) 67 }