2018-11-23 18:12:24 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-23 18:12:24 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
2018-11-24 04:26:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
2018-11-23 18:12:24 +00:00
|
|
|
|
2018-11-24 04:26:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-12-07 15:38:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-11-24 04:26:07 +00:00
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
2018-11-23 18:12:24 +00:00
|
|
|
|
2019-03-11 23:18:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/types"
|
2018-11-23 18:12:24 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// ConverterInterface is the interface for converting geth logs to our custom log type
|
2019-03-13 16:14:35 +00:00
|
|
|
type ConverterInterface interface {
|
2018-11-24 04:26:07 +00:00
|
|
|
Convert(logs []gethTypes.Log, event types.Event, headerID int64) ([]types.Log, error)
|
2018-12-18 17:31:21 +00:00
|
|
|
ConvertBatch(logs []gethTypes.Log, events map[string]types.Event, headerID int64) (map[string][]types.Log, error)
|
2018-11-23 18:12:24 +00:00
|
|
|
Update(info *contract.Contract)
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// Converter is the underlying struct for the ConverterInterface
|
2019-03-13 16:14:35 +00:00
|
|
|
type Converter struct {
|
2018-11-23 18:12:24 +00:00
|
|
|
ContractInfo *contract.Contract
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// Update is used to configure the converter with a specific contract
|
2019-03-13 16:14:35 +00:00
|
|
|
func (c *Converter) Update(info *contract.Contract) {
|
2018-11-23 18:12:24 +00:00
|
|
|
c.ContractInfo = info
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the given watched event log into a types.Log for the given event
|
2019-03-13 16:14:35 +00:00
|
|
|
func (c *Converter) Convert(logs []gethTypes.Log, event types.Event, headerID int64) ([]types.Log, error) {
|
2019-03-14 21:49:27 +00:00
|
|
|
boundContract := bind.NewBoundContract(common.HexToAddress(c.ContractInfo.Address), c.ContractInfo.ParsedAbi, nil, nil, nil)
|
2018-11-24 04:26:07 +00:00
|
|
|
returnLogs := make([]types.Log, 0, len(logs))
|
|
|
|
for _, log := range logs {
|
|
|
|
values := make(map[string]interface{})
|
|
|
|
for _, field := range event.Fields {
|
|
|
|
var i interface{}
|
|
|
|
values[field.Name] = i
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:49:27 +00:00
|
|
|
err := boundContract.UnpackLogIntoMap(values, event.Name, log)
|
2018-11-24 04:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
strValues := make(map[string]string, len(values))
|
2018-12-07 15:38:46 +00:00
|
|
|
seenAddrs := make([]interface{}, 0, len(values))
|
|
|
|
seenHashes := make([]interface{}, 0, len(values))
|
2018-11-24 04:26:07 +00:00
|
|
|
for fieldName, input := range values {
|
|
|
|
// Postgres cannot handle custom types, resolve everything to strings
|
|
|
|
switch input.(type) {
|
|
|
|
case *big.Int:
|
|
|
|
b := input.(*big.Int)
|
|
|
|
strValues[fieldName] = b.String()
|
|
|
|
case common.Address:
|
|
|
|
a := input.(common.Address)
|
|
|
|
strValues[fieldName] = a.String()
|
2018-12-07 15:38:46 +00:00
|
|
|
seenAddrs = append(seenAddrs, a)
|
2018-11-24 04:26:07 +00:00
|
|
|
case common.Hash:
|
|
|
|
h := input.(common.Hash)
|
|
|
|
strValues[fieldName] = h.String()
|
2018-12-07 15:38:46 +00:00
|
|
|
seenHashes = append(seenHashes, h)
|
2018-11-24 04:26:07 +00:00
|
|
|
case string:
|
|
|
|
strValues[fieldName] = input.(string)
|
|
|
|
case bool:
|
|
|
|
strValues[fieldName] = strconv.FormatBool(input.(bool))
|
|
|
|
case []byte:
|
|
|
|
b := input.([]byte)
|
2018-12-07 15:38:46 +00:00
|
|
|
strValues[fieldName] = hexutil.Encode(b)
|
2018-12-14 17:52:02 +00:00
|
|
|
if len(b) == 32 {
|
|
|
|
seenHashes = append(seenHashes, common.HexToHash(strValues[fieldName]))
|
|
|
|
}
|
2019-09-21 02:37:59 +00:00
|
|
|
case uint8:
|
|
|
|
u := input.(uint8)
|
|
|
|
strValues[fieldName] = strconv.Itoa(int(u))
|
2019-09-20 18:48:43 +00:00
|
|
|
case [32]uint8:
|
|
|
|
raw := input.([32]uint8)
|
|
|
|
converted := convertUintSliceToHash(raw)
|
|
|
|
strValues[fieldName] = converted.String()
|
|
|
|
seenHashes = append(seenHashes, converted)
|
2018-11-24 04:26:07 +00:00
|
|
|
default:
|
2019-10-15 18:12:17 +00:00
|
|
|
return nil, fmt.Errorf("error: unhandled abi type %T", input)
|
2018-11-24 04:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only hold onto logs that pass our address filter, if any
|
|
|
|
if c.ContractInfo.PassesEventFilter(strValues) {
|
|
|
|
raw, err := json.Marshal(log)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
returnLogs = append(returnLogs, types.Log{
|
|
|
|
LogIndex: log.Index,
|
|
|
|
Values: strValues,
|
|
|
|
Raw: raw,
|
|
|
|
TransactionIndex: log.TxIndex,
|
2019-10-15 18:12:17 +00:00
|
|
|
ID: headerID,
|
2018-11-24 04:26:07 +00:00
|
|
|
})
|
2018-12-07 15:38:46 +00:00
|
|
|
|
|
|
|
// Cache emitted values if their caching is turned on
|
|
|
|
if c.ContractInfo.EmittedAddrs != nil {
|
|
|
|
c.ContractInfo.AddEmittedAddr(seenAddrs...)
|
|
|
|
}
|
|
|
|
if c.ContractInfo.EmittedHashes != nil {
|
|
|
|
c.ContractInfo.AddEmittedHash(seenHashes...)
|
|
|
|
}
|
2018-11-24 04:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnLogs, nil
|
2018-11-23 18:12:24 +00:00
|
|
|
}
|
2018-12-18 17:31:21 +00:00
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// ConvertBatch converts the given watched event logs into types.Logs; returns a map of event names to a slice of their converted logs
|
2019-03-13 16:14:35 +00:00
|
|
|
func (c *Converter) ConvertBatch(logs []gethTypes.Log, events map[string]types.Event, headerID int64) (map[string][]types.Log, error) {
|
2019-09-24 03:39:04 +00:00
|
|
|
boundContract := bind.NewBoundContract(common.HexToAddress(c.ContractInfo.Address), c.ContractInfo.ParsedAbi, nil, nil, nil)
|
2018-12-18 17:31:21 +00:00
|
|
|
eventsToLogs := make(map[string][]types.Log)
|
|
|
|
for _, event := range events {
|
|
|
|
eventsToLogs[event.Name] = make([]types.Log, 0, len(logs))
|
|
|
|
// Iterate through all event logs
|
|
|
|
for _, log := range logs {
|
|
|
|
// If the log is of this event type, process it as such
|
|
|
|
if event.Sig() == log.Topics[0] {
|
|
|
|
values := make(map[string]interface{})
|
2019-09-24 03:39:04 +00:00
|
|
|
err := boundContract.UnpackLogIntoMap(values, event.Name, log)
|
2018-12-18 17:31:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-19 18:42:59 +00:00
|
|
|
// Postgres cannot handle custom types, so we will resolve everything to strings
|
2018-12-18 17:31:21 +00:00
|
|
|
strValues := make(map[string]string, len(values))
|
2018-12-19 18:42:59 +00:00
|
|
|
// Keep track of addresses and hashes emitted from events
|
2018-12-18 17:31:21 +00:00
|
|
|
seenAddrs := make([]interface{}, 0, len(values))
|
|
|
|
seenHashes := make([]interface{}, 0, len(values))
|
|
|
|
for fieldName, input := range values {
|
|
|
|
switch input.(type) {
|
|
|
|
case *big.Int:
|
|
|
|
b := input.(*big.Int)
|
|
|
|
strValues[fieldName] = b.String()
|
|
|
|
case common.Address:
|
|
|
|
a := input.(common.Address)
|
|
|
|
strValues[fieldName] = a.String()
|
|
|
|
seenAddrs = append(seenAddrs, a)
|
|
|
|
case common.Hash:
|
|
|
|
h := input.(common.Hash)
|
|
|
|
strValues[fieldName] = h.String()
|
|
|
|
seenHashes = append(seenHashes, h)
|
|
|
|
case string:
|
|
|
|
strValues[fieldName] = input.(string)
|
|
|
|
case bool:
|
|
|
|
strValues[fieldName] = strconv.FormatBool(input.(bool))
|
|
|
|
case []byte:
|
|
|
|
b := input.([]byte)
|
|
|
|
strValues[fieldName] = hexutil.Encode(b)
|
2018-12-19 18:42:59 +00:00
|
|
|
if len(b) == 32 { // collect byte arrays of size 32 as hashes
|
|
|
|
seenHashes = append(seenHashes, common.BytesToHash(b))
|
2018-12-18 17:31:21 +00:00
|
|
|
}
|
2019-09-21 02:37:59 +00:00
|
|
|
case uint8:
|
|
|
|
u := input.(uint8)
|
|
|
|
strValues[fieldName] = strconv.Itoa(int(u))
|
2019-09-20 18:48:43 +00:00
|
|
|
case [32]uint8:
|
|
|
|
raw := input.([32]uint8)
|
|
|
|
converted := convertUintSliceToHash(raw)
|
|
|
|
strValues[fieldName] = converted.String()
|
|
|
|
seenHashes = append(seenHashes, converted)
|
2018-12-18 17:31:21 +00:00
|
|
|
default:
|
2019-10-15 18:12:17 +00:00
|
|
|
return nil, fmt.Errorf("error: unhandled abi type %T", input)
|
2018-12-18 17:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
// Only hold onto logs that pass our argument filter, if any
|
2018-12-18 17:31:21 +00:00
|
|
|
if c.ContractInfo.PassesEventFilter(strValues) {
|
|
|
|
raw, err := json.Marshal(log)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsToLogs[event.Name] = append(eventsToLogs[event.Name], types.Log{
|
|
|
|
LogIndex: log.Index,
|
|
|
|
Values: strValues,
|
|
|
|
Raw: raw,
|
|
|
|
TransactionIndex: log.TxIndex,
|
2019-10-15 18:12:17 +00:00
|
|
|
ID: headerID,
|
2018-12-18 17:31:21 +00:00
|
|
|
})
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
// Cache emitted values that pass the argument filter if their caching is turned on
|
2018-12-18 17:31:21 +00:00
|
|
|
if c.ContractInfo.EmittedAddrs != nil {
|
|
|
|
c.ContractInfo.AddEmittedAddr(seenAddrs...)
|
|
|
|
}
|
|
|
|
if c.ContractInfo.EmittedHashes != nil {
|
|
|
|
c.ContractInfo.AddEmittedHash(seenHashes...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventsToLogs, nil
|
|
|
|
}
|
2019-09-20 18:48:43 +00:00
|
|
|
|
|
|
|
func convertUintSliceToHash(raw [32]uint8) common.Hash {
|
|
|
|
var asBytes []byte
|
|
|
|
for _, u := range raw {
|
|
|
|
asBytes = append(asBytes, u)
|
|
|
|
}
|
|
|
|
return common.BytesToHash(asBytes)
|
|
|
|
}
|