简体   繁体   English

web3.js 1.0和Solidity之间的数据不匹配

[英]Data missmatch between web3.js 1.0 and Solidity

I'm mew to Ethereum/Solidity/Web3.js. 我正在以太坊/Solidity/Web3.js。 I'm trying to use web3.js web.eth.sendsendTransaction() method in order to run a function in a deployed contract on a private chain. 我试图使用web3.js web.eth.sendsendTransaction()方法来在私有链上的已部署合同中运行功能。

The function I try to execute is: 我尝试执行的功能是:

contract Matematicas{
    uint256 ultimaSuma;
    uint256 ultimaMultiplicacion;
    uint256 contador;
    uint256 factorA;
    uint256 factorB;
    uint256 sumandoA;
    uint256 sumandoB;
    bytes datosMensaje;...

    function multiplica(uint256 a, uint256 b) public{
        datosMensaje=msg.data;
        factorA=a;
        factorB=b;
        ultimaMultiplicacion=(a*b);
    }
... 
}

I call multiplica from Mist browser runnig the following JavaScript code: 我从Mist浏览器runnig调用以下JavaScript代码的multiplica:

var contracAddress="0xXXXXXXXX";
var contractABI=[{"constant":false,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"multiplica","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},...];
var functionABI=$.grep(contractABITercero,function(metodo,index){ return metodo.name=='multiplica';});
functionABI=abiDelaFuncion[0];
var abiByteCode= web3.eth.abi.encodeFunctionCall(functionABI,[document.getElementById('firstNumber').value,document.getElementById('secondNumber').value]);
var transactionObject={from:"0xxxxxxxxxx",to:contractAddress,data:abiByteCode, gas:10000000};
web3.eth.sendTransaction(transactionObject, function(error,hash){......});

If I set firstNumber=1000 and secondNumber=2000 then abiByteCodes happens to be: 如果我设置firstNumber = 1000和secondNumber = 2000,则abiByteCodes恰好是:

0x38e836df00000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000007d0 0x38e836df00000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000000000000000007d0

0x38e836df is the sha of the fuction signature, that is correct; 0x38e836df是功能签名的阴影,是正确的;

03e8 is hexadecimal for 1000(firstNumber) right 03e8是十六进制的1000(firstNumber)右

07d0 is hex for 2000(secondNumber) ok 07d0是2000(secondNumber)的十六进制

But data stored in the block chain is: 但是存储在区块链中的数据是:

datosMensaje: 0x38e836df0000000000000000000000000000000000000000000000000000000000 9e0 3e80000000000000000000000000000000000000000000000000000000000 9e0 7d0 datosMensaje:0x38e836df0000000000000000000000000000000000000000000000000000000000000000 9e0 3e80000000000000000000000000000000000000000000000000000000000000000000000 9e0 7d0

factorA: 8520680 (0x8203E8) factorA:8520680(0x8203E8)

factorB: 8521680 (0x8207D0) factorB:8521680(0x8207D0)

What am I doing wrong? 我究竟做错了什么?

I'm using geth 1.7.3 and Mist 0.9.2 on a Windows 10 64 bit desktop. 我在Windows 10 64位桌面上使用geth 1.7.3和Mist 0.9.2。

Thank you 谢谢

PS I know there are other ways to call contract functions like instantiating the contract via new web3.eth.Contract(contractABI,contractAddress) but I'm thinking about a project that would require to use the sendTransaction() method PS我知道还有其他方法可以调用合同功能,例如通过新的web3.eth.Contract(contractABI,contractAddress)实例化合同,但是我正在考虑一个需要使用sendTransaction()方法的项目

After countless hours I've realized this is due to an error in how remix IDE shows values in the debug tab. 经过无数小时后,我意识到这是由于remix IDE在debug选项卡中显示值的方式出现错误。 If I recover the data from the blockchain using web3.js ver 1.0 method getStorageAt(address,key) I get the expected values. 如果我使用web3.js版本1.0方法getStorageAt(address,key)从区块链中恢复数据,则将获得预期值。

Thank you Adam 谢谢亚当

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM