From a3f584fc2bf41de7b9d6c03dfd79f921e2c8d4a6 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 20 Sep 2020 00:51:32 -0400 Subject: get C-Chain internal asset transfer to work --- core/state/state_object.go | 9 ++++---- core/vm/evm.go | 53 +++++++++++++++++++++++----------------------- examples/mctx_gen/main.go | 13 ++++++------ 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 9772859..bbfff1d 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -94,7 +94,7 @@ type stateObject struct { // empty returns whether the account is considered empty. func (s *stateObject) empty() bool { - return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash) + return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash) && !s.data.IsMultiCoin } // Account is the Ethereum consensus representation of accounts. @@ -430,6 +430,7 @@ func (s *stateObject) SubBalanceMultiCoin(coinID common.Hash, amount *big.Int, d } func (s *stateObject) SetBalanceMultiCoin(coinID common.Hash, amount *big.Int, db Database) { + s.data.IsMultiCoin = true NormalizeCoinID(&coinID) s.SetState(db, coinID, common.BigToHash(amount)) } @@ -566,9 +567,9 @@ func (s *stateObject) BalanceMultiCoin(coinID common.Hash, db Database) *big.Int // return true //} -func (s *stateObject) IsMultiCoin() bool { - return s.data.IsMultiCoin -} +//func (s *stateObject) IsMultiCoin() bool { +// return s.data.IsMultiCoin +//} func (s *stateObject) Nonce() uint64 { return s.data.Nonce diff --git a/core/vm/evm.go b/core/vm/evm.go index e895211..54c9c7f 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -280,8 +280,7 @@ func (evm *EVM) CallExpert(caller ContractRef, addr common.Address, input []byte return nil, gas, ErrInsufficientBalance } - var to = AccountRef(addr) - mcerr := evm.Context.CanTransferMC(evm.StateDB, caller.Address(), to.Address(), coinID, value2) + mcerr := evm.Context.CanTransferMC(evm.StateDB, caller.Address(), addr, coinID, value2) if mcerr == 1 { return nil, gas, ErrInsufficientBalance } else if mcerr != 0 { @@ -289,17 +288,17 @@ func (evm *EVM) CallExpert(caller ContractRef, addr common.Address, input []byte } snapshot := evm.StateDB.Snapshot() - p, isPrecompile := evm.precompile(addr) + //p, isPrecompile := evm.precompile(addr) if !evm.StateDB.Exist(addr) { - if !isPrecompile && evm.chainRules.IsEIP158 && value.Sign() == 0 { - // Calling a non existing account, don't do anything, but ping the tracer - if evm.vmConfig.Debug && evm.depth == 0 { - evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value) - evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil) - } - return nil, gas, nil - } + //if !isPrecompile && evm.chainRules.IsEIP158 && value.Sign() == 0 { + // // Calling a non existing account, don't do anything, but ping the tracer + // if evm.vmConfig.Debug && evm.depth == 0 { + // evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value) + // evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil) + // } + // return nil, gas, nil + //} evm.StateDB.CreateAccount(addr) } evm.Transfer(evm.StateDB, caller.Address(), addr, value) @@ -313,24 +312,24 @@ func (evm *EVM) CallExpert(caller ContractRef, addr common.Address, input []byte }(gas, time.Now()) } - if isPrecompile { - ret, gas, err = RunPrecompiledContract(p, input, gas) + //if isPrecompile { + // ret, gas, err = RunPrecompiledContract(p, input, gas) + //} else { + // Initialise a new contract and set the code that is to be used by the EVM. + // The contract is a scoped environment for this execution context only. + code := evm.StateDB.GetCode(addr) + if len(code) == 0 { + ret, err = nil, nil // gas is unchanged } else { - // Initialise a new contract and set the code that is to be used by the EVM. - // The contract is a scoped environment for this execution context only. - code := evm.StateDB.GetCode(addr) - if len(code) == 0 { - ret, err = nil, nil // gas is unchanged - } else { - addrCopy := addr - // If the account has no code, we can abort here - // The depth-check is already done, and precompiles handled above - contract := NewContract(caller, AccountRef(addrCopy), value, gas) - contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), code) - ret, err = run(evm, contract, input, false) - gas = contract.Gas - } + addrCopy := addr + // If the account has no code, we can abort here + // The depth-check is already done, and precompiles handled above + contract := NewContract(caller, AccountRef(addrCopy), value, gas) + contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), code) + ret, err = run(evm, contract, input, false) + gas = contract.Gas } + //} // When an error was returned by the EVM or when setting the creation code // above we revert to the snapshot and consume any gas remaining. Additionally // when we're in homestead this also counts for code storage gas errors. diff --git a/examples/mctx_gen/main.go b/examples/mctx_gen/main.go index 7e0f715..b0baac9 100644 --- a/examples/mctx_gen/main.go +++ b/examples/mctx_gen/main.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "flag" "fmt" "math/big" @@ -15,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" ) func checkError(err error) { @@ -36,12 +36,13 @@ func main() { var fAssetID string flag.Int64Var(&fChainID, "chainid", 43112, "tx.chainId") flag.Uint64Var(&fNonce, "nonce", 0, "tx.nonce") - flag.Int64Var(&fChainID, "gasprice", 1000000000, "tx.gasPrice") - flag.Uint64Var(&fNonce, "gaslimit", 10000000, "tx.gasLimit") + flag.Int64Var(&fGasPrice, "gasprice", 470000000000, "tx.gasPrice") + flag.Uint64Var(&fGasLimit, "gaslimit", 21000, "tx.gasLimit") flag.Int64Var(&fAmount, "amount", 100, "tx.amount") flag.StringVar(&fKey, "key", "0x56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027", "private key (hex with \"0x\")") flag.StringVar(&fTo, "to", "0x1f0e5C64AFdf53175f78846f7125776E76FA8F34", "tx.to (hex with \"0x\")") flag.StringVar(&fAssetID, "assetid", "va6gCYWu3boo3NKQgzdwfzB73dmTYKZn2EZ7nz5pbDBXAsaj4", "assetID") + flag.Parse() _pkey, err := crypto.HexToECDSA(fKey[2:]) checkError(err) @@ -64,8 +65,6 @@ func main() { txJSON, err := signedTx.MarshalJSON() checkError(err) fmt.Printf("json: %s\n", string(txJSON)) - buff := new(bytes.Buffer) - err = signedTx.EncodeRLP(buff) - checkError(err) - fmt.Printf("hex: %s\n", hexutil.Encode(buff.Bytes())) + b, err := rlp.EncodeToBytes(signedTx) + fmt.Printf("hex: %s\n", hexutil.Encode(b)) } -- cgit v1.2.3-70-g09d2