-
Notifications
You must be signed in to change notification settings - Fork 47
/
plain_state_reader.go
95 lines (85 loc) · 2.89 KB
/
plain_state_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"bytes"
"encoding/binary"
"github.com/amazechain/amc/common/account"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/modules"
"github.com/ledgerwatch/erigon-lib/kv"
)
var _ StateReader = (*PlainStateReader)(nil)
// PlainStateReader reads data from so called "plain state".
// Data in the plain state is stored using un-hashed account/storage items
// as opposed to the "normal" state that uses hashes of merkle paths to store items.
type PlainStateReader struct {
db kv.Getter
}
func NewPlainStateReader(db kv.Getter) *PlainStateReader {
return &PlainStateReader{
db: db,
}
}
func (r *PlainStateReader) ReadAccountData(address types.Address) (*account.StateAccount, error) {
enc, err := r.db.GetOne(modules.Account, address.Bytes())
if err != nil {
return nil, err
}
if len(enc) == 0 {
return nil, nil
}
var a account.StateAccount
if err = a.DecodeForStorage(enc); err != nil {
return nil, err
}
return &a, nil
}
func (r *PlainStateReader) ReadAccountStorage(address types.Address, incarnation uint16, key *types.Hash) ([]byte, error) {
compositeKey := modules.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
enc, err := r.db.GetOne(modules.Storage, compositeKey)
if err != nil {
return nil, err
}
if len(enc) == 0 {
return nil, nil
}
return enc, nil
}
func (r *PlainStateReader) ReadAccountCode(address types.Address, incarnation uint16, codeHash types.Hash) ([]byte, error) {
if bytes.Equal(codeHash.Bytes(), emptyCodeHash) {
return nil, nil
}
code, err := r.db.GetOne(modules.Code, codeHash.Bytes())
if len(code) == 0 {
return nil, nil
}
return code, err
}
func (r *PlainStateReader) ReadAccountCodeSize(address types.Address, incarnation uint16, codeHash types.Hash) (int, error) {
code, err := r.ReadAccountCode(address, incarnation, codeHash)
return len(code), err
}
func (r *PlainStateReader) ReadAccountIncarnation(address types.Address) (uint16, error) {
b, err := r.db.GetOne(modules.IncarnationMap, address.Bytes())
if err != nil {
return 0, err
}
if len(b) == 0 {
return 0, nil
}
return binary.BigEndian.Uint16(b), nil
}