abi: Add some unit tests

Co-authored-by: Victor Tran <vu.tran54@gmail.com>
This commit is contained in:
Guillaume Ballet 2019-06-24 10:59:18 +02:00
parent 8096613fda
commit 17ea9f0e64
2 changed files with 64 additions and 29 deletions

View file

@ -148,13 +148,13 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4]) return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
} }
// EventById looks up a event by the topic hash // EventByID looks up a event by the topic hash
// returns nil if none found // returns nil if none found
func (abi *ABI) EventById(topic common.Hash) *Event { func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
for _, event := range abi.Events { for _, event := range abi.Events {
if event.Id() == topic { if bytes.Equal(event.Id().Bytes(), topic.Bytes()) {
return &event return &event, nil
} }
} }
return nil return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
} }

View file

@ -747,34 +747,69 @@ func TestABI_MethodById(t *testing.T) {
} }
func TestABI_EventById(t *testing.T) { func TestABI_EventById(t *testing.T) {
const abiJSON = `[ tests := []struct {
name string
json string
event string
}{
{
name: "",
json: `[
{"type":"event","name":"received","anonymous":false,"inputs":[ {"type":"event","name":"received","anonymous":false,"inputs":[
{"indexed":false,"name":"sender","type":"address"}, {"indexed":false,"name":"sender","type":"address"},
{"indexed":false,"name":"amount","type":"uint256"}, {"indexed":false,"name":"amount","type":"uint256"},
{"indexed":false,"name":"memo","type":"bytes"} {"indexed":false,"name":"memo","type":"bytes"}
] ]
}]` }]`,
event: "received(address,uint256,bytes)",
}, {
name: "",
json: `[
{ "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" },
{ "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" },
{ "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "constant": true, "inputs": [ { "name": "_owner", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "balance", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" },
{ "constant": true, "inputs": [ { "name": "_owner", "type": "address" }, { "name": "_spender", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" },
{ "payable": true, "stateMutability": "payable", "type": "fallback" },
{ "anonymous": false, "inputs": [ { "indexed": true, "name": "owner", "type": "address" }, { "indexed": true, "name": "spender", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Approval", "type": "event" },
{ "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }
]`,
event: "Transfer(address,address,uint256)",
},
}
abi, err := JSON(strings.NewReader(abiJSON)) for testnum, test := range tests {
abi, err := JSON(strings.NewReader(test.json))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
topic := "received(address,uint256,bytes)" topic := test.event
topicID := crypto.Keccak256Hash([]byte(topic)) topicID := crypto.Keccak256Hash([]byte(topic))
event := abi.EventById(topicID) event, err := abi.EventByID(topicID)
if err != nil {
t.Fatalf("Failed to look up ABI method: %v, test #%d", err, testnum)
}
if event == nil { if event == nil {
t.Errorf("we should find a event for topic %s", topicID.Hex()) 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", event.Id().Hex(), topicID.Hex()) 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"))
unknownEvent := abi.EventById(unknowntopicID) unknownEvent, err := abi.EventByID(unknowntopicID)
if err == nil {
t.Errorf("EventByID should return an error if a topic is not found, test #%d", testnum)
}
if unknownEvent != nil { if unknownEvent != nil {
t.Errorf("we should not find any event for topic %s", unknowntopicID.Hex()) t.Errorf("We should not find any event for topic %s, test #%d", unknowntopicID.Hex(), testnum)
}
} }
} }