Backfill Frob log events

This commit is contained in:
Rob Mulholand
2018-08-14 10:47:43 -05:00
parent 55229cd2eb
commit ac63e43543
40 changed files with 1251 additions and 149 deletions
@@ -0,0 +1,27 @@
# Transformers
## Description
Transformers must be defined in order to define what events should trigger data updates and how those are performed.
## Interface
### Initializer
Accepts DB and Blockchain from Vulcanize and returns a new transformer. E.g. for a new object "Cup":
`func NewCupTransformer(db *postgres.DB, blockchain core.ContractDataFetcher) transformers.Transformer`
### Execute
Triggers operations to take in response to a given log event.
Can persist data from logs, fetch and persist arbitrary data from outside services (e.g. contract state), or take any number of other actions. E.g.:
`func (cupTransformer *CupTransformer) Execute() error`
## Additional Requirements
Transformers must define log filters and create them so that relevant watched events can be identified and retrieved. E.g.:
```$xslt
{
Name: "CupsBite",
FromBlock: 0,
ToBlock: -1,
Address: "0x448a5065aebb8e423f0896e6c5d525c040f59af3",
Topics: core.Topics{"0x40cc885400000000000000000000000000000000000000000000000000000000"},
},
```
+51
View File
@@ -0,0 +1,51 @@
// 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 (
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
type LogFetcher interface {
FetchLogs(contractAddress string, topics [][]common.Hash, blockNumber int64) ([]types.Log, error)
}
type Fetcher struct {
blockChain core.BlockChain
}
func NewFetcher(blockchain core.BlockChain) Fetcher {
return Fetcher{
blockChain: blockchain,
}
}
func (fetcher Fetcher) FetchLogs(contractAddress string, topics [][]common.Hash, blockNumber int64) ([]types.Log, error) {
block := big.NewInt(blockNumber)
address := common.HexToAddress(contractAddress)
query := ethereum.FilterQuery{
FromBlock: block,
ToBlock: block,
Addresses: []common.Address{address},
Topics: topics,
}
return fetcher.blockChain.GetEthLogsWithCustomQuery(query)
}
+63
View File
@@ -0,0 +1,63 @@
// 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_test
import (
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
)
var _ = Describe("Fetcher", func() {
Describe("FetchLogs", func() {
It("fetches logs based on the given query", func() {
blockChain := fakes.NewMockBlockChain()
fetcher := shared.NewFetcher(blockChain)
blockNumber := int64(123)
address := "0xfakeAddress"
topicZeros := [][]common.Hash{{common.BytesToHash([]byte{1, 2, 3, 4, 5})}}
_, err := fetcher.FetchLogs(address, topicZeros, blockNumber)
Expect(err).NotTo(HaveOccurred())
expectedQuery := ethereum.FilterQuery{
FromBlock: big.NewInt(blockNumber),
ToBlock: big.NewInt(blockNumber),
Addresses: []common.Address{common.HexToAddress(address)},
Topics: topicZeros,
}
blockChain.AssertGetEthLogsWithCustomQueryCalledWith(expectedQuery)
})
It("returns an error if fetching the logs fails", func() {
blockChain := fakes.NewMockBlockChain()
blockChain.SetGetLogsErr(fakes.FakeError)
fetcher := shared.NewFetcher(blockChain)
_, err := fetcher.FetchLogs("", [][]common.Hash{}, int64(1))
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
})
@@ -0,0 +1,27 @@
// 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_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestShared(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Shared Suite")
}
+38
View File
@@ -0,0 +1,38 @@
// 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/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
type Transformer interface {
Execute() error
}
type TransformerInitializer func(db *postgres.DB, blockChain core.BlockChain) Transformer
func HexToInt64(byteString string) int64 {
value := common.HexToHash(byteString)
return value.Big().Int64()
}
func HexToString(byteString string) string {
value := common.HexToHash(byteString)
return value.Big().String()
}