From f3fce74a8dd32544751c3734460deb7e2d18e597 Mon Sep 17 00:00:00 2001 From: laodouya Date: Mon, 18 Feb 2019 18:22:55 +0800 Subject: [PATCH 1/2] Combine all createRandomBlock funcs to one(exclude sqlchain/xxx_test.go) --- client/helper_test.go | 43 +-------------------------------------- cmd/cql-minerd/dbms.go | 43 +-------------------------------------- types/block_test.go | 16 +++++++-------- types/util.go | 46 ++++++++++++++++++++++++++++++++++++++++++ types/xxx_test.go | 39 ----------------------------------- worker/db_test.go | 6 +++--- worker/dbms_test.go | 2 +- worker/helper_test.go | 42 -------------------------------------- 8 files changed, 60 insertions(+), 177 deletions(-) diff --git a/client/helper_test.go b/client/helper_test.go index bfae11f71..d6447b530 100644 --- a/client/helper_test.go +++ b/client/helper_test.go @@ -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" @@ -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 { @@ -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 diff --git a/cmd/cql-minerd/dbms.go b/cmd/cql-minerd/dbms.go index d9b76fc3e..ab6696f3f 100644 --- a/cmd/cql-minerd/dbms.go +++ b/cmd/cql-minerd/dbms.go @@ -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" @@ -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 } @@ -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 -} diff --git a/types/block_test.go b/types/block_test.go index a33001bee..5c5a26eea 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -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) @@ -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) @@ -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) @@ -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) } @@ -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) @@ -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) @@ -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) diff --git a/types/util.go b/types/util.go index ab09e4310..7773106f0 100644 --- a/types/util.go +++ b/types/util.go @@ -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) } @@ -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 +} diff --git a/types/xxx_test.go b/types/xxx_test.go index 8e472a798..a15dd8666 100644 --- a/types/xxx_test.go +++ b/types/xxx_test.go @@ -33,7 +33,6 @@ import ( var ( uuidLen = 32 - genesisHash = hash.Hash{} testingPrivateKey *asymmetric.PrivateKey testingPublicKey *asymmetric.PublicKey ) @@ -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()) diff --git a/worker/db_test.go b/worker/db_test.go index 9a7e08421..ea9a00279 100644 --- a/worker/db_test.go +++ b/worker/db_test.go @@ -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 @@ -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 @@ -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 diff --git a/worker/dbms_test.go b/worker/dbms_test.go index db29cead8..e30745f67 100644 --- a/worker/dbms_test.go +++ b/worker/dbms_test.go @@ -86,7 +86,7 @@ func TestDBMS(t *testing.T) { So(err, ShouldBeNil) // create sqlchain block - block, err = createRandomBlock(rootHash, true) + block, err = types.CreateRandomBlock(rootHash, true) So(err, ShouldBeNil) // get peers diff --git a/worker/helper_test.go b/worker/helper_test.go index 22c02183d..b9a09a186 100644 --- a/worker/helper_test.go +++ b/worker/helper_test.go @@ -17,19 +17,16 @@ package worker import ( - "crypto/rand" "io/ioutil" "os" "path/filepath" "runtime" "sync" "sync/atomic" - "time" "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces" "github.com/CovenantSQL/CovenantSQL/conf" "github.com/CovenantSQL/CovenantSQL/consistent" - "github.com/CovenantSQL/CovenantSQL/crypto/asymmetric" "github.com/CovenantSQL/CovenantSQL/crypto/hash" "github.com/CovenantSQL/CovenantSQL/proto" "github.com/CovenantSQL/CovenantSQL/route" @@ -243,42 +240,3 @@ func initNode() (cleanupFunc func(), server *rpc.Server, err error) { 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 -} From 27fb7350e31ed62d858291d17130ac40f9cc67a7 Mon Sep 17 00:00:00 2001 From: laodouya Date: Mon, 18 Feb 2019 18:36:42 +0800 Subject: [PATCH 2/2] Change sqlchain/xxx_test.go createRandomBlock function to reuse types.CreateRandomBlock --- sqlchain/xxx_test.go | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/sqlchain/xxx_test.go b/sqlchain/xxx_test.go index 25b5e8350..5f2fcb45e 100644 --- a/sqlchain/xxx_test.go +++ b/sqlchain/xxx_test.go @@ -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++ { @@ -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 }