(VDB-204) Add transformer for Flap kick

This commit is contained in:
Rob Mulholand
2018-11-07 10:52:55 -06:00
parent ec362bebfa
commit cb141f7cb7
21 changed files with 736 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
// 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 flap_kick
import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
var FlapKickConfig = shared.SingleTransformerConfig{
TransformerName: shared.FlapKickLabel,
ContractAddresses: []string{shared.FlapperContractAddress},
ContractAbi: shared.FlapperABI,
Topic: shared.FlapKickSignature,
StartingBlockNumber: 0,
EndingBlockNumber: 10000000,
}
+89
View File
@@ -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 flap_kick
import (
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"time"
)
type FlapKickConverter struct {
}
func (FlapKickConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]interface{}, error) {
var entities []interface{}
for _, ethLog := range ethLogs {
entity := &FlapKickEntity{}
address := ethLog.Address
abi, err := geth.ParseAbi(contractAbi)
if err != nil {
return nil, err
}
contract := bind.NewBoundContract(address, abi, nil, nil, nil)
err = contract.UnpackLog(entity, "Kick", ethLog)
if err != nil {
return nil, err
}
entity.Raw = ethLog
entity.TransactionIndex = ethLog.TxIndex
entity.LogIndex = ethLog.Index
entities = append(entities, *entity)
}
return entities, nil
}
func (FlapKickConverter) ToModels(entities []interface{}) ([]interface{}, error) {
var models []interface{}
for _, entity := range entities {
flapKickEntity, ok := entity.(FlapKickEntity)
if !ok {
return nil, fmt.Errorf("entity of type %T, not %T", entity, FlapKickEntity{})
}
if flapKickEntity.Id == nil {
return nil, errors.New("FlapKick log ID cannot be nil.")
}
id := flapKickEntity.Id.String()
lot := shared.BigIntToString(flapKickEntity.Lot)
bid := shared.BigIntToString(flapKickEntity.Bid)
gal := flapKickEntity.Gal.String()
endValue := shared.BigIntToInt64(flapKickEntity.End)
end := time.Unix(endValue, 0)
rawLog, err := json.Marshal(flapKickEntity.Raw)
if err != nil {
return nil, err
}
model := FlapKickModel{
BidId: id,
Lot: lot,
Bid: bid,
Gal: gal,
End: end,
TransactionIndex: flapKickEntity.TransactionIndex,
LogIndex: flapKickEntity.LogIndex,
Raw: rawLog,
}
models = append(models, model)
}
return models, nil
}
@@ -0,0 +1,96 @@
// 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 flap_kick_test
import (
"encoding/json"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flap_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"math/big"
"time"
)
var _ = Describe("Flap kick converter", func() {
var converter = flap_kick.FlapKickConverter{}
Describe("ToEntity", func() {
It("converts an Eth Log to a FlapKickEntity", func() {
entities, err := converter.ToEntities(shared.FlapperABI, []types.Log{test_data.EthFlapKickLog})
Expect(err).NotTo(HaveOccurred())
Expect(len(entities)).To(Equal(1))
Expect(entities[0]).To(Equal(test_data.FlapKickEntity))
})
It("returns an error if converting log to entity fails", func() {
_, err := converter.ToEntities("error abi", []types.Log{test_data.EthFlapKickLog})
Expect(err).To(HaveOccurred())
})
})
Describe("ToModel", func() {
BeforeEach(func() {
})
It("converts an Entity to a Model", func() {
models, err := converter.ToModels([]interface{}{test_data.FlapKickEntity})
Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1))
Expect(models[0]).To(Equal(test_data.FlapKickModel))
})
It("handles nil values", func() {
emptyAddressHex := "0x0000000000000000000000000000000000000000"
emptyString := ""
emptyTime := time.Unix(0, 0)
emptyEntity := flap_kick.FlapKickEntity{}
emptyEntity.Id = big.NewInt(1)
emptyRawLogJson, err := json.Marshal(types.Log{})
Expect(err).NotTo(HaveOccurred())
models, err := converter.ToModels([]interface{}{emptyEntity})
Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1))
model := models[0].(flap_kick.FlapKickModel)
Expect(model.BidId).To(Equal("1"))
Expect(model.Lot).To(Equal(emptyString))
Expect(model.Bid).To(Equal(emptyString))
Expect(model.Gal).To(Equal(emptyAddressHex))
Expect(model.End).To(Equal(emptyTime))
Expect(model.Raw).To(Equal(emptyRawLogJson))
})
It("returns an error if the flap kick event id is nil", func() {
_, err := converter.ToModels([]interface{}{flap_kick.FlapKickEntity{}})
Expect(err).To(HaveOccurred())
})
It("returns an error if the wrong entity type is passed in", func() {
_, err := converter.ToModels([]interface{}{test_data.WrongEntity{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("entity of type"))
})
})
})
+33
View File
@@ -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 flap_kick
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type FlapKickEntity struct {
Id *big.Int
Lot *big.Int
Bid *big.Int
Gal common.Address
End *big.Int
Raw types.Log
TransactionIndex uint
LogIndex uint
}
@@ -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 flap_kick_test
import (
"io/ioutil"
"log"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestFlapKick(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "FlapKick Suite")
}
var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})
+28
View File
@@ -0,0 +1,28 @@
// 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 flap_kick
import "time"
type FlapKickModel struct {
BidId string `db:"bid_id"`
Lot string
Bid string
Gal string
End time.Time
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}
+91
View File
@@ -0,0 +1,91 @@
// 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 flap_kick
import (
"fmt"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
type FlapKickRepository struct {
db *postgres.DB
}
func (repository *FlapKickRepository) Create(headerID int64, models []interface{}) error {
tx, err := repository.db.Begin()
if err != nil {
return err
}
for _, model := range models {
flapKickModel, ok := model.(FlapKickModel)
if !ok {
return fmt.Errorf("model of type %T, not %T", model, FlapKickModel{})
}
_, err := tx.Exec(
`INSERT into maker.flap_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, log_idx, raw_log)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`,
headerID, flapKickModel.BidId, flapKickModel.Lot, flapKickModel.Bid, flapKickModel.Gal, flapKickModel.End, flapKickModel.TransactionIndex, flapKickModel.LogIndex, flapKickModel.Raw,
)
if err != nil {
tx.Rollback()
return err
}
}
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, flap_kick_checked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET flap_kick_checked = $2`, headerID, true)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func (repository *FlapKickRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, flap_kick_checked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET flap_kick_checked = $2`, headerID, true)
return err
}
func (repository FlapKickRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR flap_kick_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
if err != nil {
fmt.Println("Error:", err)
return result, err
}
return result, nil
}
func (repository *FlapKickRepository) SetDB(db *postgres.DB) {
repository.db = db
}
@@ -0,0 +1,98 @@
// 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 flap_kick_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/flap_kick"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/test_config"
)
var _ = Describe("Flap Kick Repository", func() {
var (
db *postgres.DB
flapKickRepository flap_kick.FlapKickRepository
headerRepository repositories.HeaderRepository
)
BeforeEach(func() {
db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db)
flapKickRepository = flap_kick.FlapKickRepository{}
flapKickRepository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
})
Describe("Create", func() {
modelWithDifferentLogIdx := test_data.FlapKickModel
modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: "flap_kick_checked",
LogEventTableName: "maker.flap_kick",
TestModel: test_data.FlapKickModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &flapKickRepository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("persists a flap kick record", func() {
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = flapKickRepository.Create(headerId, []interface{}{test_data.FlapKickModel})
Expect(err).NotTo(HaveOccurred())
var count int
db.QueryRow(`SELECT count(*) FROM maker.flap_kick`).Scan(&count)
Expect(count).To(Equal(1))
var dbResult flap_kick.FlapKickModel
err = db.Get(&dbResult, `SELECT bid, bid_id, "end", gal, lot, log_idx, tx_idx, raw_log FROM maker.flap_kick WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(dbResult.Bid).To(Equal(test_data.FlapKickModel.Bid))
Expect(dbResult.BidId).To(Equal(test_data.FlapKickModel.BidId))
Expect(dbResult.End.Equal(test_data.FlapKickModel.End)).To(BeTrue())
Expect(dbResult.Gal).To(Equal(test_data.FlapKickModel.Gal))
Expect(dbResult.Lot).To(Equal(test_data.FlapKickModel.Lot))
Expect(dbResult.LogIndex).To(Equal(test_data.FlapKickModel.LogIndex))
Expect(dbResult.TransactionIndex).To(Equal(test_data.FlapKickModel.TransactionIndex))
Expect(dbResult.Raw).To(MatchJSON(test_data.FlapKickModel.Raw))
})
})
Describe("MarkHeaderChecked", func() {
inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: "flap_kick_checked",
Repository: &flapKickRepository,
}
shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
})
Describe("MissingHeaders", func() {
inputs := shared_behaviors.MissingHeadersBehaviorInputs{
Repository: &flapKickRepository,
RepositoryTwo: &flap_kick.FlapKickRepository{},
}
shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
})
})