-
Notifications
You must be signed in to change notification settings - Fork 127
/
queue.go
145 lines (132 loc) · 3.79 KB
/
queue.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
package kernel
import (
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
)
func (node *Node) QueueTransaction(tx *common.VersionedTransaction) (string, error) {
hash := tx.PayloadHash()
_, finalized, err := node.persistStore.ReadTransaction(hash)
if err != nil {
return "", err
}
if len(finalized) > 0 {
return hash.String(), nil
}
old, err := node.persistStore.CacheGetTransaction(hash)
if err != nil {
return "", err
}
if old != nil {
return old.PayloadHash().String(), node.persistStore.CachePutTransaction(tx)
}
err = tx.Validate(node.persistStore, false)
if err != nil {
return "", err
}
err = node.persistStore.CachePutTransaction(tx)
if err != nil {
return "", err
}
s := &common.Snapshot{
Version: common.SnapshotVersionCommonEncoding,
NodeId: node.IdForNetwork,
}
s.AddSoleTransaction(tx.PayloadHash())
err = node.chain.AppendSelfEmpty(s)
return tx.PayloadHash().String(), err
}
func (node *Node) LoopCacheQueue() error {
defer close(node.cqc)
for {
if node.waitOrDone(time.Duration(config.SnapshotRoundGap)) {
return nil
}
caches, finals, _ := node.QueueState()
if caches > 1000 || finals > 500 {
logger.Printf("LoopCacheQueue QueueState too big %d %d\n", caches, finals)
continue
}
neighbors := node.Peer.Neighbors()
if len(neighbors) <= 0 {
continue
}
var stale []crypto.Hash
filter := make(map[crypto.Hash]bool)
txs, err := node.persistStore.CacheRetrieveTransactions(100)
for _, tx := range txs {
hash := tx.PayloadHash()
if filter[hash] {
continue
}
filter[hash] = true
_, finalized, err := node.persistStore.ReadTransaction(hash)
if err != nil {
logger.Printf("LoopCacheQueue ReadTransaction ERROR %s %s\n", hash, err)
continue
}
if len(finalized) > 0 {
stale = append(stale, hash)
continue
}
err = tx.Validate(node.persistStore, false)
if err != nil {
logger.Debugf("LoopCacheQueue Validate ERROR %s %s\n", hash, err)
// FIXME not mark invalid tx as stale is to ensure final graph sync
// but we need some way to mitigate cache transaction DoS attack from nodes
continue
}
nbor := neighbors[int(clock.Now().UnixNano())%len(neighbors)]
node.SendTransactionToPeer(nbor.IdForNetwork, hash)
s := &common.Snapshot{
Version: common.SnapshotVersionCommonEncoding,
NodeId: node.IdForNetwork,
}
s.AddSoleTransaction(tx.PayloadHash())
node.chain.AppendSelfEmpty(s)
}
if err != nil {
logger.Printf("LoopCacheQueue CacheRetrieveTransactions ERROR %s\n", err)
}
err = node.persistStore.CacheRemoveTransactions(stale)
if err != nil {
logger.Printf("LoopCacheQueue CacheRemoveTransactions ERROR %s\n", err)
}
}
}
func (node *Node) QueueState() (uint64, uint64, map[string][2]uint64) {
node.chains.RLock()
defer node.chains.RUnlock()
var caches, finals uint64
state := make(map[string][2]uint64)
accepted := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
for _, cn := range accepted {
chain := node.chains.m[cn.IdForNetwork]
sa := [2]uint64{
uint64(len(chain.CachePool)),
uint64(len(chain.finalActionsRing)),
}
round := chain.FinalPool[chain.FinalIndex]
if round != nil {
sa[1] = sa[1] + uint64(round.Size)
}
caches = caches + sa[0]
finals = finals + sa[1]
state[chain.ChainId.String()] = sa
}
return caches, finals, state
}
func (chain *Chain) clearAndQueueSnapshotOrPanic(s *common.Snapshot) error {
if chain.ChainId != s.NodeId {
panic("should never be here")
}
ns := &common.Snapshot{
Version: common.SnapshotVersionCommonEncoding,
NodeId: s.NodeId,
}
ns.AddSoleTransaction(s.SoleTransaction())
return chain.AppendSelfEmpty(ns)
}