git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/p2pool/stratum/mempool.go (about) 1 package stratum 2 3 import ( 4 "git.gammaspectra.live/P2Pool/consensus/v3/p2pool/mempool" 5 "git.gammaspectra.live/P2Pool/consensus/v3/types" 6 "github.com/dolthub/swiss" 7 "time" 8 ) 9 10 type MiningMempool swiss.Map[types.Hash, *mempool.Entry] 11 12 func (m *MiningMempool) m() *swiss.Map[types.Hash, *mempool.Entry] { 13 return (*swiss.Map[types.Hash, *mempool.Entry])(m) 14 } 15 16 // Add Inserts a transaction into the mempool. 17 func (m *MiningMempool) Add(tx *mempool.Entry) (added bool) { 18 mm := m.m() 19 if !mm.Has(tx.Id) { 20 if tx.TimeReceived.IsZero() { 21 tx.TimeReceived = time.Now() 22 } 23 mm.Put(tx.Id, tx) 24 added = true 25 } 26 27 return added 28 } 29 30 func (m *MiningMempool) Swap(pool mempool.Mempool) { 31 currentTime := time.Now() 32 33 mm := m.m() 34 for _, tx := range pool { 35 if v, ok := mm.Get(tx.Id); ok { 36 //tx is already here, use previous seen time 37 tx.TimeReceived = v.TimeReceived 38 } else { 39 tx.TimeReceived = currentTime 40 } 41 } 42 43 mm.Clear() 44 45 for _, tx := range pool { 46 mm.Put(tx.Id, tx) 47 } 48 } 49 50 func (m *MiningMempool) Select(highFee uint64, receivedSince time.Duration) (pool mempool.Mempool) { 51 pool = make(mempool.Mempool, 0, m.m().Count()) 52 53 currentTime := time.Now() 54 55 m.m().Iter(func(_ types.Hash, tx *mempool.Entry) (stop bool) { 56 if currentTime.Sub(tx.TimeReceived) > receivedSince || tx.Fee >= highFee { 57 pool = append(pool, tx) 58 } 59 return false 60 }) 61 62 return pool 63 }