This commit is contained in:
Roy Crihfield 2023-03-05 15:45:53 +08:00
parent 5ac92c5594
commit 355d58ee3d
3 changed files with 9 additions and 6 deletions

View File

@ -94,7 +94,8 @@ func (sd *stateDatabase) ContractCodeSize(_, codeHash common.Hash) (int, error)
// StateAccount satisfies Database, it returns the types.StateAccount for a provided address and block hash // StateAccount satisfies Database, it returns the types.StateAccount for a provided address and block hash
func (sd *stateDatabase) StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error) { func (sd *stateDatabase) StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error) {
res := StateAccountResult{} res := StateAccountResult{}
err := sd.pgdb.QueryRow(context.Background(), GetStateAccount, addressHash.Hex(), blockHash.Hex()).Scan(&res) err := sd.pgdb.QueryRow(context.Background(), GetStateAccount, addressHash.Hex(), blockHash.Hex()).
Scan(&res.Balance, &res.Nonce, &res.CodeHash, &res.StorageRoot, &res.Removed)
if err != nil { if err != nil {
return nil, errNotFound return nil, errNotFound
} }
@ -108,14 +109,16 @@ func (sd *stateDatabase) StateAccount(addressHash, blockHash common.Hash) (*type
Nonce: res.Nonce, Nonce: res.Nonce,
Balance: bal, Balance: bal,
Root: common.HexToHash(res.StorageRoot), Root: common.HexToHash(res.StorageRoot),
CodeHash: res.CodeHash, CodeHash: common.HexToHash(res.CodeHash).Bytes(),
}, nil }, nil
} }
// StorageValue satisfies Database, it returns the storage value for the provided address, slot, and block hash // StorageValue satisfies Database, it returns the storage value for the provided address, slot, and block hash
func (sd *stateDatabase) StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error) { func (sd *stateDatabase) StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error) {
res := StorageSlotResult{} res := StorageSlotResult{}
err := sd.pgdb.QueryRow(context.Background(), GetStorageSlot, addressHash.Hex(), slotHash.Hex(), blockHash.Hex()).Scan(&res) err := sd.pgdb.QueryRow(context.Background(), GetStorageSlot,
addressHash.Hex(), slotHash.Hex(), blockHash.Hex()).
Scan(&res.Value, &res.Removed)
if err != nil { if err != nil {
return nil, errNotFound return nil, errNotFound
} }

4
sql.go
View File

@ -1,7 +1,7 @@
package ipld_eth_statedb package ipld_eth_statedb
const ( const (
GetContractCodePgStr = `SELECT data FROM public.blocks WHERE key = $1` GetContractCodePgStr = `SELECT data FROM ipld.blocks WHERE key = $1`
GetStateAccount = `SELECT balance, nonce, code_hash, storage_root, removed FROM eth.state_cids GetStateAccount = `SELECT balance, nonce, code_hash, storage_root, removed FROM eth.state_cids
INNER JOIN eth.header_cids ON ( INNER JOIN eth.header_cids ON (
state_cids.header_id = header_cids.block_hash state_cids.header_id = header_cids.block_hash
@ -39,7 +39,7 @@ type StorageSlotResult struct {
type StateAccountResult struct { type StateAccountResult struct {
Balance string `db:"balance"` Balance string `db:"balance"`
Nonce uint64 `db:"nonce"` Nonce uint64 `db:"nonce"`
CodeHash []byte `db:"code_hash"` CodeHash string `db:"code_hash"`
StorageRoot string `db:"storage_root"` StorageRoot string `db:"storage_root"`
Removed bool `db:"removed"` Removed bool `db:"removed"`
} }

View File

@ -332,7 +332,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
s.AccountReads += time.Since(start) s.AccountReads += time.Since(start)
} }
if err != nil { if err != nil {
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err)) s.setError(fmt.Errorf("getDeletedStateObject (%x) error: %w", addr.Bytes(), err))
return nil return nil
} }
if data == nil { if data == nil {