Refactoring

* Move flip kick files pkg/transformers/flip_kick

* Consolidate test database setup

* Pull ganache ipcPath from config

* Update README to include info about using a Ganache chain
This commit is contained in:
Elizabeth
2018-08-07 15:17:29 -05:00
committed by GitHub
parent c617cd9c9d
commit b37324bf3d
257 changed files with 188 additions and 173 deletions
+31
View File
@@ -0,0 +1,31 @@
// 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 flip_kick
type TransformerConfig struct {
ContractAddress string
ContractAbi string
Topics []string
StartingBlockNumber int64
EndingBlockNumber int64
}
var FlipKickConfig = TransformerConfig{
ContractAddress: "0x08cb6176addcca2e1d1ffe21bee464b72ee4cd8d", //this is a temporary address deployed locally
ContractAbi: FlipperABI,
Topics: []string{FlipKickSignature},
StartingBlockNumber: 0,
EndingBlockNumber: 100,
}
File diff suppressed because one or more lines are too long
+107
View File
@@ -0,0 +1,107 @@
// 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 flip_kick
import (
"errors"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/geth"
)
type Converter interface {
ToEntity(contractAddress string, contractAbi string, ethLog types.Log) (*FlipKickEntity, error)
ToModel(flipKick FlipKickEntity) (FlipKickModel, error)
}
type FlipKickConverter struct{}
func (FlipKickConverter) ToEntity(contractAddress string, contractAbi string, ethLog types.Log) (*FlipKickEntity, error) {
entity := &FlipKickEntity{}
address := common.HexToAddress(contractAddress)
abi, err := geth.ParseAbi(contractAbi)
if err != nil {
return entity, err
}
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
err = contract.UnpackLog(entity, "FlipKick", ethLog)
if err != nil {
return entity, err
}
return entity, nil
}
func (FlipKickConverter) ToModel(flipKick FlipKickEntity) (FlipKickModel, error) {
//TODO: Confirm if the following values can be/ever will be nil
if flipKick.Id == nil {
return FlipKickModel{}, errors.New("FlipKick log ID cannot be nil.")
}
id := flipKick.Id.String()
mom := strings.ToLower(flipKick.Mom.String())
vat := strings.ToLower(flipKick.Vat.String())
ilk := strings.ToLower(common.ToHex(flipKick.Ilk[:]))
lot := convertNilToEmptyString(flipKick.Lot.String())
bid := convertNilToEmptyString(flipKick.Bid.String())
guy := strings.ToLower(flipKick.Guy.String())
gal := strings.ToLower(flipKick.Gal.String())
endValue := convertNilToZeroTimeValue(flipKick.End)
end := time.Unix(endValue, 0)
eraValue := convertNilToZeroTimeValue(flipKick.Era)
era := time.Unix(eraValue, 0)
lad := strings.ToLower(flipKick.Lad.String())
tab := convertNilToEmptyString(flipKick.Tab.String())
return FlipKickModel{
Id: id,
Mom: mom,
Vat: vat,
Ilk: ilk,
Lot: lot,
Bid: bid,
Guy: guy,
Gal: gal,
End: end,
Era: era,
Lad: lad,
Tab: tab,
}, nil
}
func convertNilToZeroTimeValue(value *big.Int) int64 {
if value == nil {
return int64(0)
} else {
return value.Int64()
}
}
func convertNilToEmptyString(value string) string {
if value == "<nil>" {
return ""
} else {
return value
}
}
@@ -0,0 +1,110 @@
// 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 flip_kick_test
import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
var _ = Describe("FlipKickEntity Converter", func() {
It("converts an Eth Log to and Entity", func() {
converter := flip_kick.FlipKickConverter{}
entity, err := converter.ToEntity(test_data.TemporaryFlipAddress, flip_kick.FlipperABI, test_data.EthFlipKickLog)
Expect(err).NotTo(HaveOccurred())
Expect(entity.Id).To(Equal(test_data.FlipKickEntity.Id))
Expect(entity.Mom).To(Equal(test_data.FlipKickEntity.Mom))
Expect(entity.Vat).To(Equal(test_data.FlipKickEntity.Vat))
Expect(entity.Ilk).To(Equal(test_data.FlipKickEntity.Ilk))
Expect(entity.Lot).To(Equal(test_data.FlipKickEntity.Lot))
Expect(entity.Bid).To(Equal(test_data.FlipKickEntity.Bid))
Expect(entity.Guy).To(Equal(test_data.FlipKickEntity.Guy))
Expect(entity.Gal).To(Equal(test_data.FlipKickEntity.Gal))
Expect(entity.End).To(Equal(test_data.FlipKickEntity.End))
Expect(entity.Era).To(Equal(test_data.FlipKickEntity.Era))
Expect(entity.Lad).To(Equal(test_data.FlipKickEntity.Lad))
Expect(entity.Tab).To(Equal(test_data.FlipKickEntity.Tab))
})
It("returns an error if converting log to entity fails", func() {
converter := flip_kick.FlipKickConverter{}
_, err := converter.ToEntity(test_data.TemporaryFlipAddress, "error abi", test_data.EthFlipKickLog)
Expect(err).To(HaveOccurred())
})
It("converts and Entity to a Model", func() {
converter := flip_kick.FlipKickConverter{}
model, err := converter.ToModel(test_data.FlipKickEntity)
Expect(err).NotTo(HaveOccurred())
Expect(model).To(Equal(test_data.FlipKickModel))
})
It("handles nil", func() {
emptyAddressHex := "0x0000000000000000000000000000000000000000"
emptyByteArrayHex := "0x0000000000000000000000000000000000000000000000000000000000000000"
emptyString := ""
emptyTime := time.Unix(0, 0)
converter := flip_kick.FlipKickConverter{}
emptyEntity := flip_kick.FlipKickEntity{
Id: big.NewInt(1),
Mom: common.Address{},
Vat: common.Address{},
Ilk: [32]byte{},
Lot: nil,
Bid: nil,
Guy: common.Address{},
Gal: common.Address{},
End: nil,
Era: nil,
Lad: common.Address{},
Tab: nil,
Raw: types.Log{},
}
model, err := converter.ToModel(emptyEntity)
Expect(err).NotTo(HaveOccurred())
Expect(model.Id).To(Equal("1"))
Expect(model.Mom).To(Equal(emptyAddressHex))
Expect(model.Vat).To(Equal(emptyAddressHex))
Expect(model.Ilk).To(Equal(emptyByteArrayHex))
Expect(model.Lot).To(Equal(emptyString))
Expect(model.Bid).To(Equal(emptyString))
Expect(model.Guy).To(Equal(emptyAddressHex))
Expect(model.Gal).To(Equal(emptyAddressHex))
Expect(model.End).To(Equal(emptyTime))
Expect(model.Era).To(Equal(emptyTime))
Expect(model.Lad).To(Equal(emptyAddressHex))
Expect(model.Tab).To(Equal(emptyString))
})
It("returns an error of the flip kick event id is nil", func() {
converter := flip_kick.FlipKickConverter{}
emptyEntity := flip_kick.FlipKickEntity{}
_, err := converter.ToModel(emptyEntity)
Expect(err).To(HaveOccurred())
})
})
+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 flip_kick
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type FlipKickEntity struct {
Id *big.Int
Mom common.Address
Vat common.Address
Ilk [32]byte
Lot *big.Int
Bid *big.Int
Guy common.Address
Gal common.Address
End *big.Int
Era *big.Int
Lad common.Address
Tab *big.Int
Raw types.Log
}
@@ -0,0 +1,33 @@
// 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 flip_kick
import (
"io/ioutil"
"log"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestEveryBlock(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "EveryBlock Suite")
}
var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})
+52
View File
@@ -0,0 +1,52 @@
// 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 flip_kick
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 (f Fetcher) FetchLogs(contractAddress string, topicZeros [][]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: topicZeros,
}
return f.Blockchain.GetEthLogsWithCustomQuery(query)
}
@@ -0,0 +1,62 @@
// 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 flip_kick_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/flip_kick"
)
var _ = Describe("Fetcher", func() {
Describe("FetchLogs", func() {
var blockChain *fakes.MockBlockChain
var fetcher flip_kick.Fetcher
BeforeEach(func() {
blockChain = fakes.NewMockBlockChain()
fetcher = flip_kick.Fetcher{Blockchain: blockChain}
})
It("fetches logs based on the given query", func() {
blockNumber := int64(3)
address := "0x4D2"
topicZeros := [][]common.Hash{{common.HexToHash("0x")}}
query := ethereum.FilterQuery{
FromBlock: big.NewInt(blockNumber),
ToBlock: big.NewInt(blockNumber),
Addresses: []common.Address{common.HexToAddress(address)},
Topics: topicZeros,
}
fetcher.FetchLogs(address, topicZeros, blockNumber)
blockChain.AssertGetEthLogsWithCustomQueryCalledWith(query)
})
It("returns an error if fetching the logs fails", func() {
blockChain.SetGetLogsErr(fakes.FakeError)
_, err := fetcher.FetchLogs("", [][]common.Hash{}, int64(1))
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
})
@@ -0,0 +1,89 @@
// 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 flip_kick_test
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config"
)
var _ = Describe("Integration tests", func() {
It("Fetches FlipKickEntity event logs from a local test chain", func() {
ipcPath := test_config.TestClient.IPCPath
rawRpcClient, err := rpc.Dial(ipcPath)
Expect(err).NotTo(HaveOccurred())
rpcClient := client.NewRpcClient(rawRpcClient, ipcPath)
ethClient := ethclient.NewClient(rawRpcClient)
blockChainClient := client.NewEthClient(ethClient)
realNode := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, realNode, transactionConverter)
realFetcher := flip_kick.NewFetcher(realBlockChain)
topic0 := common.HexToHash(flip_kick.FlipKickSignature)
topics := [][]common.Hash{{topic0}}
result, err := realFetcher.FetchLogs(test_data.TemporaryFlipAddress, topics, int64(10))
Expect(err).NotTo(HaveOccurred())
Expect(len(result) > 0).To(BeTrue())
Expect(result[0].Address).To(Equal(test_data.EthFlipKickLog.Address))
Expect(result[0].TxHash).To(Equal(test_data.EthFlipKickLog.TxHash))
Expect(result[0].BlockNumber).To(Equal(test_data.EthFlipKickLog.BlockNumber))
Expect(result[0].Topics).To(Equal(test_data.EthFlipKickLog.Topics))
Expect(result[0].Index).To(Equal(test_data.EthFlipKickLog.Index))
Expect(result[0].Data).To(Equal(test_data.EthFlipKickLog.Data))
})
It("unpacks an event log", func() {
address := common.HexToAddress(test_data.TemporaryFlipAddress)
abi, err := geth.ParseAbi(flip_kick.FlipperABI)
Expect(err).NotTo(HaveOccurred())
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
entity := &flip_kick.FlipKickEntity{}
var eventLog = test_data.EthFlipKickLog
err = contract.UnpackLog(entity, "FlipKick", eventLog)
Expect(err).NotTo(HaveOccurred())
expectedEntity := test_data.FlipKickEntity
Expect(entity.Id).To(Equal(expectedEntity.Id))
Expect(entity.Mom).To(Equal(expectedEntity.Mom))
Expect(entity.Vat).To(Equal(expectedEntity.Vat))
Expect(entity.Ilk).To(Equal(expectedEntity.Ilk))
Expect(entity.Lot).To(Equal(expectedEntity.Lot))
Expect(entity.Bid.String()).To(Equal(expectedEntity.Bid.String())) //FIXME
Expect(entity.Guy).To(Equal(expectedEntity.Guy))
Expect(entity.Gal).To(Equal(expectedEntity.Gal))
Expect(entity.End).To(Equal(expectedEntity.End))
Expect(entity.Era).To(Equal(expectedEntity.Era))
Expect(entity.Lad).To(Equal(expectedEntity.Lad))
Expect(entity.Tab).To(Equal(expectedEntity.Tab))
})
})
+32
View File
@@ -0,0 +1,32 @@
// 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 flip_kick
import "time"
type FlipKickModel struct {
Id string
Mom string
Vat string
Ilk string
Lot string
Bid string
Guy string
Gal string
End time.Time
Era time.Time
Lad string
Tab string
}
+71
View File
@@ -0,0 +1,71 @@
// 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 flip_kick
import (
"fmt"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
type Repository interface {
Create(headerId int64, flipKick FlipKickModel) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}
type FlipKickRepository struct {
DB *postgres.DB
}
func NewFlipKickRepository(db *postgres.DB) FlipKickRepository {
return FlipKickRepository{DB: db}
}
func (fkr FlipKickRepository) Create(headerId int64, flipKick FlipKickModel) error {
_, err := fkr.DB.Exec(
`INSERT into maker.flip_kick (header_id, id, mom, vat, ilk, lot, bid, guy, gal, "end", era, lad, tab)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
headerId, flipKick.Id, flipKick.Mom, flipKick.Vat, flipKick.Ilk, flipKick.Lot, flipKick.Bid, flipKick.Guy, flipKick.Gal, flipKick.End, flipKick.Era, flipKick.Lad, flipKick.Tab,
)
if err != nil {
return err
}
return nil
}
func (fkr FlipKickRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header
err := fkr.DB.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN maker.flip_kick on headers.id = header_id
WHERE header_id ISNULL
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
fkr.DB.Node.ID,
)
if err != nil {
fmt.Println("Error:", err)
return result, err
}
return result, nil
}
@@ -0,0 +1,177 @@
// 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 flip_kick_test
import (
"math/rand"
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config"
)
var _ = Describe("FlipKick Repository", func() {
var db *postgres.DB
var flipKickRepository flip_kick.FlipKickRepository
var headerId int64
var blockNumber int64
var flipKick = test_data.FlipKickModel
BeforeEach(func() {
node := test_config.NewTestNode()
db = test_config.NewTestDB(node)
test_config.CleanTestDB(db)
flipKickRepository = flip_kick.FlipKickRepository{DB: db}
blockNumber = rand.Int63()
headerId = createHeader(db, blockNumber)
_, err := db.Exec(`DELETE from maker.flip_kick;`)
Expect(err).NotTo(HaveOccurred())
})
Describe("Create", func() {
AfterEach(func() {
_, err := db.Exec(`DELETE from headers;`)
Expect(err).NotTo(HaveOccurred())
})
It("persists a flip_kick record", func() {
err := flipKickRepository.Create(headerId, flipKick)
Expect(err).NotTo(HaveOccurred())
assertDBRecordCount(db, "maker.flip_kick", 1)
dbResult := test_data.FlipKickDBRow{}
err = flipKickRepository.DB.QueryRowx(`SELECT * FROM maker.flip_kick`).StructScan(&dbResult)
Expect(err).NotTo(HaveOccurred())
Expect(dbResult.HeaderId).To(Equal(headerId))
Expect(dbResult.Id).To(Equal(flipKick.Id))
Expect(dbResult.Mom).To(Equal(flipKick.Mom))
Expect(dbResult.Vat).To(Equal(flipKick.Vat))
Expect(dbResult.Ilk).To(Equal(flipKick.Ilk))
Expect(dbResult.Lot).To(Equal(flipKick.Lot))
Expect(dbResult.Bid).To(Equal(flipKick.Bid))
Expect(dbResult.Guy).To(Equal(flipKick.Guy))
Expect(dbResult.Gal).To(Equal(flipKick.Gal))
Expect(dbResult.End.Equal(flipKick.End)).To(BeTrue())
Expect(dbResult.Era.Equal(flipKick.Era)).To(BeTrue())
Expect(dbResult.Lad).To(Equal(flipKick.Lad))
Expect(dbResult.Tab).To(Equal(flipKick.Tab))
})
It("returns an error if inserting the flip_kick record fails", func() {
err := flipKickRepository.Create(headerId, test_data.FlipKickModel)
Expect(err).NotTo(HaveOccurred())
err = flipKickRepository.Create(headerId, test_data.FlipKickModel)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("deletes the flip_kick records if its corresponding header record is deleted", func() {
err := flipKickRepository.Create(headerId, test_data.FlipKickModel)
Expect(err).NotTo(HaveOccurred())
assertDBRecordCount(db, "maker.flip_kick", 1)
assertDBRecordCount(db, "headers", 1)
_, err = db.Exec(`DELETE FROM headers where id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
assertDBRecordCount(db, "headers", 0)
assertDBRecordCount(db, "maker.flip_kick", 0)
})
})
Describe("When there are multiple nodes", func() {
var db2 *postgres.DB
var flipKickRepository2 flip_kick.FlipKickRepository
var headerId2 int64
BeforeEach(func() {
//create database for the second node
node2 := core.Node{
GenesisBlock: "GENESIS",
NetworkID: 1,
ID: "node2",
ClientName: "Geth/v1.7.2-stable-1db4ecdc/darwin-amd64/go1.9",
}
db2 = test_config.NewTestDB(node2)
flipKickRepository2 = flip_kick.FlipKickRepository{DB: db2}
headerId2 = createHeader(db2, blockNumber)
_, err := db2.Exec(`DELETE from maker.flip_kick;`)
Expect(err).NotTo(HaveOccurred())
})
It("only includes missing headers for the current node", func() {
node1missingHeaders, err := flipKickRepository.MissingHeaders(blockNumber, blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(node1missingHeaders)).To(Equal(1))
node2MissingHeaders, err := flipKickRepository2.MissingHeaders(blockNumber, blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(node2MissingHeaders)).To(Equal(1))
})
})
Describe("MissingHeaders", func() {
It("returns headers for which there isn't an associated flip_kick record", func() {
startingBlock := blockNumber - 3
endingBlock := blockNumber + 3
err := flipKickRepository.Create(headerId, test_data.FlipKickModel)
Expect(err).NotTo(HaveOccurred())
newBlockNumber := blockNumber + 3
newHeaderId := createHeader(db, newBlockNumber)
createHeader(db, blockNumber+10) //this one is out of the block range and shouldn't be included
headers, err := flipKickRepository.MissingHeaders(startingBlock, endingBlock)
Expect(len(headers)).To(Equal(1))
Expect(headers[0].Id).To(Equal(newHeaderId))
Expect(headers[0].BlockNumber).To(Equal(newBlockNumber))
})
})
})
func assertDBRecordCount(db *postgres.DB, dbTable string, expectedCount int) {
var count int
query := `SELECT count(*) FROM ` + dbTable
err := db.QueryRow(query).Scan(&count)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(expectedCount))
}
func createHeader(db *postgres.DB, blockNumber int64) (headerId int64) {
headerRepository := repositories.NewHeaderRepository(db)
header := core.Header{
BlockNumber: blockNumber,
Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex(),
Raw: []byte{1, 2, 3, 4, 5},
}
_, err := headerRepository.CreateOrUpdateHeader(header)
Expect(err).NotTo(HaveOccurred())
var dbHeader core.Header
err = db.Get(&dbHeader, `SELECT id, block_number, hash, raw FROM public.headers WHERE block_number = $1 AND eth_node_id = $2`, header.BlockNumber, db.NodeID)
Expect(err).NotTo(HaveOccurred())
return dbHeader.Id
}
+126
View File
@@ -0,0 +1,126 @@
// 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 flip_kick
import (
"errors"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/libraries/shared"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
type FlipKickTransformer struct {
Fetcher LogFetcher
Converter Converter
Repository Repository
Config TransformerConfig
}
type FlipKickTransformerInitializer struct {
Config TransformerConfig
}
func (i FlipKickTransformerInitializer) NewFlipKickTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
fetcher := NewFetcher(blockChain)
repository := NewFlipKickRepository(db)
transformer := FlipKickTransformer{
Fetcher: fetcher,
Repository: repository,
Converter: FlipKickConverter{},
Config: i.Config,
}
return transformer
}
func (fkt *FlipKickTransformer) SetConfig(config TransformerConfig) {
fkt.Config = config
}
const (
FetcherError = "Error fetching FlipKick log events for block number %d: %s"
LogToEntityError = "Error converting eth log to FlipKick entity for block number %d: %s"
EntityToModelError = "Error converting eth log to FlipKick entity for block number %d: %s"
RepositoryError = "Error creating flip_kick record for block number %d: %s"
TransformerError = "There has been %d error(s) transforming FlipKick event logs, see the logs for more details."
)
type transformerError struct {
err string
blockNumber int64
msg string
}
func (te *transformerError) Error() string {
return fmt.Sprintf(te.msg, te.blockNumber, te.err)
}
func newTransformerError(err error, blockNumber int64, msg string) error {
e := transformerError{err.Error(), blockNumber, msg}
log.Println(e.Error())
return &e
}
func (fkt FlipKickTransformer) Execute() error {
config := fkt.Config
topics := [][]common.Hash{{common.HexToHash(FlipKickSignature)}}
headers, err := fkt.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
if err != nil {
log.Println("Error:", err)
return err
}
log.Printf("Fetching event logs for %d headers \n", len(headers))
var resultingErrors []error
for _, header := range headers {
ethLogs, err := fkt.Fetcher.FetchLogs(config.ContractAddress, topics, header.BlockNumber)
if err != nil {
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, FetcherError))
}
for _, ethLog := range ethLogs {
entity, err := fkt.Converter.ToEntity(config.ContractAddress, config.ContractAbi, ethLog)
if err != nil {
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, LogToEntityError))
}
model, err := fkt.Converter.ToModel(*entity)
if err != nil {
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, EntityToModelError))
}
err = fkt.Repository.Create(header.Id, model)
if err != nil {
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, RepositoryError))
}
}
}
if len(resultingErrors) > 0 {
for _, err := range resultingErrors {
log.Println(err)
}
msg := fmt.Sprintf(TransformerError, len(resultingErrors))
return errors.New(msg)
}
return nil
}
@@ -0,0 +1,142 @@
// 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 flip_kick_test
import (
"math/rand"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
var _ = Describe("FlipKick Transformer", func() {
var transformer flip_kick.FlipKickTransformer
var fetcher test_data.MockLogFetcher
var converter test_data.MockFlipKickConverter
var repository test_data.MockFlipKickRepository
var testConfig flip_kick.TransformerConfig
var blockNumber int64
var headerId int64
var headers []core.Header
var logs []types.Log
BeforeEach(func() {
fetcher = test_data.MockLogFetcher{}
converter = test_data.MockFlipKickConverter{}
repository = test_data.MockFlipKickRepository{}
transformer = flip_kick.FlipKickTransformer{
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
startingBlockNumber := rand.Int63()
testConfig = flip_kick.TransformerConfig{
ContractAddress: "0x12345",
ContractAbi: "test abi",
Topics: []string{flip_kick.FlipKickSignature},
StartingBlockNumber: startingBlockNumber,
EndingBlockNumber: startingBlockNumber + 5,
}
transformer.SetConfig(testConfig)
blockNumber = rand.Int63()
headerId = rand.Int63()
headers = []core.Header{{
Id: headerId,
BlockNumber: blockNumber,
Hash: "0x",
Raw: nil,
}}
repository.SetHeadersToReturn(headers)
logs = []types.Log{test_data.EthFlipKickLog}
fetcher.SetFetchedLogs(logs)
})
It("fetches logs with the configured contract and topic(s) for each block", func() {
expectedTopics := [][]common.Hash{{common.HexToHash(flip_kick.FlipKickSignature)}}
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedContractAddress).To(Equal(testConfig.ContractAddress))
Expect(fetcher.FetchedTopics).To(Equal(expectedTopics))
Expect(fetcher.FetchedBlocks).To(Equal([]int64{blockNumber}))
})
It("returns an error if the fetcher fails", func() {
fetcher.SetFetcherError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error(s) transforming FlipKick event logs"))
})
It("converts the logs", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(converter.ConverterContract).To(Equal(testConfig.ContractAddress))
Expect(converter.ConverterAbi).To(Equal(testConfig.ContractAbi))
Expect(converter.LogsToConvert).To(Equal(logs))
Expect(converter.EntitiesToConvert).To(Equal([]flip_kick.FlipKickEntity{test_data.FlipKickEntity}))
})
It("returns an error if converting the geth log fails", func() {
converter.SetConverterError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
})
It("persists a flip_kick record", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.HeaderIds).To(Equal([]int64{headerId}))
Expect(repository.FlipKicksCreated).To(Equal([]flip_kick.FlipKickModel{test_data.FlipKickModel}))
})
It("returns an error if persisting a record fails", func() {
repository.SetCreateRecordError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
})
It("returns an error if fetching missing headers fails", func() {
repository.SetMissingHeadersError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
})
It("gets missing headers for blocks between the configured block number range", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.StartingBlockNumber).To(Equal(testConfig.StartingBlockNumber))
Expect(repository.EndingBlockNumber).To(Equal(testConfig.EndingBlockNumber))
})
})