eth-ipfs-state-validator/pkg/util_test.go

40 lines
1.0 KiB
Go
Raw Normal View History

2023-03-16 00:28:52 +00:00
package validator_test
import (
"github.com/ipfs/go-cid"
"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) error {
2023-03-16 00:28:52 +00:00
c, err := RawdataToCid(codec, raw, mh)
if err != nil {
return err
2023-03-16 00:28:52 +00:00
}
_, err = tx.Exec(
`INSERT INTO ipld.blocks (key, data, block_number) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`,
c.String(), raw, blockNumber)
return err
2023-03-16 00:28:52 +00:00
}
// 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
}
2023-04-29 06:40:03 +00:00
// ResetTestDB truncates all used tables from the test DB
2023-03-16 00:28:52 +00:00
func ResetTestDB(db *sqlx.DB) error {
2023-04-29 06:40:03 +00:00
_, err := db.Exec("TRUNCATE ipld.blocks")
2023-03-16 00:28:52 +00:00
return err
}