2019-05-21 19:27:24 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth
|
2019-05-21 19:27:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-19 20:23:45 +00:00
|
|
|
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
|
2019-05-21 19:27:24 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-07 13:42:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-02-20 22:13:19 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
2019-06-07 13:42:10 +00:00
|
|
|
|
2020-09-02 15:19:25 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/eth"
|
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/ipfs/ipld"
|
2019-05-21 19:27:24 +00:00
|
|
|
)
|
|
|
|
|
2020-08-31 15:47:06 +00:00
|
|
|
// Filterer interface for substituing mocks in tests
|
|
|
|
type Filterer interface {
|
2020-10-20 14:42:09 +00:00
|
|
|
Filter(filter SubscriptionSettings, payload eth.ConvertedPayload) (*IPLDs, error)
|
2020-08-31 15:47:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// ResponseFilterer satisfies the ResponseFilterer interface for ethereum
|
|
|
|
type ResponseFilterer struct{}
|
2019-05-21 19:27:24 +00:00
|
|
|
|
2019-08-28 19:43:27 +00:00
|
|
|
// NewResponseFilterer creates a new Filterer satisfying the ResponseFilterer interface
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewResponseFilterer() *ResponseFilterer {
|
|
|
|
return &ResponseFilterer{}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Filter is used to filter through eth data to extract and package requested data into a Payload
|
2020-10-20 14:42:09 +00:00
|
|
|
func (s *ResponseFilterer) Filter(filter SubscriptionSettings, payload eth.ConvertedPayload) (*IPLDs, error) {
|
2020-08-31 15:47:06 +00:00
|
|
|
if checkRange(filter.Start.Int64(), filter.End.Int64(), payload.Block.Number().Int64()) {
|
2020-10-20 14:42:09 +00:00
|
|
|
response := new(IPLDs)
|
2020-08-31 15:47:06 +00:00
|
|
|
response.TotalDifficulty = payload.TotalDifficulty
|
|
|
|
if err := s.filterHeaders(filter.HeaderFilter, response, payload); err != nil {
|
|
|
|
return nil, err
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
txHashes, err := s.filterTransactions(filter.TxFilter, response, payload)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-31 15:47:06 +00:00
|
|
|
return nil, err
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
var filterTxs []common.Hash
|
2020-08-31 15:47:06 +00:00
|
|
|
if filter.ReceiptFilter.MatchTxs {
|
2020-01-21 19:12:35 +00:00
|
|
|
filterTxs = txHashes
|
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
if err := s.filerReceipts(filter.ReceiptFilter, response, payload, filterTxs); err != nil {
|
|
|
|
return nil, err
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
if err := s.filterStateAndStorage(filter.StateFilter, filter.StorageFilter, response, payload); err != nil {
|
|
|
|
return nil, err
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
response.BlockNumber = payload.Block.Number()
|
|
|
|
return response, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
return nil, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 14:42:09 +00:00
|
|
|
func (s *ResponseFilterer) filterHeaders(headerFilter HeaderFilter, response *IPLDs, payload eth.ConvertedPayload) error {
|
2020-01-16 20:48:38 +00:00
|
|
|
if !headerFilter.Off {
|
2020-01-30 22:35:31 +00:00
|
|
|
headerRLP, err := rlp.EncodeToBytes(payload.Block.Header())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthHeader, headerRLP, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
response.Header = ipfs.BlockModel{
|
2020-02-20 22:13:19 +00:00
|
|
|
Data: headerRLP,
|
|
|
|
CID: cid.String(),
|
2020-02-23 23:14:29 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
if headerFilter.Uncles {
|
2020-02-20 22:13:19 +00:00
|
|
|
response.Uncles = make([]ipfs.BlockModel, len(payload.Block.Body().Uncles))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, uncle := range payload.Block.Body().Uncles {
|
2019-05-21 19:27:24 +00:00
|
|
|
uncleRlp, err := rlp.EncodeToBytes(uncle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthHeader, uncleRlp, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response.Uncles[i] = ipfs.BlockModel{
|
|
|
|
Data: uncleRlp,
|
|
|
|
CID: cid.String(),
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRange(start, end, actual int64) bool {
|
|
|
|
if (end <= 0 || end >= actual) && start <= actual {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:42:09 +00:00
|
|
|
func (s *ResponseFilterer) filterTransactions(trxFilter TxFilter, response *IPLDs, payload eth.ConvertedPayload) ([]common.Hash, error) {
|
2020-02-20 22:12:52 +00:00
|
|
|
var trxHashes []common.Hash
|
2020-01-16 20:48:38 +00:00
|
|
|
if !trxFilter.Off {
|
2020-02-20 22:12:52 +00:00
|
|
|
trxLen := len(payload.Block.Body().Transactions)
|
|
|
|
trxHashes = make([]common.Hash, 0, trxLen)
|
2020-02-20 22:13:19 +00:00
|
|
|
response.Transactions = make([]ipfs.BlockModel, 0, trxLen)
|
2020-01-17 23:16:01 +00:00
|
|
|
for i, trx := range payload.Block.Body().Transactions {
|
2020-02-23 23:14:29 +00:00
|
|
|
// TODO: check if want corresponding receipt and if we do we must include this transaction
|
2020-02-05 01:02:01 +00:00
|
|
|
if checkTransactionAddrs(trxFilter.Src, trxFilter.Dst, payload.TxMetaData[i].Src, payload.TxMetaData[i].Dst) {
|
2019-05-21 19:27:24 +00:00
|
|
|
trxBuffer := new(bytes.Buffer)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err := trx.EncodeRLP(trxBuffer); err != nil {
|
2019-05-21 19:27:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
data := trxBuffer.Bytes()
|
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthTx, data, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.Transactions = append(response.Transactions, ipfs.BlockModel{
|
|
|
|
Data: data,
|
|
|
|
CID: cid.String(),
|
|
|
|
})
|
2019-05-21 19:27:24 +00:00
|
|
|
trxHashes = append(trxHashes, trx.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trxHashes, nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 01:02:01 +00:00
|
|
|
// checkTransactionAddrs returns true if either the transaction src and dst are one of the wanted src and dst addresses
|
|
|
|
func checkTransactionAddrs(wantedSrc, wantedDst []string, actualSrc, actualDst string) bool {
|
2019-05-21 19:27:24 +00:00
|
|
|
// If we aren't filtering for any addresses, every transaction is a go
|
|
|
|
if len(wantedDst) == 0 && len(wantedSrc) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, src := range wantedSrc {
|
|
|
|
if src == actualSrc {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, dst := range wantedDst {
|
|
|
|
if dst == actualDst {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:42:09 +00:00
|
|
|
func (s *ResponseFilterer) filerReceipts(receiptFilter ReceiptFilter, response *IPLDs, payload eth.ConvertedPayload, trxHashes []common.Hash) error {
|
2020-01-16 20:48:38 +00:00
|
|
|
if !receiptFilter.Off {
|
2020-02-20 22:13:19 +00:00
|
|
|
response.Receipts = make([]ipfs.BlockModel, 0, len(payload.Receipts))
|
2019-05-21 19:27:24 +00:00
|
|
|
for i, receipt := range payload.Receipts {
|
2020-01-21 19:12:35 +00:00
|
|
|
// topics is always length 4
|
|
|
|
topics := [][]string{payload.ReceiptMetaData[i].Topic0s, payload.ReceiptMetaData[i].Topic1s, payload.ReceiptMetaData[i].Topic2s, payload.ReceiptMetaData[i].Topic3s}
|
2020-04-02 03:34:06 +00:00
|
|
|
if checkReceipts(receipt, receiptFilter.Topics, topics, receiptFilter.LogAddresses, payload.ReceiptMetaData[i].LogContracts, trxHashes) {
|
2019-05-21 19:27:24 +00:00
|
|
|
receiptBuffer := new(bytes.Buffer)
|
2020-02-20 22:13:19 +00:00
|
|
|
if err := receipt.EncodeRLP(receiptBuffer); err != nil {
|
2019-05-21 19:27:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
data := receiptBuffer.Bytes()
|
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthTxReceipt, data, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response.Receipts = append(response.Receipts, ipfs.BlockModel{
|
|
|
|
Data: data,
|
|
|
|
CID: cid.String(),
|
|
|
|
})
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:34:06 +00:00
|
|
|
func checkReceipts(rct *types.Receipt, wantedTopics, actualTopics [][]string, wantedAddresses []string, actualAddresses []string, wantedTrxHashes []common.Hash) bool {
|
2020-01-16 20:48:38 +00:00
|
|
|
// If we aren't filtering for any topics, contracts, or corresponding trxs then all receipts are a go
|
2020-04-02 03:34:06 +00:00
|
|
|
if len(wantedTopics) == 0 && len(wantedAddresses) == 0 && len(wantedTrxHashes) == 0 {
|
2019-05-21 19:27:24 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
// Keep receipts that are from watched txs
|
|
|
|
for _, wantedTrxHash := range wantedTrxHashes {
|
|
|
|
if bytes.Equal(wantedTrxHash.Bytes(), rct.TxHash.Bytes()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there are no wanted contract addresses, we keep all receipts that match the topic filter
|
2020-04-02 03:34:06 +00:00
|
|
|
if len(wantedAddresses) == 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
if match := filterMatch(wantedTopics, actualTopics); match == true {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there are wanted contract addresses to filter on
|
2020-04-02 03:34:06 +00:00
|
|
|
for _, wantedAddr := range wantedAddresses {
|
2020-01-21 19:12:35 +00:00
|
|
|
// and this is an address of interest
|
2020-04-02 03:34:06 +00:00
|
|
|
for _, actualAddr := range actualAddresses {
|
|
|
|
if wantedAddr == actualAddr {
|
|
|
|
// we keep the receipt if it matches on the topic filter
|
|
|
|
if match := filterMatch(wantedTopics, actualTopics); match == true {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-06-20 15:59:10 +00:00
|
|
|
|
2020-02-05 01:02:01 +00:00
|
|
|
// filterMatch returns true if the actualTopics conform to the wantedTopics filter
|
2020-01-21 19:12:35 +00:00
|
|
|
func filterMatch(wantedTopics, actualTopics [][]string) bool {
|
2020-02-05 01:02:01 +00:00
|
|
|
// actualTopics should always be length 4, but the members can be nil slices
|
2020-01-21 19:12:35 +00:00
|
|
|
matches := 0
|
|
|
|
for i, actualTopicSet := range actualTopics {
|
2020-02-05 01:02:01 +00:00
|
|
|
if i < len(wantedTopics) && len(wantedTopics[i]) > 0 {
|
2020-01-21 19:12:35 +00:00
|
|
|
// If we have topics in this filter slot, count as a match if one of the topics matches
|
2020-02-05 01:02:01 +00:00
|
|
|
matches += slicesShareString(actualTopicSet, wantedTopics[i])
|
2020-01-21 19:12:35 +00:00
|
|
|
} else {
|
2020-02-05 01:02:01 +00:00
|
|
|
// Filter slot is either empty or doesn't exist => not matching any topics at this slot => counts as a match
|
2020-01-21 19:12:35 +00:00
|
|
|
matches++
|
2019-06-20 15:59:10 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
|
|
|
if matches == 4 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns 1 if the two slices have a string in common, 0 if they do not
|
|
|
|
func slicesShareString(slice1, slice2 []string) int {
|
|
|
|
for _, str1 := range slice1 {
|
|
|
|
for _, str2 := range slice2 {
|
|
|
|
if str1 == str2 {
|
|
|
|
return 1
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
return 0
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:41:59 +00:00
|
|
|
// filterStateAndStorage filters state and storage nodes into the response according to the provided filters
|
2020-10-20 14:42:09 +00:00
|
|
|
func (s *ResponseFilterer) filterStateAndStorage(stateFilter StateFilter, storageFilter StorageFilter, response *IPLDs, payload eth.ConvertedPayload) error {
|
|
|
|
response.StateNodes = make([]StateNode, 0, len(payload.StateNodes))
|
|
|
|
response.StorageNodes = make([]StorageNode, 0)
|
2020-03-11 18:41:59 +00:00
|
|
|
stateAddressFilters := make([]common.Hash, len(stateFilter.Addresses))
|
|
|
|
for i, addr := range stateFilter.Addresses {
|
|
|
|
stateAddressFilters[i] = crypto.Keccak256Hash(common.HexToAddress(addr).Bytes())
|
|
|
|
}
|
|
|
|
storageAddressFilters := make([]common.Hash, len(storageFilter.Addresses))
|
|
|
|
for i, addr := range storageFilter.Addresses {
|
|
|
|
storageAddressFilters[i] = crypto.Keccak256Hash(common.HexToAddress(addr).Bytes())
|
|
|
|
}
|
|
|
|
storageKeyFilters := make([]common.Hash, len(storageFilter.StorageKeys))
|
|
|
|
for i, store := range storageFilter.StorageKeys {
|
|
|
|
storageKeyFilters[i] = common.HexToHash(store)
|
|
|
|
}
|
|
|
|
for _, stateNode := range payload.StateNodes {
|
|
|
|
if !stateFilter.Off && checkNodeKeys(stateAddressFilters, stateNode.LeafKey) {
|
2021-02-19 20:23:45 +00:00
|
|
|
if stateNode.Type == sdtypes.Leaf || stateFilter.IntermediateNodes {
|
2020-03-11 18:41:59 +00:00
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthStateTrie, stateNode.Value, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-20 14:42:09 +00:00
|
|
|
response.StateNodes = append(response.StateNodes, StateNode{
|
2020-03-11 18:41:59 +00:00
|
|
|
StateLeafKey: stateNode.LeafKey,
|
|
|
|
Path: stateNode.Path,
|
|
|
|
IPLD: ipfs.BlockModel{
|
|
|
|
Data: stateNode.Value,
|
|
|
|
CID: cid.String(),
|
|
|
|
},
|
|
|
|
Type: stateNode.Type,
|
|
|
|
})
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-03-11 18:41:59 +00:00
|
|
|
if !storageFilter.Off && checkNodeKeys(storageAddressFilters, stateNode.LeafKey) {
|
2020-05-19 20:09:30 +00:00
|
|
|
for _, storageNode := range payload.StorageNodes[common.Bytes2Hex(stateNode.Path)] {
|
2020-03-11 18:41:59 +00:00
|
|
|
if checkNodeKeys(storageKeyFilters, storageNode.LeafKey) {
|
|
|
|
cid, err := ipld.RawdataToCid(ipld.MEthStorageTrie, storageNode.Value, multihash.KECCAK_256)
|
2020-02-20 22:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-20 14:42:09 +00:00
|
|
|
response.StorageNodes = append(response.StorageNodes, StorageNode{
|
2020-03-11 18:41:59 +00:00
|
|
|
StateLeafKey: stateNode.LeafKey,
|
|
|
|
StorageLeafKey: storageNode.LeafKey,
|
2020-02-20 22:13:19 +00:00
|
|
|
IPLD: ipfs.BlockModel{
|
2020-03-11 18:41:59 +00:00
|
|
|
Data: storageNode.Value,
|
2020-02-20 22:13:19 +00:00
|
|
|
CID: cid.String(),
|
|
|
|
},
|
2020-03-11 18:41:59 +00:00
|
|
|
Type: storageNode.Type,
|
|
|
|
Path: storageNode.Path,
|
2020-02-20 22:12:52 +00:00
|
|
|
})
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNodeKeys(wantedKeys []common.Hash, actualKey common.Hash) bool {
|
|
|
|
// If we aren't filtering for any specific keys, all nodes are a go
|
|
|
|
if len(wantedKeys) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, key := range wantedKeys {
|
|
|
|
if bytes.Equal(key.Bytes(), actualKey.Bytes()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|