Skip to content

Commit fc0638f

Browse files
committed
Merge branch 'release/1.4'
2 parents dd083aa + 4b11f20 commit fc0638f

File tree

13 files changed

+351
-85
lines changed

13 files changed

+351
-85
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.3
1+
1.4.4

accounts/abi/abi.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,16 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
238238
return fmt.Errorf("abi: unmarshalling empty output")
239239
}
240240

241-
value := reflect.ValueOf(v).Elem()
242-
typ := value.Type()
241+
// make sure the passed value is a pointer
242+
valueOf := reflect.ValueOf(v)
243+
if reflect.Ptr != valueOf.Kind() {
244+
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
245+
}
246+
247+
var (
248+
value = valueOf.Elem()
249+
typ = value.Type()
250+
)
243251

244252
if len(method.Outputs) > 1 {
245253
switch value.Kind() {
@@ -268,6 +276,25 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
268276
return fmt.Errorf("abi: cannot marshal tuple in to slice %T (only []interface{} is supported)", v)
269277
}
270278

279+
// if the slice already contains values, set those instead of the interface slice itself.
280+
if value.Len() > 0 {
281+
if len(method.Outputs) > value.Len() {
282+
return fmt.Errorf("abi: cannot marshal in to slices of unequal size (require: %v, got: %v)", len(method.Outputs), value.Len())
283+
}
284+
285+
for i := 0; i < len(method.Outputs); i++ {
286+
marshalledValue, err := toGoType(i, method.Outputs[i], output)
287+
if err != nil {
288+
return err
289+
}
290+
reflectValue := reflect.ValueOf(marshalledValue)
291+
if err := set(value.Index(i).Elem(), reflectValue, method.Outputs[i]); err != nil {
292+
return err
293+
}
294+
}
295+
return nil
296+
}
297+
271298
// create a new slice and start appending the unmarshalled
272299
// values to the new interface slice.
273300
z := reflect.MakeSlice(typ, 0, len(method.Outputs))
@@ -296,34 +323,6 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
296323
return nil
297324
}
298325

