accounts/abi: address comments

This commit is contained in:
rjl493456442 2019-07-25 18:03:11 +08:00
parent 91dfad475c
commit dad8847271
7 changed files with 35 additions and 35 deletions

View file

@ -70,7 +70,7 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
return nil, err return nil, err
} }
// Pack up the method ID too if not a constructor and return // Pack up the method ID too if not a constructor and return
return append(method.Id(), arguments...), nil return append(method.ID(), arguments...), nil
} }
// Unpack output in v according to the abi specification // Unpack output in v according to the abi specification
@ -173,7 +173,7 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata)) return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata))
} }
for _, method := range abi.Methods { for _, method := range abi.Methods {
if bytes.Equal(method.Id(), sigdata[:4]) { if bytes.Equal(method.ID(), sigdata[:4]) {
return &method, nil return &method, nil
} }
} }
@ -184,7 +184,7 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
// ABI and returns nil if none found. // ABI and returns nil if none found.
func (abi *ABI) EventByID(topic common.Hash) (*Event, error) { func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
for _, event := range abi.Events { for _, event := range abi.Events {
if bytes.Equal(event.Id().Bytes(), topic.Bytes()) { if bytes.Equal(event.ID().Bytes(), topic.Bytes()) {
return &event, nil return &event, nil
} }
} }

View file

@ -180,8 +180,8 @@ func TestMethodSignature(t *testing.T) {
} }
idexp := crypto.Keccak256([]byte(exp))[:4] idexp := crypto.Keccak256([]byte(exp))[:4]
if !bytes.Equal(m.Id(), idexp) { if !bytes.Equal(m.ID(), idexp) {
t.Errorf("expected ids to match %x != %x", m.Id(), idexp) t.Errorf("expected ids to match %x != %x", m.ID(), idexp)
} }
uintt, _ := NewType("uint256", nil) uintt, _ := NewType("uint256", nil)
@ -921,13 +921,13 @@ func TestABI_MethodById(t *testing.T) {
} }
for name, m := range abi.Methods { for name, m := range abi.Methods {
a := fmt.Sprintf("%v", m) a := fmt.Sprintf("%v", m)
m2, err := abi.MethodById(m.Id()) m2, err := abi.MethodById(m.ID())
if err != nil { if err != nil {
t.Fatalf("Failed to look up ABI method: %v", err) t.Fatalf("Failed to look up ABI method: %v", err)
} }
b := fmt.Sprintf("%v", m2) b := fmt.Sprintf("%v", m2)
if a != b { if a != b {
t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id())) t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.ID()))
} }
} }
// Also test empty // Also test empty
@ -995,8 +995,8 @@ func TestABI_EventById(t *testing.T) {
t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum) t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum)
} }
if event.Id() != topicID { if event.ID() != topicID {
t.Errorf("Event id %s does not match topic %s, test #%d", event.Id().Hex(), topicID.Hex(), testnum) t.Errorf("Event id %s does not match topic %s, test #%d", event.ID().Hex(), topicID.Hex(), testnum)
} }
unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent")) unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent"))

View file

