forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance_cache.go
114 lines (104 loc) · 3.09 KB
/
balance_cache.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
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import (
"golang.org/x/crypto/ed25519"
)
// BalanceCache maintains a partial unconfirmed view of the ledger.
// It's used by Ledger when (dis-)connecting blocks and by TransactionQueueMemory
// when deciding whether or not to add a transaction to the queue.
type BalanceCache struct {
ledger Ledger
minBalance int64
cache map[[ed25519.PublicKeySize]byte]int64
}
// NewBalanceCache returns a new instance of a BalanceCache.
func NewBalanceCache(ledger Ledger, minBalance int64) *BalanceCache {
b := &BalanceCache{ledger: ledger, minBalance: minBalance}
b.Reset()
return b
}
// Reset resets the balance cache.
func (b *BalanceCache) Reset() {
b.cache = make(map[[ed25519.PublicKeySize]byte]int64)
}
// Apply applies the effect of the transaction to the invovled parties' cached balances.
// It returns false if sender balance would go negative as a result of applying this transaction.
// It also returns false if a remaining non-zero sender balance would be less than minBalance.
func (b *BalanceCache) Apply(tx *Transaction) (bool, error) {
if !tx.IsCoinbase() {
// check and debit sender balance
var fpk [ed25519.PublicKeySize]byte
copy(fpk[:], tx.From)
senderBalance, ok := b.cache[fpk]
if !ok {
var err error
senderBalance, err = b.ledger.GetPublicKeyBalance(tx.From)
if err != nil {
return false, err
}
}
totalSpent := tx.Amount + tx.Fee
if totalSpent > senderBalance {
return false, nil
}
senderBalance -= totalSpent
if senderBalance > 0 && senderBalance < b.minBalance {
return false, nil
}
b.cache[fpk] = senderBalance
}
// credit recipient balance
var tpk [ed25519.PublicKeySize]byte
copy(tpk[:], tx.To)
recipientBalance, ok := b.cache[tpk]
if !ok {
var err error
recipientBalance, err = b.ledger.GetPublicKeyBalance(tx.To)
if err != nil {
return false, err
}
}
recipientBalance += tx.Amount
b.cache[tpk] = recipientBalance
return true, nil
}
// Undo undoes the effects of a transaction on the invovled parties' cached balances.
func (b *BalanceCache) Undo(tx *Transaction) error {
if !tx.IsCoinbase() {
// credit balance for sender
var fpk [ed25519.PublicKeySize]byte
copy(fpk[:], tx.From)
senderBalance, ok := b.cache[fpk]
if !ok {
var err error
senderBalance, err = b.ledger.GetPublicKeyBalance(tx.From)
if err != nil {
return err
}
}
totalSpent := tx.Amount + tx.Fee
senderBalance += totalSpent
b.cache[fpk] = senderBalance
}
// debit recipient balance
var tpk [ed25519.PublicKeySize]byte
copy(tpk[:], tx.To)
recipientBalance, ok := b.cache[tpk]
if !ok {
var err error
recipientBalance, err = b.ledger.GetPublicKeyBalance(tx.To)
if err != nil {
return err
}
}
if recipientBalance < tx.Amount {
panic("Recipient balance went negative")
}
b.cache[tpk] = recipientBalance - tx.Amount
return nil
}
// Balances returns the underlying cache of balances.
func (b *BalanceCache) Balances() map[[ed25519.PublicKeySize]byte]int64 {
return b.cache
}