/
node.go
154 lines (135 loc) · 3.46 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package node
import (
"crypto/ecdsa"
"time"
"github.com/arnaucube/slowlorisdb/core"
)
// Node
type Node struct {
PrivK *ecdsa.PrivateKey
Addr core.Address
Bc *core.Blockchain
Miner bool // indicates if the node is running as a miner
PendingTxs []core.Tx
}
// NewNode creates a new node
func NewNode(privK *ecdsa.PrivateKey, bc *core.Blockchain, isMiner bool) (*Node, error) {
addr := core.AddressFromPrivK(privK)
node := &Node{
PrivK: privK,
Addr: addr,
Bc: bc,
Miner: isMiner,
PendingTxs: []core.Tx{},
}
return node, nil
}
// SignBlock performs a signature of a byte array with the node private key
func (node *Node) Sign(m []byte) (*core.Signature, error) {
return core.Sign(node.PrivK, m)
}
// SignBlock performs a signature of a block with the node private key
func (node *Node) SignBlock(block *core.Block) error {
block.CalculateHash()
sig, err := core.Sign(node.PrivK, block.Hash[:])
if err != nil {
return err
}
block.Signature = sig.Bytes()
return nil
}
// AddToPendingTxs adds a transaction the the node.PendingTxs
func (node *Node) AddToPendingTxs(tx core.Tx) {
node.PendingTxs = append(node.PendingTxs, tx)
}
// BlockFromPendingTxs creates a new block from the pending transactions
func (node *Node) BlockFromPendingTxs() (*core.Block, error) {
block, err := node.NewBlock(node.PendingTxs)
if err != nil {
return nil, err
}
block.PrevHash = node.Bc.LastBlock.Hash
err = block.CalculatePoW(node.Bc.Difficulty)
if err != nil {
return nil, err
}
err = node.SignBlock(block)
if err != nil {
return nil, err
}
block.CalculateHash()
return block, nil
}
// NewBlock creates a new block with the given txs
func (node *Node) NewBlock(txs []core.Tx) (*core.Block, error) {
block := &core.Block{
Height: node.Bc.GetHeight() + 1,
PrevHash: node.Bc.LastBlock.Hash,
Txs: txs,
Miner: core.Address{},
MinerPubK: &node.PrivK.PublicKey,
Timestamp: time.Now(),
Nonce: 0,
Hash: core.Hash{},
Signature: []byte{},
}
block.CalculateHash()
err := node.SignBlock(block)
if err != nil {
return nil, err
}
return block, nil
}
// CreateGenesis creates the genesis block
// pubK is the wallet where the first coins will be created
// amount is the amount of coins that will be created
func (node *Node) CreateGenesis(pubK *ecdsa.PublicKey, amount uint64) (*core.Block, error) {
in := core.Input{
TxId: core.GenesisHashTxInput,
Vout: 0,
Value: amount,
}
var ins []core.Input
ins = append(ins, in)
out := core.Output{
Value: amount,
}
var outs []core.Output
outs = append(outs, out)
tx := core.Tx{
From: &ecdsa.PublicKey{},
To: pubK,
InputCount: uint64(0),
Inputs: []core.Input{},
Outputs: outs,
Signature: []byte{},
}
// calculate TxId
// tx.CalculateTxId()
tx.TxId = core.GenesisHashTxInput
// sign transaction
var txs []core.Tx
txs = append(txs, tx)
block := &core.Block{
Height: node.Bc.LastBlock.Height + 1,
PrevHash: node.Bc.LastBlock.Hash,
Txs: txs,
Miner: node.Addr,
MinerPubK: &node.PrivK.PublicKey,
Timestamp: time.Now(),
Nonce: uint64(0),
Hash: core.Hash{},
Signature: []byte{},
}
block.CalculateHash()
err := node.SignBlock(block)
if err != nil {
return nil, err
}
return block, nil
}
// ParseReceivedBlock is just a caller of node.Bc.AddBlock() at the Node level
func (node *Node) ParseReceivedBlock(block *core.Block) error {
err := node.Bc.AddBlock(block)
return err
}