-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
62 lines (51 loc) · 1.58 KB
/
utils.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
package exported
import (
storetypes "github.com/evoblockchain/evochain/libs/cosmos-sdk/store/types"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
)
var kvGasConfig storetypes.GasConfig
func init() {
kvGasConfig = storetypes.KVGasConfig()
}
type SizerAccountKeeper interface {
GetEncodedAccountSize(acc Account) int
}
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) Account
}
func TryAddGetAccountGas(gasMeter sdk.GasMeter, ak SizerAccountKeeper, acc Account) (bool, sdk.Gas) {
if ak == nil || gasMeter == nil || acc == nil {
return false, 0
}
size := ak.GetEncodedAccountSize(acc)
if size == 0 {
return false, 0
}
gas := kvGasConfig.ReadCostFlat + storetypes.Gas(size)*kvGasConfig.ReadCostPerByte
gasMeter.ConsumeGas(gas, "x/bank/internal/keeper/keeper.BaseSendKeeper")
return true, gas
}
func GetAccountGas(ak SizerAccountKeeper, acc Account) (sdk.Gas, bool) {
if acc == nil || ak == nil {
return 0, false
}
size := ak.GetEncodedAccountSize(acc)
if size == 0 {
return 0, false
}
gas := kvGasConfig.ReadCostFlat + storetypes.Gas(size)*kvGasConfig.ReadCostPerByte
return gas, true
}
func GetAccountAndGas(ctx *sdk.Context, keeper AccountKeeper, addr sdk.AccAddress) (Account, sdk.Gas) {
gasMeter := ctx.GasMeter()
tmpGasMeter := sdk.GetReusableInfiniteGasMeter()
ctx.SetGasMeter(tmpGasMeter)
defer func() {
ctx.SetGasMeter(gasMeter)
sdk.ReturnInfiniteGasMeter(tmpGasMeter)
}()
acc := keeper.GetAccount(*ctx, addr)
gasUsed := tmpGasMeter.GasConsumed()
gasMeter.ConsumeGas(gasUsed, "get account")
return acc, gasUsed
}