mobile, accounts: generate correct java binding

This commit is contained in:
rjl493456442 2019-03-15 10:39:40 +08:00
parent acf308b25e
commit 07bea32326
5 changed files with 34 additions and 47 deletions

View file

@ -460,15 +460,15 @@ import org.ethereum.geth.*;
{{if .InputBin}} {{if .InputBin}}
// BYTECODE is the compiled bytecode used for deploying new contracts. // BYTECODE is the compiled bytecode used for deploying new contracts.
public final static String BYTECODE = "{{.InputBin}}"; public final static String BYTECODE = "0x{{.InputBin}}";
// deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it. // 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 { public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}}); Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
{{range $index, $element := .Constructor.Inputs}} {{range $index, $element := .Constructor.Inputs}}
args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}}); args.set({{$index}}, Geth.new{{namedtype (bindtype .Type) .Type}}({{.Name}});
{{end}} {{end}}
return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.fromHex(BYTECODE), client, args)); return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(BYTECODE), client, args));
} }
// Internal constructor used by contract deployment. // Internal constructor used by contract deployment.
@ -507,7 +507,7 @@ import org.ethereum.geth.*;
// Solidity: {{.Original.String}} // 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 { 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)}}); Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
{{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}}); {{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}}
Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}}); Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
@ -534,9 +534,8 @@ import org.ethereum.geth.*;
// Solidity: {{.Original.String}} // Solidity: {{.Original.String}}
public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception { public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
{{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}}); {{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); return this.Contract.transact(opts, "{{.Original.Name}}" , args);
} }
{{end}} {{end}}

View file

@ -19,16 +19,16 @@
package geth package geth
import ( import (
"errors"
"math/big" "math/big"
"strings" "strings"
"errors"
"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" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/accounts/keystore"
) )
// Signer is an interface defining the callback when a contract requires a // Signer is an interface defining the callback when a contract requires a
@ -151,7 +151,7 @@ func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client
if err != nil { if err != nil {
return nil, err return nil, err
} }
addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.value()...) addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -187,27 +187,25 @@ func (c *BoundContract) GetDeployer() *Transaction {
// sets the output to result. // sets the output to result.
func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error { func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
if len(out.objects) == 1 { if len(out.objects) == 1 {
result := out.objects[0].object result := out.objects[0]
if err := c.contract.Call(&opts.opts, &result, method, args.value()...); err != nil { if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil {
return err return err
} }
out.objects[0].object = result out.objects[0] = result
} else { } else {
results := make([]interface{}, len(out.objects)) results := make([]interface{}, len(out.objects))
copy(results, out.value()) copy(results, out.objects)
if err := c.contract.Call(&opts.opts, &results, method, args.value()...); err != nil { if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
return err return err
} }
for index, result := range results { copy(out.objects, results)
out.objects[index].object = result
}
} }
return nil return nil
} }
// Transact invokes the (paid) contract method with params as input values. // Transact invokes the (paid) contract method with params as input values.
func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) { func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
rawTx, err := c.contract.Transact(&opts.opts, method, args.value()...) rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -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.
@ -229,16 +230,12 @@ func (a *Addresses) Append(address *Address) {
a.addresses = append(a.addresses, address.address) a.addresses = append(a.addresses, address.address)
} }
// ToHex returns the hex representation of b, prefixed with '0x'. // EncodeToHex encodes b as a hex string with 0x prefix.
// For empty slices, the return value is "0x0". func EncodeToHex(b []byte) string {
// return hexutil.Encode(b)
// Deprecated: use hexutil.Encode instead.
func ToHex(b []byte) string {
return common.ToHex(b)
} }
// FromHex returns the bytes represented by the hexadecimal string s. // DecodeFromHex decodes a hex string with 0x prefix.
// s may be prefixed with "0x". func DecodeFromHex(s string) ([]byte, error) {
func FromHex(s string) []byte { return hexutil.Decode(s)
return common.FromHex(s)
} }

View file

@ -115,16 +115,12 @@ func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.I
// Interfaces is a slices of wrapped generic objects. // Interfaces is a slices of wrapped generic objects.
type Interfaces struct { type Interfaces struct {
objects []*Interface // Notably, each element in the objects is not nil objects []interface{}
} }
// NewInterfaces creates a slice of uninitialized interfaces. // NewInterfaces creates a slice of uninitialized interfaces.
func NewInterfaces(size int) *Interfaces { func NewInterfaces(size int) *Interfaces {
ifaces := &Interfaces{objects: make([]*Interface, size)} return &Interfaces{objects: make([]interface{}, size)}
for i := 0; i < ifaces.Size(); i++ {
ifaces.objects[i] = NewInterface()
}
return ifaces
} }
// Size returns the number of interfaces in the slice. // Size returns the number of interfaces in the slice.
@ -133,11 +129,13 @@ func (i *Interfaces) Size() int {
} }
// Get returns the bigint at the given index from the slice. // Get returns the bigint at the given index from the slice.
// Notably the returned value can be changed without affecting the
// interfaces itself.
func (i *Interfaces) Get(index int) (iface *Interface, _ error) { func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
if index < 0 || index >= len(i.objects) { if index < 0 || index >= len(i.objects) {
return nil, errors.New("index out of bounds") return nil, errors.New("index out of bounds")
} }
return i.objects[index], nil return &Interface{object: i.objects[index]}, nil
} }
// Set sets the big int at the given index in the slice. // Set sets the big int at the given index in the slice.
@ -145,17 +143,6 @@ func (i *Interfaces) Set(index int, object *Interface) error {
if index < 0 || index >= len(i.objects) { if index < 0 || index >= len(i.objects) {
return errors.New("index out of bounds") return errors.New("index out of bounds")
} }
i.objects[index].object = object.object i.objects[index] = object.object
return nil return nil
} }
// value returns a batch of embedded values.
//
// Notably, this function won't be exposed.
func (i *Interfaces) value() []interface{} {
values := make([]interface{}, 0, i.Size())
for index := 0; index < i.Size(); index += 1 {
values = append(values, i.objects[index].object)
}
return values
}

View file

@ -67,10 +67,16 @@ func TestInterfaceGetSet(t *testing.T) {
} }
for index, c := range tests { for index, c := range tests {
// In theory the change of iface shouldn't effect the args value
iface, _ := args.Get(index) iface, _ := args.Get(index)
result := callFn(iface, c.method, c.input) result := callFn(iface, c.method, c.input)
if !reflect.DeepEqual(result, c.expect) { if !reflect.DeepEqual(result, c.expect) {
t.Errorf("Interface get/set mismatch, want %v, got %v", c.expect, result) 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")
}
} }
} }