From 670b215e0ad78409e3228211685b4b2ce8d65915 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Mon, 8 Apr 2019 20:58:32 +0200 Subject: [PATCH] add block.CalculateHash, fix SignBlock, started CreateGenesis, started VerifyBlock --- core/block.go | 9 +++++++++ core/blockchain.go | 30 +++++++++++++++++++++++++++++- node/node.go | 35 +++++++++++++++++++++++++++++++---- node/node_test.go | 6 ++++-- 4 files changed, 73 insertions(+), 7 deletions(-) diff --git a/core/block.go b/core/block.go index f69b59e..9250c69 100644 --- a/core/block.go +++ b/core/block.go @@ -35,10 +35,19 @@ func (block Block) Copy() *Block { // Bytes outputs a byte array containing the data of the Block func (blk Block) Bytes() []byte { + // TODO add parser, to use minimum amount of bytes b, _ := json.Marshal(blk) return b } +func (block *Block) CalculateHash() { + blockCopy := block.Copy() + blockCopy.Hash = Hash{} + blockCopy.Signature = []byte{} + hash := HashBytes(blockCopy.Bytes()) + block.Hash = hash +} + func (blk *Block) GetNonce() uint64 { return blk.Nonce } diff --git a/core/blockchain.go b/core/blockchain.go index 9d3b4b2..e158f6d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/ecdsa" "errors" + "fmt" "github.com/arnaucube/slowlorisdb/db" ) @@ -91,7 +92,7 @@ func (bc *Blockchain) GetPrevBlock(hash Hash) (*Block, error) { return prevBlock, nil } -func (bc *Blockchain) VerifyBlockSignature(block *Block) bool { +func (bc *Blockchain) verifyBlockSignature(block *Block) bool { // check if the signer is one of the blockchain.AuthMiners signerIsMiner := false for _, pubK := range bc.PoA.AuthMiners { @@ -113,6 +114,33 @@ func (bc *Blockchain) VerifyBlockSignature(block *Block) bool { return VerifySignature(block.MinerPubK, block.Hash[:], *sig) } +func (bc *Blockchain) VerifyBlock(block *Block) bool { + // verify block signature + if !bc.verifyBlockSignature(block) { + return false + } + + // verify timestamp + + // verify prev hash + // check that the block.PrevHash is the blockchain current last block + fmt.Println(block.PrevHash) + fmt.Println(bc.LastBlock.Hash) + if !bytes.Equal(block.PrevHash[:], bc.LastBlock.Hash[:]) { + return false + } + + // verify block height + // check that the block height is the last block + 1 + if block.Height != bc.LastBlock.Height+1 { + return false + } + + // verify block transactions + + return true +} + // func (bc *Blockchain) Mint(toAddr Address, amount uint64) error { // fromAddr := Address(HashBytes([]byte("mint"))) // out := diff --git a/node/node.go b/node/node.go index 2bcddba..a960c9f 100644 --- a/node/node.go +++ b/node/node.go @@ -32,8 +32,13 @@ func (node *Node) Sign(m []byte) (*core.Signature, error) { return core.Sign(node.PrivK, m) } -func (node *Node) SignBlock(block *core.Block) (*core.Signature, error) { - return core.Sign(node.PrivK, block.Hash[:]) +func (node *Node) SignBlock(block *core.Block) error { + sig, err := core.Sign(node.PrivK, block.Hash[:]) + if err != nil { + return err + } + block.Signature = sig.Bytes() + return nil } func (node *Node) AddToPendingTxs(tx core.Tx) { @@ -42,15 +47,17 @@ func (node *Node) AddToPendingTxs(tx core.Tx) { func (node *Node) BlockFromPendingTxs() (*core.Block, error) { block := node.NewBlock(node.PendingTxs) + block.PrevHash = node.Bc.LastBlock.Hash err := block.CalculatePoW(node.Bc.Difficulty) if err != nil { return nil, err } - sig, err := node.SignBlock(block) + err = node.SignBlock(block) if err != nil { return nil, err } - block.Signature = sig.Bytes() + block.CalculateHash() + return block, nil } @@ -68,3 +75,23 @@ func (node *Node) NewBlock(txs []core.Tx) *core.Block { } return block } + +func (node *Node) CreateGenesis() (*core.Block, error) { + block := &core.Block{ + Height: uint64(0), + PrevHash: core.Hash{}, + Txs: []core.Tx{}, + Miner: node.Addr, + MinerPubK: &node.PrivK.PublicKey, + Timestamp: time.Now(), + Nonce: uint64(0), + Hash: core.Hash{}, + Signature: []byte{}, + } + + err := node.SignBlock(block) + if err != nil { + return nil, err + } + return block, nil +} diff --git a/node/node_test.go b/node/node_test.go index 7a31e7d..0f0395f 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -1,6 +1,7 @@ package node import ( + "fmt" "io/ioutil" "testing" @@ -67,8 +68,9 @@ func TestBlockFromPendingTxs(t *testing.T) { node.AddToPendingTxs(*tx) block, err := node.BlockFromPendingTxs() assert.Nil(t, err) + fmt.Println("h", block.Hash) assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty)) - assert.True(t, node.Bc.VerifyBlockSignature(block)) + assert.True(t, node.Bc.VerifyBlock(block)) } func TestBlockFromPendingTxsIteration(t *testing.T) { @@ -94,5 +96,5 @@ func TestBlockFromPendingTxsIteration(t *testing.T) { block, err := node.BlockFromPendingTxs() assert.Nil(t, err) assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty)) - assert.True(t, node.Bc.VerifyBlockSignature(block)) + assert.True(t, node.Bc.VerifyBlock(block)) }