Skip to content

abigen and the geth library can't work together with bytes32[] <=> [][32]byte #15212

Closed
@coreyog

Description

@coreyog

System information

Geth version: 1.7.0-stable
OS & Version: Linux Mint 18.2 x64
Commit hash : N/A
Go Version: go version go1.9 linux/amd64

Expected behaviour

When I run the latest version of abigen on a contract I expect the output go code to work with the latest version of go-ethereum. My contract uses bytes32 as the type for IDs of objects. In one struct I have a list of IDs and use bytes32[] to hold that list. Abigen properly outputs it to the go type [][32]byte. I expect that when I use the generated code to call my contract, that the proper types are returned from the call without error.

Actual behaviour

When I call in to the generated code, I receive an error stating abi: cannot unmarshal [][]uint8 in to [][32]uint8. If I modify the generated go code so that it's just [][]byte instead of [][32]byte then the function call works as expected.

Steps to reproduce the behaviour

Contract Test.sol:

pragma solidity ^0.4.15;

contract Test {
  function testDynamicFixedBytes() constant returns (bytes32[]) {
    var array = new bytes32[](2);
    array[0] = "0x1234567890";
    array[1] = "0x0987654321";
    return array;
  }
}

Run abigen -pkg main -sol Test.sol -out Test.go

Go:

package main

import (
	"context"
	"encoding/hex"
	"fmt"

	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
)

func main() {
	contract := "b1815f478391d8fb7a22af0c9a1a5523a16e2429"
	ethURL := "/home/corey/eth/data/geth.ipc"
	fromAddress := "fcf5e6a6ce24cdeef66697a41ba75b039c17d2cb"
	contractAddress := common.HexToAddress(contract)
	client, err := ethclient.Dial(ethURL)
	if err != nil {
		panic(err)
	}
	test, err := NewTest(contractAddress, client)
	if err != nil {
		panic(err)
	}

	opts := bind.CallOpts{
		Context: context.Background(),
		From:    common.HexToAddress(fromAddress),
	}
	array, err := test.TestDynamicFixedBytes(&opts)
	if err != nil {
		panic(err)
	}
	for _, v := range array {
		fmt.Println(hex.EncodeToString(v[:]))
	}
}

Backtrace

panic: abi: cannot unmarshal [][]uint8 in to [][32]uint8

goroutine 1 [running]:
main.main()
        /home/corey/src/goprojects/src/playground/main.go:33 +0x32d

The line that's generating the error is in go-ethereum/accounts/abi/reflect.go at line 90.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions