forked from cerc-io/ipld-eth-server
Update flip kick and tend transformers due to contract changes
* Update FlipperAddress to new local ganache address * Update flip_kick table * Update flipkick transformer to handle new signature and abi * Update tend table * Update tend converter
This commit is contained in:
@@ -17,9 +17,9 @@ package tend
|
||||
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
|
||||
var TendConfig = shared.TransformerConfig{
|
||||
ContractAddresses: "0x08cb6176addcca2e1d1ffe21bee464b72ee4cd8d", //this is a temporary address deployed locally
|
||||
ContractAddresses: shared.FlipperContractAddress,
|
||||
ContractAbi: shared.FlipperABI,
|
||||
Topics: []string{shared.TendSignature},
|
||||
Topics: []string{shared.TendFunctionSignature},
|
||||
StartingBlockNumber: 0,
|
||||
EndingBlockNumber: 100,
|
||||
}
|
||||
|
||||
@@ -16,61 +16,37 @@ package tend
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
"math/big"
|
||||
|
||||
"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)
|
||||
Convert(contractAddress string, contractAbi string, ethLog types.Log) (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)
|
||||
func (c TendConverter) Convert(contractAddress string, contractAbi string, ethLog types.Log) (TendModel, error) {
|
||||
entity := TendModel{}
|
||||
entity.Guy = common.HexToAddress(ethLog.Topics[1].Hex()).String()
|
||||
entity.BidId = ethLog.Topics[2].Big().String()
|
||||
entity.Lot = ethLog.Topics[3].Big().String()
|
||||
|
||||
if err != nil {
|
||||
return entity, err
|
||||
}
|
||||
itemByteLength := 32
|
||||
lastDataItemStartIndex := len(ethLog.Data) - itemByteLength
|
||||
lastItem := ethLog.Data[lastDataItemStartIndex:]
|
||||
last := big.NewInt(0).SetBytes(lastItem)
|
||||
entity.Bid = last.String()
|
||||
|
||||
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
|
||||
err = contract.UnpackLog(&entity, "Tend", ethLog)
|
||||
if err != nil {
|
||||
return entity, err
|
||||
}
|
||||
entity.Tic = "0" //TODO: how do we get the bid tic?
|
||||
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)
|
||||
rawJson, err := json.Marshal(ethLog)
|
||||
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
|
||||
|
||||
entity.Raw = string(rawJson)
|
||||
return entity, err
|
||||
}
|
||||
|
||||
@@ -15,11 +15,6 @@
|
||||
package tend_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
@@ -30,74 +25,16 @@ import (
|
||||
|
||||
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)
|
||||
|
||||
Describe("Convert", func() {
|
||||
It("converts an eth log to a db model", func() {
|
||||
model, err := converter.Convert(shared.FlipperContractAddress, shared.FlipperABI, test_data.TendLogNote)
|
||||
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{}))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// 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
|
||||
}
|
||||
@@ -15,7 +15,6 @@
|
||||
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"
|
||||
@@ -27,14 +26,13 @@ import (
|
||||
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() {
|
||||
// These test are marked as pending until the Flip contract is deployed to Kovan.
|
||||
var _ = Describe("Integration tests", func() {
|
||||
XIt("Fetches Tend event logs from a local test chain", func() {
|
||||
ipcPath := test_config.TestClient.IPCPath
|
||||
|
||||
rawRpcClient, err := rpc.Dial(ipcPath)
|
||||
@@ -47,10 +45,10 @@ var _ = XDescribe("Integration tests", func() {
|
||||
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
||||
realBlockChain := geth.NewBlockChain(blockChainClient, realNode, transactionConverter)
|
||||
realFetcher := shared.NewFetcher(realBlockChain)
|
||||
topic0 := common.HexToHash(shared.TendSignature)
|
||||
topic0 := common.HexToHash(shared.TendFunctionSignature)
|
||||
topics := [][]common.Hash{{topic0}}
|
||||
|
||||
result, err := realFetcher.FetchLogs(test_data.FlipAddress, topics, test_data.FlipKickBlockNumber)
|
||||
result, err := realFetcher.FetchLogs(shared.FlipperContractAddress, topics, test_data.FlipKickBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
Expect(len(result) > 0).To(BeTrue())
|
||||
@@ -60,27 +58,4 @@ var _ = XDescribe("Integration tests", func() {
|
||||
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))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,17 +14,12 @@
|
||||
|
||||
package tend
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type TendModel struct {
|
||||
Id string
|
||||
BidId string `db:"bid_id"`
|
||||
Lot string
|
||||
Bid string
|
||||
Guy []byte
|
||||
Guy string
|
||||
Tic string
|
||||
Era time.Time
|
||||
TransactionIndex uint `db:"tx_idx"`
|
||||
Raw string `db:"raw_log"`
|
||||
}
|
||||
|
||||
@@ -34,9 +34,9 @@ func NewTendRepository(db *postgres.DB) TendRepository {
|
||||
|
||||
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,
|
||||
`INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log)
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.TransactionIndex, tend.Raw,
|
||||
)
|
||||
|
||||
return err
|
||||
|
||||
@@ -58,17 +58,16 @@ var _ = Describe("TendRepository", func() {
|
||||
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)
|
||||
err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, 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.BidId).To(Equal(test_data.TendModel.BidId))
|
||||
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))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson))
|
||||
})
|
||||
|
||||
It("returns an error if inserting a tend record fails", func() {
|
||||
@@ -111,7 +110,7 @@ var _ = Describe("TendRepository", func() {
|
||||
outOfRangeBlockNumber = tendBlockNumber + 2
|
||||
})
|
||||
|
||||
It("returns headers for which there isn't an associated flip_kick record", func() {
|
||||
It("returns headers for which there isn't an associated tend record", func() {
|
||||
var headerIds []int64
|
||||
|
||||
for _, number := range []int64{startingBlockNumber, tendBlockNumber, endingBlockNumber, outOfRangeBlockNumber} {
|
||||
|
||||
@@ -50,7 +50,7 @@ func (i TendTransformerInitializer) NewTendTransformer(db *postgres.DB, blockCha
|
||||
|
||||
func (t TendTransformer) Execute() error {
|
||||
config := t.Config
|
||||
topics := [][]common.Hash{{common.HexToHash(shared.TendSignature)}}
|
||||
topics := [][]common.Hash{{common.HexToHash(shared.TendFunctionSignature)}}
|
||||
|
||||
missingHeaders, err := t.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
|
||||
if err != nil {
|
||||
@@ -66,8 +66,7 @@ func (t TendTransformer) Execute() error {
|
||||
}
|
||||
|
||||
for _, ethLog := range ethLogs {
|
||||
entity, err := t.Converter.ToEntity(config.ContractAddresses, config.ContractAbi, ethLog)
|
||||
model, err := t.Converter.ToModel(entity)
|
||||
model, err := t.Converter.Convert(config.ContractAddresses, config.ContractAbi, ethLog)
|
||||
if err != nil {
|
||||
log.Println("Error converting logs:", err)
|
||||
return err
|
||||
|
||||
@@ -69,13 +69,13 @@ var _ = Describe("Tend Transformer", func() {
|
||||
|
||||
It("fetches eth logs for each missing header", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}, {BlockNumber: blockNumber2}})
|
||||
expectedTopics := [][]common.Hash{{common.HexToHash(shared.TendSignature)}}
|
||||
expectedTopics := [][]common.Hash{{common.HexToHash(shared.TendFunctionSignature)}}
|
||||
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))
|
||||
Expect(fetcher.FetchedContractAddress).To(Equal(shared.FlipperContractAddress))
|
||||
})
|
||||
|
||||
It("returns an error if fetching logs fails", func() {
|
||||
@@ -87,40 +87,30 @@ var _ = Describe("Tend Transformer", func() {
|
||||
Expect(err).To(MatchError(fakes.FakeError))
|
||||
})
|
||||
|
||||
It("converts an eth log to an Entity", func() {
|
||||
It("converts an eth log to an Model", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
|
||||
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}))
|
||||
Expect(converter.LogsToConvert).To(Equal([]types.Log{test_data.TendLogNote}))
|
||||
})
|
||||
|
||||
It("returns an error if converter fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
|
||||
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})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
|
||||
|
||||
err := transformer.Execute()
|
||||
|
||||
@@ -131,7 +121,7 @@ var _ = Describe("Tend Transformer", func() {
|
||||
|
||||
It("returns error if persisting tend record fails", func() {
|
||||
repository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumber1}})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLog})
|
||||
fetcher.SetFetchedLogs([]types.Log{test_data.TendLogNote})
|
||||
repository.SetCreateError(fakes.FakeError)
|
||||
err := transformer.Execute()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user