Solidity

Solidity 合約狀態不會通過 nodejs 中的簽名交易更新

  • October 2, 2018

通過在 nodejs 中使用 web3 簽署交易

交易雜湊

0x3c7688326afc83e6524e8c491f203852e34aa0f572d952569b6253e98dbd83a1

數據

0xf9bcd5450000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000036162630000000000000000000000000000000000000000000000000000000000

收據

{"receipt":{"blockHash":"0x54f01d72226e3cd75d7b5c6952cac149dab47aabffbf5251d87f86613f223657","blockNumber":549010,"contractAddress":null,"cumulativeGasUsed":23384,"from":"0x0a889e242e6afaaaf51d9e40b2f089ab03889f97","gasUsed":23384,"logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","root":"0x4a0f79b6b74ce735aebfaa2e19ef066f500a6fa3051922fd193abe4219a158b8","to":"0x32ffcced3e2d7979cf7005fd8a9de681408f70dc","transactionHash":"0x3c7688326afc83e6524e8c491f203852e34aa0f572d952569b6253e98dbd83a1","transactionIndex":0}}

使用混音

交易雜湊

0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9

數據

0xf9bcd5450000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000036162630000000000000000000000000000000000000000000000000000000000

收據

receipt={"receipt":{"blockHash":"0x8e365df7f4d07510b69a9f540cefc39c47ec117e7cfdd5516c15b92abc1d7488","blockNumber":549012,"contractAddress":null,"cumulativeGasUsed":99095,"from":"0x0a889e242e6afaaaf51d9e40b2f089ab03889f97","gasUsed":99095,"logs":[{"address":"0x32fFcceD3e2d7979cf7005Fd8A9DE681408F70Dc","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000a889e242e6afaaaf51d9e40b2f089ab03889f97","0x0000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed"],"data":"0x000000000000000000000000000000000000000000000000000000000000012c","blockNumber":549012,"transactionHash":"0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9","transactionIndex":0,"blockHash":"0x8e365df7f4d07510b69a9f540cefc39c47ec117e7cfdd5516c15b92abc1d7488","logIndex":0,"removed":false,"id":"log_8e8a9b3e"}],"logsBloom":"0x00000000000000000000000000000000000080000000000000000000000000000000000000000400000000000000002000000000000000000000000000000000000000000000000000000008000000000000000000000000000000200000000000000000000000000000000004000000000000000000000000000010000008000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000","root":"0x156468c1bbde543ecae47841270c2037f4bc284df744e4a1ea7b5171bea1704e","to":"0x32ffcced3e2d7979cf7005fd8a9de681408f70dc","transactionHash":"0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9","transactionIndex":0}}

堅固性(功能)

function createUser(address _address, string _name) onlyOwner public {
        User memory newUser;// for temporary data
        newUser.id=_address;
        newUser.name=_name;

        //check if exists
        users[newUser.id]=newUser;
        _totalUser++;

        //Send initial ether to that public key
        balances[_address] = balances[_address].add(NEW_ACCOUNT_TOKEN);
        _totalSupply=_totalSupply.add(NEW_ACCOUNT_TOKEN);

        emit Transfer(owner,_address , _totalSupply);
   }

Nodejs + web3js

const contract = new web3.eth.Contract(abi, process.env.CONTRACT_ADDRESS);
const data = contract.methods.createUser('0x40c6f8cda775e5fc26c3f7782518f146a1fdf746', 'abc').encodeABI();
var privateKey = Buffer.from(process.env.WALLET_PRIVATE_KEY, 'hex');

web3.eth.estimateGas({
   to: process.env.WALLET_ADDRESS,
   data: data
}).then((estimateGas) => {
   log('estimateGas: ' + estimateGas);

   web3.eth.getTransactionCount(process.env.WALLET_ADDRESS).then((nonce) => {
       log('nonce:' + nonce); //
       var rawTx = {
           nonce: nonce,
           from: process.env.WALLET_ADDRESS,
           to: process.env.CONTRACT_ADDRESS,
           gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
           gas: estimateGas,
           data: data
       }

       var ethTx = new EthereumTx(rawTx);
       ethTx.sign(privateKey);

       var serializedTx = ethTx.serialize();

       web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
           .once('transactionHash', function (hash) {
               log('hash: ' + hash)
               res.json({
                   'status': true,
                   'msg': 'Successfully submitted',
                   'data': {
                       'tx': hash
                   }
               });
           })
           .once('receipt', function (receipt) {
               log('receipt for ' + receipt.transactionHash);
           })
           .on('error', function (error) {
               log(error);
           })
           .then(function (receipt) { // will be fired once the receipt is mined
               log(receipt.transactionHash + ' is mined'  );
               log('tx: ' + receipt.transactionHash + ' \n' + 'cumulativeGasUsed  : '  + receipt.cumulativeGasUsed
                   + ', blockNumber: ' + receipt.blockNumber +', blockHash:  '+ receipt.blockHash + ' \nlogs : ' + receipt.logs)
           });
   });
});

兩者都是通過相同的私鑰在同一個合約中進行的交易。在這兩種情況下,函式的輸入參數都是相同的,這就是為什麼兩種情況下的數據都是相同的。但是 Remix 事務正在更新內部狀態,而 web3(使用 nodejs)不是。web3js (nodejs)事務也缺少*事務日誌。*web3js+nodejs 的總使用者數沒有變化

可能的原因是什麼?

收到收據欄位後,狀態欄位未定義,即由於供應的氣體不足,交易未被探勘。終於在預設提供足夠的gas後問題解決了

var rawTx = {
           nonce: nonce,
           from: process.env.WALLET_ADDRESS,
           to: process.env.CONTRACT_ADDRESS,
           gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
           gas: 1500000,
           data: data
       }

引用自:https://ethereum.stackexchange.com/questions/59735