Skip to content

Commit 2a5324c

Browse files
Initial implementation of native kava transfer
1 parent da7973e commit 2a5324c

File tree

8 files changed

+29
-14
lines changed

8 files changed

+29
-14
lines changed

core/vm/contracts_stateful.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package vm
55

66
import (
7+
"math/big"
8+
79
"github.com/ethereum/go-ethereum/common"
810
"github.com/ethereum/go-ethereum/precompile/contract"
911
)
@@ -28,6 +30,7 @@ func (w *wrappedPrecompiledContract) Run(
2830
input []byte,
2931
suppliedGas uint64,
3032
readOnly bool,
33+
value *big.Int,
3134
) (ret []byte, remainingGas uint64, err error) {
3235
return RunPrecompiledContract(w.p, input, suppliedGas)
3336
}
@@ -41,6 +44,7 @@ func RunStatefulPrecompiledContract(
4144
input []byte,
4245
suppliedGas uint64,
4346
readOnly bool,
47+
value *big.Int,
4448
) (ret []byte, remainingGas uint64, err error) {
45-
return precompile.Run(accessibleState, caller, addr, input, suppliedGas, readOnly)
49+
return precompile.Run(accessibleState, caller, addr, input, suppliedGas, readOnly, value)
4650
}

core/vm/evm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
231231
}
232232

