Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions client/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ package client
import (
"database/sql"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"time"

pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
"github.com/CovenantSQL/CovenantSQL/conf"
Expand Down Expand Up @@ -143,7 +141,7 @@ func startTestService() (stopTestService func(), tempDir string, err error) {
dbID := proto.DatabaseID("db")

// create sqlchain block
block, err = createRandomBlock(rootHash, true)
block, err = types.CreateRandomBlock(rootHash, true)

// get database peers
if peers, err = genPeers(1); err != nil {
Expand Down Expand Up @@ -267,45 +265,6 @@ func initNode() (cleanupFunc func(), tempDir string, server *rpc.Server, err err
return
}

// copied from sqlchain.xxx_test.
func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err error) {
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()

if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])

b = &types.Block{
SignedHeader: types.SignedHeader{
Header: types.Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: rootHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
}

if isGenesis {
emptyNode := &proto.RawNodeID{}
b.SignedHeader.ParentHash = hash.Hash{}
b.SignedHeader.GenesisHash = hash.Hash{}
b.SignedHeader.Producer = emptyNode.ToNodeID()
b.SignedHeader.MerkleRoot = hash.Hash{}

err = b.PackAsGenesis()
return
}

err = b.PackAndSignBlock(priv)
return
}

func testRequest(method route.RemoteFunc, req interface{}, response interface{}) (err error) {
// get node id
var nodeID proto.NodeID
Expand Down
43 changes: 1 addition & 42 deletions cmd/cql-minerd/dbms.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ package main
import (
"bytes"
"io/ioutil"
"math/rand"
"os"
"time"

"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
Expand Down Expand Up @@ -123,7 +121,7 @@ func loadGenesisBlock(fixture *conf.MinerDatabaseFixture) (block *types.Block, e

if os.IsNotExist(err) && fixture.AutoGenerateGenesisBlock {
// generate
if block, err = createRandomBlock(rootHash, true); err != nil {
if block, err = types.CreateRandomBlock(rootHash, true); err != nil {
err = errors.Wrap(err, "create random block failed")
return
}
Expand All @@ -145,42 +143,3 @@ func loadGenesisBlock(fixture *conf.MinerDatabaseFixture) (block *types.Block, e

return
}

// copied from sqlchain.xxx_test.
func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err error) {
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()

if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])

b = &types.Block{
SignedHeader: types.SignedHeader{
Header: types.Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: rootHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
}

if isGenesis {
emptyNode := &proto.RawNodeID{}
b.SignedHeader.ParentHash = hash.Hash{}
b.SignedHeader.GenesisHash = hash.Hash{}
b.SignedHeader.Producer = emptyNode.ToNodeID()
b.SignedHeader.MerkleRoot = hash.Hash{}

err = b.PackAsGenesis()
return
}

err = b.PackAndSignBlock(priv)
return
}
35 changes: 8 additions & 27 deletions sqlchain/xxx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,26 +236,19 @@ func registerNodesWithPublicKey(pub *asymmetric.PublicKey, diff int, num int) (
}

func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err error) {
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()

b, err = types.CreateRandomBlock(parent, isGenesis)
if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])
if isGenesis {
return
}

b = &types.Block{
SignedHeader: types.SignedHeader{
Header: types.Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: genesisHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()
if err != nil {
return
}

for i, n := 0, rand.Intn(10)+10; i < n; i++ {
Expand All @@ -270,18 +263,6 @@ func createRandomBlock(parent hash.Hash, isGenesis bool) (b *types.Block, err er
}
}

if isGenesis {
emptyNode := &proto.RawNodeID{}
b.SignedHeader.ParentHash = hash.Hash{}
b.SignedHeader.GenesisHash = hash.Hash{}
b.SignedHeader.Producer = emptyNode.ToNodeID()
b.SignedHeader.MerkleRoot = hash.Hash{}
b.Acks = nil

err = b.PackAsGenesis()
return
}

err = b.PackAndSignBlock(priv)
return
}
Expand Down
16 changes: 8 additions & 8 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func TestSignAndVerify(t *testing.T) {
block, err := createRandomBlock(genesisHash, false)
block, err := CreateRandomBlock(genesisHash, false)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestSignAndVerify(t *testing.T) {
}

func TestHeaderMarshalUnmarshaler(t *testing.T) {
block, err := createRandomBlock(genesisHash, false)
block, err := CreateRandomBlock(genesisHash, false)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestHeaderMarshalUnmarshaler(t *testing.T) {
}

func TestSignedHeaderMarshaleUnmarshaler(t *testing.T) {
block, err := createRandomBlock(genesisHash, false)
block, err := CreateRandomBlock(genesisHash, false)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand Down Expand Up @@ -143,11 +143,11 @@ func TestSignedHeaderMarshaleUnmarshaler(t *testing.T) {
}

func TestBlockMarshalUnmarshaler(t *testing.T) {
origin, err := createRandomBlock(genesisHash, false)
origin, err := CreateRandomBlock(genesisHash, false)
if err != nil {
t.Fatalf("error occurred: %v", err)
}
origin2, err := createRandomBlock(genesisHash, false)
origin2, err := CreateRandomBlock(genesisHash, false)
if err != nil {
t.Fatalf("error occurred: %v", err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestBlockMarshalUnmarshaler(t *testing.T) {
}

func TestGenesis(t *testing.T) {
genesis, err := createRandomBlock(genesisHash, true)
genesis, err := CreateRandomBlock(genesisHash, true)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand All @@ -219,7 +219,7 @@ func TestGenesis(t *testing.T) {
}

// Test non-genesis block
genesis, err = createRandomBlock(genesisHash, false)
genesis, err = CreateRandomBlock(genesisHash, false)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand All @@ -232,7 +232,7 @@ func TestGenesis(t *testing.T) {
}

// Test altered block
genesis, err = createRandomBlock(genesisHash, true)
genesis, err = CreateRandomBlock(genesisHash, true)

if err != nil {
t.Fatalf("error occurred: %v", err)
Expand Down
46 changes: 46 additions & 0 deletions types/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
package types

import (
"crypto/rand"
"time"

"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/crypto/verifier"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/pkg/errors"
)

var genesisHash = hash.Hash{}

type canMarshalHash interface {
MarshalHash() ([]byte, error)
}
Expand All @@ -46,3 +53,42 @@ func buildHash(data canMarshalHash, h *hash.Hash) (err error) {
copy(h[:], newHash[:])
return
}

// CreateRandomBlock create a new random block
func CreateRandomBlock(parent hash.Hash, isGenesis bool) (b *Block, err error) {
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()

if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])

b = &Block{
SignedHeader: SignedHeader{
Header: Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: genesisHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
}

if isGenesis {
emptyNode := &proto.RawNodeID{}
b.SignedHeader.ParentHash = hash.Hash{}
b.SignedHeader.GenesisHash = hash.Hash{}
b.SignedHeader.Producer = emptyNode.ToNodeID()
b.SignedHeader.MerkleRoot = hash.Hash{}

err = b.PackAsGenesis()
return
}

err = b.PackAndSignBlock(priv)
return
}
39 changes: 0 additions & 39 deletions types/xxx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

var (
uuidLen = 32
genesisHash = hash.Hash{}
testingPrivateKey *asymmetric.PrivateKey
testingPublicKey *asymmetric.PublicKey
)
Expand Down Expand Up @@ -299,44 +298,6 @@ func setup() {
log.SetLevel(log.DebugLevel)
}

func createRandomBlock(parent hash.Hash, isGenesis bool) (b *Block, err error) {
// Generate key pair
priv, _, err := asymmetric.GenSecp256k1KeyPair()

if err != nil {
return
}

h := hash.Hash{}
rand.Read(h[:])

b = &Block{
SignedHeader: SignedHeader{
Header: Header{
Version: 0x01000000,
Producer: proto.NodeID(h.String()),
GenesisHash: genesisHash,
ParentHash: parent,
Timestamp: time.Now().UTC(),
},
},
}

if isGenesis {
emptyNode := &proto.RawNodeID{}
b.SignedHeader.ParentHash = hash.Hash{}
b.SignedHeader.GenesisHash = hash.Hash{}
b.SignedHeader.Producer = emptyNode.ToNodeID()
b.SignedHeader.MerkleRoot = hash.Hash{}

err = b.PackAsGenesis()
return
}

err = b.PackAndSignBlock(priv)
return
}

func TestMain(m *testing.M) {
setup()
os.Exit(m.Run())
Expand Down
6 changes: 3 additions & 3 deletions worker/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestSingleDatabase(t *testing.T) {

// create genesis block
var block *types.Block
block, err = createRandomBlock(rootHash, true)
block, err = types.CreateRandomBlock(rootHash, true)
So(err, ShouldBeNil)

// create database
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestInitFailed(t *testing.T) {

// create genesis block
var block *types.Block
block, err = createRandomBlock(rootHash, true)
block, err = types.CreateRandomBlock(rootHash, true)
So(err, ShouldBeNil)

// broken peers configuration
Expand Down Expand Up @@ -472,7 +472,7 @@ func TestDatabaseRecycle(t *testing.T) {

// create genesis block
var block *types.Block
block, err = createRandomBlock(rootHash, true)
block, err = types.CreateRandomBlock(rootHash, true)
So(err, ShouldBeNil)

// create database
Expand Down
Loading