From 4b6ef1e58be23ea3cf2a78e33659774ba24848a7 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Tue, 11 Sep 2018 16:23:47 -0500 Subject: [PATCH] Verify pit file logs - assure required topics + data available before parsing --- .../pit_file/debt_ceiling/converter.go | 23 +++++++++++++--- .../pit_file/debt_ceiling/converter_test.go | 27 +++++++++++++++++-- .../pit_file/debt_ceiling/transformer.go | 2 +- .../pit_file/debt_ceiling/transformer_test.go | 2 -- pkg/transformers/pit_file/ilk/converter.go | 23 +++++++++++++--- .../pit_file/ilk/converter_test.go | 27 +++++++++++++++++-- pkg/transformers/pit_file/ilk/transformer.go | 2 +- .../pit_file/ilk/transformer_test.go | 2 -- .../pit_file/stability_fee/converter.go | 16 +++++++++-- .../pit_file/stability_fee/converter_test.go | 13 +++++++-- .../pit_file/stability_fee/transformer.go | 2 +- .../stability_fee/transformer_test.go | 2 -- pkg/transformers/shared/constants.go | 2 ++ .../mocks/pit_file/debt_ceiling/converter.go | 10 +++---- .../test_data/mocks/pit_file/ilk/converter.go | 10 +++---- .../mocks/pit_file/stability_fee/converter.go | 10 +++---- 16 files changed, 127 insertions(+), 46 deletions(-) diff --git a/pkg/transformers/pit_file/debt_ceiling/converter.go b/pkg/transformers/pit_file/debt_ceiling/converter.go index 939145da..58595ab4 100644 --- a/pkg/transformers/pit_file/debt_ceiling/converter.go +++ b/pkg/transformers/pit_file/debt_ceiling/converter.go @@ -18,20 +18,25 @@ import ( "encoding/json" "math/big" + "errors" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) type Converter interface { - ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileDebtCeilingModel, error) + ToModel(ethLog types.Log) (PitFileDebtCeilingModel, error) } type PitFileDebtCeilingConverter struct{} -func (PitFileDebtCeilingConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileDebtCeilingModel, error) { +func (PitFileDebtCeilingConverter) ToModel(ethLog types.Log) (PitFileDebtCeilingModel, error) { + err := verifyLog(ethLog) + if err != nil { + return PitFileDebtCeilingModel{}, err + } what := common.HexToAddress(ethLog.Topics[1].String()).String() - itemByteLength := 32 - riskBytes := ethLog.Data[len(ethLog.Data)-itemByteLength:] + riskBytes := ethLog.Data[len(ethLog.Data)-shared.DataItemLength:] data := big.NewInt(0).SetBytes(riskBytes).String() raw, err := json.Marshal(ethLog) @@ -42,3 +47,13 @@ func (PitFileDebtCeilingConverter) ToModel(contractAddress string, contractAbi s Raw: raw, }, err } + +func verifyLog(log types.Log) error { + if len(log.Topics) < 2 { + return errors.New("log missing topics") + } + if len(log.Data) < shared.DataItemLength { + return errors.New("log missing data") + } + return nil +} diff --git a/pkg/transformers/pit_file/debt_ceiling/converter_test.go b/pkg/transformers/pit_file/debt_ceiling/converter_test.go index 3d4720ce..0fe07f40 100644 --- a/pkg/transformers/pit_file/debt_ceiling/converter_test.go +++ b/pkg/transformers/pit_file/debt_ceiling/converter_test.go @@ -18,16 +18,39 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) var _ = Describe("", func() { + It("returns err if log is missing topics", func() { + converter := debt_ceiling.PitFileDebtCeilingConverter{} + badLog := types.Log{ + Data: []byte{1, 1, 1, 1, 1}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("returns err if log is missing data", func() { + converter := debt_ceiling.PitFileDebtCeilingConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}, {}, {}, {}}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + It("converts a log to an model", func() { converter := debt_ceiling.PitFileDebtCeilingConverter{} - model, err := converter.ToModel(shared.PitContractAddress, shared.PitABI, test_data.EthPitFileDebtCeilingLog) + model, err := converter.ToModel(test_data.EthPitFileDebtCeilingLog) Expect(err).NotTo(HaveOccurred()) Expect(model).To(Equal(test_data.PitFileDebtCeilingModel)) diff --git a/pkg/transformers/pit_file/debt_ceiling/transformer.go b/pkg/transformers/pit_file/debt_ceiling/transformer.go index f54eb823..7b6dfdd4 100644 --- a/pkg/transformers/pit_file/debt_ceiling/transformer.go +++ b/pkg/transformers/pit_file/debt_ceiling/transformer.go @@ -58,7 +58,7 @@ func (transformer PitFileDebtCeilingTransformer) Execute() error { return err } for _, log := range matchingLogs { - model, err := transformer.Converter.ToModel(pit_file.PitFileConfig.ContractAddress, shared.PitABI, log) + model, err := transformer.Converter.ToModel(log) if err != nil { return err } diff --git a/pkg/transformers/pit_file/debt_ceiling/transformer_test.go b/pkg/transformers/pit_file/debt_ceiling/transformer_test.go index 1b460d94..9b441fd8 100644 --- a/pkg/transformers/pit_file/debt_ceiling/transformer_test.go +++ b/pkg/transformers/pit_file/debt_ceiling/transformer_test.go @@ -112,8 +112,6 @@ var _ = Describe("", func() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(converter.PassedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) - Expect(converter.PassedContractABI).To(Equal(pit_file.PitFileConfig.ContractAbi)) Expect(converter.PassedLog).To(Equal(test_data.EthPitFileDebtCeilingLog)) }) diff --git a/pkg/transformers/pit_file/ilk/converter.go b/pkg/transformers/pit_file/ilk/converter.go index 324cebb2..9bee67e8 100644 --- a/pkg/transformers/pit_file/ilk/converter.go +++ b/pkg/transformers/pit_file/ilk/converter.go @@ -19,20 +19,25 @@ import ( "encoding/json" "math/big" + "errors" "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) type Converter interface { - ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileIlkModel, error) + ToModel(ethLog types.Log) (PitFileIlkModel, error) } type PitFileIlkConverter struct{} -func (PitFileIlkConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (entity PitFileIlkModel, err error) { +func (PitFileIlkConverter) ToModel(ethLog types.Log) (PitFileIlkModel, error) { + err := verifyLog(ethLog) + if err != nil { + return PitFileIlkModel{}, err + } ilk := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) what := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00")) - itemByteLength := 32 - riskBytes := ethLog.Data[len(ethLog.Data)-itemByteLength:] + riskBytes := ethLog.Data[len(ethLog.Data)-shared.DataItemLength:] risk := big.NewInt(0).SetBytes(riskBytes).String() raw, err := json.Marshal(ethLog) @@ -44,3 +49,13 @@ func (PitFileIlkConverter) ToModel(contractAddress string, contractAbi string, e Raw: raw, }, err } + +func verifyLog(log types.Log) error { + if len(log.Topics) < 4 { + return errors.New("log missing topics") + } + if len(log.Data) < shared.DataItemLength { + return errors.New("log missing data") + } + return nil +} diff --git a/pkg/transformers/pit_file/ilk/converter_test.go b/pkg/transformers/pit_file/ilk/converter_test.go index fc02bbe4..38e5ffe0 100644 --- a/pkg/transformers/pit_file/ilk/converter_test.go +++ b/pkg/transformers/pit_file/ilk/converter_test.go @@ -18,16 +18,39 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) var _ = Describe("Pit file ilk converter", func() { + It("returns err if log is missing topics", func() { + converter := ilk.PitFileIlkConverter{} + badLog := types.Log{ + Data: []byte{1, 1, 1, 1, 1}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("returns err if log is missing data", func() { + converter := ilk.PitFileIlkConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}, {}, {}, {}}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + It("converts a log to an model", func() { converter := ilk.PitFileIlkConverter{} - model, err := converter.ToModel(shared.PitContractAddress, shared.PitABI, test_data.EthPitFileIlkLog) + model, err := converter.ToModel(test_data.EthPitFileIlkLog) Expect(err).NotTo(HaveOccurred()) Expect(model).To(Equal(test_data.PitFileIlkModel)) diff --git a/pkg/transformers/pit_file/ilk/transformer.go b/pkg/transformers/pit_file/ilk/transformer.go index 4199adb5..e5e5a54a 100644 --- a/pkg/transformers/pit_file/ilk/transformer.go +++ b/pkg/transformers/pit_file/ilk/transformer.go @@ -58,7 +58,7 @@ func (transformer PitFileIlkTransformer) Execute() error { return err } for _, log := range matchingLogs { - model, err := transformer.Converter.ToModel(pit_file.PitFileConfig.ContractAddress, shared.PitABI, log) + model, err := transformer.Converter.ToModel(log) if err != nil { return err } diff --git a/pkg/transformers/pit_file/ilk/transformer_test.go b/pkg/transformers/pit_file/ilk/transformer_test.go index cbb29d95..5d5769c2 100644 --- a/pkg/transformers/pit_file/ilk/transformer_test.go +++ b/pkg/transformers/pit_file/ilk/transformer_test.go @@ -112,8 +112,6 @@ var _ = Describe("Pit file ilk transformer", func() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(converter.PassedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) - Expect(converter.PassedContractABI).To(Equal(pit_file.PitFileConfig.ContractAbi)) Expect(converter.PassedLog).To(Equal(test_data.EthPitFileIlkLog)) }) diff --git a/pkg/transformers/pit_file/stability_fee/converter.go b/pkg/transformers/pit_file/stability_fee/converter.go index d7d1693d..d78d6b2a 100644 --- a/pkg/transformers/pit_file/stability_fee/converter.go +++ b/pkg/transformers/pit_file/stability_fee/converter.go @@ -18,17 +18,22 @@ import ( "bytes" "encoding/json" + "errors" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) type Converter interface { - ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileStabilityFeeModel, error) + ToModel(ethLog types.Log) (PitFileStabilityFeeModel, error) } type PitFileStabilityFeeConverter struct{} -func (PitFileStabilityFeeConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileStabilityFeeModel, error) { +func (PitFileStabilityFeeConverter) ToModel(ethLog types.Log) (PitFileStabilityFeeModel, error) { + err := verifyLog(ethLog) + if err != nil { + return PitFileStabilityFeeModel{}, err + } what := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) data := common.HexToAddress(ethLog.Topics[1].String()).Hex() @@ -40,3 +45,10 @@ func (PitFileStabilityFeeConverter) ToModel(contractAddress string, contractAbi Raw: raw, }, err } + +func verifyLog(log types.Log) error { + if len(log.Topics) < 3 { + return errors.New("log missing topics") + } + return nil +} diff --git a/pkg/transformers/pit_file/stability_fee/converter_test.go b/pkg/transformers/pit_file/stability_fee/converter_test.go index 1efa470b..f91cf927 100644 --- a/pkg/transformers/pit_file/stability_fee/converter_test.go +++ b/pkg/transformers/pit_file/stability_fee/converter_test.go @@ -18,16 +18,25 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) var _ = Describe("Pit file stability fee converter", func() { + It("returns err if log is missing topics", func() { + converter := stability_fee.PitFileStabilityFeeConverter{} + badLog := types.Log{} + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + It("converts a log to an model", func() { converter := stability_fee.PitFileStabilityFeeConverter{} - model, err := converter.ToModel(shared.PitContractAddress, shared.PitABI, test_data.EthPitFileStabilityFeeLog) + model, err := converter.ToModel(test_data.EthPitFileStabilityFeeLog) Expect(err).NotTo(HaveOccurred()) Expect(model).To(Equal(test_data.PitFileStabilityFeeModel)) diff --git a/pkg/transformers/pit_file/stability_fee/transformer.go b/pkg/transformers/pit_file/stability_fee/transformer.go index 5568d5f3..16fa1f37 100644 --- a/pkg/transformers/pit_file/stability_fee/transformer.go +++ b/pkg/transformers/pit_file/stability_fee/transformer.go @@ -58,7 +58,7 @@ func (transformer PitFileStabilityFeeTransformer) Execute() error { return err } for _, log := range matchingLogs { - model, err := transformer.Converter.ToModel(pit_file.PitFileConfig.ContractAddress, shared.PitABI, log) + model, err := transformer.Converter.ToModel(log) if err != nil { return err } diff --git a/pkg/transformers/pit_file/stability_fee/transformer_test.go b/pkg/transformers/pit_file/stability_fee/transformer_test.go index d2e2fdd5..7106c3dd 100644 --- a/pkg/transformers/pit_file/stability_fee/transformer_test.go +++ b/pkg/transformers/pit_file/stability_fee/transformer_test.go @@ -112,8 +112,6 @@ var _ = Describe("", func() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(converter.PassedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) - Expect(converter.PassedContractABI).To(Equal(pit_file.PitFileConfig.ContractAbi)) Expect(converter.PassedLog).To(Equal(test_data.EthPitFileStabilityFeeLog)) }) diff --git a/pkg/transformers/shared/constants.go b/pkg/transformers/shared/constants.go index 9a2d33ed..813aa1aa 100644 --- a/pkg/transformers/shared/constants.go +++ b/pkg/transformers/shared/constants.go @@ -15,6 +15,8 @@ package shared var ( + DataItemLength = 32 + biteMethod = "Bite(bytes32,bytes32,uint256,uint256,uint256,uint256,uint256)" dentMethod = "dent(uint256,uint256,uint256)" flipKickMethod = "Kick(uint256,uint256,uint256,address,uint48,bytes32,uint256)" diff --git a/pkg/transformers/test_data/mocks/pit_file/debt_ceiling/converter.go b/pkg/transformers/test_data/mocks/pit_file/debt_ceiling/converter.go index 9b6e96d0..98aa0d79 100644 --- a/pkg/transformers/test_data/mocks/pit_file/debt_ceiling/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/debt_ceiling/converter.go @@ -22,15 +22,11 @@ import ( ) type MockPitFileDebtCeilingConverter struct { - converterErr error - PassedContractAddress string - PassedContractABI string - PassedLog types.Log + converterErr error + PassedLog types.Log } -func (converter *MockPitFileDebtCeilingConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (debt_ceiling.PitFileDebtCeilingModel, error) { - converter.PassedContractAddress = contractAddress - converter.PassedContractABI = contractAbi +func (converter *MockPitFileDebtCeilingConverter) ToModel(ethLog types.Log) (debt_ceiling.PitFileDebtCeilingModel, error) { converter.PassedLog = ethLog return test_data.PitFileDebtCeilingModel, converter.converterErr } diff --git a/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go index 377acb08..47da762a 100644 --- a/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go @@ -22,19 +22,15 @@ import ( ) type MockPitFileIlkConverter struct { - PassedContractAddress string - PassedContractABI string - PassedLog types.Log - converterError error + PassedLog types.Log + converterError error } func (converter *MockPitFileIlkConverter) SetConverterError(err error) { converter.converterError = err } -func (converter *MockPitFileIlkConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (ilk.PitFileIlkModel, error) { - converter.PassedContractAddress = contractAddress - converter.PassedContractABI = contractAbi +func (converter *MockPitFileIlkConverter) ToModel(ethLog types.Log) (ilk.PitFileIlkModel, error) { converter.PassedLog = ethLog return test_data.PitFileIlkModel, converter.converterError } diff --git a/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go index e49e918e..198cbecb 100644 --- a/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go @@ -22,15 +22,11 @@ import ( ) type MockPitFileStabilityFeeConverter struct { - converterErr error - PassedContractAddress string - PassedContractABI string - PassedLog types.Log + converterErr error + PassedLog types.Log } -func (converter *MockPitFileStabilityFeeConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (stability_fee.PitFileStabilityFeeModel, error) { - converter.PassedContractAddress = contractAddress - converter.PassedContractABI = contractAbi +func (converter *MockPitFileStabilityFeeConverter) ToModel(ethLog types.Log) (stability_fee.PitFileStabilityFeeModel, error) { converter.PassedLog = ethLog return test_data.PitFileStabilityFeeModel, converter.converterErr }