3

So I'm trying to work with two-dimensional byte arrays and am experiencing some results that I don't quite understand. I am using Truffle v2.0.4 for compiling/deploying my contracts with ethereumjs-testrpc as a blockchain backend.

Consider this super simple contract. It initilizes a 2D array of bytes, assigns the third element in the first array, and returns:

contract Sandbox {

    function retArr() 
    public constant returns (byte[3][10] ret) {
        ret[0][2] = byte(2);
    }

}

I can then access this contract via Truffle's Web3 wrapper:

Sandbox.deployed().retArr().then(function(res) {
    console.log(res)
})

Which produces the following output:

[ [ '0x00', '0x00', '0x02' ],
  [ '0x00', '0x02', '0x00' ],
  [ '0x02', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ],
  [ '0x00', '0x00', '0x00' ] ]

The first child array looks correct (0x02 is assigned to the third position), but then 0x02 pops up in the second and third arrays as well.

Why did multiple elements get assigned with one call? If this is expected behavior, what's the rationale behind this design decision? How can I only assign one element at a time in a 2D array?

  • 1
    to be honest, it seems to me like a bug in web3 byte two dimensional array decoding: 1. It works fine with address[16][16] which I use. 2. I have a proposed fix that works for your case, but I've yet to generalize it. – kobigurk Aug 31 '16 at 14:02
  • 1
    since eventually, byte (=bytes1) is padded to 32 bytes, a quick workaround for you would be to return bytes32[3][10] instead – kobigurk Aug 31 '16 at 14:12
  • It could very well be a bug in web3. Maybe I should open an issue on Github? And thanks for the bytes32 suggestion, I'll play around with that :) – thoCoStuff Sep 2 '16 at 3:16
3

I read here that:

If possible (i.e. from anything to memory and from anything to a storage reference that is not a pointer), conversions between these data locations are performed automatically by the compiler. Sometimes, this is still not possible, i.e. mappings cannot reside in memory (as their size is unknown) and for now, some types in memory are not yet implemented, this includes structs and multi-dimensional arrays.

This is probably the reason.

  • The answer probably rests somewhere between a lack of implementation in Solidity and/or a decoding bug in Web3 (as @kobigurk had suggested). Also, thanks for that awesome link! :D – thoCoStuff Sep 2 '16 at 3:12

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.