Skip to content

Commit 13b566e

Browse files
committed
accounts/abi: Add one-parameter event test case from enriquefynn/unpack_one_arg_event
2 parents 1e72271 + 1548518 commit 13b566e

File tree

256 files changed

+14983
-4481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+14983
-4481
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ accounts/usbwallet @karalabe
55
consensus @karalabe
66
core/ @karalabe @holiman
77
eth/ @karalabe
8+
les/ @zsfelfoldi
9+
light/ @zsfelfoldi
810
mobile/ @karalabe
911
p2p/ @fjl @zsfelfoldi

.github/stale.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Number of days of inactivity before an issue becomes stale
2+
daysUntilStale: 366
3+
# Number of days of inactivity before a stale issue is closed
4+
daysUntilClose: 42
5+
# Issues with these labels will never be considered stale
6+
exemptLabels:
7+
- pinned
8+
- security
9+
# Label to use when marking an issue as stale
10+
staleLabel: stale
11+
# Comment to post when marking an issue as stale. Set to `false` to disable
12+
markComment: >
13+
This issue has been automatically marked as stale because it has not had
14+
recent activity. It will be closed if no further activity occurs. Thank you
15+
for your contributions.
16+
# Comment to post when closing a stale issue. Set to `false` to disable
17+
closeComment: false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ profile.cov
3434
# IdeaIDE
3535
.idea
3636

37+
# VS Code
38+
.vscode
39+
3740
# dashboard
3841
/dashboard/assets/flow-typed
3942
/dashboard/assets/node_modules

