stasatdaglabs d14809694f
[NOD-1223] Reorganize directory structure (#874)
* [NOD-1223] Delete unused files/packages.

* [NOD-1223] Move signal and limits to the os package.

* [NOD-1223] Put database and dbaccess into the db package.

* [NOD-1223] Fold the logs package into the logger package.

* [NOD-1223] Rename domainmessage to appmessage.

* [NOD-1223] Rename to/from DomainMessage to AppMessage.

* [NOD-1223] Move appmessage to the app packge.

* [NOD-1223] Move protocol to the app packge.

* [NOD-1223] Move the network package to the infrastructure packge.

* [NOD-1223] Rename cmd to executables.

* [NOD-1223] Fix go.doc in the logger package.
2020-08-18 10:26:39 +03:00

85 lines
2.3 KiB
Go

package dbaccess
import (
"io/ioutil"
"reflect"
"testing"
"github.com/kaspanet/kaspad/domain/dagconfig"
"github.com/kaspanet/kaspad/util"
)
func TestBlockStoreSanity(t *testing.T) {
// Create a temp db to run tests against
path, err := ioutil.TempDir("", "TestBlockStoreSanity")
if err != nil {
t.Fatalf("TestBlockStoreSanity: TempDir unexpectedly "+
"failed: %s", err)
}
databaseContext, err := New(path)
if err != nil {
t.Fatalf("TestBlockStoreSanity: Open unexpectedly "+
"failed: %s", err)
}
defer func() {
err := databaseContext.Close()
if err != nil {
t.Fatalf("TestBlockStoreSanity: Close unexpectedly "+
"failed: %s", err)
}
}()
// Store the genesis block
genesis := util.NewBlock(dagconfig.MainnetParams.GenesisBlock)
genesisHash := genesis.Hash()
genesisBytes, err := genesis.Bytes()
if err != nil {
t.Fatalf("TestBlockStoreSanity: util.Block.Bytes unexpectedly "+
"failed: %s", err)
}
dbTx, err := databaseContext.NewTx()
if err != nil {
t.Fatalf("Failed to open database "+
"transaction: %s", err)
}
defer dbTx.RollbackUnlessClosed()
err = StoreBlock(dbTx, genesisHash, genesisBytes)
if err != nil {
t.Fatalf("TestBlockStoreSanity: StoreBlock unexpectedly "+
"failed: %s", err)
}
err = dbTx.Commit()
if err != nil {
t.Fatalf("Failed to commit database "+
"transaction: %s", err)
}
// Make sure the genesis block now exists in the db
exists, err := HasBlock(databaseContext, genesisHash)
if err != nil {
t.Fatalf("TestBlockStoreSanity: HasBlock unexpectedly "+
"failed: %s", err)
}
if !exists {
t.Fatalf("TestBlockStoreSanity: just-inserted block is " +
"missing from the database")
}
// Fetch the genesis block back from the db and make sure
// that it's equal to the original
fetchedGenesisBytes, err := FetchBlock(databaseContext, genesisHash)
if err != nil {
t.Fatalf("TestBlockStoreSanity: FetchBlock unexpectedly "+
"failed: %s", err)
}
fetchedGenesis, err := util.NewBlockFromBytes(fetchedGenesisBytes)
if err != nil {
t.Fatalf("TestBlockStoreSanity: NewBlockFromBytes unexpectedly "+
"failed: %s", err)
}
if !reflect.DeepEqual(genesis.MsgBlock(), fetchedGenesis.MsgBlock()) {
t.Fatalf("TestBlockStoreSanity: just-inserted block is " +
"not equal to its database counterpart.")
}
}