github.com/amazechain/amc@v0.1.3/modules/changeset/account_changeset.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package changeset 18 19 import ( 20 "bytes" 21 "encoding/binary" 22 "fmt" 23 "github.com/amazechain/amc/common/types" 24 "github.com/amazechain/amc/modules" 25 "sort" 26 27 "github.com/ledgerwatch/erigon-lib/common/length" 28 "github.com/ledgerwatch/erigon-lib/kv" 29 ) 30 31 type Encoder func(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error 32 type Decoder func(dbKey, dbValue []byte) (blockN uint64, k, v []byte, err error) 33 34 func NewAccountChangeSet() *ChangeSet { 35 return &ChangeSet{ 36 Changes: make([]Change, 0), 37 keyLen: length.Addr, 38 } 39 } 40 41 func EncodeAccounts(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error { 42 sort.Sort(s) 43 newK := modules.EncodeBlockNumber(blockN) 44 for _, cs := range s.Changes { 45 newV := make([]byte, len(cs.Key)+len(cs.Value)) 46 copy(newV, cs.Key) 47 copy(newV[len(cs.Key):], cs.Value) 48 if err := f(newK, newV); err != nil { 49 return err 50 } 51 } 52 return nil 53 } 54 55 func DecodeAccounts(dbKey, dbValue []byte) (uint64, []byte, []byte, error) { 56 blockN := binary.BigEndian.Uint64(dbKey) 57 if len(dbValue) < length.Addr { 58 return 0, nil, nil, fmt.Errorf("account changes purged for block %d", blockN) 59 } 60 k := dbValue[:length.Addr] 61 v := dbValue[length.Addr:] 62 return blockN, k, v, nil 63 } 64 65 func FindAccount(c kv.CursorDupSort, blockNumber uint64, key []byte) ([]byte, error) { 66 k := modules.EncodeBlockNumber(blockNumber) 67 v, err := c.SeekBothRange(k, key) 68 if err != nil { 69 return nil, err 70 } 71 _, k, v, err = DecodeAccounts(k, v) 72 if err != nil { 73 return nil, err 74 } 75 if !bytes.HasPrefix(k, key) { 76 return nil, nil 77 } 78 return v, nil 79 } 80 81 // GetModifiedAccounts returns a list of addresses that were modified in the block range 82 // [startNum:endNum) 83 func GetModifiedAccounts(db kv.Tx, startNum, endNum uint64) ([]types.Address, error) { 84 changedAddrs := make(map[types.Address]struct{}) 85 if err := ForRange(db, modules.AccountChangeSet, startNum, endNum, func(blockN uint64, k, v []byte) error { 86 changedAddrs[types.BytesToAddress(k)] = struct{}{} 87 return nil 88 }); err != nil { 89 return nil, err 90 } 91 92 if len(changedAddrs) == 0 { 93 return nil, nil 94 } 95 96 idx := 0 97 result := make([]types.Address, len(changedAddrs)) 98 for addr := range changedAddrs { 99 copy(result[idx][:], addr[:]) 100 idx++ 101 } 102 103 return result, nil 104 }