299-
// set attempts to assign src to dst by either setting, copying or otherwise.
300-
//
301-
// set is a bit more lenient when it comes to assignment and doesn't force an as
302-
// strict ruleset as bare `reflect` does.
303-
func set(dst, src reflect.Value, output Argument) error {
304-
dstType := dst.Type()
305-
srcType := src.Type()
306-
307-
switch {
308-
case dstType.AssignableTo(src.Type()):
309-
dst.Set(src)
310-
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
311-
if !dstType.Elem().AssignableTo(r_byte) {
312-
return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
313-
}
314-
315-
if dst.Len() < output.Type.SliceSize {
316-
return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
317-
}
318-
reflect.Copy(dst, src)
319-
case dstType.Kind() == reflect.Interface:
320-
dst.Set(src)
321-
default:
322-
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
323-
}
324-
return nil
325-
}
326-
327326
func (abi *ABI) UnmarshalJSON(data []byte) error {
328327
var fields []struct {
329328
Type string

accounts/abi/abi_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,37 @@ func TestSimpleMethodUnpack(t *testing.T) {
289289
}
290290
}
291291

292+
func TestUnpackSetInterfaceSlice(t *testing.T) {
293+
var (
294+
var1 = new(uint8)
295+
var2 = new(uint8)
296+
)
297+
out := []interface{}{var1, var2}
298+
abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`))
299+
if err != nil {
300+
t.Fatal(err)
301+
}
302+
marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
303+
err = abi.Unpack(&out, "ints", marshalledReturn)
304+
if err != nil {
305+
t.Fatal(err)
306+
}
307+
if *var1 != 1 {
308+
t.Errorf("expected var1 to be 1, got", *var1)
309+
}
310+
if *var2 != 2 {
311+
t.Errorf("expected var2 to be 2, got", *var2)
312+
}
313+
314+
out = []interface{}{var1}
315+
err = abi.Unpack(&out, "ints", marshalledReturn)
316+
317+
expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)"
318+
if err == nil || err.Error() != expErr {
319+
t.Error("expected err:", expErr, "Got:", err)
320+
}
321+
}
322+
292323
func TestPack(t *testing.T) {
293324
for i, test := range []struct {
294325
typ string

accounts/abi/bind/bind_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,44 @@ var bindTests = []struct {
194194
}
195195
`,
196196
},
197+
// Tests that plain values can be properly returned and deserialized
198+
{
199+
`Getter`,
200+
`
201+
contract Getter {
202+
function getter() constant returns (string, int, bytes32) {
203+
return ("Hi", 1, sha3(""));
204+
}
205+
}
206+
`,
207+
`606060405260dc8060106000396000f3606060405260e060020a6000350463993a04b78114601a575b005b600060605260c0604052600260809081527f486900000000000000000000000000000000000000000000000000000000000060a05260017fc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a47060e0829052610100819052606060c0908152600261012081905281906101409060a09080838184600060046012f1505081517fffff000000000000000000000000000000000000000000000000000000000000169091525050604051610160819003945092505050f3`,
208+
`[{"constant":true,"inputs":[],"name":"getter","outputs":[{"name":"","type":"string"},{"name":"","type":"int256"},{"name":"","type":"bytes32"}],"type":"function"}]`,
209+
`
210+
// Generate a new random account and a funded simulator
211+
key, _ := crypto.GenerateKey()
212+
auth := bind.NewKeyedTransactor(key)
213+
sim := backends.NewSimulatedBackend(core.GenesisAccount{Address: auth.From, Balance: big.NewInt(10000000000)})
214+
215+
// Deploy a tuple tester contract and execute a structured call on it
216+
_, _, getter, err := DeployGetter(auth, sim)
217+
if err != nil {
218+
t.Fatalf("Failed to deploy getter contract: %v", err)
219+
}
220+
sim.Commit()
221+
222+
if str, num, _, err := getter.Getter(nil); err != nil {
223+
t.Fatalf("Failed to call anonymous field retriever: %v", err)
224+
} else if str != "Hi" || num.Cmp(big.NewInt(1)) != 0 {
225+
t.Fatalf("Retrieved value mismatch: have %v/%v, want %v/%v", str, num, "Hi", 1)
226+
}
227+
`,
228+
},
197229
// Tests that tuples can be properly returned and deserialized
198230
{
199231
`Tupler`,
200232
`
201233
contract Tupler {
202-
function tuple() returns (string a, int b, bytes32 c) {
234+
function tuple() constant returns (string a, int b, bytes32 c) {
203235
return ("Hi", 1, sha3(""));
204236
}
205237
}
@@ -219,8 +251,10 @@ var bindTests = []struct {
219251
}
220252
sim.Commit()
221253
222-
if _, err := tupler.Tuple(nil); err != nil {
254+
if res, err := tupler.Tuple(nil); err != nil {
223255
t.Fatalf("Failed to call structure retriever: %v", err)
256+
} else if res.A != "Hi" || res.B.Cmp(big.NewInt(1)) != 0 {
257+
t.Fatalf("Retrieved value mismatch: have %v/%v, want %v/%v", res.A, res.B, "Hi", 1)
224258
}
225259
`,
226260
},

accounts/abi/bind/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ package {{.Package}}
211211
{{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type}})
212212
{{end}}
213213
){{end}}
214-
out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}[]interface{}{
214+
out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
215215
{{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
216216
{{end}}
217217
}{{end}}{{end}}

accounts/abi/reflect.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package abi
1818

19-
import "reflect"
19+
import (
20+
"fmt"
21+
"reflect"
22+
)
2023

2124
// indirect recursively dereferences the value until it either gets the value
2225
// or finds a big.Int
@@ -62,3 +65,33 @@ func mustArrayToByteSlice(value reflect.Value) reflect.Value {
6265
reflect.Copy(slice, value)
6366
return slice
6467
}
68+
69+
// set attempts to assign src to dst by either setting, copying or otherwise.
70+
//
71+
// set is a bit more lenient when it comes to assignment and doesn't force an as
72+
// strict ruleset as bare `reflect` does.
73+
func set(dst, src reflect.Value, output Argument) error {
74+
dstType := dst.Type()
75+
srcType := src.Type()
76+
77+
switch {
78+
case dstType.AssignableTo(src.Type()):
79+
dst.Set(src)
80+
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
81+
if !dstType.Elem().AssignableTo(r_byte) {
82+
return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
83+
}
84+
85+
if dst.Len() < output.Type.SliceSize {
86+
return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
87+
}
88+
reflect.Copy(dst, src)
89+
case dstType.Kind() == reflect.Interface:
90+
dst.Set(src)
91+
case dstType.Kind() == reflect.Ptr:
92+
return set(dst.Elem(), src, output)
93+
default:
94+
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
95+
}
96+
return nil
97+
}

cmd/geth/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const (
5050
clientIdentifier = "Geth" // Client identifier to advertise over the network
5151
versionMajor = 1 // Major version component of the current release
5252
versionMinor = 4 // Minor version component of the current release
53-
versionPatch = 3 // Patch version component of the current release
53+
versionPatch = 4 // Patch version component of the current release
5454
versionMeta = "stable" // Version metadata to append to the version string
5555

5656
versionOracle = "0xfa7b9770ca4cb04296cac84f37736d4041251cdf" // Ethereum address of the Geth release oracle

core/block_validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func calcDifficultyHomestead(time, parentTime uint64, parentNumber, parentDiff *
292292

293293
// minimum difficulty can ever be (before exponential factor)
294294
if x.Cmp(params.MinimumDifficulty) < 0 {
295-
x = params.MinimumDifficulty
295+
x.Set(params.MinimumDifficulty)
296296
}
297297

298298
// for the exponential factor
@@ -325,7 +325,7 @@ func calcDifficultyFrontier(time, parentTime uint64, parentNumber, parentDiff *b
325325
diff.Sub(parentDiff, adjust)
326326
}
327327
if diff.Cmp(params.MinimumDifficulty) < 0 {
328-
diff = params.MinimumDifficulty
328+
diff.Set(params.MinimumDifficulty)
329329
}
330330

331331
periodCount := new(big.Int).Add(parentNumber, common.Big1)

0 commit comments

Comments
 (0)