kaspad/app/protocol/flowcontext/shared_requested_transactions.go
Lazzeruz 3bfd50a3d9 Dev init
Just some name changes, put in a stand in emission amount, and started copying the algo from Karlsen. Not release worthy yet. Therefore Dev branch exists now.

Also, for now this is for research purposes only. I got no clue what to build on top of Kaspa yet.
Help would be appreciated for ideas and implementations.
2023-12-09 20:46:33 +01:00

50 lines
1.4 KiB
Go

package flowcontext
import (
"sync"
"github.com/zoomy-network/zoomyd/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{}),
}
}