2019-01-24 20:41:30 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-24 20:41:30 +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/>.
|
2018-08-07 15:51:34 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package fetcher
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2018-08-07 15:51:34 +00:00
|
|
|
)
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
type ILogFetcher interface {
|
2018-11-30 16:28:52 +00:00
|
|
|
FetchLogs(contractAddresses []common.Address, topics []common.Hash, missingHeader core.Header) ([]types.Log, error)
|
2019-08-06 21:57:08 +00:00
|
|
|
// TODO Extend FetchLogs for doing several blocks at a time
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
type LogFetcher struct {
|
2018-08-09 21:55:02 +00:00
|
|
|
blockChain core.BlockChain
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
func NewLogFetcher(blockchain core.BlockChain) *LogFetcher {
|
|
|
|
return &LogFetcher{
|
2018-08-09 21:55:02 +00:00
|
|
|
blockChain: blockchain,
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 16:28:52 +00:00
|
|
|
// Checks all topic0s, on all addresses, fetching matching logs for the given header
|
2019-04-24 20:05:57 +00:00
|
|
|
func (logFetcher LogFetcher) FetchLogs(addresses []common.Address, topic0s []common.Hash, header core.Header) ([]types.Log, error) {
|
2018-11-21 15:30:00 +00:00
|
|
|
blockHash := common.HexToHash(header.Hash)
|
2018-08-07 15:51:34 +00:00
|
|
|
query := ethereum.FilterQuery{
|
2018-11-21 15:30:00 +00:00
|
|
|
BlockHash: &blockHash,
|
2018-09-27 15:12:29 +00:00
|
|
|
Addresses: addresses,
|
2018-11-30 16:28:52 +00:00
|
|
|
// Search for _any_ of the topics in topic0 position; see docs on `FilterQuery`
|
|
|
|
Topics: [][]common.Hash{topic0s},
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
2018-09-27 15:12:29 +00:00
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
logs, err := logFetcher.blockChain.GetEthLogsWithCustomQuery(query)
|
2018-11-30 16:28:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO review aggregate fetching error handling
|
|
|
|
return []types.Log{}, err
|
2018-09-27 15:12:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 16:28:52 +00:00
|
|
|
return logs, nil
|
2018-10-12 14:13:13 +00:00
|
|
|
}
|