/
keeper.go
417 lines (368 loc) · 12.4 KB
/
keeper.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Package keeper specifies the keeper for the cvm module.
package keeper
import (
"bytes"
gobin "encoding/binary"
"math/big"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/engine"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/execution/wasm"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/txs/payload"
"github.com/shentufoundation/shentu/v2/vm"
"github.com/shentufoundation/shentu/v2/x/cvm/types"
)
// TransactionGasLimit is the gas limit of the block.
const TransactionGasLimit = uint64(5000000)
// Keeper implements SDK Keeper.
type Keeper struct {
cdc codec.BinaryCodec
key sdk.StoreKey
ak types.AccountKeeper
bk types.BankKeeper
dk types.DistributionKeeper
ck types.CertKeeper
sk types.StakingKeeper
paramSpace types.ParamSubspace
}
// NewKeeper creates a new instance of the CVM keeper.
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
dk types.DistributionKeeper, ck types.CertKeeper, sk types.StakingKeeper, paramSpace types.ParamSubspace) Keeper {
return Keeper{
cdc: cdc,
key: key,
ak: ak,
bk: bk,
dk: dk,
ck: ck,
sk: sk,
paramSpace: paramSpace,
}
}
func (k Keeper) Deploy(ctx sdk.Context, msg *types.MsgDeploy) ([]byte, error) {
callerAddr, err := sdk.AccAddressFromBech32(msg.Caller)
if err != nil {
return []byte{}, err
}
res, err := k.Tx(ctx, callerAddr, nil, msg.Value, msg.Code, msg.Meta, false, msg.IsEWASM, msg.IsRuntime)
if err != nil {
return []byte{}, err
}
return res, nil
}
func (k Keeper) Call(ctx sdk.Context, msg *types.MsgCall, view bool) ([]byte, error) {
callerAddr, err := sdk.AccAddressFromBech32(msg.Caller)
if err != nil {
return []byte{}, err
}
calleeAddr, err := sdk.AccAddressFromBech32(msg.Callee)
if err != nil {
return []byte{}, err
}
return k.Tx(ctx, callerAddr, calleeAddr, msg.Value, msg.Data, []*payload.ContractMeta{}, view, false, false)
}
// Call executes the CVM call from caller to callee with the given data and gas limit.
func (k Keeper) Tx(ctx sdk.Context, caller, callee sdk.AccAddress, value uint64, data []byte, payloadMeta []*payload.ContractMeta,
view, isEWASM, isRuntime bool) ([]byte, error) {
state := k.NewState(ctx)
callframe := engine.NewCallFrame(state, acmstate.Named("TxCache"))
callerAddr := crypto.MustAddressFromBytes(caller)
cache := callframe.Cache
var sequenceBytes []byte
if view {
callframe.ReadOnly()
sequenceBytes = make([]byte, 1)
} else {
sequenceBytes = k.GetAccountSeqNum(ctx, caller)
}
var calleeAddr crypto.Address
var code acm.Bytecode
var err error
var input []byte
if callee == nil {
calleeAddr = crypto.NewContractAddress(callerAddr, sequenceBytes)
if err = engine.CreateAccount(cache, calleeAddr); err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
code = data
} else {
input = data
calleeAddr, code, isEWASM, err = getCallee(callee, cache)
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
if len(code) == 0 && !bytes.Equal(data, []byte{}) {
return nil, types.ErrCodedError(errors.Codes.CodeOutOfBounds)
}
}
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
gasRate := k.GetGasRate(ctx)
originalGas, err := k.getOriginalGas(ctx, gasRate)
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
gasTracker := originalGas
callParams := engine.CallParams{
Origin: callerAddr,
Caller: callerAddr,
Callee: calleeAddr,
Input: input,
Value: *big.NewInt(int64(value)),
Gas: big.NewInt(int64(gasTracker)),
}
cc := CertificateCallable{
ctx: ctx,
certKeeper: k.ck,
}
options := registerCVMNative(cc, sequenceBytes)
newCVM := vm.NewCVM(options)
bc := NewBlockChain(ctx, k)
var ret []byte
if isEWASM {
if isRuntime {
ret = code
} else {
wvm := wasm.New(options)
ret, err = wvm.Execute(cache, bc, NewEventSink(ctx), callParams, code)
}
} else {
ret, err = newCVM.Execute(cache, bc, NewEventSink(ctx), callParams, code)
}
// Refund cannot exceed half of the total gas cost.
// Only refund when there is no error.
if err != nil {
gasTracker += vm.Min((originalGas-gasTracker)/2, newCVM.GetRefund())
}
// GasTracker is guaranteed to not underflow during CVM execution.
fee := originalGas - gasTracker
ctx.GasMeter().ConsumeGas((fee+gasRate-1)/gasRate, "CVM execution fee")
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
if callee == nil {
if isEWASM {
err = engine.InitWASMCode(cache, calleeAddr, ret)
} else {
err = engine.InitEVMCode(cache, calleeAddr, ret)
}
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
err = engine.UpdateContractMeta(cache, state, calleeAddr, payloadMeta)
if err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
ret = calleeAddr.Bytes()
}
if err = cache.Sync(state); err != nil {
return nil, types.ErrCodedError(errors.GetCode(err))
}
return ret, nil
}
// Send executes the send transaction from caller to callee with the given amount of tokens.
func (k Keeper) Send(ctx sdk.Context, caller, callee sdk.AccAddress, coins sdk.Coins) error {
value := coins.AmountOf(k.sk.BondDenom(ctx)).Uint64()
if value <= 0 {
return sdkerrors.ErrInvalidCoins
}
_, err := k.Tx(ctx, caller, callee, value, nil, nil, false, false, false)
return err
}
// GetCode returns the code at the given account address.
func (k Keeper) GetCode(ctx sdk.Context, addr crypto.Address) ([]byte, error) {
state := k.NewState(ctx)
acc, err := state.GetAccount(addr)
if err != nil {
return nil, err
}
if acc == nil {
return nil, nil
}
if len(acc.EVMCode) == 0 {
return acc.WASMCode, nil
}
return acc.EVMCode, nil
}
// getCallee returns the callee address and bytecode of a given account address.
func getCallee(callee sdk.AccAddress, cache *acmstate.Cache) (crypto.Address, acm.Bytecode, bool, error) {
calleeAddr := crypto.MustAddressFromBytes(callee)
acc, err := cache.GetAccount(calleeAddr)
if err != nil {
return crypto.Address{}, nil, false, err
}
if len(acc.WASMCode) != 0 {
return calleeAddr, acc.WASMCode, true, err
}
return calleeAddr, acc.EVMCode, false, err
}
// getOriginalGas returns the original gas cost.
func (k Keeper) getOriginalGas(ctx sdk.Context, gasRate uint64) (uint64, error) {
gasCurrent := ctx.GasMeter().Limit() - ctx.GasMeter().GasConsumed()
originalGas := gasCurrent * gasRate
if originalGas < gasCurrent {
return 0, types.ErrCodedError(errors.Codes.IntegerOverflow)
}
originalGas = vm.Min(originalGas, TransactionGasLimit)
return originalGas, nil
}
// getAccount returns the account at a given address.
func (k Keeper) getAccount(ctx sdk.Context, address crypto.Address) *acm.Account {
account, _ := k.NewState(ctx).GetAccount(address)
return account
}
// GetStorage returns the value stored given the address and key.
func (k Keeper) GetStorage(ctx sdk.Context, address crypto.Address, key binary.Word256) ([]byte, error) {
return k.NewState(ctx).GetStorage(address, key)
}
// SetAbi stores the abi for the address.
func (k Keeper) SetAbi(ctx sdk.Context, address crypto.Address, abi []byte) {
ctx.KVStore(k.key).Set(types.AbiStoreKey(address), abi)
}
// GetAbi returns the abi at the given address.
func (k Keeper) GetAbi(ctx sdk.Context, address crypto.Address) []byte {
return ctx.KVStore(k.key).Get(types.AbiStoreKey(address))
}
// getAddrMeta returns the meta hash of an address.
func (k Keeper) getAddrMeta(ctx sdk.Context, address crypto.Address) ([]*acm.ContractMeta, error) {
state := k.NewState(ctx)
return state.GetAddressMeta(address)
}
// getMeta returns the meta data of a meta hash.
func (k Keeper) getMeta(ctx sdk.Context, metaHash acmstate.MetadataHash) (string, error) {
state := k.NewState(ctx)
return state.GetMetadata(metaHash)
}
// StoreLastBlockHash stores the last block hash.
func (k Keeper) StoreLastBlockHash(ctx sdk.Context) {
if ctx.BlockHeight() == 0 {
return
}
block := ctx.BlockHeader().LastBlockId
hash := block.GetHash()
if hash == nil {
return
}
ctx.KVStore(k.key).Set(types.BlockHashStoreKey(ctx.BlockHeight()), hash)
}
type logger struct {
log.Logger
}
// Log implements github.com/go-kit/kit/log.Logger.
func (l *logger) Log(keyvals ...interface{}) error {
l.Info("CVM", keyvals...)
return nil
}
// WrapLogger converts a Tendermint logger into Burrow logger.
func WrapLogger(l log.Logger) *logging.Logger {
return logging.NewLogger(&logger{l})
}
// GetAccountSeqNum returns the account sequence number.
func (k Keeper) GetAccountSeqNum(ctx sdk.Context, address sdk.AccAddress) []byte {
callerAcc := k.ak.GetAccount(ctx, address)
callerSequence := callerAcc.GetSequence()
accountByte := make([]byte, 8)
gobin.LittleEndian.PutUint64(accountByte, callerSequence)
return accountByte
}
// GetAllContracts gets all contracts for genesis export.
func (k Keeper) GetAllContracts(ctx sdk.Context) []types.Contract {
contracts := make([]types.Contract, 0)
store := ctx.KVStore(k.key)
contractIterator := sdk.KVStorePrefixIterator(store, types.CodeStoreKeyPrefix)
defer contractIterator.Close()
for ; contractIterator.Valid(); contractIterator.Next() {
addressBytes := contractIterator.Key()[len(types.CodeStoreKeyPrefix):]
address, err := crypto.AddressFromBytes(addressBytes)
if err != nil {
panic(err)
}
var code types.CVMCode
k.cdc.MustUnmarshal(contractIterator.Value(), &code)
abi := k.GetAbi(ctx, address)
addrMeta, err := k.getAddrMeta(ctx, address)
var meta []types.ContractMeta
for _, adm := range addrMeta {
meta = append(meta, types.ContractMeta{CodeHash: adm.CodeHash, MetadataHash: adm.MetadataHash})
}
if err != nil {
panic(err)
}
storeIterator := sdk.KVStorePrefixIterator(store, append(types.StorageStoreKeyPrefix, addressBytes...))
prefixAddrLen := len(append(types.StorageStoreKeyPrefix, addressBytes...))
var storage []types.Storage
for ; storeIterator.Valid(); storeIterator.Next() {
keyBytes := storeIterator.Key()[prefixAddrLen:]
var key binary.Word256
copy(key[:], keyBytes)
storage = append(storage, types.Storage{Key: key, Value: storeIterator.Value()})
}
storeIterator.Close()
contracts = append(contracts, types.Contract{
Address: address,
Code: code,
Storage: storage,
Abi: abi,
Meta: meta,
})
}
return contracts
}
// GetAllMetas gets all metadata for genesis export.
func (k Keeper) GetAllMetas(ctx sdk.Context) []types.Metadata {
contracts := make([]types.Metadata, 0)
store := ctx.KVStore(k.key)
iterator := sdk.KVStorePrefixIterator(store, types.MetaHashStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
metahashBytes := iterator.Key()[len(types.MetaHashStoreKeyPrefix):]
var metahash acmstate.MetadataHash
copy(metahash[:], metahashBytes)
meta, err := k.getMeta(ctx, metahash)
if err != nil {
panic(err)
}
contracts = append(contracts, types.Metadata{Hash: metahash.Bytes(), Metadata: meta})
}
return contracts
}
// AuthKeeper returns keeper's AccountKeeper.
func (k Keeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI {
return k.ak.GetAccount(ctx, addr)
}
// RegisterGlobalPermissionAcc registers the zero address as the global permission account.
func RegisterGlobalPermissionAcc(ctx sdk.Context, k Keeper) {
state := k.NewState(ctx)
gpacc := &acm.Account{
Address: acm.GlobalPermissionsAddress,
Balance: 0,
Permissions: permission.DefaultAccountPermissions,
}
gpacc.Permissions.Base.SetBit = permission.AllPermFlags
err := state.UpdateAccount(gpacc)
if err != nil {
panic(err)
}
}
// SetGasRate sets parameters subspace for gas rate.
func (k Keeper) SetGasRate(ctx sdk.Context, gasRate uint64) {
k.paramSpace.Set(ctx, types.ParamStoreKeyGasRate, &gasRate)
}
// GetGasRate returns the gas rate in parameters subspace.
func (k *Keeper) GetGasRate(ctx sdk.Context) uint64 {
var gasRate uint64
k.paramSpace.Get(ctx, types.ParamStoreKeyGasRate, &gasRate)
return gasRate
}