Browse Source

node block PoW, sign and verify

master
arnaucube 5 years ago
parent
commit
01963a8f41
3 changed files with 39 additions and 18 deletions
  1. +12
    -0
      core/block.go
  2. +5
    -1
      node/node.go
  3. +22
    -17
      node/node_test.go

+ 12
- 0
core/block.go

@ -1,6 +1,7 @@
package core package core
import ( import (
"crypto/ecdsa"
"encoding/json" "encoding/json"
"time" "time"
) )
@ -80,15 +81,26 @@ func (block *Block) CalculatePoW(difficulty uint64) error {
hash = HashBytes(blockCopy.Bytes()) hash = HashBytes(blockCopy.Bytes())
} }
block.Hash = hash block.Hash = hash
block.Nonce = blockCopy.Nonce
return nil return nil
} }
func CheckBlockPoW(block *Block, difficulty uint64) bool { func CheckBlockPoW(block *Block, difficulty uint64) bool {
blockCopy := block.Copy() blockCopy := block.Copy()
blockCopy.Hash = Hash{} blockCopy.Hash = Hash{}
blockCopy.Signature = []byte{}
return CheckPoW(HashBytes(blockCopy.Bytes()), difficulty) return CheckPoW(HashBytes(blockCopy.Bytes()), difficulty)
} }
func VerifyBlockSignature(pubK *ecdsa.PublicKey, block *Block) bool {
sig, err := SignatureFromBytes(block.Signature)
if err != nil {
return false
}
return VerifySignature(pubK, block.Hash[:], *sig)
}
func BlockFromBytes(b []byte) (*Block, error) { func BlockFromBytes(b []byte) (*Block, error) {
var block *Block var block *Block
err := json.Unmarshal(b, &block) err := json.Unmarshal(b, &block)

+ 5
- 1
node/node.go

@ -35,6 +35,10 @@ func (node *Node) Sign(m []byte) (*core.Signature, error) {
return core.Sign(node.PrivK, m) 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) AddToPendingTxs(tx core.Tx) { func (node *Node) AddToPendingTxs(tx core.Tx) {
node.PendingTxs = append(node.PendingTxs, tx) node.PendingTxs = append(node.PendingTxs, tx)
} }
@ -45,7 +49,7 @@ func (node *Node) BlockFromPendingTxs() (*core.Block, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
sig, err := node.Sign(block.Bytes())
sig, err := node.SignBlock(block)
if err != nil { if err != nil {
return nil, err return nil, err
} }

+ 22
- 17
node/node_test.go

@ -51,23 +51,28 @@ func TestBlockFromPendingTxs(t *testing.T) {
tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{}) tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
node.AddToPendingTxs(*tx) node.AddToPendingTxs(*tx)
block, err := node.BlockFromPendingTxs() block, err := node.BlockFromPendingTxs()
assert.Nil(t, err)
assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty)) assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
// TODO add VerifyBlockSignature
assert.True(t, core.VerifyBlockSignature(&node.PrivK.PublicKey, block))
} }
// func TestBlockFromPendingTxsIteration(t *testing.T) {
// dir, err := ioutil.TempDir("", "db")
// assert.Nil(t, err)
// db, err := db.New(dir)
// assert.Nil(t, err)
//
// node, err := NewNode(db, uint64(1))
// assert.Nil(t, err)
//
// addr0 := core.Address(core.HashBytes([]byte("addr0")))
// addr1 := core.Address(core.HashBytes([]byte("addr1")))
// for i := 0; i < 10; i++ {
// tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
//
// }
// }
func TestBlockFromPendingTxsIteration(t *testing.T) {
dir, err := ioutil.TempDir("", "db")
assert.Nil(t, err)
db, err := db.New(dir)
assert.Nil(t, err)
node, err := NewNode(db, uint64(1))
assert.Nil(t, err)
addr0 := core.Address(core.HashBytes([]byte("addr0")))
addr1 := core.Address(core.HashBytes([]byte("addr1")))
for i := 0; i < 10; i++ {
tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
node.AddToPendingTxs(*tx)
}
block, err := node.BlockFromPendingTxs()
assert.Nil(t, err)
assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
assert.True(t, core.VerifyBlockSignature(&node.PrivK.PublicKey, block))
}

Loading…
Cancel
Save