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-11-28 14:40:15 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package chunker
|
2018-11-28 14:40:15 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-30 17:37:16 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-11-28 14:40:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-07-23 19:57:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2018-11-28 14:40:15 +00:00
|
|
|
)
|
|
|
|
|
2018-12-12 14:41:29 +00:00
|
|
|
type Chunker interface {
|
2019-08-06 21:57:08 +00:00
|
|
|
AddConfig(transformerConfig transformer.EventTransformerConfig)
|
2019-07-23 19:57:52 +00:00
|
|
|
ChunkLogs(logs []core.HeaderSyncLog) map[string][]core.HeaderSyncLog
|
2018-12-12 14:41:29 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
// Configures the chunker by adding one config with more addresses and topics to consider.
|
|
|
|
func (chunker *LogChunker) AddConfig(transformerConfig transformer.EventTransformerConfig) {
|
|
|
|
for _, address := range transformerConfig.ContractAddresses {
|
|
|
|
var lowerCaseAddress = strings.ToLower(address)
|
|
|
|
chunker.AddressToNames[lowerCaseAddress] = append(chunker.AddressToNames[lowerCaseAddress], transformerConfig.TransformerName)
|
|
|
|
chunker.NameToTopic0[transformerConfig.TransformerName] = common.HexToHash(transformerConfig.Topic)
|
2018-11-28 14:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:19:51 +00:00
|
|
|
// Goes through a slice of logs, associating relevant logs (matching addresses and topic) with transformers
|
2019-07-23 19:57:52 +00:00
|
|
|
func (chunker *LogChunker) ChunkLogs(logs []core.HeaderSyncLog) map[string][]core.HeaderSyncLog {
|
|
|
|
chunks := map[string][]core.HeaderSyncLog{}
|
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
|
2019-07-23 19:57:52 +00:00
|
|
|
relevantTransformers := chunker.AddressToNames[strings.ToLower(log.Log.Address.Hex())]
|
2018-12-11 14:02:32 +00:00
|
|
|
|
2019-07-23 19:57:52 +00:00
|
|
|
for _, t := range relevantTransformers {
|
|
|
|
if chunker.NameToTopic0[t] == log.Log.Topics[0] {
|
|
|
|
chunks[t] = append(chunks[t], log)
|
2018-11-28 14:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 20:11:25 +00:00
|
|
|
return chunks
|
2018-11-28 14:40:15 +00:00
|
|
|
}
|