github.com/amazechain/amc@v0.1.3/modules/state/reader.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 state 18 19 import ( 20 "bytes" 21 "github.com/amazechain/amc/common/account" 22 "github.com/amazechain/amc/common/crypto" 23 "github.com/amazechain/amc/common/types" 24 "github.com/amazechain/amc/modules" 25 "github.com/ledgerwatch/erigon-lib/kv" 26 ) 27 28 type HistoryStateReader struct { 29 accHistoryC, storageHistoryC kv.Cursor 30 accChangesC, storageChangesC kv.CursorDupSort 31 blockNr uint64 32 tx kv.Tx 33 db kv.Getter 34 } 35 36 func NewStateHistoryReader(tx kv.Tx, db kv.Getter, blockNr uint64) *HistoryStateReader { 37 c1, _ := tx.Cursor(modules.AccountsHistory) 38 c2, _ := tx.Cursor(modules.StorageHistory) 39 c3, _ := tx.CursorDupSort(modules.AccountChangeSet) 40 c4, _ := tx.CursorDupSort(modules.StorageChangeSet) 41 return &HistoryStateReader{ 42 tx: tx, 43 blockNr: blockNr, 44 db: db, 45 accHistoryC: c1, storageHistoryC: c2, accChangesC: c3, storageChangesC: c4, 46 } 47 } 48 49 func (dbr *HistoryStateReader) Rollback() { 50 dbr.tx.Rollback() 51 } 52 53 func (dbr *HistoryStateReader) SetBlockNumber(blockNr uint64) { 54 dbr.blockNr = blockNr 55 } 56 57 func (dbr *HistoryStateReader) GetOne(bucket string, key []byte) ([]byte, error) { 58 if len(bucket) == 0 { 59 return nil, nil 60 } 61 return dbr.db.GetOne(bucket, key[:]) 62 } 63 64 func (r *HistoryStateReader) ReadAccountData(address types.Address) (*account.StateAccount, error) { 65 acc := new(account.StateAccount) 66 // defer fmt.Printf("ReadAccount address: %s, account: %+v \n", address, acc) 67 v, err := r.db.GetOne(modules.Account, address[:]) 68 if err == nil && len(v) > 0 { 69 //var acc account.StateAccount 70 if err := acc.DecodeForStorage(v); err != nil { 71 return nil, err 72 } 73 return acc, nil 74 } 75 enc, err := GetAsOf(r.tx, r.accHistoryC, r.accChangesC, false /* storage */, address[:], r.blockNr) 76 if err != nil || enc == nil || len(enc) == 0 { 77 return nil, nil 78 } 79 //var acc account.StateAccount 80 if err := acc.DecodeForStorage(enc); err != nil { 81 return nil, err 82 } 83 return acc, nil 84 } 85 86 func (r *HistoryStateReader) ReadAccountStorage(address types.Address, incarnation uint16, key *types.Hash) ([]byte, error) { 87 compositeKey := modules.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes()) 88 v, err := r.db.GetOne(modules.Storage, compositeKey) 89 if err == nil && len(v) > 0 { 90 return v, nil 91 } 92 return GetAsOf(r.tx, r.storageHistoryC, r.storageChangesC, true /* storage */, compositeKey, r.blockNr) 93 } 94 95 func (r *HistoryStateReader) ReadAccountCode(address types.Address, incarnation uint16, codeHash types.Hash) ([]byte, error) { 96 if bytes.Equal(codeHash[:], crypto.Keccak256(nil)) { 97 return nil, nil 98 } 99 var val []byte 100 v, err := r.tx.GetOne(modules.Code, codeHash[:]) 101 if err != nil || len(v) == 0 { 102 panic(err) 103 return nil, err 104 } 105 val = types.CopyBytes(v) 106 return val, nil 107 } 108 109 func (r *HistoryStateReader) ReadAccountCodeSize(address types.Address, incarnation uint16, codeHash types.Hash) (int, error) { 110 code, err := r.ReadAccountCode(address, incarnation, codeHash) 111 if err != nil { 112 return 0, err 113 } 114 return len(code), nil 115 } 116 117 func (r *HistoryStateReader) ReadAccountIncarnation(address types.Address) (uint16, error) { 118 return 0, nil 119 }