From f4985f4076fc91b9622c17ea242f518c31633359 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 17:47:27 -0800 Subject: [PATCH 1/7] smart contract invocation --- node/node.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/node/node.go b/node/node.go index 010882f34..2b3a91e67 100644 --- a/node/node.go +++ b/node/node.go @@ -14,6 +14,7 @@ import ( "sync/atomic" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/harmony-one/harmony/blockchain" @@ -105,6 +106,7 @@ type Node struct { // Test only TestBankKeys []*ecdsa.PrivateKey + ContractKeys []*ecdsa.PrivateKey // The p2p host used to send/receive p2p messages host host.Host @@ -377,6 +379,14 @@ func (node *Node) GetSyncingPeers() []p2p.Peer { return res } +// func (node) +func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddress common.Address) { + var nonce uint64 + nonce = 0 + tx, _ := types.SignTx(types.NewTransaction(nonce, contractAddress, node.Consensus.ShardID, big.NewInt(7000000000000000000), params.TxGasContractCreation*10, nil, nil), types.HomesteadSigner{}, node.ContractKeys[0]) + node.pendingTransactionsAccount = append(node.pendingTransactionsAccount, tx) +} + // JoinShard helps a new node to join a shard. func (node *Node) JoinShard(leader p2p.Peer) { // try to join the shard, send ping message every 1 second, with a 10 minutes time-out From b92ef4b9ff3e51d0461ba57b29664060c582748f Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 18:23:21 -0800 Subject: [PATCH 2/7] adding contract data --- node/node.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/node/node.go b/node/node.go index 5840e768c..5bb85b42b 100644 --- a/node/node.go +++ b/node/node.go @@ -428,7 +428,13 @@ func (node *Node) GetSyncingPeers() []p2p.Peer { func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddress common.Address) { var nonce uint64 nonce = 0 - tx, _ := types.SignTx(types.NewTransaction(nonce, contractAddress, node.Consensus.ShardID, big.NewInt(7000000000000000000), params.TxGasContractCreation*10, nil, nil), types.HomesteadSigner{}, node.ContractKeys[0]) + callingFunction := "0x27c78c42000000000000000000000000" + contractData := callingFunction + walletAddress.Hex() + dataEnc := common.FromHex(contractData) + tx, _ := types.SignTx(types.NewTransaction(nonce, contractAddress, node.Consensus.ShardID, big.NewInt(7000000000000000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, node.ContractKeys[0]) + var txs types.Transactions + txs[0] = tx + node.addPendingTransactionsAccount(txs) node.pendingTransactionsAccount = append(node.pendingTransactionsAccount, tx) } From dfc8a13baa167e290ab8e5a31e69e2c7464ad768 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 18:26:07 -0800 Subject: [PATCH 3/7] fixing smart contracts adding --- node/node.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/node/node.go b/node/node.go index 5bb85b42b..c16afde4d 100644 --- a/node/node.go +++ b/node/node.go @@ -260,7 +260,10 @@ func (node *Node) AddSmartContractsToPendingTransactions() { dataEnc := common.FromHex(contractData) // Unsigned transaction to avoid the case of transaction address. mycontracttx, _ := types.SignTx(types.NewContractCreation(uint64(0), 0, big.NewInt(1000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, priKey) - node.pendingTransactionsAccount = append(node.pendingTransactionsAccount, mycontracttx) + var txs types.Transactions + txs[0] = mycontracttx + node.addPendingTransactionsAccount(txs) + } // New creates a new node. @@ -424,7 +427,7 @@ func (node *Node) GetSyncingPeers() []p2p.Peer { return res } -// func (node) +//CallFaucetContract invokes the faucet contract to give the walletAddress initial money func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddress common.Address) { var nonce uint64 nonce = 0 @@ -435,7 +438,6 @@ func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddre var txs types.Transactions txs[0] = tx node.addPendingTransactionsAccount(txs) - node.pendingTransactionsAccount = append(node.pendingTransactionsAccount, tx) } // JoinShard helps a new node to join a shard. From 23487d8c9e2fd03ac4b3382f27873ef9d64e6eb0 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 18:37:08 -0800 Subject: [PATCH 4/7] making transaction declaration inline --- node/node.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/node/node.go b/node/node.go index c16afde4d..569e2439b 100644 --- a/node/node.go +++ b/node/node.go @@ -260,9 +260,7 @@ func (node *Node) AddSmartContractsToPendingTransactions() { dataEnc := common.FromHex(contractData) // Unsigned transaction to avoid the case of transaction address. mycontracttx, _ := types.SignTx(types.NewContractCreation(uint64(0), 0, big.NewInt(1000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, priKey) - var txs types.Transactions - txs[0] = mycontracttx - node.addPendingTransactionsAccount(txs) + node.addPendingTransactionsAccount(types.Transactions{mycontracttx}) } @@ -435,9 +433,7 @@ func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddre contractData := callingFunction + walletAddress.Hex() dataEnc := common.FromHex(contractData) tx, _ := types.SignTx(types.NewTransaction(nonce, contractAddress, node.Consensus.ShardID, big.NewInt(7000000000000000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, node.ContractKeys[0]) - var txs types.Transactions - txs[0] = tx - node.addPendingTransactionsAccount(txs) + node.addPendingTransactionsAccount(types.Transactions{tx}) } // JoinShard helps a new node to join a shard. From 3d04f91a35a7f80c5f243ffef8d24abba8198a58 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 18:38:15 -0800 Subject: [PATCH 5/7] fix golint gofmt --- node/node.go | 1 - 1 file changed, 1 deletion(-) diff --git a/node/node.go b/node/node.go index 569e2439b..8c2f2cdf0 100644 --- a/node/node.go +++ b/node/node.go @@ -261,7 +261,6 @@ func (node *Node) AddSmartContractsToPendingTransactions() { // Unsigned transaction to avoid the case of transaction address. mycontracttx, _ := types.SignTx(types.NewContractCreation(uint64(0), 0, big.NewInt(1000000), params.TxGasContractCreation*10, nil, dataEnc), types.HomesteadSigner{}, priKey) node.addPendingTransactionsAccount(types.Transactions{mycontracttx}) - } // New creates a new node. From f905d075b5eb83e502525e1733764b077117632b Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 18:44:57 -0800 Subject: [PATCH 6/7] getting the correct nonce --- node/node.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/node/node.go b/node/node.go index 8c2f2cdf0..621737f79 100644 --- a/node/node.go +++ b/node/node.go @@ -426,8 +426,7 @@ func (node *Node) GetSyncingPeers() []p2p.Peer { //CallFaucetContract invokes the faucet contract to give the walletAddress initial money func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddress common.Address) { - var nonce uint64 - nonce = 0 + nonce := node.Worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(node.ContractKeys[0])) callingFunction := "0x27c78c42000000000000000000000000" contractData := callingFunction + walletAddress.Hex() dataEnc := common.FromHex(contractData) From 24cb7a83ee9f52f9d47e817042725041a409825b Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 12 Dec 2018 19:58:54 -0800 Subject: [PATCH 7/7] adding publickey --- node/node.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/node.go b/node/node.go index 621737f79..55b2476e8 100644 --- a/node/node.go +++ b/node/node.go @@ -426,7 +426,7 @@ func (node *Node) GetSyncingPeers() []p2p.Peer { //CallFaucetContract invokes the faucet contract to give the walletAddress initial money func (node *Node) CallFaucetContract(contractAddress common.Address, walletAddress common.Address) { - nonce := node.Worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(node.ContractKeys[0])) + nonce := node.Worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(node.ContractKeys[0].PublicKey)) callingFunction := "0x27c78c42000000000000000000000000" contractData := callingFunction + walletAddress.Hex() dataEnc := common.FromHex(contractData)