Skip to content

Commit 5377b75

Browse files
committed
re-structure
1 parent b296565 commit 5377b75

File tree

9 files changed

+71
-67
lines changed

9 files changed

+71
-67
lines changed

precompiles/bank/bank.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ const (
3232
GasSupplyOf = 2_477
3333
)
3434

35-
var (
36-
_ vm.PrecompiledContract = &Precompile{}
37-
_ cmn.NativeExecutor = &Precompile{}
38-
)
35+
var _ vm.PrecompiledContract = &Precompile{}
3936

4037
var (
4138
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -70,7 +67,7 @@ func NewPrecompile(
7067
) *Precompile {
7168
// NOTE: we set an empty gas configuration to avoid extra gas costs
7269
// during the run execution
73-
p := &Precompile{
70+
return &Precompile{
7471
Precompile: cmn.Precompile{
7572
KvGasConfig: storetypes.GasConfig{},
7673
TransientKVGasConfig: storetypes.GasConfig{},
@@ -80,8 +77,6 @@ func NewPrecompile(
8077
bankKeeper: bankKeeper,
8178
erc20Keeper: erc20Keeper,
8279
}
83-
p.Executor = p
84-
return p
8580
}
8681

8782
// RequiredGas calculates the precompiled contract's base gas rate.
@@ -111,8 +106,14 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
111106
return 0
112107
}
113108

109+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
110+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
111+
return p.Execute(ctx, contract, readonly)
112+
})
113+
}
114+
114115
// Execute executes the precompiled contract bank query methods defined in the ABI.
115-
func (p Precompile) Execute(ctx sdk.Context, _ vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
116+
func (p Precompile) Execute(ctx sdk.Context, contract *vm.Contract, readOnly bool) ([]byte, error) {
116117
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
117118
if err != nil {
118119
return nil, err

precompiles/common/precompile.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ import (
1515
sdk "github.com/cosmos/cosmos-sdk/types"
1616
)
1717

18-
// NativeExecutor abstract the native execution logic of the stateful precompile, it's passed to the base `Precompile`
18+
// NativeAction abstract the native execution logic of the stateful precompile, it's passed to the base `Precompile`
1919
// struct, base `Precompile` struct will handle things the native context setup, gas management, panic recovery etc,
2020
// before and after the execution.
2121
//
2222
// It's usually implemented by the precompile itself.
23-
type NativeExecutor interface {
24-
Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error)
25-
}
23+
type NativeAction func(ctx sdk.Context) ([]byte, error)
2624

2725
// Precompile is the base struct for precompiles that requires to access cosmos native storage.
2826
type Precompile struct {
@@ -32,8 +30,6 @@ type Precompile struct {
3230

3331
// BalanceHandler is optional
3432
BalanceHandler *BalanceHandler
35-
36-
Executor NativeExecutor
3733
}
3834

3935
// RequiredGas calculates the base minimum required gas for a transaction or a query.
@@ -49,16 +45,16 @@ func (p Precompile) RequiredGas(input []byte, isTransaction bool) uint64 {
4945

5046
// Run prepare the native context to execute native action for stateful precompile,
5147
// it manages the snapshot and revert of the multi-store.
52-
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) ([]byte, error) {
53-
bz, err := p.run(evm, contract, readOnly)
48+
func (p Precompile) RunNativeAction(evm *vm.EVM, contract *vm.Contract, action NativeAction) ([]byte, error) {
49+
bz, err := p.runNativeAction(evm, contract, action)
5450
if err != nil {
5551
return ReturnRevertError(evm, err)
5652
}
5753

5854
return bz, nil
5955
}
6056

61-
func (p Precompile) run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error) {
57+
func (p Precompile) runNativeAction(evm *vm.EVM, contract *vm.Contract, action NativeAction) (bz []byte, err error) {
6258
stateDB, ok := evm.StateDB.(*statedb.StateDB)
6359
if !ok {
6460
return nil, errors.New(ErrNotRunInEvm)
@@ -104,7 +100,7 @@ func (p Precompile) run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
104100
p.BalanceHandler.BeforeBalanceChange(ctx)
105101
}
106102

107-
bz, err = p.Executor.Execute(ctx, stateDB, contract, readOnly)
103+
bz, err = action(ctx)
108104
if err != nil {
109105
return bz, err
110106
}

precompiles/distribution/distribution.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import (
1818
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
1919
)
2020

21-
var (
22-
_ vm.PrecompiledContract = &Precompile{}
23-
_ cmn.NativeExecutor = &Precompile{}
24-
)
21+
var _ vm.PrecompiledContract = &Precompile{}
2522

2623
var (
2724
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -61,7 +58,7 @@ func NewPrecompile(
6158
bankKeeper cmn.BankKeeper,
6259
addrCdc address.Codec,
6360
) *Precompile {
64-
p := &Precompile{
61+
return &Precompile{
6562
Precompile: cmn.Precompile{
6663
KvGasConfig: storetypes.KVGasConfig(),
6764
TransientKVGasConfig: storetypes.TransientGasConfig(),
@@ -75,8 +72,6 @@ func NewPrecompile(
7572
distributionQuerier: distributionQuerier,
7673
addrCdc: addrCdc,
7774
}
78-
p.Executor = p
79-
return p
8075
}
8176

8277
// RequiredGas calculates the precompiled contract's base gas rate.
@@ -99,6 +94,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9994
return p.Precompile.RequiredGas(input, p.IsTransaction(method))
10095
}
10196

97+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
98+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
99+
return p.Execute(ctx, evm.StateDB, contract, readonly)
100+
})
101+
}
102+
102103
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
103104
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
104105
if err != nil {

precompiles/erc20/erc20.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ func init() {
5454
}
5555
}
5656

57-
var (
58-
_ vm.PrecompiledContract = &Precompile{}
59-
_ cmn.NativeExecutor = &Precompile{}
60-
)
57+
var _ vm.PrecompiledContract = &Precompile{}
6158

6259
// Precompile defines the precompiled contract for ERC-20.
6360
type Precompile struct {
@@ -79,7 +76,7 @@ func NewPrecompile(
7976
erc20Keeper Erc20Keeper,
8077
transferKeeper ibcutils.TransferKeeper,
8178
) *Precompile {
82-
p := &Precompile{
79+
return &Precompile{
8380
Precompile: cmn.Precompile{
8481
KvGasConfig: storetypes.GasConfig{},
8582
TransientKVGasConfig: storetypes.GasConfig{},
@@ -92,8 +89,6 @@ func NewPrecompile(
9289
erc20Keeper: erc20Keeper,
9390
transferKeeper: transferKeeper,
9491
}
95-
p.Executor = p
96-
return p
9792
}
9893

9994
// RequiredGas calculates the contract gas used for the
@@ -138,6 +133,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
138133
}
139134
}
140135

136+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
137+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
138+
return p.Execute(ctx, evm.StateDB, contract, readonly)
139+
})
140+
}
141+
141142
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
142143
// ERC20 precompiles cannot receive funds because they are not managed by an
143144
// EOA and will not be possible to recover funds sent to an instance of

precompiles/gov/gov.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import (
1919
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
2020
)
2121

22-
var (
23-
_ vm.PrecompiledContract = &Precompile{}
24-
_ cmn.NativeExecutor = &Precompile{}
25-
)
22+
var _ vm.PrecompiledContract = &Precompile{}
2623

2724
var (
2825
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -60,7 +57,7 @@ func NewPrecompile(
6057
codec codec.Codec,
6158
addrCdc address.Codec,
6259
) *Precompile {
63-
p := &Precompile{
60+
return &Precompile{
6461
Precompile: cmn.Precompile{
6562
KvGasConfig: storetypes.KVGasConfig(),
6663
TransientKVGasConfig: storetypes.TransientGasConfig(),
@@ -73,8 +70,6 @@ func NewPrecompile(
7370
codec: codec,
7471
addrCdc: addrCdc,
7572
}
76-
p.Executor = p
77-
return p
7873
}
7974

8075
// RequiredGas calculates the precompiled contract's base gas rate.
@@ -94,6 +89,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9489
return p.Precompile.RequiredGas(input, p.IsTransaction(method))
9590
}
9691

92+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
93+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
94+
return p.Execute(ctx, evm.StateDB, contract, readonly)
95+
})
96+
}
97+
9798
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
9899
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
99100
if err != nil {

precompiles/ics20/ics20.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import (
1919
// PrecompileAddress of the ICS-20 EVM extension in hex format.
2020
const PrecompileAddress = "0x0000000000000000000000000000000000000802"
2121

22-
var (
23-
_ vm.PrecompiledContract = &Precompile{}
24-
_ cmn.NativeExecutor = &Precompile{}
25-
)
22+
var _ vm.PrecompiledContract = &Precompile{}
2623

2724
var (
2825
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -58,7 +55,7 @@ func NewPrecompile(
5855
transferKeeper cmn.TransferKeeper,
5956
channelKeeper cmn.ChannelKeeper,
6057
) *Precompile {
61-
p := &Precompile{
58+
return &Precompile{
6259
Precompile: cmn.Precompile{
6360
KvGasConfig: storetypes.KVGasConfig(),
6461
TransientKVGasConfig: storetypes.TransientGasConfig(),
@@ -71,8 +68,6 @@ func NewPrecompile(
7168
channelKeeper: channelKeeper,
7269
stakingKeeper: stakingKeeper,
7370
}
74-
p.Executor = p
75-
return p
7671
}
7772

7873
// RequiredGas calculates the precompiled contract's base gas rate.
@@ -93,6 +88,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9388
return p.Precompile.RequiredGas(input, p.IsTransaction(method))
9489
}
9590

91+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
92+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
93+
return p.Execute(ctx, evm.StateDB, contract, readonly)
94+
})
95+
}
96+
9697
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
9798
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
9899
if err != nil {

precompiles/slashing/slashing.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import (
2020
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
2121
)
2222

23-
var (
24-
_ vm.PrecompiledContract = &Precompile{}
25-
_ cmn.NativeExecutor = &Precompile{}
26-
)
23+
var _ vm.PrecompiledContract = &Precompile{}
2724

2825
var (
2926
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -60,7 +57,7 @@ func NewPrecompile(
6057
bankKeeper cmn.BankKeeper,
6158
valCdc, consCdc address.Codec,
6259
) *Precompile {
63-
p := &Precompile{
60+
return &Precompile{
6461
Precompile: cmn.Precompile{
6562
KvGasConfig: storetypes.KVGasConfig(),
6663
TransientKVGasConfig: storetypes.TransientGasConfig(),
@@ -73,8 +70,6 @@ func NewPrecompile(
7370
valCodec: valCdc,
7471
consCodec: consCdc,
7572
}
76-
p.Executor = p
77-
return p
7873
}
7974

8075
// RequiredGas calculates the precompiled contract's base gas rate.
@@ -94,6 +89,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9489
return p.Precompile.RequiredGas(input, p.IsTransaction(method))
9590
}
9691

92+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
93+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
94+
return p.Execute(ctx, evm.StateDB, contract, readonly)
95+
})
96+
}
97+
9798
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
9899
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
99100
if err != nil {

precompiles/staking/staking.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import (
1919
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2020
)
2121

22-
var (
23-
_ vm.PrecompiledContract = &Precompile{}
24-
_ cmn.NativeExecutor = &Precompile{}
25-
)
22+
var _ vm.PrecompiledContract = &Precompile{}
2623

2724
var (
2825
// Embed abi json file to the executable binary. Needed when importing as dependency.
@@ -60,7 +57,7 @@ func NewPrecompile(
6057
bankKeeper cmn.BankKeeper,
6158
addrCdc address.Codec,
6259
) *Precompile {
63-
p := &Precompile{
60+
return &Precompile{
6461
Precompile: cmn.Precompile{
6562
KvGasConfig: storetypes.KVGasConfig(),
6663
TransientKVGasConfig: storetypes.TransientGasConfig(),
@@ -73,8 +70,6 @@ func NewPrecompile(
7370
stakingQuerier: stakingQuerier,
7471
addrCdc: addrCdc,
7572
}
76-
p.Executor = p
77-
return p
7873
}
7974

8075
// RequiredGas returns the required bare minimum gas to execute the precompile.
@@ -95,6 +90,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9590
return p.Precompile.RequiredGas(input, p.IsTransaction(method))
9691
}
9792

93+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
94+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
95+
return p.Execute(ctx, evm.StateDB, contract, readonly)
96+
})
97+
}
98+
9899
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
99100
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
100101
if err != nil {

precompiles/werc20/werc20.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ func init() {
3434
}
3535
}
3636

37-
var (
38-
_ vm.PrecompiledContract = &Precompile{}
39-
_ cmn.NativeExecutor = &Precompile{}
40-
)
37+
var _ vm.PrecompiledContract = &Precompile{}
4138

4239
// Precompile defines the precompiled contract for WERC20.
4340
type Precompile struct {
@@ -65,11 +62,9 @@ func NewPrecompile(
6562
// use the IWERC20 ABI
6663
erc20Precompile.ABI = ABI
6764

68-
p := &Precompile{
65+
return &Precompile{
6966
Precompile: erc20Precompile,
7067
}
71-
p.Executor = p
72-
return p
7368
}
7469

7570
// RequiredGas calculates the contract gas use.
@@ -99,6 +94,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
9994
}
10095
}
10196

97+
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
98+
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
99+
return p.Execute(ctx, evm.StateDB, contract, readonly)
100+
})
101+
}
102+
102103
func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Contract, readOnly bool) ([]byte, error) {
103104
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
104105
if err != nil {

0 commit comments

Comments
 (0)