-
Notifications
You must be signed in to change notification settings - Fork 4
/
shared_requested_transactions.go
49 lines (42 loc) · 1.39 KB
/
shared_requested_transactions.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
package flowcontext
import (
"sync"
"github.com/kaspaclassic/caspad/domain/consensus/model/externalapi"
)
// SharedRequestedTransactions is a data structure that is shared between peers that
// holds the IDs of all the requested transactions to prevent redundant requests.
type SharedRequestedTransactions struct {
transactions map[externalapi.DomainTransactionID]struct{}
sync.Mutex
}
// Remove removes a transaction from the set.
func (s *SharedRequestedTransactions) Remove(txID *externalapi.DomainTransactionID) {
s.Lock()
defer s.Unlock()
delete(s.transactions, *txID)
}
// RemoveMany removes a set of transactions from the set.
func (s *SharedRequestedTransactions) RemoveMany(txIDs []*externalapi.DomainTransactionID) {
s.Lock()
defer s.Unlock()
for _, txID := range txIDs {
delete(s.transactions, *txID)
}
}
// AddIfNotExists adds a transaction to the set if it doesn't exist yet.
func (s *SharedRequestedTransactions) AddIfNotExists(txID *externalapi.DomainTransactionID) (exists bool) {
s.Lock()
defer s.Unlock()
_, ok := s.transactions[*txID]
if ok {
return true
}
s.transactions[*txID] = struct{}{}
return false
}
// NewSharedRequestedTransactions returns a new instance of SharedRequestedTransactions.
func NewSharedRequestedTransactions() *SharedRequestedTransactions {
return &SharedRequestedTransactions{
transactions: make(map[externalapi.DomainTransactionID]struct{}),
}
}