mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-16 17:03:46 +00:00
accounts/abi: Indexed inputs are not present in output
This commit is contained in:
parent
0dbf55d478
commit
e3bca640ca
2 changed files with 64 additions and 0 deletions
|
|
@ -69,6 +69,8 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error {
|
|||
for i := 0; i < len(e.Inputs); i++ {
|
||||
input := e.Inputs[i]
|
||||
if input.Indexed {
|
||||
// indexed inputs are not available in log output
|
||||
j--
|
||||
// can't read, continue
|
||||
continue
|
||||
} else if input.Type.T == ArrayTy {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,14 @@
|
|||
package abi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
||||
|
|
@ -54,3 +58,61 @@ func TestEventId(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testResult struct {
|
||||
Value1 *big.Int
|
||||
Value2 *big.Int
|
||||
}
|
||||
type testCase struct {
|
||||
definition string
|
||||
want testResult
|
||||
}
|
||||
|
||||
func (tc testCase) encoded() []byte {
|
||||
var b bytes.Buffer
|
||||
if tc.want.Value1 != nil {
|
||||
b.Write(math.PaddedBigBytes(math.U256(tc.want.Value1), 32))
|
||||
}
|
||||
if tc.want.Value2 != nil {
|
||||
b.Write(math.PaddedBigBytes(math.U256(tc.want.Value2), 32))
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func TestEventUnpack(t *testing.T) {
|
||||
|
||||
table := []testCase{
|
||||
{
|
||||
definition: `[{"anonymous":false,"inputs":[{"indexed":true,"name":"value1","type":"uint256"},{"indexed":false,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
||||
want: testResult{Value2: big.NewInt(10)},
|
||||
},
|
||||
{
|
||||
definition: `[{"anonymous":false,"inputs":[{"indexed":false,"name":"value1","type":"uint256"},{"indexed":false,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
||||
want: testResult{Value1: big.NewInt(100), Value2: big.NewInt(1)},
|
||||
},
|
||||
{
|
||||
definition: `[{"anonymous":false,"inputs":[{"indexed":false,"name":"value1","type":"uint256"},{"indexed":true,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
||||
want: testResult{Value1: big.NewInt(100)},
|
||||
},
|
||||
}
|
||||
for i, row := range table {
|
||||
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
|
||||
t.Logf("unpacking %b with expected %v", row.encoded(), row.want)
|
||||
abi, err := JSON(strings.NewReader(row.definition))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rst testResult
|
||||
if err := abi.Unpack(&rst, "transfer", row.encoded()); err != nil {
|
||||
t.Fatalf("error unpacking %s: %v", row.definition, err)
|
||||
}
|
||||
if row.want.Value1 != nil && rst.Value1.Cmp(row.want.Value1) != 0 {
|
||||
t.Errorf("result value1 %v is not equal to expected %v", rst.Value1, row.want.Value1)
|
||||
}
|
||||
|
||||
if row.want.Value2 != nil && rst.Value2.Cmp(row.want.Value2) != 0 {
|
||||
t.Errorf("result value2 %v is not equal to expected %v", rst.Value2, row.want.Value2)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue