-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger.go
183 lines (155 loc) · 4.3 KB
/
ledger.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package coin
import (
"bytes"
"sync"
)
// Ledger is a list of transactions.
type Ledger struct {
trns []Transaction
bals map[PublicKey]uint32
lock sync.RWMutex
}
// NewLedger returns a new, empty ledger.
func NewLedger() *Ledger {
return &Ledger{bals: make(map[PublicKey]uint32)}
}
// AddGenesisTransaction adds a genesis transaction to the ledger. Only a
// genesis transaction is valid as the first transaction of the ledger.
func (led *Ledger) AddGenesisTransaction(trn Transaction) error {
// ledger should not have a genesis transaction
if len(led.trns) > 0 {
return ErrLedAlreadyGenesis
}
// amount must not be zero
if trn.Amount == 0 {
return ErrTrnAmountZero
}
// signature must be ok
if !trn.Verify(led.Signature()) {
return ErrTrnBadSignature
}
// update internal state
led.lock.Lock()
defer led.lock.Unlock()
led.trns = append(led.trns, trn)
led.bals[trn.To] += trn.Amount
return nil
}
// AddTransaction verifies that a transaction is valid and adds it to the ledger
// if it is.
func (led *Ledger) AddTransaction(trn Transaction) error {
// ledger should have a genesis transaction
if len(led.trns) == 0 {
return ErrLedNoGenesis
}
// amount must not be zero
if trn.Amount == 0 {
return ErrTrnAmountZero
}
// sender and receiver must not be the same
if bytes.Equal(trn.From[:], trn.To[:]) {
return ErrTrnSameReceiver
}
// sender must have enough coin
if trn.Amount > led.BalanceOf(trn.From) {
return ErrTrnAmountBalance
}
// signature must be ok
if !trn.Verify(led.Signature()) {
return ErrTrnBadSignature
}
// update internal state
led.lock.Lock()
defer led.lock.Unlock()
led.trns = append(led.trns, trn)
led.bals[trn.To] += trn.Amount
led.bals[trn.From] -= trn.Amount
return nil
}
// Signature returns the current signature of the ledger. This is the signature
// of the latest transaction.
func (led *Ledger) Signature() Signature {
led.lock.RLock()
defer led.lock.RUnlock()
trn, _ := led.LatestTransaction()
return trn.Signature
}
// Transactions returns all transactions in the ledger.
func (led *Ledger) Transactions() []Transaction {
trns := make([]Transaction, len(led.trns))
copy(trns, led.trns)
return trns
}
// Do calls the given function on each transaction in the ledger, in order. If
// any of the calls return an error, Do will return that error immediately.
func (led *Ledger) Do(f func(trn Transaction) error) error {
led.lock.RLock()
defer led.lock.RUnlock()
for _, trn := range led.trns {
if err := f(trn); err != nil {
return err
}
}
return nil
}
// TransactionsOf will return a slice of transactions in the ledger involving an
// account given its public key.
func (led *Ledger) TransactionsOf(pubkey PublicKey) []Transaction {
led.lock.RLock()
defer led.lock.RUnlock()
var trns []Transaction
for _, trn := range led.trns {
if bytes.Equal(trn.From[:], pubkey[:]) || bytes.Equal(trn.To[:], pubkey[:]) {
trns = append(trns, trn)
}
}
return trns
}
// GenesisTransaction returns the first transaction of the ledger.
func (led *Ledger) GenesisTransaction() (trn Transaction, err error) {
// genesis transaction must exist
if len(led.trns) == 0 {
err = ErrLedNoGenesis
return
}
// genesis transaction
led.lock.RLock()
defer led.lock.RUnlock()
trn = led.trns[0]
return
}
// LatestTransaction returns the latest transaction of the ledger.
func (led *Ledger) LatestTransaction() (trn Transaction, err error) {
// genesis transaction must exist
if len(led.trns) == 0 {
err = ErrLedNoGenesis
return
}
// latest transaction
led.lock.RLock()
defer led.lock.RUnlock()
trn = led.trns[len(led.trns)-1]
return
}
// Balances returns a map of every public key in the ledger and the balances of
// the accounts associated with those public keys.
func (led *Ledger) Balances() map[PublicKey]uint32 {
led.lock.RLock()
defer led.lock.RUnlock()
bals := make(map[PublicKey]uint32)
for pubKey, amt := range led.bals {
bals[pubKey] = amt
}
return bals
}
// BalanceOf returns the balance of an account in the ledger given its public
// key. If the account is not in the ledger, it will return 0.
func (led *Ledger) BalanceOf(pubKey PublicKey) uint32 {
led.lock.RLock()
defer led.lock.RUnlock()
return led.bals[pubKey]
}
// Size returns the number of transactions in the ledger.
func (led *Ledger) Size() uint64 {
return uint64(len(led.trns))
}