From 7935334888db8bde7723124d7fbea59b1605e7f4 Mon Sep 17 00:00:00 2001 From: i-norden Date: Wed, 15 Mar 2023 14:09:43 -0500 Subject: [PATCH] fix unit tests --- pkg/util.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/validator_test.go | 10 +++++----- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/pkg/util.go b/pkg/util.go index 5a2ca8e..2d9ca3f 100644 --- a/pkg/util.go +++ b/pkg/util.go @@ -19,6 +19,11 @@ 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" @@ -41,3 +46,36 @@ 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/validator_test.go b/pkg/validator_test.go index 731be27..a73f38c 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 = ResetTestDB(db) + err = validator.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 = ResetTestDB(db) + err = validator.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 = ResetTestDB(db) + err = validator.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 := PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node, blockNumber) + _, err := validator.PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node, blockNumber) Expect(err).ToNot(HaveOccurred()) } for _, node := range storageNodes { - _, err := PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node, blockNumber) + _, err := validator.PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node, blockNumber) Expect(err).ToNot(HaveOccurred()) } err = tx.Commit()