add block.CalculateHash, fix SignBlock, started CreateGenesis, started VerifyBlock
arnaucube arnaucube@gmail.com
Mon, 08 Apr 2019 20:58:32 +0200
4 files changed,
73 insertions(+),
7 deletions(-)
M
core/block.go
→
core/block.go
@@ -35,8 +35,17 @@ }
// 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 {
M
core/blockchain.go
→
core/blockchain.go
@@ -4,6 +4,7 @@ import (
"bytes" "crypto/ecdsa" "errors" + "fmt" "github.com/arnaucube/slowlorisdb/db" )@@ -91,7 +92,7 @@
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 {@@ -111,6 +112,33 @@ }
// check if the signature is by the miner 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 {
M
node/node.go
→
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) 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 @@ Signature: []byte{},
} 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 +}
M
node/node_test.go
→
node/node_test.go
@@ -1,6 +1,7 @@
package node import ( + "fmt" "io/ioutil" "testing"@@ -67,8 +68,9 @@ tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
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 @@ }
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)) }