question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JS test hangs when solidity method is named 'toString'

See original GitHub issue
  • I’ve asked for help in the Truffle Gitter before filing this issue.

Issue

I have this utility function

library ByteUtils {
    function toString(bytes32[10] _data) internal pure returns (string) {
        bytes memory allBytes = new bytes(10 * 32);
        uint length;
        for (uint i = 0; i < 10; i++) {
            for (uint j = 0; j < 32; j++) {
                byte char = _data[i][j];
                if (char != 0) {
                    allBytes[length] = char;
                    length++;
                }
            }
        }

        bytes memory trimmedBytes = new bytes(length + 1);
        for (i = 0; i < length; i++) {
            trimmedBytes[i] = allBytes[i];
        }

        return string(trimmedBytes);
    }
}

that hangs when trying to test. I created a wrapper mock class to test it since it is internal:

contract ByteUtilsMock {
    function toString(bytes32[10] _data) public pure returns (string) {
        return ByteUtils.toString(_data);
    }
}

Here is my test class:

contract('ByteUtils', function(accounts) {
    const blockHeightManager = new BlockHeightManager(web3);

    let instance;

    beforeEach(blockHeightManager.snapshot);
    afterEach(blockHeightManager.revert);

    beforeEach(async function() {
        instance = await ByteUtilsMock.new();
    });

    describe('toString', async function() {
        it('should return the string', async function() {
            var test = ["Who will be the next president i", "n the 2020 election?"];
            assert.equal(await instance.toString(test), test.join(''), 'test 2 does not match');
        });
    });
});

It just stops at this test and nothing happens after that (and eventually times out). Please note I have another contract/tests that uses ByteUtils directly as a library and those tests pass with no issues.

Steps to Reproduce

Copy my utility function toString() and make a wrapper contract class. Then truffle test

Expected Behavior

Test should pass and not hang.

Actual Results

Doing truffle test all the other tests pass up to this test then it just hangs and stays there.

Environment

  • Operating System: Mac OSX 10.12.6
  • Truffle version: 4.0.1
  • Ethereum client: cpp-ethereum
  • node version: 8.8.0
  • npm version: 5.4.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dwalintukancommented, Jan 16, 2018

@Elaniobro I’ve tested the fix and it works so it should be closed. I didn’t create that one on gitcoin.co.

0reactions
Elaniobrocommented, Jan 16, 2018

Is this issue still open? I see it on gitcoin.co but its marked closed by @dwalintukan

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can you handle an expected throw in a contract test ...
Solidity implements throw by JUMPing to an invalid destination, so we catch the error and then look for the string "invalid JUMP" in...
Read more >
Mocha tests hang after completion when working with web3 ...
Someone pointed me that there is an --exit flag that you can pass to mocha and it kills the process after all tests...
Read more >
Working with Strings in Solidity - Coder's Errand
Initially, it shows only a line indicating the account that deployed the contract, the contract and method that was called, ie String.
Read more >
ConsenSys/truffle - Gitter
I've got a weird issue with my test hanging on this function: function toString(bytes32[10] _data) internal pure returns (string) { bytes memory allBytes ......
Read more >
web3.utils — web3.js 1.0.0 documentation - Read the Docs
Later, when we want to check if an element is in the set, we simply hash the element ... the sha3 of given...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found