forked from cerc-io/ipld-eth-server
Transform and persist Tend log events
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
// 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 tend
|
||||
|
||||
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
|
||||
var TendConfig = shared.TransformerConfig{
|
||||
ContractAddresses: "0x08cb6176addcca2e1d1ffe21bee464b72ee4cd8d", //this is a temporary address deployed locally
|
||||
ContractAbi: shared.FlipperABI,
|
||||
Topics: []string{shared.TendSignature},
|
||||
StartingBlockNumber: 0,
|
||||
EndingBlockNumber: 100,
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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 tend
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"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"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/utilities"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
ToEntity(contractAddress string, contractAbi string, ethLog types.Log) (TendEntity, error)
|
||||
ToModel(entity TendEntity) (TendModel, error)
|
||||
}
|
||||
|
||||
type TendConverter struct{}
|
||||
|
||||
func (c TendConverter) ToEntity(contractAddress string, contractAbi string, ethLog types.Log) (TendEntity, error) {
|
||||
entity := TendEntity{}
|
||||
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, "Tend", ethLog)
|
||||
if err != nil {
|
||||
return entity, err
|
||||
}
|
||||
entity.TransactionIndex = ethLog.TxIndex
|
||||
entity.Raw = ethLog
|
||||
return entity, nil
|
||||
}
|
||||
|
||||
func (c TendConverter) ToModel(entity TendEntity) (TendModel, error) {
|
||||
if entity.Id == nil {
|
||||
return TendModel{}, errors.New("Tend log ID cannot be nil.")
|
||||
}
|
||||
|
||||
rawJson, err := json.Marshal(entity.Raw)
|
||||
if err != nil {
|
||||
return TendModel{}, err
|
||||
}
|
||||
era := utilities.ConvertNilToZeroTimeValue(entity.Era)
|
||||
return TendModel{
|
||||
Id: utilities.ConvertNilToEmptyString(entity.Id.String()),
|
||||
Lot: utilities.ConvertNilToEmptyString(entity.Lot.String()),
|
||||
Bid: utilities.ConvertNilToEmptyString(entity.Bid.String()),
|
||||
Guy: entity.Guy[:],
|
||||
Tic: utilities.ConvertNilToEmptyString(entity.Tic.String()),
|
||||
Era: time.Unix(era, 0),
|
||||
TransactionIndex: entity.TransactionIndex,
|
||||
Raw: string(rawJson),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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 tend_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
)
|
||||
|
||||
var _ = Describe("Tend TendConverter", func() {
|
||||
var converter tend.TendConverter
|
||||
var emptyEntity tend.TendEntity
|
||||
var testEntity tend.TendEntity
|
||||
|
||||
BeforeEach(func() {
|
||||
converter = tend.TendConverter{}
|
||||
emptyEntity = tend.TendEntity{}
|
||||
testEntity = test_data.TendEntity
|
||||
})
|
||||
|
||||
Describe("ToEntity", func() {
|
||||
It("converts a log to an entity", func() {
|
||||
entity, err := converter.ToEntity(test_data.FlipAddress, shared.FlipperABI, test_data.TendLog)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(entity).To(Equal(testEntity))
|
||||
})
|
||||
|
||||
It("returns an error if there is a failure in parsing the abi", func() {
|
||||
malformedAbi := "bad"
|
||||
entity, err := converter.ToEntity(test_data.FlipAddress, malformedAbi, test_data.TendLog)
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("invalid abi"))
|
||||
Expect(entity).To(Equal(emptyEntity))
|
||||
})
|
||||
|
||||
It("returns an error if there is a failure unpacking the log", func() {
|
||||
incompleteAbi := "[{}]"
|
||||
entity, err := converter.ToEntity(test_data.FlipAddress, incompleteAbi, test_data.TendLog)
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("abi: could not locate"))
|
||||
Expect(entity).To(Equal(emptyEntity))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ToModel", func() {
|
||||
It("converts an entity to a model", func() {
|
||||
model, err := converter.ToModel(testEntity)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(model).To(Equal(test_data.TendModel))
|
||||
})
|
||||
|
||||
It("handles nil values", func() {
|
||||
emptyEntity.Id = big.NewInt(1)
|
||||
emptyLog, err := json.Marshal(types.Log{})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
expectedModel := tend.TendModel{
|
||||
Id: "1",
|
||||
Lot: "",
|
||||
Bid: "",
|
||||
Guy: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
Tic: "",
|
||||
Era: time.Unix(0, 0),
|
||||
Raw: string(emptyLog),
|
||||
}
|
||||
model, err := converter.ToModel(emptyEntity)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(model).To(Equal(expectedModel))
|
||||
})
|
||||
|
||||
It("returns an error if the log Id is nil", func() {
|
||||
model, err := converter.ToModel(emptyEntity)
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(model).To(Equal(tend.TendModel{}))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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 tend
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type TendEntity struct {
|
||||
Id *big.Int
|
||||
Lot *big.Int
|
||||
Bid *big.Int
|
||||
Guy common.Address
|
||||
Tic *big.Int
|
||||
Era *big.Int
|
||||
TransactionIndex uint
|
||||
Raw types.Log
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// 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 tend_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/shared"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
)
|
||||
|
||||
// These test are pending either being able to emit a Tend event on a Ganache test chain or until the contracts are deployed to Kovan.
|
||||
var _ = XDescribe("Integration tests", func() {
|
||||
It("Fetches Tend 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 := shared.NewFetcher(realBlockChain)
|
||||
topic0 := common.HexToHash(shared.TendSignature)
|
||||
topics := [][]common.Hash{{topic0}}
|
||||
|
||||
result, err := realFetcher.FetchLogs(test_data.FlipAddress, topics, test_data.FlipKickBlockNumber)
|
||||
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))
|
||||
})
|
||||
|
||||
It("unpacks an event log", func() {
|
||||
address := common.HexToAddress(test_data.FlipAddress)
|
||||
abi, err := geth.ParseAbi(shared.FlipperABI)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
|
||||
entity := tend.TendEntity{}
|
||||
|
||||
var eventLog = test_data.TendLog
|
||||
|
||||
err = contract.UnpackLog(&entity, "Tend", eventLog)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
expectedEntity := test_data.TendEntity
|
||||
Expect(entity.Id).To(Equal(expectedEntity.Id))
|
||||
Expect(entity.Lot).To(Equal(expectedEntity.Lot))
|
||||
Expect(entity.Bid).To(Equal(expectedEntity.Bid))
|
||||
Expect(entity.Guy).To(Equal(expectedEntity.Guy))
|
||||
Expect(entity.Tic).To(Equal(expectedEntity.Tic))
|
||||
Expect(entity.Era).To(Equal(expectedEntity.Era))
|
||||
Expect(entity.Raw).To(Equal(expectedEntity.Raw))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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 tend
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type TendModel struct {
|
||||
Id string
|
||||
Lot string
|
||||
Bid string
|
||||
Guy []byte
|
||||
Tic string
|
||||
Era time.Time
|
||||
TransactionIndex uint `db:"tx_idx"`
|
||||
Raw string `db:"raw_log"`
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// 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 tend
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Create(headerId int64, tend TendModel) error
|
||||
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
|
||||
}
|
||||
|
||||
type TendRepository struct {
|
||||
DB *postgres.DB
|
||||
}
|
||||
|
||||
func NewTendRepository(db *postgres.DB) TendRepository {
|
||||
return TendRepository{DB: db}
|
||||
}
|
||||
|
||||
func (r TendRepository) Create(headerId int64, tend TendModel) error {
|
||||
_, err := r.DB.Exec(
|
||||
`INSERT into maker.tend (header_id, id, lot, bid, guy, tic, era, tx_idx, raw_log)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
||||
headerId, tend.Id, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.Era, tend.TransactionIndex, tend.Raw,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r TendRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
|
||||
var result []core.Header
|
||||
err := r.DB.Select(
|
||||
&result,
|
||||
`SELECT headers.id, headers.block_number FROM headers
|
||||
LEFT JOIN maker.tend 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,
|
||||
r.DB.Node.ID,
|
||||
)
|
||||
|
||||
return result, err
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// 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 tend_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
. "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/tend"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
)
|
||||
|
||||
var _ = Describe("TendRepository", func() {
|
||||
var db *postgres.DB
|
||||
var tendRepository tend.TendRepository
|
||||
var headerRepository repositories.HeaderRepository
|
||||
var headerId int64
|
||||
var err error
|
||||
|
||||
BeforeEach(func() {
|
||||
node := test_config.NewTestNode()
|
||||
db = test_config.NewTestDB(node)
|
||||
test_config.CleanTestDB(db)
|
||||
|
||||
headerRepository = repositories.NewHeaderRepository(db)
|
||||
headerId, err = headerRepository.CreateOrUpdateHeader(core.Header{})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
tendRepository = tend.NewTendRepository(db)
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
It("persists a tend record", func() {
|
||||
err := tendRepository.Create(headerId, test_data.TendModel)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var count int
|
||||
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(count).To(Equal(1))
|
||||
|
||||
dbResult := tend.TendModel{}
|
||||
err = db.Get(&dbResult, `SELECT id, lot, bid, guy, tic, era, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
Expect(dbResult.Id).To(Equal(test_data.TendModel.Id))
|
||||
Expect(dbResult.Lot).To(Equal(test_data.TendModel.Lot))
|
||||
Expect(dbResult.Bid).To(Equal(test_data.TendModel.Bid))
|
||||
Expect(dbResult.Guy).To(Equal(test_data.TendModel.Guy))
|
||||
Expect(dbResult.Tic).To(Equal(test_data.TendModel.Tic))
|
||||
Expect(dbResult.Era.Equal(test_data.TendModel.Era)).To(BeTrue())
|
||||
Expect(dbResult.TransactionIndex).To(Equal(test_data.TendModel.TransactionIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.RawJson))
|
||||
})
|
||||
|
||||
It("returns an error if inserting a tend record fails", func() {
|
||||
err := tendRepository.Create(headerId, test_data.TendModel)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = tendRepository.Create(headerId, test_data.TendModel)
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("deletes the tend record if its corresponding header record is deleted", func() {
|
||||
err := tendRepository.Create(headerId, test_data.TendModel)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var count int
|
||||
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(count).To(Equal(1))
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers where id = $1`, headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(count).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("MissingHeaders", func() {
|
||||
var tendBlockNumber int64
|
||||
var startingBlockNumber int64
|
||||
var endingBlockNumber int64
|
||||
var outOfRangeBlockNumber int64
|
||||
|
||||
BeforeEach(func() {
|
||||
tendBlockNumber = rand.Int63()
|
||||
startingBlockNumber = tendBlockNumber - 1
|
||||
endingBlockNumber = tendBlockNumber + 1
|
||||
outOfRangeBlockNumber = tendBlockNumber + 2
|
||||
})
|
||||
|
||||
It("returns headers for which there isn't an associated flip_kick record", func() {
|
||||
var headerIds []int64
|
||||
|
||||
for _, number := range []int64{startingBlockNumber, tendBlockNumber, endingBlockNumber, outOfRangeBlockNumber} {
|
||||
headerId, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: number})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
headerIds = append(headerIds, headerId)
|
||||
}
|
||||
|
||||
err = tendRepository.Create(headerIds[1], test_data.TendModel)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := tendRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(2))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
|
||||
})
|
||||
|
||||
It("only returns missing headers for the current node", func() {
|
||||
var headerIds []int64
|
||||
node2 := core.Node{}
|
||||
db2 := test_config.NewTestDB(node2)
|
||||
headerRepository2 := repositories.NewHeaderRepository(db2)
|
||||
tendRepository2 := tend.NewTendRepository(db2)
|
||||
|
||||
for _, number := range []int64{startingBlockNumber, tendBlockNumber, endingBlockNumber} {
|
||||
headerId, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: number})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
headerIds = append(headerIds, headerId)
|
||||
|
||||
headerRepository2.CreateOrUpdateHeader(core.Header{BlockNumber: number})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
err = tendRepository.Create(headerIds[1], test_data.TendModel)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
node1MissingHeaders, err := tendRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(node1MissingHeaders)).To(Equal(2))
|
||||
|
||||
node2MissingHeaders, err := tendRepository2.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(node2MissingHeaders)).To(Equal(3))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,19 @@
|
||||
package tend_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestTend(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Tend Suite")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
})
|
||||
@@ -0,0 +1,85 @@
|
||||
// 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 tend
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
)
|
||||
|
||||
type TendTransformer struct {
|
||||
Repository Repository
|
||||
Fetcher shared.LogFetcher
|
||||
Converter Converter
|
||||
Config shared.TransformerConfig
|
||||
}
|
||||
|
||||
type TendTransformerInitializer struct {
|
||||
Config shared.TransformerConfig
|
||||
}
|
||||
|
||||
func (i TendTransformerInitializer) NewTendTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
||||
fetcher := shared.NewFetcher(blockChain)
|
||||
repository := NewTendRepository(db)
|
||||
transformer := TendTransformer{
|
||||
Fetcher: fetcher,
|
||||
Repository: repository,
|
||||
Converter: TendConverter{},
|
||||
Config: i.Config,
|
||||
}
|
||||
|
||||
return transformer
|
||||
}
|
||||
|
||||
func (t TendTransformer) Execute() error {
|
||||
config := t.Config
|
||||
topics := [][]common.Hash{{common.HexToHash(shared.TendSignature)}}
|
||||
|
||||
missingHeaders, err := t.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
|
||||
if err != nil {
|
||||
log.Println("Error fetching missing headers:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, header := range missingHeaders {
|
||||
ethLogs, err := t.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber)
|
||||
if err != nil {
|
||||
log.Println("Error fetching matching logs:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ethLog := range ethLogs {
|
||||
entity, err := t.Converter.ToEntity(config.ContractAddresses, config.ContractAbi, ethLog)
|
||||
model, err := t.Converter.ToModel(entity)
|
||||
if err != nil {
|
||||
log.Println("Error converting logs:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = t.Repository.Create(header.Id, model)
|
||||
if err != nil {
|
||||
log.Println("Error persisting tend record:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// 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 tend_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
|
||||
tend_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/tend"
|
||||
)
|
||||
|
||||
var _ = Describe("Tend Transformer", func() {
|
||||
var repository tend_mocks.MockTendRepository
|
||||
var fetcher mocks.MockLogFetcher
|
||||
var converter tend_mocks.MockTendConverter
|
||||
var transformer tend.TendTransformer
|
||||
var blockNumber1 = rand.Int63()
|
||||
var blockNumber2 = rand.Int63()
|
||||
|
||||
BeforeEach(func() {
|
||||
repository = tend_mocks.MockTendRepository{}
|
||||
fetcher = mocks.MockLogFetcher{}
|
||||
converter = tend_mocks.MockTendConverter{}
|
||||
|
||||
transformer = tend.TendTransformer{
|
||||
Repository: &repository,
|
||||
Fetcher: &fetcher,
|
||||
Converter: &converter,
|
||||
Config: tend.TendConfig,
|
||||
}
|
||||
})
|
||||
|
||||
It("gets missing headers for blocks in the configured range", func() {
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(repository.PassedStartingBlockNumber).To(Equal(tend.TendConfig.StartingBlockNumber))
|
||||
Expect(repository.PassedEndingBlockNumber).To(Equal(tend.TendConfig.EndingBlockNumber))
|
||||
})
|
||||
|
||||
It("returns an error if it fails to get missing headers", func() {
|
||||
repository.SetMissingHeadersErr(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("fetches eth logs for each missing header", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}, {BlockNumber: blockNumber2}})
|
||||
expectedTopics := [][]common.Hash{{common.HexToHash(shared.TendSignature)}}
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(fetcher.FetchedBlocks).To(Equal([]int64{blockNumber1, blockNumber2}))
|
||||
Expect(fetcher.FetchedTopics).To(Equal(expectedTopics))
|
||||
Expect(fetcher.FetchedContractAddress).To(Equal(test_data.FlipAddress))
|
||||
})
|
||||
|
||||
It("returns an error if fetching logs fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetcherError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err).To(MatchError(fakes.FakeError))
|
||||
})
|
||||
|
||||
It("converts an eth log to an Entity", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(converter.ConverterContract).To(Equal(tend.TendConfig.ContractAddresses))
|
||||
Expect(converter.ConverterAbi).To(Equal(tend.TendConfig.ContractAbi))
|
||||
Expect(converter.LogsToConvert).To(Equal([]types.Log{test_data.TendLog}))
|
||||
})
|
||||
|
||||
It("returns an error if converter fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
converter.SetConverterError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err).To(MatchError(fakes.FakeError))
|
||||
})
|
||||
|
||||
It("returns an error if converter fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(converter.EntitiesToConvert).To(ContainElement(test_data.TendEntity))
|
||||
})
|
||||
|
||||
It("persists the tend record", func() {
|
||||
headerId := int64(1)
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1, Id: headerId}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(repository.PassedHeaderID).To(Equal(headerId))
|
||||
Expect(repository.PassedTendModel).To(Equal(test_data.TendModel))
|
||||
})
|
||||
|
||||
It("returns error if persisting tend record fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
repository.SetCreateError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err).To(MatchError(fakes.FakeError))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user