ipld-eth-server/pkg/transformers/shared/log_chunker.go

72 lines
2.5 KiB
Go
Raw Normal View History

2019-01-24 20:41:30 +00:00
// VulcanizeDB
// Copyright © 2018 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/>.
2018-11-28 14:40:15 +00:00
package shared
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"strings"
shared_t "github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
2018-11-28 14:40:15 +00:00
)
2018-12-12 14:41:29 +00:00
type Chunker interface {
AddConfigs(transformerConfigs []shared_t.TransformerConfig)
2018-12-12 14:41:29 +00:00
ChunkLogs(logs []types.Log) map[string][]types.Log
}
2018-11-28 14:40:15 +00:00
type LogChunker struct {
2018-12-10 20:11:25 +00:00
AddressToNames map[string][]string
NameToTopic0 map[string]common.Hash
2018-11-28 14:40:15 +00:00
}
2018-12-12 14:41:29 +00:00
// Returns a new log chunker with initialised maps.
// Needs to have configs added with `AddConfigs` to consider logs for the respective transformer.
func NewLogChunker() *LogChunker {
return &LogChunker{
AddressToNames: map[string][]string{},
NameToTopic0: map[string]common.Hash{},
}
}
2018-11-28 14:40:15 +00:00
2018-12-12 14:41:29 +00:00
// Configures the chunker by adding more addreses and topics to consider.
func (chunker *LogChunker) AddConfigs(transformerConfigs []shared_t.TransformerConfig) {
2018-11-28 14:40:15 +00:00
for _, config := range transformerConfigs {
for _, address := range config.ContractAddresses {
var lowerCaseAddress = strings.ToLower(address)
chunker.AddressToNames[lowerCaseAddress] = append(chunker.AddressToNames[lowerCaseAddress], config.TransformerName)
2018-12-12 14:41:29 +00:00
chunker.NameToTopic0[config.TransformerName] = common.HexToHash(config.Topic)
2018-11-28 14:40:15 +00:00
}
}
}
2018-12-10 20:11:25 +00:00
// Goes through an array of logs, associating relevant logs (matching addresses and topic) with transformers
2018-12-12 14:41:29 +00:00
func (chunker *LogChunker) ChunkLogs(logs []types.Log) map[string][]types.Log {
2018-12-10 20:11:25 +00:00
chunks := map[string][]types.Log{}
2018-11-28 14:40:15 +00:00
for _, log := range logs {
// Topic0 is not unique to each transformer, also need to consider the contract address
relevantTransformers := chunker.AddressToNames[strings.ToLower(log.Address.String())]
2018-12-11 14:02:32 +00:00
2018-11-28 14:40:15 +00:00
for _, transformer := range relevantTransformers {
2018-12-10 20:11:25 +00:00
if chunker.NameToTopic0[transformer] == log.Topics[0] {
2018-11-28 14:40:15 +00:00
chunks[transformer] = append(chunks[transformer], log)
}
}
}
2018-12-10 20:11:25 +00:00
return chunks
2018-11-28 14:40:15 +00:00
}