3

I'm working on a proof-of-concept "email server" dapp and am getting different behavior in atom-ethereum-interface vs. browser-solidity

I'm running a private blockchain in a Docker container with my own nodes successfully mining all my transactions.

Here's my solidity code:

// EmailServer.sol

pragma solidity ^0.4.2;

contract EmailServer {
  address owner;
  bytes[] inbox;

  function EmailServer() {
    owner = msg.sender;
  }

  function addEmail(bytes x) constant {
    inbox.push(x);
  }

  function getInboxCount() returns (uint count) {
    return inbox.length;
  }

  function getEmails() returns (bytes email) {
    bytes emailsOutput;
    uint inboxCount = getInboxCount();
    uint emailLength;

    if (inboxCount == 0) {
      throw;
    }

    emailsOutput.push('[');
    for (uint i = 0; i < inboxCount; i++) {
      emailLength = inbox[i].length;
      emailsOutput.push('"');
      for (uint j = 0; j < emailLength; j++) {
        emailsOutput.push(inbox[i][j]);
      }
      emailsOutput.push('"');

      if (i < inboxCount - 1) {
        emailsOutput.push(',');
      }
    }
    emailsOutput.push(']');

    return emailsOutput;
  }

  function getOwner() returns (address) {
    return owner;
  }

  // Kill contract and return funds to owner
  function kill() {
    if (msg.sender == owner) {
      suicide(owner);
    }
  }
}

This contract correctly compiles and deploys on both atom-ethereum-interface and browser-solidity.

In both places you can run addEmail("QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN") and it works fine.

But then in browser-solidity when you run getInboxCount() you get a result of: 0x0000000000000000000000000000000000000000000000000000000000000001 which decodes to a uint of 1.

When you run getInboxCount() in atom-ethereum-interface, the contract output is 0xab979d45598f6f3a548113f896a8d24681e9d231a5e3d614cae4b0dcee19e578 and is different every time you run the function. It doesn't seem to decode to anything.

Likewise with any of the other functions. In browser-solidity, it seems to work fine, but in atom-ethereum-interface it returns something I can't seem to decode to anything meaningful.

2

You need to use constant label:

function getInboxCount() constant returns (uint count) {
    return inbox.length;
}

Without it, web3.js / atom-ethereum-interface is giving you a transaction hash, like Reading a uint from a mapping keeps returning a random address

Without constant, invoking getInboxCount with web3.js is a sendTransaction which always returns a transaction hash. See What is the difference between a transaction and a call?

  • Thank you, this helps a lot and has fixed a couple of the methods. The method it doesn't fix is getEmails() which is going through the bytes[], and going through each bytes and decoding each letter, to return a serialized array. This is erroring with Uncaught BigNumber Error: new BigNumber() not a base 16 number. I'm sure there's a better way to return an array? – jaybee Oct 14 '16 at 2:15
  • Returning an array, and the error when returning bytes, might be best asked as 2 separate questions. Also, try a simpler function that returns bytes. – eth Oct 16 '16 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.