我有一个简单的契约,如果输入小于9个字符/字节,它将记录一个错误。它可以很好地与混音,但不是在松露js测试和松露控制台。我把bytes32输入传递为"123456789“是不是做错了什么?
合同:
pragma solidity ^0.4.11;
contract TestLength {
event Error(string message, bytes32 input);
bytes32[] allInputs;
function test(bytes32 input) {
if (!validateLength(input, 9 , 9)) {
Error("minimum 9 characters", input);
}
allInputs.push(input);
}
function validateLength(bytes32 b, uint minLength, uint maxLength)
constant internal returns (bool) {
for (uint i = 0; i < 32; i++) {
if (i < minLength) {
if(b[i] == 0) return false;
}
if (i > maxLength - 1) {
if(b[i] != 0) return false;
}
}
return true;
}
}松露JS测试:
var TestLength = artifacts.require("./test/TestLength.sol");
contract('TestLength', function(accounts) {
it("should not throw an error when passing 9 characters", function() {
var testLength;
return TestLength.deployed().then(function(instance) {
testLength = instance;
return testLength.test("123456789",{from: accounts[0]});
}).then(function(instance) {
console.log("test: " + JSON.stringify(instance));
assert.equal(instance.logs.length,0,"no event should be triggered.");
});
});
});发布于 2018-02-04 05:50:45
问题在于混合和JS控制台在接受字符串参数方面有何不同。
解决方案是将输入从"123456789“改为web3(从The 8(”123456789“))。
来自also的web3也能工作。
使用web3版本1.0 (目前处于测试版)。这些功能已转移到"utils“项下。https://web3js.readthedocs.io/en/1.0/web3-utils.html?highlight=fromUtf8#asciitohex
https://ethereum.stackexchange.com/questions/38476
复制相似问题