forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cow.go
263 lines (229 loc) · 8.85 KB
/
cow.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package ledger
import (
"fmt"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/protocol"
)
// ___________________
// < cow = Copy On Write >
// -------------------
// \ ^__^
// \ (oo)\_______
// (__)\ )\/\
// ||----w |
// || ||
type roundCowParent interface {
lookup(basics.Address) (basics.AccountData, error)
checkDup(basics.Round, basics.Round, transactions.Txid, ledgercore.Txlease) error
txnCounter() uint64
getCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error)
compactCertNext() basics.Round
blockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error)
getStorageCounts(addr basics.Address, aidx basics.AppIndex, global bool) (basics.StateSchema, error)
// note: getStorageLimits is redundant with the other methods
// and is provided to optimize state schema lookups
getStorageLimits(addr basics.Address, aidx basics.AppIndex, global bool) (basics.StateSchema, error)
allocated(addr basics.Address, aidx basics.AppIndex, global bool) (bool, error)
getKey(addr basics.Address, aidx basics.AppIndex, global bool, key string, accountIdx uint64) (basics.TealValue, bool, error)
}
type roundCowState struct {
lookupParent roundCowParent
commitParent *roundCowState
proto config.ConsensusParams
mods ledgercore.StateDelta
// storage deltas populated as side effects of AppCall transaction
// 1. Opt-in/Close actions (see Allocate/Deallocate)
// 2. Stateful TEAL evaluation (see SetKey/DelKey)
// must be incorporated into mods.accts before passing deltas forward
sdeltas map[basics.Address]map[storagePtr]*storageDelta
// either or not maintain compatibility with original app refactoring behavior
// this is needed for generating old eval delta in new code
compatibilityMode bool
// cache mainaining accountIdx used in getKey for local keys access
compatibilityGetKeyCache map[basics.Address]map[storagePtr]uint64
}
func makeRoundCowState(b roundCowParent, hdr bookkeeping.BlockHeader, prevTimestamp int64, hint int) *roundCowState {
cb := roundCowState{
lookupParent: b,
commitParent: nil,
proto: config.Consensus[hdr.CurrentProtocol],
mods: ledgercore.MakeStateDelta(&hdr, prevTimestamp, hint, 0),
sdeltas: make(map[basics.Address]map[storagePtr]*storageDelta),
}
// compatibilityMode retains producing application' eval deltas under the following rule:
// local delta has account index as it specified in TEAL either in set/del key or prior get key calls.
// The predicate is that complex in order to cover all the block seen on testnet and mainnet.
compatibilityMode := (hdr.CurrentProtocol == protocol.ConsensusV24) &&
(hdr.NextProtocol != protocol.ConsensusV26 || (hdr.UpgradePropose == "" && hdr.UpgradeApprove == false && hdr.Round < hdr.UpgradeState.NextProtocolVoteBefore))
if compatibilityMode {
cb.compatibilityMode = true
cb.compatibilityGetKeyCache = make(map[basics.Address]map[storagePtr]uint64)
}
return &cb
}
func (cb *roundCowState) deltas() ledgercore.StateDelta {
var err error
if len(cb.sdeltas) == 0 {
return cb.mods
}
// Apply storage deltas to account deltas
// 1. Ensure all addresses from sdeltas have entries in accts because
// SetKey/DelKey work only with sdeltas, so need to pull missing accounts
// 2. Call applyStorageDelta for every delta per account
for addr, smap := range cb.sdeltas {
var delta basics.AccountData
var exist bool
if delta, exist = cb.mods.Accts.Get(addr); !exist {
ad, err := cb.lookup(addr)
if err != nil {
panic(fmt.Sprintf("fetching account data failed for addr %s: %s", addr.String(), err.Error()))
}
delta = ad
}
for aapp, storeDelta := range smap {
if delta, err = applyStorageDelta(delta, aapp, storeDelta); err != nil {
panic(fmt.Sprintf("applying storage delta failed for addr %s app %d: %s", addr.String(), aapp.aidx, err.Error()))
}
}
cb.mods.Accts.Upsert(addr, delta)
}
return cb.mods
}
func (cb *roundCowState) rewardsLevel() uint64 {
return cb.mods.Hdr.RewardsLevel
}
func (cb *roundCowState) round() basics.Round {
return cb.mods.Hdr.Round
}
func (cb *roundCowState) prevTimestamp() int64 {
return cb.mods.PrevTimestamp
}
func (cb *roundCowState) getCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (creator basics.Address, ok bool, err error) {
delta, ok := cb.mods.Creatables[cidx]
if ok {
if delta.Created && delta.Ctype == ctype {
return delta.Creator, true, nil
}
return basics.Address{}, false, nil
}
return cb.lookupParent.getCreator(cidx, ctype)
}
func (cb *roundCowState) lookup(addr basics.Address) (data basics.AccountData, err error) {
d, ok := cb.mods.Accts.Get(addr)
if ok {
return d, nil
}
return cb.lookupParent.lookup(addr)
}
func (cb *roundCowState) checkDup(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
_, present := cb.mods.Txids[txid]
if present {
return &ledgercore.TransactionInLedgerError{Txid: txid}
}
if cb.proto.SupportTransactionLeases && (txl.Lease != [32]byte{}) {
expires, ok := cb.mods.Txleases[txl]
if ok && cb.mods.Hdr.Round <= expires {
return ledgercore.MakeLeaseInLedgerError(txid, txl)
}
}
return cb.lookupParent.checkDup(firstValid, lastValid, txid, txl)
}
func (cb *roundCowState) txnCounter() uint64 {
return cb.lookupParent.txnCounter() + uint64(len(cb.mods.Txids))
}
func (cb *roundCowState) compactCertNext() basics.Round {
if cb.mods.CompactCertNext != 0 {
return cb.mods.CompactCertNext
}
return cb.lookupParent.compactCertNext()
}
func (cb *roundCowState) blockHdr(r basics.Round) (bookkeeping.BlockHeader, error) {
return cb.lookupParent.blockHdr(r)
}
func (cb *roundCowState) put(addr basics.Address, new basics.AccountData, newCreatable *basics.CreatableLocator, deletedCreatable *basics.CreatableLocator) {
cb.mods.Accts.Upsert(addr, new)
if newCreatable != nil {
cb.mods.Creatables[newCreatable.Index] = ledgercore.ModifiedCreatable{
Ctype: newCreatable.Type,
Creator: newCreatable.Creator,
Created: true,
}
}
if deletedCreatable != nil {
cb.mods.Creatables[deletedCreatable.Index] = ledgercore.ModifiedCreatable{
Ctype: deletedCreatable.Type,
Creator: deletedCreatable.Creator,
Created: false,
}
}
}
func (cb *roundCowState) addTx(txn transactions.Transaction, txid transactions.Txid) {
cb.mods.Txids[txid] = txn.LastValid
cb.mods.Txleases[ledgercore.Txlease{Sender: txn.Sender, Lease: txn.Lease}] = txn.LastValid
}
func (cb *roundCowState) setCompactCertNext(rnd basics.Round) {
cb.mods.CompactCertNext = rnd
}
func (cb *roundCowState) child(hint int) *roundCowState {
ch := roundCowState{
lookupParent: cb,
commitParent: cb,
proto: cb.proto,
mods: ledgercore.MakeStateDelta(cb.mods.Hdr, cb.mods.PrevTimestamp, hint, cb.mods.CompactCertNext),
sdeltas: make(map[basics.Address]map[storagePtr]*storageDelta),
}
if cb.compatibilityMode {
ch.compatibilityMode = cb.compatibilityMode
ch.compatibilityGetKeyCache = make(map[basics.Address]map[storagePtr]uint64)
}
return &ch
}
func (cb *roundCowState) commitToParent() {
cb.commitParent.mods.Accts.MergeAccounts(cb.mods.Accts)
for txid, lv := range cb.mods.Txids {
cb.commitParent.mods.Txids[txid] = lv
}
for txl, expires := range cb.mods.Txleases {
cb.commitParent.mods.Txleases[txl] = expires
}
for cidx, delta := range cb.mods.Creatables {
cb.commitParent.mods.Creatables[cidx] = delta
}
for addr, smod := range cb.sdeltas {
for aapp, nsd := range smod {
lsd, ok := cb.commitParent.sdeltas[addr][aapp]
if ok {
lsd.applyChild(nsd)
} else {
_, ok = cb.commitParent.sdeltas[addr]
if !ok {
cb.commitParent.sdeltas[addr] = make(map[storagePtr]*storageDelta)
}
cb.commitParent.sdeltas[addr][aapp] = nsd
}
}
}
cb.commitParent.mods.CompactCertNext = cb.mods.CompactCertNext
}
func (cb *roundCowState) modifiedAccounts() []basics.Address {
return cb.mods.Accts.ModifiedAccounts()
}