.travis.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ go_import_path: github.com/ethereum/go-ethereum
33
sudo: false
44
matrix:
55
include:
6-
- os: linux
7-
dist: trusty
8-
sudo: required
9-
go: 1.7.x
10-
script:
11-
- sudo modprobe fuse
12-
- sudo chmod 666 /dev/fuse
13-
- sudo chown root:$USER /etc/fuse.conf
14-
- go run build/ci.go install
15-
- go run build/ci.go test -coverage
16-
176
- os: linux
187
dist: trusty
198
sudo: required

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Official golang implementation of the Ethereum protocol.
55
[![API Reference](
66
https://camo.githubusercontent.com/915b7be44ada53c290eb157634330494ebe3e30a/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f676f6c616e672f6764646f3f7374617475732e737667
77
)](https://godoc.org/github.com/ethereum/go-ethereum)
8+
[![Go Report Card](https://goreportcard.com/badge/github.com/ethereum/go-ethereum)](https://goreportcard.com/report/github.com/ethereum/go-ethereum)
9+
[![Travis](https://travis-ci.org/ethereum/go-ethereum.svg?branch=master)](https://travis-ci.org/ethereum/go-ethereum)
810
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ethereum/go-ethereum?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
911

1012
Automated builds are available for stable releases and the unstable master branch.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.1
1+
1.8.3

accounts/abi/abi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
136136

137137
// MethodById looks up a method by the 4-byte id
138138
// returns nil if none found
139-
func (abi *ABI) MethodById(sigdata []byte) *Method {
139+
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
140140
for _, method := range abi.Methods {
141141
if bytes.Equal(method.Id(), sigdata[:4]) {
142-
return &method
142+
return &method, nil
143143
}
144144
}
145-
return nil
145+
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
146146
}

accounts/abi/abi_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,11 @@ func TestABI_MethodById(t *testing.T) {
702702
}
703703
for name, m := range abi.Methods {
704704
a := fmt.Sprintf("%v", m)
705-
b := fmt.Sprintf("%v", abi.MethodById(m.Id()))
705+
m2, err := abi.MethodById(m.Id())
706+
if err != nil {
707+
t.Fatalf("Failed to look up ABI method: %v", err)
708+
}
709+
b := fmt.Sprintf("%v", m2)
706710
if a != b {
707711
t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
708712
}

accounts/abi/argument.go

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,43 @@ func (arguments Arguments) LengthNonIndexed() int {
6767
return out
6868
}
6969

70+
// NonIndexed returns the arguments with indexed arguments filtered out
71+
func (arguments Arguments) NonIndexed() Arguments {
72+
var ret []Argument
73+
for _, arg := range arguments {
74+
if !arg.Indexed {
75+
ret = append(ret, arg)
76+
}
77+
}
78+
return ret
79+
}
80+
7081
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
7182
func (arguments Arguments) isTuple() bool {
7283
return len(arguments) > 1
7384
}
7485

7586
// Unpack performs the operation hexdata -> Go format
7687
func (arguments Arguments) Unpack(v interface{}, data []byte) error {
77-
if arguments.isTuple() {
78-
return arguments.unpackTuple(v, data)
79-
}
80-
return arguments.unpackAtomic(v, data)
81-
}
8288

83-
func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
8489
// make sure the passed value is arguments pointer
85-
valueOf := reflect.ValueOf(v)
86-
if reflect.Ptr != valueOf.Kind() {
90+
if reflect.Ptr != reflect.ValueOf(v).Kind() {
8791
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
8892
}
93+
marshalledValues, err := arguments.UnpackValues(data)
94+
if err != nil {
95+
return err
96+
}
97+
if arguments.isTuple() {
98+
return arguments.unpackTuple(v, marshalledValues)
99+
}
100+
return arguments.unpackAtomic(v, marshalledValues)
101+
}
102+
103+
func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interface{}) error {
89104

90105
var (
91-
value = valueOf.Elem()
106+
value = reflect.ValueOf(v).Elem()
92107
typ = value.Type()
93108
kind = value.Kind()
94109
)
@@ -110,30 +125,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
110125
exists[field] = true
111126
}
112127
}
113-
// `i` counts the nonindexed arguments.
114-
// `j` counts the number of complex types.
115-
// both `i` and `j` are used to to correctly compute `data` offset.
128+
for i, arg := range arguments.NonIndexed() {
116129

117-
i, j := -1, 0
118-
for _, arg := range arguments {
119-
120-
if arg.Indexed {
121-
// can't read, continue
122-
continue
123-
}
124-
i++
125-
marshalledValue, err := toGoType((i+j)*32, arg.Type, output)
126-
if err != nil {
127-
return err
128-
}
129-
130-
if arg.Type.T == ArrayTy {
131-
// combined index ('i' + 'j') need to be adjusted only by size of array, thus
132-
// we need to decrement 'j' because 'i' was incremented
133-
j += arg.Type.Size - 1
134-
}
135-
136-
reflectValue := reflect.ValueOf(marshalledValue)
130+
reflectValue := reflect.ValueOf(marshalledValues[i])
137131

138132
switch kind {
139133
case reflect.Struct:
@@ -166,34 +160,72 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
166160
}
167161

168162
// unpackAtomic unpacks ( hexdata -> go ) a single value
169-
func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
170-
// make sure the passed value is arguments pointer
171-
valueOf := reflect.ValueOf(v)
172-
if reflect.Ptr != valueOf.Kind() {
173-
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
174-
}
175-
arg := arguments[0]
176-
if arg.Indexed {
177-
return fmt.Errorf("abi: attempting to unpack indexed variable into element.")
163+
func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues []interface{}) error {
164+
if len(marshalledValues) != 1 {
165+
return fmt.Errorf("abi: wrong length, expected single value, got %d", len(marshalledValues))
178166
}
167+
elem := reflect.ValueOf(v).Elem()
168+
reflectValue := reflect.ValueOf(marshalledValues[0])
169+
return set(elem, reflectValue, arguments.NonIndexed()[0])
170+
}
179171

180-
value := valueOf.Elem()
172+
// Computes the full size of an array;
173+
// i.e. counting nested arrays, which count towards size for unpacking.
174+
func getArraySize(arr *Type) int {
175+
size := arr.Size
176+
// Arrays can be nested, with each element being the same size
177+
arr = arr.Elem
178+
for arr.T == ArrayTy {
179+
// Keep multiplying by elem.Size while the elem is an array.
180+
size *= arr.Size
181+
arr = arr.Elem
182+
}
183+
// Now we have the full array size, including its children.
184+
return size
185+
}
181186

182-
marshalledValue, err := toGoType(0, arg.Type, output)
183-
if err != nil {
184-
return err
187+
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
188+
// without supplying a struct to unpack into. Instead, this method returns a list containing the
189+
// values. An atomic argument will be a list with one element.
190+
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
191+
retval := make([]interface{}, 0, arguments.LengthNonIndexed())
192+
virtualArgs := 0
193+
for index, arg := range arguments.NonIndexed() {
194+
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
195+
if arg.Type.T == ArrayTy {
196+
// If we have a static array, like [3]uint256, these are coded as
197+
// just like uint256,uint256,uint256.
198+
// This means that we need to add two 'virtual' arguments when
199+
// we count the index from now on.
200+
//
201+
// Array values nested multiple levels deep are also encoded inline:
202+
// [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
203+
//
204+
// Calculate the full array size to get the correct offset for the next argument.
205+
// Decrement it by 1, as the normal index increment is still applied.
206+
virtualArgs += getArraySize(&arg.Type) - 1
207+
}
208+
if err != nil {
209+
return nil, err
210+
}
211+
retval = append(retval, marshalledValue)
185212
}
186-
return set(value, reflect.ValueOf(marshalledValue), arg)
213+
return retval, nil
187214
}
188215

189-
// Unpack performs the operation Go format -> Hexdata
216+
// PackValues performs the operation Go format -> Hexdata
217+
// It is the semantic opposite of UnpackValues
218+
func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
219+
return arguments.Pack(args...)
220+
}
221+
222+
// Pack performs the operation Go format -> Hexdata
190223
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
191224
// Make sure arguments match up and pack them
192225
abiArgs := arguments
193226
if len(args) != len(abiArgs) {
194227
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
195228
}
196-
197229
// variable input is the output appended at the end of packed
198230
// output. This is used for strings and bytes types input.
199231
var variableInput []byte
@@ -207,7 +239,6 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
207239
inputOffset += 32
208240
}
209241
}
210-
211242
var ret []byte
212243
for i, a := range args {
213244
input := abiArgs[i]
@@ -216,7 +247,6 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
216247
if err != nil {
217248
return nil, err
218249
}
219-
220250
// check for a slice type (string, bytes, slice)
221251
if input.Type.requiresLengthPrefix() {
222252
// calculate the offset

accounts/abi/bind/backends/simulated.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,23 @@ func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumb
428428
}
429429
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
430430
}
431+
431432
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
432433
return core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash)), nil
433434
}
434435

436+
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
437+
receipts := core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash))
438+
if receipts == nil {
439+
return nil, nil
440+
}
441+
logs := make([][]*types.Log, len(receipts))
442+
for i, receipt := range receipts {
443+
logs[i] = receipt.Logs
444+
}
445+
return logs, nil
446+
}
447+
435448
func (fb *filterBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
436449
return event.NewSubscription(func(quit <-chan struct{}) error {
437450
<-quit

0 commit comments

Comments
 (0)