-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
116 lines (101 loc) · 2.34 KB
/
Copy pathnode.go
File metadata and controls
116 lines (101 loc) · 2.34 KB
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
// ABOUTME: Node owns the blockchain, mempool, and miner identity, and
// ABOUTME: exposes the operations a peer performs on the network.
package quark
import (
"errors"
"sync"
)
type Node struct {
mu sync.Mutex
Chain *BlockChain
Mempool *Mempool
Miner *Miner
}
func NewNode() (*Node, error) {
miner, err := NewMiner()
if err != nil {
return nil, err
}
return &Node{
Chain: NewBlockChain(),
Mempool: NewMempool(),
Miner: miner,
}, nil
}
func (n *Node) Address() string {
return n.Miner.Wallet.Address()
}
func (n *Node) SubmitTransaction(tx *Transaction) error {
n.mu.Lock()
defer n.mu.Unlock()
if tx.IsCoinbase() {
return errors.New("coinbase transactions cannot be submitted directly")
}
if tx.Amount <= 0 {
return errors.New("transaction amount must be positive")
}
if !tx.Verify() {
return errors.New("transaction signature is invalid")
}
hash := tx.Hash()
if n.Mempool.Has(hash) {
return errors.New("transaction already in mempool")
}
if n.chainContainsTransaction(hash) {
return errors.New("transaction already in chain")
}
pendingDebit := int64(0)
for _, p := range n.Mempool.Pending() {
if p.Sender == tx.Sender {
pendingDebit += p.Amount
}
}
available := n.Chain.Balance(tx.Sender) - pendingDebit
if available < tx.Amount {
return errors.New("sender has insufficient balance")
}
return n.Mempool.Add(tx)
}
func (n *Node) Mine() (*Block, error) {
n.mu.Lock()
defer n.mu.Unlock()
pending := n.Mempool.Pending()
block, err := n.Miner.Mine(n.Chain, pending)
if err != nil {
return nil, err
}
hashes := make([]string, 0, len(block.Data))
for _, tx := range block.Data {
hashes = append(hashes, tx.Hash())
}
n.Mempool.Remove(hashes...)
return block, nil
}
func (n *Node) ReceiveBlock(block *Block) error {
n.mu.Lock()
defer n.mu.Unlock()
if err := n.Chain.Append(block); err != nil {
return err
}
hashes := make([]string, 0, len(block.Data))
for _, tx := range block.Data {
hashes = append(hashes, tx.Hash())
}
n.Mempool.Remove(hashes...)
return nil
}
func (n *Node) Balance(address string) int64 {
n.mu.Lock()
defer n.mu.Unlock()
return n.Chain.Balance(address)
}
func (n *Node) chainContainsTransaction(hash string) bool {
for _, block := range n.Chain.Blocks {
for _, tx := range block.Data {
if tx.Hash() == hash {
return true
}
}
}
return false
}