2018-11-28 14:40:15 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Initialises a chunker by creating efficient lookup maps
|
|
|
|
func NewLogChunker(transformerConfigs []TransformerConfig) LogChunker {
|
|
|
|
addressToNames := map[string][]string{}
|
2018-12-07 17:10:36 +00:00
|
|
|
nameToTopic0 := map[string]common.Hash{}
|
2018-11-28 14:40:15 +00:00
|
|
|
|
|
|
|
for _, config := range transformerConfigs {
|
|
|
|
for _, address := range config.ContractAddresses {
|
|
|
|
addressToNames[address] = append(addressToNames[address], config.TransformerName)
|
|
|
|
nameToTopic0[config.TransformerName] = common.HexToHash(config.Topic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return LogChunker{
|
2018-12-10 20:11:25 +00:00
|
|
|
AddressToNames: addressToNames,
|
2018-12-11 12:47:04 +00:00
|
|
|
NameToTopic0: nameToTopic0,
|
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
|
|
|
|
func (chunker LogChunker) ChunkLogs(logs []types.Log) map[string][]types.Log {
|
|
|
|
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
|
2018-12-10 20:11:25 +00:00
|
|
|
relevantTransformers := chunker.AddressToNames[log.Address.String()]
|
2018-12-11 14:02:32 +00:00
|
|
|
|
|
|
|
// TODO What should happen if log can't be assigned?
|
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
|
|
|
}
|