@ -252,7 +252,7 @@ func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]int
opts = new(FilterOpts) opts = new(FilterOpts)
} }
// Append the event selector to the query parameters and construct the topic set // Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{c.abi.Events[name].Id()}}, query...) query = append([][]interface{}{{c.abi.Events[name].ID()}}, query...)
topics, err := makeTopics(query...) topics, err := makeTopics(query...)
if err != nil { if err != nil {
@ -301,7 +301,7 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
opts = new(WatchOpts) opts = new(WatchOpts)
} }
// Append the event selector to the query parameters and construct the topic set // Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{c.abi.Events[name].Id()}}, query...) query = append([][]interface{}{{c.abi.Events[name].ID()}}, query...)
topics, err := makeTopics(query...) topics, err := makeTopics(query...)
if err != nil { if err != nil {

View file

@ -28,18 +28,17 @@ import (
// holds type information (inputs) about the yielded output. Anonymous events // holds type information (inputs) about the yielded output. Anonymous events
// don't get the signature canonical representation as the first LOG topic. // don't get the signature canonical representation as the first LOG topic.
type Event struct { type Event struct {
// Name the name of the event used for internal representation. which // Name is the event name used for internal representation. It's derived from
// It's derived from raw name and will be added suffix when event // the raw name and a suffix will be added in the case of a event overload.
// overload occurs.
// //
// e.g. // e.g.
// There are two events have same name: // There are two events have same name:
// * foo(int,int) // * foo(int,int)
// * foo(uint,uint) // * foo(uint,uint)
// The method name of the first one can be resolved as foo while the // The method name of the first one wll be resolved as foo while the second one
// second one can be resolved as foo0. // will be resolved as foo0.
Name string Name string
// RawName raw method name parsed from ABI // RawName is the raw event name parsed from ABI.
RawName string RawName string
Anonymous bool Anonymous bool
Inputs Arguments Inputs Arguments
@ -71,8 +70,8 @@ func (e Event) Sig() string {
return fmt.Sprintf("%v(%v)", e.RawName, strings.Join(types, ",")) return fmt.Sprintf("%v(%v)", e.RawName, strings.Join(types, ","))
} }
// Id returns the canonical representation of the event's signature used by the // ID returns the canonical representation of the event's signature used by the
// abi definition to identify event names and types. // abi definition to identify event names and types.
func (e Event) Id() common.Hash { func (e Event) ID() common.Hash {
return common.BytesToHash(crypto.Keccak256([]byte(e.Sig()))) return common.BytesToHash(crypto.Keccak256([]byte(e.Sig())))
} }

View file

@ -104,8 +104,8 @@ func TestEventId(t *testing.T) {
} }
for name, event := range abi.Events { for name, event := range abi.Events {
if event.Id() != test.expectations[name] { if event.ID() != test.expectations[name] {
t.Errorf("expected id to be %x, got %x", test.expectations[name], event.Id()) t.Errorf("expected id to be %x, got %x", test.expectations[name], event.ID())
} }
} }
} }

View file

@ -32,18 +32,17 @@ import (
// be flagged `false`. // be flagged `false`.
// Input specifies the required input parameters for this gives method. // Input specifies the required input parameters for this gives method.
type Method struct { type Method struct {
// Name the name of the method used for internal representation. which // Name is the method name used for internal representation. It's derived from
// It's derived from raw name and will be added suffix when function // the raw name and a suffix will be added in the case of a function overload.
// overload occurs.
// //
// e.g. // e.g.
// There are two functions have same name: // There are two functions have same name:
// * foo(int,int) // * foo(int,int)
// * foo(uint,uint) // * foo(uint,uint)
// The method name of the first one can be resolved as foo while the // The method name of the first one will be resolved as foo while the second one
// second one can be resolved as foo0. // will be resolved as foo0.
Name string Name string
// RawName raw method name parsed from ABI // RawName is the raw method name parsed from ABI.
RawName string RawName string
Const bool Const bool
Inputs Arguments Inputs Arguments
@ -84,6 +83,8 @@ func (method Method) String() string {
return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
} }
func (method Method) Id() []byte { // ID returns the canonical representation of the method's signature used by the
// abi definition to identify method names and types.
func (method Method) ID() []byte {
return crypto.Keccak256([]byte(method.Sig()))[:4] return crypto.Keccak256([]byte(method.Sig()))[:4]
} }

View file

@ -634,7 +634,7 @@ func TestMethodPack(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
sig := abi.Methods["slice"].Id() sig := abi.Methods["slice"].ID()
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
@ -648,7 +648,7 @@ func TestMethodPack(t *testing.T) {
} }
var addrA, addrB = common.Address{1}, common.Address{2} var addrA, addrB = common.Address{1}, common.Address{2}
sig = abi.Methods["sliceAddress"].Id() sig = abi.Methods["sliceAddress"].ID()
sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
@ -663,7 +663,7 @@ func TestMethodPack(t *testing.T) {
} }
var addrC, addrD = common.Address{3}, common.Address{4} var addrC, addrD = common.Address{3}, common.Address{4}
sig = abi.Methods["sliceMultiAddress"].Id() sig = abi.Methods["sliceMultiAddress"].ID()
sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
@ -681,7 +681,7 @@ func TestMethodPack(t *testing.T) {
t.Errorf("expected %x got %x", sig, packed) t.Errorf("expected %x got %x", sig, packed)
} }
sig = abi.Methods["slice256"].Id() sig = abi.Methods["slice256"].ID()
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
@ -695,7 +695,7 @@ func TestMethodPack(t *testing.T) {
} }
a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}} a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
sig = abi.Methods["nestedArray"].Id() sig = abi.Methods["nestedArray"].ID()
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
@ -712,7 +712,7 @@ func TestMethodPack(t *testing.T) {
t.Errorf("expected %x got %x", sig, packed) t.Errorf("expected %x got %x", sig, packed)
} }
sig = abi.Methods["nestedArray2"].Id() sig = abi.Methods["nestedArray2"].ID()
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
@ -728,7 +728,7 @@ func TestMethodPack(t *testing.T) {
t.Errorf("expected %x got %x", sig, packed) t.Errorf("expected %x got %x", sig, packed)
} }
sig = abi.Methods["nestedSlice"].Id() sig = abi.Methods["nestedSlice"].ID()
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)