Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

How to debug the "truffle test transactions" #1050

Closed
carbonnetwork opened this issue Jun 21, 2018 · 12 comments
Closed

How to debug the "truffle test transactions" #1050

carbonnetwork opened this issue Jun 21, 2018 · 12 comments

Comments

@carbonnetwork
Copy link

carbonnetwork commented Jun 21, 2018

My test contract imported truffle/Assert.sol & truffle/DeployedAddresses.sol.

  1. When I compile used "truffle compile" , I got error message: "Error: Could not find truffle/Assert.sol from any sources;.
  2. Then I used truffle test run it, it can be compiled well, but throw exception:Error: VM Exception while processing transaction: revert.
  3. I found the the transaction id and run truffle debug <transaction id> , and it gave error message "Error: Could not find truffle/Assert.sol from any sources; too.
    What can I do for debug the test transaction.
@cgewecke
Copy link
Contributor

@carbonnetwork Are you using VSCode? This error happens quite a bit with that IDE - there's discussion and (possibly) a workaround discussed at #756.

@carbonnetwork
Copy link
Author

@cgewecke My OS is macOs high Sierra, and I used sublime text and terminal.

@cgewecke
Copy link
Contributor

@carbonnetwork Can you show the file that's triggering this so I can try to reproduce?

@carbonnetwork
Copy link
Author

@cgewecke Yes. The No. 2 problem above have solved, but "import problem" not. The codes:

// contracts/ERC20.sol
pragma solidity ^0.4.23;

contract ERC20 {

  uint256 public _totalSupply;
  
  function totalSupply() public constant returns (uint256 total);
  
  function balanceOf(address _owner) public constant returns (uint256 balance);

  function transfer(address _to, uint256 _value) public returns (bool success);

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);

  function approve(address _spender, uint256 _value) public returns (bool success);

  function allowance(address _owner, address _spender) public constant returns (uint256 remaining);

  event Transfer(address indexed _from, address indexed _to, uint256 _value);

  event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
// contracts/SafeMathLib.sol
pragma solidity ^0.4.23;

library SafeMathLib {

  function add(uint256 a, uint256 b) public pure returns (uint256) {
    uint256 c = a + b;
    assert(c>=a && c>=b);
    return c;
  }

  function sub(uint256 a, uint256 b) public pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function mul(uint256 a, uint256 b) public pure returns (uint256) {
    uint256 c = a * b;
    assert(a == 0 || c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) public pure returns (uint256) {
    uint256 c = a / b;
    return c;
  }
}
//contracts/StandardToken.sol
pragma solidity ^0.4.23;

import "./ERC20.sol";
import "./SafeMathLib.sol";
contract StandardToken is ERC20 {

	using SafeMathLib for uint256;

	address public owner;

	mapping(address => uint) balances;

	mapping (address => mapping (address => uint)) allowed;

	constructor() public {
		owner = msg.sender;
		_totalSupply = 1000000 * 10**uint256(18);
                balances[owner] = _totalSupply;
	}
	
	/*
	 *fix short address bug
	 */
	modifier checkAddressSize(uint size) {
		uint len = msg.data.length;
	    if(len < size + 4){
	    	revert();
	    } 
	    _;
	}

	function totalSupply() public constant returns (uint256 total){
		return _totalSupply;
	}

	function balanceOf(address _owner) public constant returns (uint256 balance) {
	    return balances[_owner];
	}

	function transfer(address _to, uint256 _value) public checkAddressSize(2 * 32) returns (bool success){
	    balances[msg.sender] = balances[msg.sender].sub(_value);
	    balances[_to] = balances[_to].add(_value);

	    emit Transfer(msg.sender, _to, _value);
	    return true;
	}

	function transferFrom(address _from, address _to, uint256 _value) public checkAddressSize(3 * 32) returns (bool success) {
	    uint _allowance = allowed[_from][msg.sender];

	    balances[_to] = balances[_to].add(_value);
	    balances[_from] = balances[_from].sub(_value);
	    allowed[_from][msg.sender] = _allowance.sub(_value);

	    emit Transfer(_from, _to, _value);
	    return true;
	}

	function approve(address _spender, uint256 _value) public returns (bool success) {
	    require ((_value != 0) && (allowed[msg.sender][_spender] != 0));

	    allowed[msg.sender][_spender] = _value;

	    emit Approval(msg.sender, _spender, _value);
	    return true;
	}

	function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
	    return allowed[_owner][_spender];
	}
}

// contracts/test/TestStrandardToken.sol
pragma solidity ^0.4.23;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../StandardToken.sol";

contract TestStandardToken {
	StandardToken token;

	function beforeEach() public {
		token = StandardToken(DeployedAddresses.StandardToken());
	}

	function testTransfer () public {
		address x = 0x15373C2C51410d160e395e00a40eF4E36843a2B8;
		token.transfer(x, 1000);
		Assert.equal(token.balanceOf(x), 1000, "The balance is wrong!");
	}
}
// test/standard_token.test.js
var StandardToken = artifacts.require("./StandardToken.sol");

contract('StandardToken', function(accounts) {

  it("should put 1 StandardToken in the fourth account", function() {
    return StandardToken.deployed().then(function(instance) {
      return instance.transfer(accounts[3], 1000 );
    }).then(function(balance) {
      console.log(balance.valueOf());
      assert.equal(balance.valueOf(), 1000, "1 wasn't in the fourth account");
    });
  });
});

The result:

$ truffle compile
Error: Could not find truffle/Assert.sol from any sources; imported from /Users/iclick/blockchain/ethereum/carbon-ico/contracts/test/TestStandardToken.sol
at /usr/local/lib/node_modules/truffle/build/webpack://truffle-resolver/index.js:76:1
at /usr/local/lib/node_modules/truffle/build/webpack:/
/truffle-resolver//async/internal/onlyOnce.js:12:1
at next (/usr/local/lib/node_modules/truffle/build/webpack:/
/truffle-resolver/~/async/whilst.js:68:1)


$ truffle debug 0x8b62c6156a88196128cca9a23c9f4aa648fb7f1b3f601327c650568225f11b67
Error: Could not find truffle/Assert.sol from any sources; imported from /Users/iclick/blockchain/ethereum/carbon-ico/contracts/test/TestStandardToken.sol
at /usr/local/lib/node_modules/truffle/build/webpack://truffle-resolver/index.js:76:1
at /usr/local/lib/node_modules/truffle/build/webpack:/
/truffle-resolver//async/internal/onlyOnce.js:12:1
at next (/usr/local/lib/node_modules/truffle/build/webpack:/
/truffle-resolver/~/async/whilst.js:68:1)

@cgewecke
Copy link
Contributor

@carbonnetwork I think the issue is this:

// contracts/test/TestStrandardToken.sol

Truffle expects Solidity tests to be in the outer test directory alongside the JS tests. Could you try moving that file there? There's an example Solidity test in the metacoin-box here for reference.

@carbonnetwork
Copy link
Author

carbonnetwork commented Jun 22, 2018

@cgewecke Thanks. It worked, but the test contract throw exception:"Error: VM Exception while processing transaction: revert". Then I debug it used "truffle debug" command , got exception:

Error: Error: Unknown transaction 0xb61be753053a543396769d978874573cca7d82234af26896adf72aaeda3f2ac4
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:22360:23
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:22547:18
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:87487:23
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:85776:15
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:85541:19
    at /usr/local/lib/node_modules/ganache-cli/build/cli.node.js:87686:21
    at ReadFileContext.callback (/usr/local/lib/node_modules/ganache-cli/build/cli.node.js:87583:14)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-debugger/dist/debugger.js:6148:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
    at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
    at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
    at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
truffle(development)> TypeError: Cannot read property 'view' of undefined
    at printAddressesAffected (/usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/lib/commands/debug.js:102:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-core/lib/commands/debug.js:442:1
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

But it may not be the same issue with #721 [https://github.com//issues/721].
And The javascript test transaction can debug normally.

truffle(development)> version
Truffle v4.1.11 (core: 4.1.11)
Solidity v0.4.24 (solc-js)

@cgewecke
Copy link
Contributor

If you only run single test it should work. Javascript tests can be skipped by prefixing the contract test descriptor with skip, like this:

contract.skip('MyContract', function(accounts){
  .. etc..

I think the reason the tx isn't found is that Truffle resets the blockchain between each test file in order to keep a 'clean-room' environment for individual suites. This means only the transactions made in the last test file to run are still available in the blockchain db.

@carbonnetwork
Copy link
Author

@cgewecke Thank you! The problem was solved. But got another problem that is same with https://ethereum.stackexchange.com/questions/50423/truffle-debugger-throws-error. And much like #39

@cgewecke
Copy link
Contributor

@carbonnetwork - Thanks, I've edited your issue title to narrow the problem a bit. . .

@carbonnetwork
Copy link
Author

@cgewecke OK,thanks

@stale
Copy link

stale bot commented Nov 8, 2018

Thank you for raising this issue! It has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you would like to keep this issue open, please respond with information about the current state of this problem.

@stale stale bot added the stale label Nov 8, 2018
@stale
Copy link

stale bot commented Nov 15, 2018

There has been no new activity on this issue since it was marked as stale 7 days ago, so it is being automatically closed. If you'd like help with this or a different problem, please open a new issue. Thanks!

@stale stale bot closed this as completed Nov 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants