github.com/amazechain/amc@v0.1.3/modules/state/plain_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 "encoding/binary" 22 "github.com/amazechain/amc/common/account" 23 "github.com/amazechain/amc/common/types" 24 "github.com/amazechain/amc/modules" 25 "github.com/ledgerwatch/erigon-lib/kv" 26 ) 27 28 var _ StateReader = (*PlainStateReader)(nil) 29 30 // PlainStateReader reads data from so called "plain state". 31 // Data in the plain state is stored using un-hashed account/storage items 32 // as opposed to the "normal" state that uses hashes of merkle paths to store items. 33 type PlainStateReader struct { 34 db kv.Getter 35 } 36 37 func NewPlainStateReader(db kv.Getter) *PlainStateReader { 38 return &PlainStateReader{ 39 db: db, 40 } 41 } 42 43 func (r *PlainStateReader) ReadAccountData(address types.Address) (*account.StateAccount, error) { 44 enc, err := r.db.GetOne(modules.Account, address.Bytes()) 45 if err != nil { 46 return nil, err 47 } 48 if len(enc) == 0 { 49 return nil, nil 50 } 51 var a account.StateAccount 52 if err = a.DecodeForStorage(enc); err != nil { 53 return nil, err 54 } 55 return &a, nil 56 } 57 58 func (r *PlainStateReader) ReadAccountStorage(address types.Address, incarnation uint16, key *types.Hash) ([]byte, error) { 59 compositeKey := modules.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes()) 60 enc, err := r.db.GetOne(modules.Storage, compositeKey) 61 if err != nil { 62 return nil, err 63 } 64 if len(enc) == 0 { 65 return nil, nil 66 } 67 return enc, nil 68 } 69 70 func (r *PlainStateReader) ReadAccountCode(address types.Address, incarnation uint16, codeHash types.Hash) ([]byte, error) { 71 if bytes.Equal(codeHash.Bytes(), emptyCodeHash) { 72 return nil, nil 73 } 74 code, err := r.db.GetOne(modules.Code, codeHash.Bytes()) 75 if len(code) == 0 { 76 return nil, nil 77 } 78 return code, err 79 } 80 81 func (r *PlainStateReader) ReadAccountCodeSize(address types.Address, incarnation uint16, codeHash types.Hash) (int, error) { 82 code, err := r.ReadAccountCode(address, incarnation, codeHash) 83 return len(code), err 84 } 85 86 func (r *PlainStateReader) ReadAccountIncarnation(address types.Address) (uint16, error) { 87 b, err := r.db.GetOne(modules.IncarnationMap, address.Bytes()) 88 if err != nil { 89 return 0, err 90 } 91 if len(b) == 0 { 92 return 0, nil 93 } 94 return binary.BigEndian.Uint16(b), nil 95 }