Merge pull request #60 from cerc-io/unit_tests_fix

fix broken unit tests
This commit is contained in:
Michael 2023-03-15 16:02:53 -04:00 committed by GitHub
commit 449376ee1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 5 deletions

View File

@ -19,6 +19,11 @@ package validator
import ( import (
"context" "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/go-blockservice"
"github.com/ipfs/kubo/core" "github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/repo/fsrepo" "github.com/ipfs/kubo/repo/fsrepo"
@ -41,3 +46,36 @@ func InitIPFSBlockService(ipfsPath string) (blockservice.BlockService, error) {
} }
return ipfsNode.Blocks, nil 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
}

View File

@ -228,7 +228,7 @@ var _ = Describe("PG-IPFS Validator", func() {
}) })
Describe("ValidateTrie", func() { Describe("ValidateTrie", func() {
AfterEach(func() { AfterEach(func() {
err = ResetTestDB(db) err = validator.ResetTestDB(db)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("Returns an error if the state root node is missing", func() { 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() { Describe("ValidateStateTrie", func() {
AfterEach(func() { AfterEach(func() {
err = ResetTestDB(db) err = validator.ResetTestDB(db)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("Returns an error the state root node is missing", func() { It("Returns an error the state root node is missing", func() {
@ -303,7 +303,7 @@ var _ = Describe("PG-IPFS Validator", func() {
Describe("ValidateStorageTrie", func() { Describe("ValidateStorageTrie", func() {
AfterEach(func() { AfterEach(func() {
err = ResetTestDB(db) err = validator.ResetTestDB(db)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("Returns an error the storage root node is missing", func() { It("Returns an error the storage root node is missing", func() {
@ -330,11 +330,11 @@ func loadTrie(stateNodes, storageNodes [][]byte) {
tx, err := db.Beginx() tx, err := db.Beginx()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
for _, node := range stateNodes { 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()) Expect(err).ToNot(HaveOccurred())
} }
for _, node := range storageNodes { 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()) Expect(err).ToNot(HaveOccurred())
} }
err = tx.Commit() err = tx.Commit()