首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >松露试验通过bytes32

松露试验通过bytes32
EN

Ethereum用户
提问于 2018-02-04 04:35:28
回答 1查看 3.3K关注 0票数 3

我有一个简单的契约,如果输入小于9个字符/字节,它将记录一个错误。它可以很好地与混音,但不是在松露js测试和松露控制台。我把bytes32输入传递为"123456789“是不是做错了什么?

合同:

代码语言:javascript
复制
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测试:

代码语言:javascript
复制
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.");
    });

  });

});
EN

回答 1

Ethereum用户

发布于 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

票数 5
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/38476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档