233233
if isPrecompile {
234-
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly)
234+
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly, value)
235235
} else {
236236
// Initialise a new contract and set the code that is to be used by the EVM.
237237
// The contract is a scoped environment for this execution context only.
@@ -294,7 +294,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
294294

295295
// It is allowed to call precompiles, even via delegatecall
296296
if p, isPrecompile := evm.precompile(addr); isPrecompile {
297-
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly)
297+
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly, value)
298298
} else {
299299
addrCopy := addr
300300
// Initialise a new contract and set the code that is to be used by the EVM.
@@ -335,7 +335,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
335335

336336
// It is allowed to call precompiles, even via delegatecall
337337
if p, isPrecompile := evm.precompile(addr); isPrecompile {
338-
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly)
338+
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, evm.interpreter.readOnly, big0)
339339
} else {
340340
addrCopy := addr
341341
// Initialise a new contract and make initialise the delegate values
@@ -387,7 +387,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
387387
// Explicitly pass readOnly as true for StaticCall OpCode.
388388
// Precompile implementation is responsible for honoring readOnly flag.
389389
// No state should be modified, return an error instead.
390-
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, true)
390+
ret, gas, err = RunStatefulPrecompiledContract(p, evm, caller.Address(), addr, input, gas, true, big0)
391391
} else {
392392
// At this point, we use a copy of address. If we don't, the go compiler will
393393
// leak the 'contract' to the outer scope, and make allocation for 'contract'

core/vm/precompile_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vm
22

33
import (
4+
"math/big"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -20,6 +21,7 @@ func (c *mockStatefulPrecompiledContract) Run(
2021
input []byte,
2122
suppliedGas uint64,
2223
readOnly bool,
24+
value *big.Int,
2325
) (ret []byte, remainingGas uint64, err error) {
2426
return []byte{}, 0, nil
2527
}

precompile/contract/contract.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package contract
55

66
import (
77
"fmt"
8+
"math/big"
89

910
"github.com/ethereum/go-ethereum/common"
1011
)
@@ -20,6 +21,7 @@ type RunStatefulPrecompileFunc func(
2021
input []byte,
2122
suppliedGas uint64,
2223
readOnly bool,
24+
value *big.Int,
2325
) (ret []byte, remainingGas uint64, err error)
2426

2527
// StatefulPrecompileFunction defines a function implemented by a stateful precompile
@@ -76,6 +78,7 @@ func (s *statefulPrecompileWithFunctionSelectors) Run(
7678
input []byte,
7779
suppliedGas uint64,
7880
readOnly bool,
81+
value *big.Int,
7982
) (ret []byte, remainingGas uint64, err error) {
8083
// Otherwise, an unexpected input size will result in an error.
8184
if len(input) < SelectorLen {
@@ -90,5 +93,5 @@ func (s *statefulPrecompileWithFunctionSelectors) Run(
9093
return nil, suppliedGas, fmt.Errorf("invalid function selector %#x", selector)
9194
}
9295

93-
return function.execute(accessibleState, caller, addr, functionInput, suppliedGas, readOnly)
96+
return function.execute(accessibleState, caller, addr, functionInput, suppliedGas, readOnly, value)
9497
}

precompile/contract/contract_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package contract_test
22

33
import (
44
"errors"
5+
"math/big"
56
"testing"
67

78
"github.com/ethereum/go-ethereum/common"
@@ -37,6 +38,7 @@ func test(
3738
input []byte,
3839
suppliedGas uint64,
3940
readOnly bool,
41+
value *big.Int,
4042
) (ret []byte, remainingGas uint64, err error) {
4143
return nil, 0, nil
4244
}
@@ -139,7 +141,7 @@ func TestPrecompileInvalidCalls(t *testing.T) {
139141
} {
140142
t.Run(tc.desc, func(t *testing.T) {
141143
{
142-
_, _, err := precompiledContract.Run(accessibleState, callerAddr, contractAddr, tc.input, suppliedGas, readOnly)
144+
_, _, err := precompiledContract.Run(accessibleState, callerAddr, contractAddr, tc.input, suppliedGas, readOnly, common.Big0)
143145
require.Error(t, err)
144146
require.Contains(t, err.Error(), tc.err)
145147
}
@@ -176,6 +178,7 @@ func TestPrecompileCustomStateDBExtension(t *testing.T) {
176178
input []byte,
177179
suppliedGas uint64,
178180
readOnly bool,
181+
value *big.Int,
179182
) ([]byte, uint64, error) {
180183
customStateDB, ok := accessibleState.GetStateDB().(*stateDBWithExt)
181184
if !ok {
@@ -198,14 +201,14 @@ func TestPrecompileCustomStateDBExtension(t *testing.T) {
198201
accessibleState := newAccessibleState(newStateDBWithExt(t, customVal))
199202

200203
// run and see the returned value from calling CustomValueExt() on the injected statedb
201-
retVal, remainingGas, err := precompiledContract.Run(accessibleState, callerAddr, contractAddr, funcSelector, 1, true)
204+
retVal, remainingGas, err := precompiledContract.Run(accessibleState, callerAddr, contractAddr, funcSelector, 1, true, common.Big0)
202205
require.NoError(t, err)
203206
assert.Equal(t, uint64(0), remainingGas)
204207
assert.Equal(t, customVal, retVal, "expected contract to access and return customVal from extended statedb")
205208

206209
// test contract with unsupported statedb
207210
accessibleState = newAccessibleState(state.NewTestStateDB(t))
208-
retVal, remainingGas, err = precompiledContract.Run(accessibleState, callerAddr, contractAddr, funcSelector, 1, true)
211+
retVal, remainingGas, err = precompiledContract.Run(accessibleState, callerAddr, contractAddr, funcSelector, 1, true, common.Big0)
209212
require.Error(t, err)
210213
assert.Equal(t, unsupportedErr, err)
211214
assert.Equal(t, uint64(1), remainingGas)

precompile/contract/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type StatefulPrecompiledContract interface {
2121
input []byte,
2222
suppliedGas uint64,
2323
readOnly bool,
24+
value *big.Int,
2425
) (ret []byte, remainingGas uint64, err error)
2526
}
2627

precompile/contracts/sum3/contract.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func calcSum3(
5858
input []byte,
5959
suppliedGas uint64,
6060
readOnly bool,
61+
value *big.Int,
6162
) (ret []byte, remainingGas uint64, err error) {
6263
if remainingGas, err = contract.DeductGas(suppliedGas, calcSum3GasCost); err != nil {
6364
return nil, 0, err
@@ -100,6 +101,7 @@ func getSum3(
100101
input []byte,
101102
suppliedGas uint64,
102103
readOnly bool,
104+
value *big.Int,
103105
) (ret []byte, remainingGas uint64, err error) {
104106
if remainingGas, err = contract.DeductGas(suppliedGas, getSum3GasCost); err != nil {
105107
return nil, 0, err

precompile/contracts/sum3/contract_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestCalcSum3GasCalculations(t *testing.T) {
8080
input, err := PackCalcSum3(tc.calcSum3Input)
8181
require.NoError(t, err)
8282

83-
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, tc.suppliedGas, readOnly)
83+
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, tc.suppliedGas, readOnly, common.Big0)
8484
if tc.err != "" {
8585
require.Error(t, err)
8686
require.Contains(t, err.Error(), tc.err)
@@ -131,7 +131,7 @@ func TestGetSum3GasCalculations(t *testing.T) {
131131
input, err := PackGetSum3()
132132
require.NoError(t, err)
133133

134-
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, tc.suppliedGas, readOnly)
134+
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, tc.suppliedGas, readOnly, common.Big0)
135135
if tc.err != "" {
136136
require.Error(t, err)
137137
require.Contains(t, err.Error(), tc.err)
@@ -185,7 +185,7 @@ func TestSum3Precompile(t *testing.T) {
185185
input, err := PackCalcSum3(tc.calcSum3Input)
186186
require.NoError(t, err)
187187

188-
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, calcSum3GasCost, readOnly)
188+
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, calcSum3GasCost, readOnly, common.Big0)
189189
require.NoError(t, err)
190190
require.Empty(t, ret)
191191
require.Equal(t, uint64(0), remainingGas)
@@ -196,7 +196,7 @@ func TestSum3Precompile(t *testing.T) {
196196
input, err := PackGetSum3()
197197
require.NoError(t, err)
198198

199-
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, getSum3GasCost, readOnly)
199+
ret, remainingGas, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, input, getSum3GasCost, readOnly, common.Big0)
200200
require.NoError(t, err)
201201

202202
sum, err := UnpackGetSum3Output(ret)
@@ -243,7 +243,7 @@ func TestSum3PrecompileInvalidCalls(t *testing.T) {
243243
} {
244244
t.Run(tc.desc, func(t *testing.T) {
245245
{
246-
_, _, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, tc.input, calcSum3GasCost, readOnly)
246+
_, _, err := Module.Contract.Run(accessibleState, callerAddr, contractAddr, tc.input, calcSum3GasCost, readOnly, common.Big0)
247247
require.Error(t, err)
248248
require.Contains(t, err.Error(), tc.err)
249249
}

0 commit comments

Comments
 (0)