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-12-10 20:11:25 +00:00
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package chunker_test
|
2018-12-10 20:11:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-01-31 23:14:42 +00:00
|
|
|
|
|
|
|
chunk "github.com/vulcanize/vulcanizedb/libraries/shared/chunker"
|
2019-07-23 19:57:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-12-10 20:11:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Log chunker", func() {
|
|
|
|
var (
|
2019-07-23 19:57:52 +00:00
|
|
|
configs []transformer.EventTransformerConfig
|
2019-01-31 23:14:42 +00:00
|
|
|
chunker *chunk.LogChunker
|
2018-12-10 20:11:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-07-23 19:57:52 +00:00
|
|
|
configA := transformer.EventTransformerConfig{
|
2018-12-10 20:11:25 +00:00
|
|
|
TransformerName: "TransformerA",
|
|
|
|
ContractAddresses: []string{"0x00000000000000000000000000000000000000A1", "0x00000000000000000000000000000000000000A2"},
|
|
|
|
Topic: "0xA",
|
|
|
|
}
|
2019-07-23 19:57:52 +00:00
|
|
|
configB := transformer.EventTransformerConfig{
|
2018-12-10 20:11:25 +00:00
|
|
|
TransformerName: "TransformerB",
|
|
|
|
ContractAddresses: []string{"0x00000000000000000000000000000000000000B1"},
|
|
|
|
Topic: "0xB",
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:57:52 +00:00
|
|
|
configC := transformer.EventTransformerConfig{
|
2018-12-10 20:11:25 +00:00
|
|
|
TransformerName: "TransformerC",
|
|
|
|
ContractAddresses: []string{"0x00000000000000000000000000000000000000A2"},
|
|
|
|
Topic: "0xC",
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:57:52 +00:00
|
|
|
configs = []transformer.EventTransformerConfig{configA, configB, configC}
|
2019-01-31 23:14:42 +00:00
|
|
|
chunker = chunk.NewLogChunker()
|
2018-12-12 14:41:44 +00:00
|
|
|
chunker.AddConfigs(configs)
|
2018-12-10 20:11:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("initialisation", func() {
|
|
|
|
It("creates lookup maps correctly", func() {
|
|
|
|
Expect(chunker.AddressToNames).To(Equal(map[string][]string{
|
2019-02-28 14:59:04 +00:00
|
|
|
"0x00000000000000000000000000000000000000a1": {"TransformerA"},
|
|
|
|
"0x00000000000000000000000000000000000000a2": {"TransformerA", "TransformerC"},
|
|
|
|
"0x00000000000000000000000000000000000000b1": {"TransformerB"},
|
2018-12-10 20:11:25 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
Expect(chunker.NameToTopic0).To(Equal(map[string]common.Hash{
|
|
|
|
"TransformerA": common.HexToHash("0xA"),
|
|
|
|
"TransformerB": common.HexToHash("0xB"),
|
|
|
|
"TransformerC": common.HexToHash("0xC"),
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-12-12 14:41:44 +00:00
|
|
|
Describe("AddConfigs", func() {
|
|
|
|
It("can add more configs later", func() {
|
2019-07-23 19:57:52 +00:00
|
|
|
configD := transformer.EventTransformerConfig{
|
2018-12-12 14:41:44 +00:00
|
|
|
TransformerName: "TransformerD",
|
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000D"},
|
|
|
|
Topic: "0xD",
|
|
|
|
}
|
2019-07-23 19:57:52 +00:00
|
|
|
chunker.AddConfigs([]transformer.EventTransformerConfig{configD})
|
2018-12-12 14:41:44 +00:00
|
|
|
|
|
|
|
Expect(chunker.AddressToNames).To(ContainElement([]string{"TransformerD"}))
|
|
|
|
Expect(chunker.NameToTopic0).To(ContainElement(common.HexToHash("0xD")))
|
|
|
|
})
|
2019-01-16 20:24:00 +00:00
|
|
|
|
|
|
|
It("lower cases address", func() {
|
2019-07-23 19:57:52 +00:00
|
|
|
configD := transformer.EventTransformerConfig{
|
2019-01-16 20:24:00 +00:00
|
|
|
TransformerName: "TransformerD",
|
|
|
|
ContractAddresses: []string{"0x000000000000000000000000000000000000000D"},
|
|
|
|
Topic: "0xD",
|
|
|
|
}
|
2019-07-23 19:57:52 +00:00
|
|
|
chunker.AddConfigs([]transformer.EventTransformerConfig{configD})
|
2019-01-16 20:24:00 +00:00
|
|
|
|
|
|
|
Expect(chunker.AddressToNames["0x000000000000000000000000000000000000000d"]).To(Equal([]string{"TransformerD"}))
|
|
|
|
})
|
2018-12-12 14:41:44 +00:00
|
|
|
})
|
|
|
|
|
2018-12-10 20:11:25 +00:00
|
|
|
Describe("ChunkLogs", func() {
|
|
|
|
It("only associates logs with relevant topic0 and address to transformers", func() {
|
2019-07-23 19:57:52 +00:00
|
|
|
logs := []core.HeaderSyncLog{log1, log2, log3, log4, log5}
|
2018-12-10 20:11:25 +00:00
|
|
|
chunks := chunker.ChunkLogs(logs)
|
|
|
|
|
2018-12-11 12:47:04 +00:00
|
|
|
Expect(chunks["TransformerA"]).To(And(ContainElement(log1), ContainElement(log4)))
|
2018-12-10 20:11:25 +00:00
|
|
|
Expect(chunks["TransformerB"]).To(BeEmpty())
|
|
|
|
Expect(chunks["TransformerC"]).To(ContainElement(log5))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Match TransformerA
|
2019-07-23 19:57:52 +00:00
|
|
|
log1 = core.HeaderSyncLog{
|
|
|
|
Log: types.Log{
|
|
|
|
Address: common.HexToAddress("0xA1"),
|
|
|
|
Topics: []common.Hash{
|
|
|
|
common.HexToHash("0xA"),
|
|
|
|
common.HexToHash("0xLogTopic1"),
|
|
|
|
},
|
2018-12-10 20:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// Match TransformerA address, but not topic0
|
2019-07-23 19:57:52 +00:00
|
|
|
log2 = core.HeaderSyncLog{
|
|
|
|
Log: types.Log{
|
|
|
|
Address: common.HexToAddress("0xA1"),
|
|
|
|
Topics: []common.Hash{
|
|
|
|
common.HexToHash("0xB"),
|
|
|
|
common.HexToHash("0xLogTopic2"),
|
|
|
|
},
|
2018-12-10 20:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// Match TransformerA topic, but TransformerB address
|
2019-07-23 19:57:52 +00:00
|
|
|
log3 = core.HeaderSyncLog{
|
|
|
|
Log: types.Log{
|
|
|
|
Address: common.HexToAddress("0xB1"),
|
|
|
|
Topics: []common.Hash{
|
|
|
|
common.HexToHash("0xA"),
|
|
|
|
common.HexToHash("0xLogTopic3"),
|
|
|
|
},
|
2018-12-10 20:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// Match TransformerA, with the other address
|
2019-07-23 19:57:52 +00:00
|
|
|
log4 = core.HeaderSyncLog{
|
|
|
|
Log: types.Log{
|
|
|
|
Address: common.HexToAddress("0xA2"),
|
|
|
|
Topics: []common.Hash{
|
|
|
|
common.HexToHash("0xA"),
|
|
|
|
common.HexToHash("0xLogTopic4"),
|
|
|
|
},
|
2018-12-10 20:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// Match TransformerC, which shares address with TransformerA
|
2019-07-23 19:57:52 +00:00
|
|
|
log5 = core.HeaderSyncLog{
|
|
|
|
Log: types.Log{
|
|
|
|
Address: common.HexToAddress("0xA2"),
|
|
|
|
Topics: []common.Hash{
|
|
|
|
common.HexToHash("0xC"),
|
|
|
|
common.HexToHash("0xLogTopic5"),
|
|
|
|
},
|
2018-12-10 20:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
2018-12-11 12:47:04 +00:00
|
|
|
)
|