mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-26 23:46:08 +00:00
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.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package flowcontext
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/zoomy-network/zoomyd/domain/consensus/model/externalapi"
|
|
)
|
|
|
|
// SharedRequestedBlocks is a data structure that is shared between peers that
|
|
// holds the hashes of all the requested blocks to prevent redundant requests.
|
|
type SharedRequestedBlocks struct {
|
|
blocks map[externalapi.DomainHash]struct{}
|
|
sync.Mutex
|
|
}
|
|
|
|
// Remove removes a block from the set.
|
|
func (s *SharedRequestedBlocks) Remove(hash *externalapi.DomainHash) {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
delete(s.blocks, *hash)
|
|
}
|
|
|
|
// RemoveSet removes a set of blocks from the set.
|
|
func (s *SharedRequestedBlocks) RemoveSet(blockHashes map[externalapi.DomainHash]struct{}) {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
for hash := range blockHashes {
|
|
delete(s.blocks, hash)
|
|
}
|
|
}
|
|
|
|
// AddIfNotExists adds a block to the set if it doesn't exist yet.
|
|
func (s *SharedRequestedBlocks) AddIfNotExists(hash *externalapi.DomainHash) (exists bool) {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
_, ok := s.blocks[*hash]
|
|
if ok {
|
|
return true
|
|
}
|
|
s.blocks[*hash] = struct{}{}
|
|
return false
|
|
}
|
|
|
|
// NewSharedRequestedBlocks returns a new instance of SharedRequestedBlocks.
|
|
func NewSharedRequestedBlocks() *SharedRequestedBlocks {
|
|
return &SharedRequestedBlocks{
|
|
blocks: make(map[externalapi.DomainHash]struct{}),
|
|
}
|
|
}
|