-
Notifications
You must be signed in to change notification settings - Fork 808
/
statedb.go
213 lines (177 loc) · 5.75 KB
/
statedb.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package state
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/sei-protocol/sei-chain/utils"
)
// Initialized for each transaction individually
type DBImpl struct {
ctx sdk.Context
snapshottedCtxs []sdk.Context
tempStateCurrent *TemporaryState
tempStatesHist []*TemporaryState
// If err is not nil at the end of the execution, the transaction will be rolled
// back.
err error
// whenever this is set, the same error would also cause EVM to revert, which is
// why we don't put it in `tempState`, since we still want to be able to access it later.
precompileErr error
// a temporary address that collects fees for this particular transaction so that there is
// no single bottleneck for fee collection. Its account state and balance will be deleted
// before the block commits
coinbaseAddress sdk.AccAddress
coinbaseEvmAddress common.Address
k EVMKeeper
simulation bool
// for cases like bank.send_native, we want to suppress transfer events
eventsSuppressed bool
logger *tracing.Hooks
}
func NewDBImpl(ctx sdk.Context, k EVMKeeper, simulation bool) *DBImpl {
feeCollector, _ := k.GetFeeCollectorAddress(ctx)
s := &DBImpl{
ctx: ctx,
k: k,
snapshottedCtxs: []sdk.Context{},
coinbaseAddress: GetCoinbaseAddress(ctx.TxIndex()),
simulation: simulation,
tempStateCurrent: NewTemporaryState(),
coinbaseEvmAddress: feeCollector,
}
s.Snapshot() // take an initial snapshot for GetCommitted
return s
}
func (s *DBImpl) AddSurplus(surplus sdk.Int) {
if surplus.IsNil() || surplus.IsZero() {
return
}
s.tempStateCurrent.surplus = s.tempStateCurrent.surplus.Add(surplus)
}
func (s *DBImpl) DisableEvents() {
s.eventsSuppressed = true
}
func (s *DBImpl) EnableEvents() {
s.eventsSuppressed = false
}
func (s *DBImpl) SetLogger(logger *tracing.Hooks) {
s.logger = logger
}
// for interface compliance
func (s *DBImpl) SetEVM(evm *vm.EVM) {}
// AddPreimage records a SHA3 preimage seen by the VM.
// AddPreimage performs a no-op since the EnablePreimageRecording flag is disabled
// on the vm.Config during state transitions. No store trie preimages are written
// to the database.
func (s *DBImpl) AddPreimage(_ common.Hash, _ []byte) {}
func (s *DBImpl) Finalize() (surplus sdk.Int, err error) {
if s.simulation {
panic("should never call finalize on a simulation DB")
}
if s.err != nil {
err = s.err
return
}
// delete state of self-destructed accounts
s.handleResidualFundsInDestructedAccounts(s.tempStateCurrent)
s.clearAccountStateIfDestructed(s.tempStateCurrent)
for _, ts := range s.tempStatesHist {
s.handleResidualFundsInDestructedAccounts(ts)
s.clearAccountStateIfDestructed(ts)
}
// remove transient states
// write cache to underlying
s.flushCtx(s.ctx)
// write all snapshotted caches in reverse order, except the very first one (base) which will be written by baseapp::runTx
for i := len(s.snapshottedCtxs) - 1; i > 0; i-- {
s.flushCtx(s.snapshottedCtxs[i])
}
surplus = s.tempStateCurrent.surplus
for _, ts := range s.tempStatesHist {
surplus = surplus.Add(ts.surplus)
}
return
}
func (s *DBImpl) flushCtx(ctx sdk.Context) {
ctx.MultiStore().(sdk.CacheMultiStore).Write()
}
// Backward-compatibility functions
func (s *DBImpl) Error() error {
return s.Err()
}
func (s *DBImpl) GetStorageRoot(common.Address) common.Hash {
panic("GetStorageRoot is not implemented and called unexpectedly")
}
func (s *DBImpl) Copy() vm.StateDB {
newCtx := s.ctx.WithMultiStore(s.ctx.MultiStore().CacheMultiStore())
return &DBImpl{
ctx: newCtx,
snapshottedCtxs: append(s.snapshottedCtxs, s.ctx),
tempStateCurrent: NewTemporaryState(),
tempStatesHist: append(s.tempStatesHist, s.tempStateCurrent),
k: s.k,
coinbaseAddress: s.coinbaseAddress,
coinbaseEvmAddress: s.coinbaseEvmAddress,
simulation: s.simulation,
err: s.err,
precompileErr: s.precompileErr,
logger: s.logger,
}
}
func (s *DBImpl) Finalise(bool) {
s.ctx.Logger().Info("Finalise should only be called during simulation and will no-op")
}
func (s *DBImpl) Commit(uint64, bool) (common.Hash, error) {
panic("Commit is not implemented and called unexpectedly")
}
func (s *DBImpl) SetTxContext(common.Hash, int) {
//noop
}
func (s *DBImpl) IntermediateRoot(bool) common.Hash {
panic("IntermediateRoot is not implemented and called unexpectedly")
}
func (s *DBImpl) TxIndex() int {
return s.ctx.TxIndex()
}
func (s *DBImpl) Preimages() map[common.Hash][]byte {
return map[common.Hash][]byte{}
}
func (s *DBImpl) SetPrecompileError(err error) {
s.precompileErr = err
}
func (s *DBImpl) GetPrecompileError() error {
return s.precompileErr
}
// ** TEST ONLY FUNCTIONS **//
func (s *DBImpl) Err() error {
return s.err
}
func (s *DBImpl) WithErr(err error) {
s.err = err
}
func (s *DBImpl) Ctx() sdk.Context {
return s.ctx
}
func (s *DBImpl) WithCtx(ctx sdk.Context) {
s.ctx = ctx
}
// in-memory state that's generated by a specific
// EVM snapshot in a single transaction
type TemporaryState struct {
logs []*ethtypes.Log
transientStates map[string]map[string]common.Hash
transientAccounts map[string][]byte
transientModuleStates map[string][]byte
surplus sdk.Int // in wei
}
func NewTemporaryState() *TemporaryState {
return &TemporaryState{
logs: []*ethtypes.Log{},
transientStates: make(map[string]map[string]common.Hash),
transientAccounts: make(map[string][]byte),
transientModuleStates: make(map[string][]byte),
surplus: utils.Sdk0,
}
}