blockchain实践错误及解决方法

Solidity编程错误

1.错误: VM error : Invalid opcode.

解决:可能是涉及assert的语句不满足;

2. str to bytes32 会变成0x 30 23 45 65 62 ....这种;

 见Solidity string, bytes, byte32

3. contracts可以接受bytes32[] 作为输入 eg., ["0x....", "0x....."]

4. VM error: revert revert the transaction to initial state.

解决:查看可能触发revert语句;可能是gas不足;

注意在msg.sender.transfer之前合约中要有ether, 之前调用过payable function,将msg.value转入合约。

5. storage赋值相当于指针指向一块内存,对象同时改变,永久存储在区块链。memeory只是创造副本,值传递。

 

web3和contract交互:

1. TypeError: web3.eth.Contract is not a constructor错误

解决:请检查你的web3.js版本:

如果version<1.0.0,使用: web3.eth.contract(studentFactoryArtifact,address);// 注意contract小写

如果version>1.0.0,使用:new web3.eth.Contract(studentFactoryArtifact,address); // 注意web3版本区别

 

2. MetaMask链接不上http://localhost:8545网络

解决: 启动geth时加上  --rpccorsdomain="moz-extension://dd70654e-21bd-4a65-ae05-12b15d937823" 这是插件在FireFox的ID

 

3.npm start启动不了,no such file ...package.json

解决:查看执行npm start的目录,是否有node_modules/和package.json文件,需要在它的安装目录下执行这个命令。

需要先npm install; 修改truffle-config.js文件;最后npm run dev.

 

4.注意$(window).load() 和 $(window).on('load', function{})后者才适用于jquery-3.3.1.min.js及以上版本

 

5.在JS文件中调用contract的方法报错 TypeError: t.forEach() is not a function

解决:涉及到数组操作,查看输入参数的类型是否是Array(eg. byte[]); 这种入参必须输入array: ["a", "b"].

 

6. 报错Metamask no conversation rate; gas limit must 2100;   (Metamask估计EstimateGas出错了;)

报错 ERR="already known"//"replacement transaction underpriced";  (和Nonce值有关;)

报错 "gas required exceeds allowance or always failing transaction".

解决:合理控制交易速率,每笔交易最好设置合理的参数{from:"", to:"", gas:} 不断增大gas,直到较快被确认。

具体解释见解决replacement transaction underpriced以太坊交易异常

 

 

 

 



 

 

 

 

 

你可能感兴趣的:(区块链)