mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Add get & increment nonce
This commit is contained in:
parent
073b488c74
commit
0f77f8b8a0
2 changed files with 52 additions and 1 deletions
|
|
@ -19,6 +19,8 @@ var funcs = map[string]stateManagerFunction{
|
|||
"getStorage(address,bytes32)": getStorage,
|
||||
"setStorage(address,bytes32,bytes32)": setStorage,
|
||||
"deployContract(address,bytes,bool,address)": deployContract,
|
||||
"getOvmContractNonce(address)": getOvmContractNonce,
|
||||
"incrementOvmContractNonce(address)": incrementOvmContractNonce,
|
||||
}
|
||||
var methodIds map[[4]byte]stateManagerFunction
|
||||
var executionMangerBytecode []byte
|
||||
|
|
@ -58,6 +60,21 @@ func getStorage(evm *EVM, contract *Contract, input []byte) (ret []byte, err err
|
|||
return val.Bytes(), nil
|
||||
}
|
||||
|
||||
func getOvmContractNonce(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) {
|
||||
address := common.BytesToAddress(input[4:36])
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, evm.StateDB.GetNonce(address))
|
||||
val := append(make([]byte, 24), b[:]...)
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func incrementOvmContractNonce(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) {
|
||||
address := common.BytesToAddress(input[4:36])
|
||||
oldNonce := evm.StateDB.GetNonce(address)
|
||||
evm.StateDB.SetNonce(address, oldNonce+1)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func deployContract(evm *EVM, contract *Contract, input []byte) (ret []byte, err error) {
|
||||
address := common.BytesToAddress(input[4:36])
|
||||
callerAddress := common.BytesToAddress(input[100:132])
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package tests
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
|
|
@ -67,10 +68,43 @@ func TestCreate(t *testing.T) {
|
|||
deployContractCalldata, _ := stateManagerAbi.Pack("deployContract", address, initCode, true, callerAddress)
|
||||
createdContractAddr, _ := call(t, state, vm.StateManagerAddress, deployContractCalldata)
|
||||
if !bytes.Equal(createdContractAddr, []byte{}) {
|
||||
t.Errorf("Expected %020x; got %020x", createdContractAddr, []byte{})
|
||||
t.Errorf("Expected %020x; got %020x", []byte{}, createdContractAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAndIncrementNonce(t *testing.T) {
|
||||
rawStateManagerAbi, _ := ioutil.ReadFile("./StateManagerABI.json")
|
||||
stateManagerAbi, _ := abi.JSON(strings.NewReader(string(rawStateManagerAbi)))
|
||||
state := newState()
|
||||
|
||||
address := common.HexToAddress("9999999999999999999999999999999999999999")
|
||||
|
||||
getNonceCalldata, _ := stateManagerAbi.Pack("getOvmContractNonce", address)
|
||||
incrementNonceCalldata, _ := stateManagerAbi.Pack("incrementOvmContractNonce", address)
|
||||
|
||||
getStorageReturnValue1, _ := call(t, state, vm.StateManagerAddress, getNonceCalldata)
|
||||
|
||||
expectedReturnValue1 := makeUint256WithUint64(0)
|
||||
if !bytes.Equal(getStorageReturnValue1, expectedReturnValue1) {
|
||||
t.Errorf("Expected %020x; got %020x", expectedReturnValue1, getStorageReturnValue1)
|
||||
}
|
||||
|
||||
call(t, state, vm.StateManagerAddress, incrementNonceCalldata)
|
||||
getStorageReturnValue2, _ := call(t, state, vm.StateManagerAddress, getNonceCalldata)
|
||||
|
||||
expectedReturnValue2 := makeUint256WithUint64(1)
|
||||
if !bytes.Equal(getStorageReturnValue2, expectedReturnValue2) {
|
||||
t.Errorf("Expected %020x; got %020x", expectedReturnValue2, getStorageReturnValue2)
|
||||
}
|
||||
}
|
||||
|
||||
func makeUint256WithUint64(num uint64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, num)
|
||||
val := append(make([]byte, 24), b[:]...)
|
||||
return val
|
||||
}
|
||||
|
||||
func newState() *state.StateDB {
|
||||
db := state.NewDatabase(rawdb.NewMemoryDatabase())
|
||||
state, _ := state.New(common.Hash{}, db, nil)
|
||||
|
|
|
|||
Loading…
Reference in a new issue