Exemplo n.º 1
0
// TransactionPoolSubscribe adds a subscriber to the transaction pool.
// Subscribers will receive all consensus set changes as well as transaction
// pool changes, and should not subscribe to both.
func (tp *TransactionPool) TransactionPoolSubscribe(subscriber modules.TransactionPoolSubscriber) {
	lockID := tp.mu.Lock()
	tp.subscribers = append(tp.subscribers, subscriber)
	for i := 0; i <= tp.consensusChangeIndex; i++ {
		cc, err := tp.consensusSet.ConsensusChange(i)
		if err != nil && build.DEBUG {
			panic(err)
		}
		subscriber.ProcessConsensusChange(cc)

		// Release the lock between iterations to smooth out performance a bit
		// - tpool does not need to hold the lock for 15,000 consensus change
		// objects.
		tp.mu.Unlock(lockID)
		runtime.Gosched()
		lockID = tp.mu.Lock()
	}

	// Send the new subscriber the transaction pool set.
	var txns []types.Transaction
	var cc modules.ConsensusChange
	for _, tSet := range tp.transactionSets {
		txns = append(txns, tSet...)
	}
	for _, tSetDiff := range tp.transactionSetDiffs {
		cc = cc.Append(tSetDiff)
	}
	subscriber.ReceiveUpdatedUnconfirmedTransactions(txns, cc)
	tp.mu.Unlock(lockID)
}
Exemplo n.º 2
0
// TransactionPoolSubscribe adds a subscriber to the transaction pool.
// Subscribers will receive all consensus set changes as well as transaction
// pool changes, and should not subscribe to both.
func (tp *TransactionPool) TransactionPoolSubscribe(subscriber modules.TransactionPoolSubscriber) {
	id := tp.mu.Lock()
	tp.subscribers = append(tp.subscribers, subscriber)
	for i := 0; i <= tp.consensusChangeIndex; i++ {
		cc, err := tp.consensusSet.ConsensusChange(i)
		if err != nil && build.DEBUG {
			panic(err)
		}
		subscriber.ProcessConsensusChange(cc)
	}

	// Send the new subscriber the transaction pool set.
	var txns []types.Transaction
	var cc modules.ConsensusChange
	for _, tSet := range tp.transactionSets {
		txns = append(txns, tSet...)
	}
	for _, tSetDiff := range tp.transactionSetDiffs {
		cc = cc.Append(tSetDiff)
	}
	subscriber.ReceiveUpdatedUnconfirmedTransactions(txns, cc)
	tp.mu.Unlock(id)
}