-
Notifications
You must be signed in to change notification settings - Fork 47
/
plain_state_writer.go
165 lines (147 loc) · 4.72 KB
/
plain_state_writer.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 (
"encoding/binary"
"github.com/amazechain/amc/common/account"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/modules"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/kv"
"google.golang.org/protobuf/proto"
)
var _ WriterWithChangeSets = (*PlainStateWriter)(nil)
type putDel interface {
kv.Putter
kv.Deleter
}
type PlainStateWriter struct {
db putDel
csw *ChangeSetWriter
//accumulator *shards.Accumulator
}
func NewPlainStateWriter(db putDel, changeSetsDB kv.RwTx, blockNumber uint64) *PlainStateWriter {
return &PlainStateWriter{
db: db,
csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber),
}
}
func NewPlainStateWriterNoHistory(db putDel) *PlainStateWriter {
return &PlainStateWriter{
db: db,
}
}
//func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *PlainStateWriter {
// w.accumulator = accumulator
// return w
//}
func (w *PlainStateWriter) UpdateAccountData(address types.Address, original, account *account.StateAccount) error {
//fmt.Printf("balance,%x,%d\n", address, &account.Balance)
if w.csw != nil {
if err := w.csw.UpdateAccountData(address, original, account); err != nil {
return err
}
}
//var value []byte
//account.EncodeForStorage(value)
pb := account.ToProtoMessage()
data, _ := proto.Marshal(pb)
//if w.accumulator != nil {
// w.accumulator.ChangeAccount(address, account.Incarnation, value)
//}
// defer fmt.Printf("Write Account address: %s, balance: %d \n", address, data)
return w.db.Put(modules.Account, address[:], data)
}
func (w *PlainStateWriter) UpdateAccountCode(address types.Address, incarnation uint16, codeHash types.Hash, code []byte) error {
//fmt.Printf("code,%x,%x\n", address, code)
if w.csw != nil {
if err := w.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
return err
}
}
//if w.accumulator != nil {
// w.accumulator.ChangeCode(address, incarnation, code)
//}
if err := w.db.Put(modules.Code, codeHash[:], code); err != nil {
return err
}
return w.db.Put(modules.PlainContractCode, modules.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:])
}
func (w *PlainStateWriter) DeleteAccount(address types.Address, original *account.StateAccount) error {
//fmt.Printf("delete,%x\n", address)
if w.csw != nil {
if err := w.csw.DeleteAccount(address, original); err != nil {
return err
}
}
//if w.accumulator != nil {
// w.accumulator.DeleteAccount(address)
//}
if err := w.db.Delete(modules.Account, address[:]); err != nil {
return err
}
if original.Incarnation > 0 {
var b [8]byte
binary.BigEndian.PutUint16(b[:], original.Incarnation)
if err := w.db.Put(modules.IncarnationMap, address[:], b[:]); err != nil {
return err
}
}
return nil
}
func (w *PlainStateWriter) WriteAccountStorage(address types.Address, incarnation uint16, key *types.Hash, original, value *uint256.Int) error {
//fmt.Printf("storage,%x,%x,%x\n", address, *key, value.Bytes())
if w.csw != nil {
if err := w.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
return err
}
}
if *original == *value {
return nil
}
compositeKey := modules.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
v := value.Bytes()
//if w.accumulator != nil {
// w.accumulator.ChangeStorage(address, incarnation, *key, v)
//}
if len(v) == 0 {
return w.db.Delete(modules.Storage, compositeKey)
}
return w.db.Put(modules.Storage, compositeKey, v)
}
func (w *PlainStateWriter) CreateContract(address types.Address) error {
if w.csw != nil {
if err := w.csw.CreateContract(address); err != nil {
return err
}
}
return nil
}
func (w *PlainStateWriter) WriteChangeSets() error {
if w.csw != nil {
return w.csw.WriteChangeSets()
}
return nil
}
func (w *PlainStateWriter) WriteHistory() error {
if w.csw != nil {
return w.csw.WriteHistory()
}
return nil
}
func (w *PlainStateWriter) ChangeSetWriter() *ChangeSetWriter {
return w.csw
}