From 3e2e8bc295dde27dc0449b6d8bb70be2ccad19b7 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Thu, 16 Mar 2023 08:28:52 +0800 Subject: [PATCH] Add missing file to fix unit tests --- pkg/util.go | 38 -------------------------------------- pkg/util_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ pkg/validator_test.go | 10 +++++----- 3 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 pkg/util_test.go diff --git a/pkg/util.go b/pkg/util.go index 2d9ca3f..5a2ca8e 100644 --- a/pkg/util.go +++ b/pkg/util.go @@ -19,11 +19,6 @@ package validator import ( "context" - "github.com/ipfs/go-cid" - blockstore "github.com/ipfs/go-ipfs-blockstore" - dshelp "github.com/ipfs/go-ipfs-ds-help" - "github.com/jmoiron/sqlx" - "github.com/ipfs/go-blockservice" "github.com/ipfs/kubo/core" "github.com/ipfs/kubo/repo/fsrepo" @@ -46,36 +41,3 @@ func InitIPFSBlockService(ipfsPath string) (blockservice.BlockService, error) { } return ipfsNode.Blocks, nil } - -// PublishRaw derives a cid from raw bytes and provided codec and multihash type, and writes it to the db tx -func PublishRaw(tx *sqlx.Tx, codec, mh uint64, raw []byte, blockNumber uint64) (string, error) { - c, err := RawdataToCid(codec, raw, mh) - if err != nil { - return "", err - } - dbKey := dshelp.MultihashToDsKey(c.Hash()) - prefixedKey := blockstore.BlockPrefix.String() + dbKey.String() - _, err = tx.Exec(`INSERT INTO public.blocks (key, data, block_number) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`, prefixedKey, raw, blockNumber) - return c.String(), err -} - -// RawdataToCid takes the desired codec, multihash type, and a slice of bytes -// and returns the proper cid of the object. -func RawdataToCid(codec uint64, rawdata []byte, multiHash uint64) (cid.Cid, error) { - c, err := cid.Prefix{ - Codec: codec, - Version: 1, - MhType: multiHash, - MhLength: -1, - }.Sum(rawdata) - if err != nil { - return cid.Cid{}, err - } - return c, nil -} - -// ResetTestDB drops all rows in the test db public.blocks table -func ResetTestDB(db *sqlx.DB) error { - _, err := db.Exec("DELETE FROM public.blocks") - return err -} diff --git a/pkg/util_test.go b/pkg/util_test.go new file mode 100644 index 0000000..46bc5af --- /dev/null +++ b/pkg/util_test.go @@ -0,0 +1,41 @@ +package validator_test + +import ( + "github.com/ipfs/go-cid" + blockstore "github.com/ipfs/go-ipfs-blockstore" + dshelp "github.com/ipfs/go-ipfs-ds-help" + "github.com/jmoiron/sqlx" +) + +// PublishRaw derives a cid from raw bytes and provided codec and multihash type, and writes it to the db tx +func PublishRaw(tx *sqlx.Tx, codec, mh uint64, raw []byte, blockNumber uint64) (string, error) { + c, err := RawdataToCid(codec, raw, mh) + if err != nil { + return "", err + } + dbKey := dshelp.MultihashToDsKey(c.Hash()) + prefixedKey := blockstore.BlockPrefix.String() + dbKey.String() + _, err = tx.Exec(`INSERT INTO public.blocks (key, data, block_number) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`, prefixedKey, raw, blockNumber) + return c.String(), err +} + +// RawdataToCid takes the desired codec, multihash type, and a slice of bytes +// and returns the proper cid of the object. +func RawdataToCid(codec uint64, rawdata []byte, multiHash uint64) (cid.Cid, error) { + c, err := cid.Prefix{ + Codec: codec, + Version: 1, + MhType: multiHash, + MhLength: -1, + }.Sum(rawdata) + if err != nil { + return cid.Cid{}, err + } + return c, nil +} + +// ResetTestDB drops all rows in the test db public.blocks table +func ResetTestDB(db *sqlx.DB) error { + _, err := db.Exec("DELETE FROM public.blocks") + return err +} diff --git a/pkg/validator_test.go b/pkg/validator_test.go index 4ccb996..8c50b85 100644 --- a/pkg/validator_test.go +++ b/pkg/validator_test.go @@ -228,7 +228,7 @@ var _ = Describe("PG-IPFS Validator", func() { }) Describe("ValidateTrie", func() { AfterEach(func() { - err = validator.ResetTestDB(db) + err = ResetTestDB(db) Expect(err).ToNot(HaveOccurred()) }) It("Returns an error if the state root node is missing", func() { @@ -279,7 +279,7 @@ var _ = Describe("PG-IPFS Validator", func() { Describe("ValidateStateTrie", func() { AfterEach(func() { - err = validator.ResetTestDB(db) + err = ResetTestDB(db) Expect(err).ToNot(HaveOccurred()) }) It("Returns an error the state root node is missing", func() { @@ -303,7 +303,7 @@ var _ = Describe("PG-IPFS Validator", func() { Describe("ValidateStorageTrie", func() { AfterEach(func() { - err = validator.ResetTestDB(db) + err = ResetTestDB(db) Expect(err).ToNot(HaveOccurred()) }) It("Returns an error the storage root node is missing", func() { @@ -330,11 +330,11 @@ func loadTrie(stateNodes, storageNodes [][]byte) { tx, err := db.Beginx() Expect(err).ToNot(HaveOccurred()) for _, node := range stateNodes { - _, err := validator.PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node, blockNumber) + _, err := PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node, blockNumber) Expect(err).ToNot(HaveOccurred()) } for _, node := range storageNodes { - _, err := validator.PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node, blockNumber) + _, err := PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node, blockNumber) Expect(err).ToNot(HaveOccurred()) } err = tx.Commit()