Browse Source

add block.CalculateHash, fix SignBlock, started CreateGenesis, started VerifyBlock

master
arnaucube 5 years ago
parent
commit
670b215e0a
4 changed files with 73 additions and 7 deletions
  1. +9
    -0
      core/block.go
  2. +29
    -1
      core/blockchain.go
  3. +31
    -4
      node/node.go
  4. +4
    -2
      node/node_test.go

+ 9
- 0
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
}

+ 29
- 1
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 :=

+ 31
- 4
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
}

+ 4
- 2
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))
}

Loading…
Cancel
Save