Commit 6069b1a5 authored by gary rong's avatar gary rong Committed by Péter Szilágyi

mobile: fix mobile interface (#19180)

* mobile: fix mobile interface

* mobile, accounts: generate correct java binding

* accounts: fix java type binding

* mobile: support integer slice

* accounts/abi/bind, cmd/abigen: implement java binding tests
parent fd072c2f
...@@ -38,7 +38,6 @@ type Lang int ...@@ -38,7 +38,6 @@ type Lang int
const ( const (
LangGo Lang = iota LangGo Lang = iota
LangJava LangJava
LangObjC
) )
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant // Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
...@@ -164,124 +163,62 @@ var bindType = map[Lang]func(kind abi.Type) string{ ...@@ -164,124 +163,62 @@ var bindType = map[Lang]func(kind abi.Type) string{
LangJava: bindTypeJava, LangJava: bindTypeJava,
} }
// Helper function for the binding generators. // bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go one.
// It reads the unmatched characters after the inner type-match, func bindBasicTypeGo(kind abi.Type) string {
// (since the inner type is a prefix of the total type declaration), switch kind.T {
// looks for valid arrays (possibly a dynamic one) wrapping the inner type, case abi.AddressTy:
// and returns the sizes of these arrays. return "common.Address"
// case abi.IntTy, abi.UintTy:
// Returned array sizes are in the same order as solidity signatures; inner array size first. parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
// Array sizes may also be "", indicating a dynamic array.
func wrapArray(stringKind string, innerLen int, innerMapping string) (string, []string) {
remainder := stringKind[innerLen:]
//find all the sizes
matches := regexp.MustCompile(`\[(\d*)\]`).FindAllStringSubmatch(remainder, -1)
parts := make([]string, 0, len(matches))
for _, match := range matches {
//get group 1 from the regex match
parts = append(parts, match[1])
}
return innerMapping, parts
}
// Translates the array sizes to a Go-lang declaration of a (nested) array of the inner type.
// Simply returns the inner type if arraySizes is empty.
func arrayBindingGo(inner string, arraySizes []string) string {
out := ""
//prepend all array sizes, from outer (end arraySizes) to inner (start arraySizes)
for i := len(arraySizes) - 1; i >= 0; i-- {
out += "[" + arraySizes[i] + "]"
}
out += inner
return out
}
// bindTypeGo converts a Solidity type to a Go one. Since there is no clear mapping
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. *big.Int).
func bindTypeGo(kind abi.Type) string {
stringKind := kind.String()
innerLen, innerMapping := bindUnnestedTypeGo(stringKind)
return arrayBindingGo(wrapArray(stringKind, innerLen, innerMapping))
}
// The inner function of bindTypeGo, this finds the inner type of stringKind.
// (Or just the type itself if it is not an array or slice)
// The length of the matched part is returned, with the translated type.
func bindUnnestedTypeGo(stringKind string) (int, string) {
switch {
case strings.HasPrefix(stringKind, "address"):
return len("address"), "common.Address"
case strings.HasPrefix(stringKind, "bytes"):
parts := regexp.MustCompile(`bytes([0-9]*)`).FindStringSubmatch(stringKind)
return len(parts[0]), fmt.Sprintf("[%s]byte", parts[1])
case strings.HasPrefix(stringKind, "int") || strings.HasPrefix(stringKind, "uint"):
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(stringKind)
switch parts[2] { switch parts[2] {
case "8", "16", "32", "64": case "8", "16", "32", "64":
return len(parts[0]), fmt.Sprintf("%sint%s", parts[1], parts[2]) return fmt.Sprintf("%sint%s", parts[1], parts[2])
} }
return len(parts[0]), "*big.Int" return "*big.Int"
case abi.FixedBytesTy:
case strings.HasPrefix(stringKind, "bool"): return fmt.Sprintf("[%d]byte", kind.Size)
return len("bool"), "bool" case abi.BytesTy:
return "[]byte"
case strings.HasPrefix(stringKind, "string"): case abi.FunctionTy:
return len("string"), "string" // todo(rjl493456442)
return ""
default: default:
return len(stringKind), stringKind // string, bool types
return kind.String()
} }
} }
// Translates the array sizes to a Java declaration of a (nested) array of the inner type. // bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
// Simply returns the inner type if arraySizes is empty. // from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
func arrayBindingJava(inner string, arraySizes []string) string {
// Java array type declarations do not include the length.
return inner + strings.Repeat("[]", len(arraySizes))
}
// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. BigDecimal). // mapped will use an upscaled type (e.g. BigDecimal).
func bindTypeJava(kind abi.Type) string { func bindTypeGo(kind abi.Type) string {
stringKind := kind.String() // todo(rjl493456442) tuple
innerLen, innerMapping := bindUnnestedTypeJava(stringKind) switch kind.T {
return arrayBindingJava(wrapArray(stringKind, innerLen, innerMapping)) case abi.ArrayTy:
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem)
case abi.SliceTy:
return "[]" + bindTypeGo(*kind.Elem)
default:
return bindBasicTypeGo(kind)
}
} }
// The inner function of bindTypeJava, this finds the inner type of stringKind. // bindBasicTypeJava converts basic solidity types(except array, slice and tuple) to Java one.
// (Or just the type itself if it is not an array or slice) func bindBasicTypeJava(kind abi.Type) string {
// The length of the matched part is returned, with the translated type. switch kind.T {
func bindUnnestedTypeJava(stringKind string) (int, string) { case abi.AddressTy:
return "Address"
switch { case abi.IntTy, abi.UintTy:
case strings.HasPrefix(stringKind, "address"): // Note that uint and int (without digits) are also matched,
parts := regexp.MustCompile(`address(\[[0-9]*\])?`).FindStringSubmatch(stringKind)
if len(parts) != 2 {
return len(stringKind), stringKind
}
if parts[1] == "" {
return len("address"), "Address"
}
return len(parts[0]), "Addresses"
case strings.HasPrefix(stringKind, "bytes"):
parts := regexp.MustCompile(`bytes([0-9]*)`).FindStringSubmatch(stringKind)
if len(parts) != 2 {
return len(stringKind), stringKind
}
return len(parts[0]), "byte[]"
case strings.HasPrefix(stringKind, "int") || strings.HasPrefix(stringKind, "uint"):
//Note that uint and int (without digits) are also matched,
// these are size 256, and will translate to BigInt (the default). // these are size 256, and will translate to BigInt (the default).
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(stringKind) parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
if len(parts) != 3 { if len(parts) != 3 {
return len(stringKind), stringKind return kind.String()
}
// All unsigned integers should be translated to BigInt since gomobile doesn't
// support them.
if parts[1] == "u" {
return "BigInt"
} }
namedSize := map[string]string{ namedSize := map[string]string{
...@@ -291,20 +228,48 @@ func bindUnnestedTypeJava(stringKind string) (int, string) { ...@@ -291,20 +228,48 @@ func bindUnnestedTypeJava(stringKind string) (int, string) {
"64": "long", "64": "long",
}[parts[2]] }[parts[2]]
//default to BigInt // default to BigInt
if namedSize == "" { if namedSize == "" {
namedSize = "BigInt" namedSize = "BigInt"
} }
return len(parts[0]), namedSize return namedSize
case abi.FixedBytesTy, abi.BytesTy:
case strings.HasPrefix(stringKind, "bool"): return "byte[]"
return len("bool"), "boolean" case abi.BoolTy:
return "boolean"
case strings.HasPrefix(stringKind, "string"): case abi.StringTy:
return len("string"), "String" return "String"
case abi.FunctionTy:
// todo(rjl493456442)
return ""
default:
return kind.String()
}
}
// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. BigDecimal).
func bindTypeJava(kind abi.Type) string {
switch kind.T {
case abi.ArrayTy, abi.SliceTy:
// Explicitly convert multidimensional types to predefined type in go side.
inner := bindTypeJava(*kind.Elem)
switch inner {
case "boolean":
return "Bools"
case "String":
return "Strings"
case "Address":
return "Addresses"
case "byte[]":
return "Binaries"
case "BigInt":
return "BigInts"
}
return inner + "[]"
default: default:
return len(stringKind), stringKind return bindBasicTypeJava(kind)
} }
} }
...@@ -329,7 +294,7 @@ func bindTopicTypeGo(kind abi.Type) string { ...@@ -329,7 +294,7 @@ func bindTopicTypeGo(kind abi.Type) string {
// funcionality as for simple types, but dynamic types get converted to hashes. // funcionality as for simple types, but dynamic types get converted to hashes.
func bindTopicTypeJava(kind abi.Type) string { func bindTopicTypeJava(kind abi.Type) string {
bound := bindTypeJava(kind) bound := bindTypeJava(kind)
if bound == "String" || bound == "Bytes" { if bound == "String" || bound == "byte[]" {
bound = "Hash" bound = "Hash"
} }
return bound return bound
...@@ -348,18 +313,8 @@ func namedTypeJava(javaKind string, solKind abi.Type) string { ...@@ -348,18 +313,8 @@ func namedTypeJava(javaKind string, solKind abi.Type) string {
switch javaKind { switch javaKind {
case "byte[]": case "byte[]":
return "Binary" return "Binary"
case "byte[][]":
return "Binaries"
case "string":
return "String"
case "string[]":
return "Strings"
case "boolean": case "boolean":
return "Bool" return "Bool"
case "boolean[]":
return "Bools"
case "BigInt[]":
return "BigInts"
default: default:
parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String()) parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String())
if len(parts) != 4 { if len(parts) != 4 {
......
This diff is collapsed.
...@@ -452,95 +452,92 @@ const tmplSourceJava = ` ...@@ -452,95 +452,92 @@ const tmplSourceJava = `
package {{.Package}}; package {{.Package}};
import org.ethereum.geth.*; import org.ethereum.geth.*;
import org.ethereum.geth.internal.*;
{{range $contract := .Contracts}} {{range $contract := .Contracts}}
public class {{.Type}} { public class {{.Type}} {
// ABI is the input ABI used to generate the binding from. // ABI is the input ABI used to generate the binding from.
public final static String ABI = "{{.InputABI}}"; public final static String ABI = "{{.InputABI}}";
{{if .InputBin}}
// BYTECODE is the compiled bytecode used for deploying new contracts.
public final static byte[] BYTECODE = "{{.InputBin}}".getBytes();
// deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
{{range $index, $element := .Constructor.Inputs}}
args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}});
{{end}}
return new {{.Type}}(Geth.deployContract(auth, ABI, BYTECODE, client, args));
}
// Internal constructor used by contract deployment. {{if .InputBin}}
private {{.Type}}(BoundContract deployment) { // BYTECODE is the compiled bytecode used for deploying new contracts.
this.Address = deployment.getAddress(); public final static String BYTECODE = "0x{{.InputBin}}";
this.Deployer = deployment.getDeployer();
this.Contract = deployment; // deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
} public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
{{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
{{end}} {{end}}
return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(BYTECODE), client, args));
}
// Ethereum address where this contract is located at. // Internal constructor used by contract deployment.
public final Address Address; private {{.Type}}(BoundContract deployment) {
this.Address = deployment.getAddress();
this.Deployer = deployment.getDeployer();
this.Contract = deployment;
}
{{end}}
// Ethereum transaction in which this contract was deployed (if known!). // Ethereum address where this contract is located at.
public final Transaction Deployer; public final Address Address;
// Contract instance bound to a blockchain address. // Ethereum transaction in which this contract was deployed (if known!).
private final BoundContract Contract; public final Transaction Deployer;
// Creates a new instance of {{.Type}}, bound to a specific deployed contract. // Contract instance bound to a blockchain address.
public {{.Type}}(Address address, EthereumClient client) throws Exception { private final BoundContract Contract;
this(Geth.bindContract(address, ABI, client));
}
{{range .Calls}} // Creates a new instance of {{.Type}}, bound to a specific deployed contract.
{{if gt (len .Normalized.Outputs) 1}} public {{.Type}}(Address address, EthereumClient client) throws Exception {
// {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}. this(Geth.bindContract(address, ABI, client));
public class {{capitalise .Normalized.Name}}Results { }
{{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
{{end}}
}
{{end}}
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}. {{range .Calls}}
// {{if gt (len .Normalized.Outputs) 1}}
// Solidity: {{.Original.String}} // {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else}}{{range .Normalized.Outputs}}{{bindtype .Type}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception { public class {{capitalise .Normalized.Name}}Results {
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); {{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
{{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}}); {{end}}
{{end}} }
{{end}}
Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}}); // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
{{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type) .Type}}(); results.set({{$index}}, result{{$index}}); //
{{end}} // Solidity: {{.Original.String}}
public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else}}{{range .Normalized.Outputs}}{{bindtype .Type}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
{{end}}
if (opts == null) { Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
opts = Geth.newCallOpts(); {{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type) .Type}}(); results.set({{$index}}, result{{$index}});
}
this.Contract.call(opts, results, "{{.Original.Name}}", args);
{{if gt (len .Normalized.Outputs) 1}}
{{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
{{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type) .Type}}();
{{end}}
return result;
{{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type) .Type}}();{{end}}
{{end}}
}
{{end}} {{end}}
{{range .Transacts}} if (opts == null) {
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}. opts = Geth.newCallOpts();
// }
// Solidity: {{.Original.String}} this.Contract.call(opts, results, "{{.Original.Name}}", args);
public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception { {{if gt (len .Normalized.Outputs) 1}}
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); {{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
{{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}}); {{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type) .Type}}();
{{end}} {{end}}
return result;
{{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type) .Type}}();{{end}}
{{end}}
}
{{end}}
return this.Contract.transact(opts, "{{.Original.Name}}" , args); {{range .Transacts}}
} // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
//
// Solidity: {{.Original.String}}
public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
{{end}} {{end}}
return this.Contract.transact(opts, "{{.Original.Name}}" , args);
} }
{{end}}
}
{{end}} {{end}}
` `
...@@ -69,8 +69,6 @@ func main() { ...@@ -69,8 +69,6 @@ func main() {
lang = bind.LangGo lang = bind.LangGo
case "java": case "java":
lang = bind.LangJava lang = bind.LangJava
case "objc":
lang = bind.LangObjC
default: default:
fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag) fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag)
os.Exit(-1) os.Exit(-1)
......
...@@ -19,27 +19,30 @@ ...@@ -19,27 +19,30 @@
package geth package geth
import ( import (
"errors"
"math/big" "math/big"
"strings" "strings"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
) )
// Signer is an interaface defining the callback when a contract requires a // Signer is an interface defining the callback when a contract requires a
// method to sign the transaction before submission. // method to sign the transaction before submission.
type Signer interface { type Signer interface {
Sign(*Address, *Transaction) (tx *Transaction, _ error) Sign(*Address, *Transaction) (tx *Transaction, _ error)
} }
type signer struct { type MobileSigner struct {
sign bind.SignerFn sign bind.SignerFn
} }
func (s *signer) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) { func (s *MobileSigner) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
sig, err := s.sign(types.HomesteadSigner{}, addr.address, unsignedTx.tx) sig, err := s.sign(types.EIP155Signer{}, addr.address, unsignedTx.tx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -73,6 +76,35 @@ type TransactOpts struct { ...@@ -73,6 +76,35 @@ type TransactOpts struct {
opts bind.TransactOpts opts bind.TransactOpts
} }
// NewTransactOpts creates a new option set for contract transaction.
func NewTransactOpts() *TransactOpts {
return new(TransactOpts)
}
// NewKeyedTransactor is a utility method to easily create a transaction signer
// from a single private key.
func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
key, err := keystore.DecryptKey(keyJson, passphrase)
if err != nil {
return nil, err
}
keyAddr := crypto.PubkeyToAddress(key.PrivateKey.PublicKey)
opts := bind.TransactOpts{
From: keyAddr,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
}
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key.PrivateKey)
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
},
}
return &TransactOpts{opts}, nil
}
func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} } func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() } func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() }
func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} } func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} }
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
// Hash represents the 32 byte Keccak256 hash of arbitrary data. // Hash represents the 32 byte Keccak256 hash of arbitrary data.
...@@ -228,3 +229,13 @@ func (a *Addresses) Set(index int, address *Address) error { ...@@ -228,3 +229,13 @@ func (a *Addresses) Set(index int, address *Address) error {
func (a *Addresses) Append(address *Address) { func (a *Addresses) Append(address *Address) {
a.addresses = append(a.addresses, address.address) a.addresses = append(a.addresses, address.address)
} }
// EncodeToHex encodes b as a hex string with 0x prefix.
func EncodeToHex(b []byte) string {
return hexutil.Encode(b)
}
// DecodeFromHex decodes a hex string with 0x prefix.
func DecodeFromHex(s string) ([]byte, error) {
return hexutil.Decode(s)
}
This diff is collapsed.
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package geth
import (
"fmt"
"math/big"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
)
func TestInterfaceGetSet(t *testing.T) {
var tests = []struct {
method string
input interface{}
expect interface{}
}{
{"Bool", true, true},
{"Bool", false, false},
{"Bools", &Bools{[]bool{false, true}}, &Bools{[]bool{false, true}}},
{"String", "go-ethereum", "go-ethereum"},
{"Strings", &Strings{strs: []string{"hello", "world"}}, &Strings{strs: []string{"hello", "world"}}},
{"Binary", []byte{0x01, 0x02}, []byte{0x01, 0x02}},
{"Binaries", &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}, &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}},
{"Address", &Address{common.HexToAddress("deadbeef")}, &Address{common.HexToAddress("deadbeef")}},
{"Addresses", &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}, &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}},
{"Hash", &Hash{common.HexToHash("deadbeef")}, &Hash{common.HexToHash("deadbeef")}},
{"Hashes", &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}, &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}},
{"Int8", int8(1), int8(1)},
{"Int16", int16(1), int16(1)},
{"Int32", int32(1), int32(1)},
{"Int64", int64(1), int64(1)},
{"Int8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Int16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Int32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Int64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Uint8", NewBigInt(1), NewBigInt(1)},
{"Uint16", NewBigInt(1), NewBigInt(1)},
{"Uint32", NewBigInt(1), NewBigInt(1)},
{"Uint64", NewBigInt(1), NewBigInt(1)},
{"Uint8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Uint16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Uint32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"Uint64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
{"BigInt", NewBigInt(1), NewBigInt(1)},
{"BigInts", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
}
args := NewInterfaces(len(tests))
callFn := func(receiver interface{}, method string, arg interface{}) interface{} {
rval := reflect.ValueOf(receiver)
rval.MethodByName(fmt.Sprintf("Set%s", method)).Call([]reflect.Value{reflect.ValueOf(arg)})
res := rval.MethodByName(fmt.Sprintf("Get%s", method)).Call(nil)
if len(res) > 0 {
return res[0].Interface()
}
return nil
}
for index, c := range tests {
// In theory the change of iface shouldn't effect the args value
iface, _ := args.Get(index)
result := callFn(iface, c.method, c.input)
if !reflect.DeepEqual(result, c.expect) {
t.Errorf("Interface get/set mismatch, want %v, got %v", c.expect, result)
}
// Check whether the underlying value in args is still zero
iface, _ = args.Get(index)
if iface.object != nil {
t.Error("Get operation is not write safe")
}
}
}
...@@ -21,6 +21,8 @@ package geth ...@@ -21,6 +21,8 @@ package geth
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common"
) )
// Strings represents s slice of strs. // Strings represents s slice of strs.
...@@ -52,3 +54,63 @@ func (s *Strings) Set(index int, str string) error { ...@@ -52,3 +54,63 @@ func (s *Strings) Set(index int, str string) error {
func (s *Strings) String() string { func (s *Strings) String() string {
return fmt.Sprintf("%v", s.strs) return fmt.Sprintf("%v", s.strs)
} }
// Bools represents a slice of bool.
type Bools struct{ bools []bool }
// Size returns the number of bool in the slice.
func (bs *Bools) Size() int {
return len(bs.bools)
}
// Get returns the bool at the given index from the slice.
func (bs *Bools) Get(index int) (b bool, _ error) {
if index < 0 || index >= len(bs.bools) {
return false, errors.New("index out of bounds")
}
return bs.bools[index], nil
}
// Set sets the bool at the given index in the slice.
func (bs *Bools) Set(index int, b bool) error {
if index < 0 || index >= len(bs.bools) {
return errors.New("index out of bounds")
}
bs.bools[index] = b
return nil
}
// String implements the Stringer interface.
func (bs *Bools) String() string {
return fmt.Sprintf("%v", bs.bools)
}
// Binaries represents a slice of byte slice
type Binaries struct{ binaries [][]byte }
// Size returns the number of byte slice in the slice.
func (bs *Binaries) Size() int {
return len(bs.binaries)
}
// Get returns the byte slice at the given index from the slice.
func (bs *Binaries) Get(index int) (binary []byte, _ error) {
if index < 0 || index >= len(bs.binaries) {
return nil, errors.New("index out of bounds")
}
return common.CopyBytes(bs.binaries[index]), nil
}
// Set sets the byte slice at the given index in the slice.
func (bs *Binaries) Set(index int, binary []byte) error {
if index < 0 || index >= len(bs.binaries) {
return errors.New("index out of bounds")
}
bs.binaries[index] = common.CopyBytes(binary)
return nil
}
// String implements the Stringer interface.
func (bs *Binaries) String() string {
return fmt.Sprintf("%v", bs.binaries)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment