0

To store data on the blockchain, a storage container bytes[6] data is used, each array stores maximum amount based on block gas limit. Can assembly be used to add those 6 arrays together, and return one single bytes array?

function getData() public view {
    uint length;
    for(uint i = 0; i<6; i++) {
        length += data[i].length;
    }
    bytes memory result = new bytes(length);

    uint index = 32; // bytes is an array, and in EVM the first 
                     // 32 bytes of an array stores the length
                     // of the array, so add data 32 byte in

    for(i = 0; i < 6; i++) {
        bytes memory oneSixth = new bytes(data[i].length);
        oneSixth = data[i];
        assembly {
            mstore(add(result, index), add(oneSixth, 32))
        }
        index += data[i].length;
    }
}
0

OK found how to get it to work, from looking at this post, Concat two bytes arrays with Assembly

had to add mload(add(oneSixth, 32)

So instead of

mstore(add(result, index), add(oneSixth, 32))

it is

mstore(add(result, index), mload(add(oneSixth, 32)))

update: mload could only load 32 byte, so can not be used to load arbitrarily long byte arrays

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