From d627f3205f5df225b267cdd7e28fe0ba629612d4 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Thu, 6 Dec 2018 14:36:17 -0800 Subject: [PATCH 1/4] Add contract creation tx --- harmony/main.go | 44 ++++++++++++++++++++++++++++++++++++++++-- node/worker/worker.go | 5 +++++ transactions.rlp | Bin 0 -> 300 bytes 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/harmony/main.go b/harmony/main.go index 8fad3d413..e174136c5 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "github.com/ethereum/go-ethereum/common" "log" "math/big" @@ -83,7 +85,7 @@ func main() { } } - txs := make([]*types.Transaction, 100) + txs := make([]*types.Transaction, 10) worker := worker.New(params.TestChainConfig, chain, consensus.NewFaker(), crypto.PubkeyToAddress(testBankKey.PublicKey)) nonce := worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(testBankKey.PublicKey)) for i := range txs { @@ -93,5 +95,43 @@ func main() { txs[i] = tx } - worker.CommitTransactions(txs) + // Add a contract deployment transaction + contractData := "0x608060405234801561001057600080fd5b506040516020806104c08339810180604052602081101561003057600080fd5b505160008054600160a060020a0319163317808255600160a060020a031681526001602081905260409091205560ff811661006c600282610073565b50506100bd565b8154818355818111156100975760008381526020902061009791810190830161009c565b505050565b6100ba91905b808211156100b657600081556001016100a2565b5090565b90565b6103f4806100cc6000396000f3fe60806040526004361061005b577c010000000000000000000000000000000000000000000000000000000060003504635c19a95c8114610060578063609ff1bd146100955780639e7b8d61146100c0578063b3f98adc146100f3575b600080fd5b34801561006c57600080fd5b506100936004803603602081101561008357600080fd5b5035600160a060020a0316610120565b005b3480156100a157600080fd5b506100aa610280565b6040805160ff9092168252519081900360200190f35b3480156100cc57600080fd5b50610093600480360360208110156100e357600080fd5b5035600160a060020a03166102eb565b3480156100ff57600080fd5b506100936004803603602081101561011657600080fd5b503560ff16610348565b3360009081526001602081905260409091209081015460ff1615610144575061027d565b5b600160a060020a0382811660009081526001602081905260409091200154620100009004161580159061019c5750600160a060020a0382811660009081526001602081905260409091200154620100009004163314155b156101ce57600160a060020a039182166000908152600160208190526040909120015462010000900490911690610145565b600160a060020a0382163314156101e5575061027d565b6001818101805460ff1916821775ffffffffffffffffffffffffffffffffffffffff0000191662010000600160a060020a0386169081029190911790915560009081526020829052604090209081015460ff16156102725781546001820154600280549091610100900460ff1690811061025b57fe5b60009182526020909120018054909101905561027a565b815481540181555b50505b50565b600080805b60025460ff821610156102e6578160028260ff168154811015156102a557fe5b906000526020600020016000015411156102de576002805460ff83169081106102ca57fe5b906000526020600020016000015491508092505b600101610285565b505090565b600054600160a060020a0316331415806103215750600160a060020a0381166000908152600160208190526040909120015460ff165b1561032b5761027d565b600160a060020a0316600090815260016020819052604090912055565b3360009081526001602081905260409091209081015460ff1680610371575060025460ff831610155b1561037c575061027d565b6001818101805460ff191690911761ff00191661010060ff8516908102919091179091558154600280549192909181106103b257fe5b600091825260209091200180549091019055505056fea165627a7a72305820164189ef302b4648e01e22456b0a725191604cb63ee472f230ef6a2d17d702f900290000000000000000000000000000000000000000000000000000000000000002" + _ = contractData + dataEnc := common.FromHex(contractData) + + tx, _ := types.SignTx(types.NewContractCreation(nonce+uint64(10), 0, big.NewInt(1000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, testBankKey) + // Figure out the contract address which is determined by sender.address and nonce + contractAddress := crypto.CreateAddress(testBankAddress, nonce+uint64(10)) + + state := worker.GetCurrentState() + // Before the contract is deployed the code is empty + fmt.Println(state.GetCodeHash(contractAddress)) + + txs = append(txs, tx) + err := worker.CommitTransactions(txs) + if err != nil { + fmt.Println(err) + } + block, _ := worker.Commit() + _, err = chain.InsertChain(types.Blocks{block}) + if err != nil { + fmt.Println(err) + } + fmt.Println(contractAddress) + + receipts := worker.GetCurrentReceipts() + fmt.Println(receipts[len(receipts)-1].ContractAddress) + fmt.Println(receipts[len(receipts)-1]) + fmt.Println(state.GetNonce(testBankAddress)) + fmt.Println(state.GetNonce(contractAddress)) + fmt.Println(state.GetBalance(contractAddress)) + + //tx, _ = types.SignTx(types.NewTransaction(nonce+uint64(11), contractAddress, 0, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + // + //err = worker.CommitTransactions(types.Transactions{tx}) + //if err != nil { + // fmt.Println(err) + //} + fmt.Println(state.GetCodeHash(contractAddress)) } diff --git a/node/worker/worker.go b/node/worker/worker.go index c8825c186..bacbbb00b 100644 --- a/node/worker/worker.go +++ b/node/worker/worker.go @@ -134,6 +134,11 @@ func (w *Worker) GetCurrentState() *state.StateDB { return w.current.state } +// GetCurrentState ... +func (w *Worker) GetCurrentReceipts() []*types.Receipt { + return w.current.receipts +} + // Commit ... func (w *Worker) Commit() (*types.Block, error) { s := w.current.state.Copy() diff --git a/transactions.rlp b/transactions.rlp index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0bcf0ff4fdf8eacaf6cd1f742a738ea16c760947 100755 GIT binary patch literal 300 zcmey7)X>n-6vQzlOSoZGtH_CJ;n3SXryJUyWgcCt+{FB%L1w|Xl`~xg_vY0;TE5($ zk1^oB)_kRvK5S2RhU8p3_WbvL(*=dc9rq;`@0#Ly{@(4&b2t60d>PCee~njR%dU)l z=lJ$3|43paWQT+LgQ_mGzvfRqKl)yBp@V_l{?(VOxx9s+%su;>Z#)lQ;IFxuBP5h3 zv+yl; z#t;2}1qGYUuBDx9S&;T!#v Date: Mon, 10 Dec 2018 15:20:32 -0800 Subject: [PATCH 2/4] test smart contract calling; remove and ignore rlp journal --- .gitignore | 3 +++ harmony/main.go | 48 ++++++++++++++++++++++++++++++++++++++--------- transactions.rlp | Bin 300 -> 0 bytes 3 files changed, 42 insertions(+), 9 deletions(-) delete mode 100755 transactions.rlp diff --git a/.gitignore b/.gitignore index 951de7ccf..beb3dea19 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ cscope.* # local runtime logs tmp_log + +# RLP encoding +*.rlp \ No newline at end of file diff --git a/harmony/main.go b/harmony/main.go index e174136c5..55c60e591 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -20,7 +20,7 @@ var ( // Test accounts testBankKey, _ = crypto.GenerateKey() testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) - testBankFunds = big.NewInt(1000000000000000000) + testBankFunds = big.NewInt(8000000000000000000) testUserKey, _ = crypto.GenerateKey() testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey) @@ -96,11 +96,31 @@ func main() { } // Add a contract deployment transaction - contractData := "0x608060405234801561001057600080fd5b506040516020806104c08339810180604052602081101561003057600080fd5b505160008054600160a060020a0319163317808255600160a060020a031681526001602081905260409091205560ff811661006c600282610073565b50506100bd565b8154818355818111156100975760008381526020902061009791810190830161009c565b505050565b6100ba91905b808211156100b657600081556001016100a2565b5090565b90565b6103f4806100cc6000396000f3fe60806040526004361061005b577c010000000000000000000000000000000000000000000000000000000060003504635c19a95c8114610060578063609ff1bd146100955780639e7b8d61146100c0578063b3f98adc146100f3575b600080fd5b34801561006c57600080fd5b506100936004803603602081101561008357600080fd5b5035600160a060020a0316610120565b005b3480156100a157600080fd5b506100aa610280565b6040805160ff9092168252519081900360200190f35b3480156100cc57600080fd5b50610093600480360360208110156100e357600080fd5b5035600160a060020a03166102eb565b3480156100ff57600080fd5b506100936004803603602081101561011657600080fd5b503560ff16610348565b3360009081526001602081905260409091209081015460ff1615610144575061027d565b5b600160a060020a0382811660009081526001602081905260409091200154620100009004161580159061019c5750600160a060020a0382811660009081526001602081905260409091200154620100009004163314155b156101ce57600160a060020a039182166000908152600160208190526040909120015462010000900490911690610145565b600160a060020a0382163314156101e5575061027d565b6001818101805460ff1916821775ffffffffffffffffffffffffffffffffffffffff0000191662010000600160a060020a0386169081029190911790915560009081526020829052604090209081015460ff16156102725781546001820154600280549091610100900460ff1690811061025b57fe5b60009182526020909120018054909101905561027a565b815481540181555b50505b50565b600080805b60025460ff821610156102e6578160028260ff168154811015156102a557fe5b906000526020600020016000015411156102de576002805460ff83169081106102ca57fe5b906000526020600020016000015491508092505b600101610285565b505090565b600054600160a060020a0316331415806103215750600160a060020a0381166000908152600160208190526040909120015460ff165b1561032b5761027d565b600160a060020a0316600090815260016020819052604090912055565b3360009081526001602081905260409091209081015460ff1680610371575060025460ff831610155b1561037c575061027d565b6001818101805460ff191690911761ff00191661010060ff8516908102919091179091558154600280549192909181106103b257fe5b600091825260209091200180549091019055505056fea165627a7a72305820164189ef302b4648e01e22456b0a725191604cb63ee472f230ef6a2d17d702f900290000000000000000000000000000000000000000000000000000000000000002" + //pragma solidity >=0.4.22 <0.6.0; + // + //contract Faucet { + // mapping(address => bool) processed; + // uint quota = 0.5 ether; + // address owner; + // constructor() public payable { + // owner = msg.sender; + //} + // function request(address payable requestor) public { + // require(msg.sender == owner); + // require(quota <= address(this).balance); + // require(!processed[requestor]); + // processed[requestor] = true; + // requestor.transfer(quota); + //} + // function money() public view returns(uint) { + // return address(this).balance; + //} + //} + contractData := "0x60806040526706f05b59d3b2000060015560028054600160a060020a031916331790556101aa806100316000396000f3fe608060405260043610610045577c0100000000000000000000000000000000000000000000000000000000600035046327c78c42811461004a5780634ddd108a1461008c575b600080fd5b34801561005657600080fd5b5061008a6004803603602081101561006d57600080fd5b503573ffffffffffffffffffffffffffffffffffffffff166100b3565b005b34801561009857600080fd5b506100a1610179565b60408051918252519081900360200190f35b60025473ffffffffffffffffffffffffffffffffffffffff1633146100d757600080fd5b600154303110156100e757600080fd5b73ffffffffffffffffffffffffffffffffffffffff811660009081526020819052604090205460ff161561011a57600080fd5b73ffffffffffffffffffffffffffffffffffffffff8116600081815260208190526040808220805460ff1916600190811790915554905181156108fc0292818181858888f19350505050158015610175573d6000803e3d6000fd5b5050565b30319056fea165627a7a72305820fc2f7280f3590bd63f7b13a34411e0086c18e5a947b4437759ee79fbc566782f0029" _ = contractData dataEnc := common.FromHex(contractData) - tx, _ := types.SignTx(types.NewContractCreation(nonce+uint64(10), 0, big.NewInt(1000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, testBankKey) + tx, _ := types.SignTx(types.NewContractCreation(nonce+uint64(10), 0, big.NewInt(7000000000000000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, testBankKey) // Figure out the contract address which is determined by sender.address and nonce contractAddress := crypto.CreateAddress(testBankAddress, nonce+uint64(10)) @@ -126,12 +146,22 @@ func main() { fmt.Println(state.GetNonce(testBankAddress)) fmt.Println(state.GetNonce(contractAddress)) fmt.Println(state.GetBalance(contractAddress)) + fmt.Println(state.GetCodeHash(contractAddress)) - //tx, _ = types.SignTx(types.NewTransaction(nonce+uint64(11), contractAddress, 0, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) - // - //err = worker.CommitTransactions(types.Transactions{tx}) - //if err != nil { - // fmt.Println(err) - //} + + callData := "0x27c78c4200000000000000000000000024182601fe6e2e5da0b831496cc0489b7173b44f" + callEnc := common.FromHex(callData) + tx, _ = types.SignTx(types.NewTransaction(nonce+uint64(11), contractAddress, 0, big.NewInt(0), params.TxGasContractCreation*10, nil, callEnc), types.HomesteadSigner{}, testBankKey) + + err = worker.CommitTransactions(types.Transactions{tx}) + + if err != nil { + fmt.Println(err) + } + fmt.Println(receipts[len(receipts)-1].ContractAddress) + fmt.Println(receipts[len(receipts)-1]) + fmt.Println(state.GetNonce(testBankAddress)) + fmt.Println(state.GetNonce(contractAddress)) + fmt.Println(state.GetBalance(contractAddress)) fmt.Println(state.GetCodeHash(contractAddress)) } diff --git a/transactions.rlp b/transactions.rlp deleted file mode 100755 index 0bcf0ff4fdf8eacaf6cd1f742a738ea16c760947..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 300 zcmey7)X>n-6vQzlOSoZGtH_CJ;n3SXryJUyWgcCt+{FB%L1w|Xl`~xg_vY0;TE5($ zk1^oB)_kRvK5S2RhU8p3_WbvL(*=dc9rq;`@0#Ly{@(4&b2t60d>PCee~njR%dU)l z=lJ$3|43paWQT+LgQ_mGzvfRqKl)yBp@V_l{?(VOxx9s+%su;>Z#)lQ;IFxuBP5h3 zv+yl; z#t;2}1qGYUuBDx9S&;T!#v Date: Mon, 10 Dec 2018 15:29:50 -0800 Subject: [PATCH 3/4] Fix fmt --- harmony/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/harmony/main.go b/harmony/main.go index 55c60e591..6a7821874 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -148,7 +148,6 @@ func main() { fmt.Println(state.GetBalance(contractAddress)) fmt.Println(state.GetCodeHash(contractAddress)) - callData := "0x27c78c4200000000000000000000000024182601fe6e2e5da0b831496cc0489b7173b44f" callEnc := common.FromHex(callData) tx, _ = types.SignTx(types.NewTransaction(nonce+uint64(11), contractAddress, 0, big.NewInt(0), params.TxGasContractCreation*10, nil, callEnc), types.HomesteadSigner{}, testBankKey) From ce307dc75824355b953ab0ea2c597a9d07d7da66 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 10 Dec 2018 16:13:01 -0800 Subject: [PATCH 4/4] Fix golint error --- node/worker/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/worker/worker.go b/node/worker/worker.go index bacbbb00b..56981f1a1 100644 --- a/node/worker/worker.go +++ b/node/worker/worker.go @@ -134,7 +134,7 @@ func (w *Worker) GetCurrentState() *state.StateDB { return w.current.state } -// GetCurrentState ... +// GetCurrentReceipts ... func (w *Worker) GetCurrentReceipts() []*types.Receipt { return w.current.receipts }