예제 #1
0
// Tell mempool that these txs were committed.
// Mempool will discard these txs.
// NOTE: this should be called *after* block is committed by consensus.
func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) {
	mem.mtx.Lock()
	defer mem.mtx.Unlock()

	mem.state = state.Copy()
	mem.cache = sm.NewBlockCache(mem.state)

	// First, create a lookup map of txns in new txs.
	txsMap := make(map[string]struct{})
	for _, tx := range block.Data.Txs {
		txsMap[mem.TxID(tx)] = struct{}{}
	}

	// Set height
	mem.height = block.Height
	// Remove transactions that are already in txs.
	goodTxs := mem.filterTxs(txsMap)
	// Recheck mempool txs
	// TODO: make optional
	mem.recheckTxs(goodTxs)

	// At this point, mem.txs are being rechecked.
	// mem.recheckCursor re-scans mem.txs and possibly removes some txs.
	// Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
}
예제 #2
0
// "block" is the new block being committed.
// "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded.
func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) ResetInfo {
	mem.mtx.Lock()
	defer mem.mtx.Unlock()
	mem.state = state.Copy()
	mem.cache = sm.NewBlockCache(mem.state)

	// First, create a lookup map of txns in new block.
	blockTxsMap := make(map[string]struct{})
	for _, tx := range block.Data.Txs {
		blockTxsMap[string(types.TxID(state.ChainID, tx))] = struct{}{}
	}

	// Now we filter all txs from mem.txs that are in blockTxsMap,
	// and ExecTx on what remains. Only valid txs are kept.
	// We track the ranges of txs included in the block and invalidated by it
	// so we can tell peer routines
	var ri = ResetInfo{Height: block.Height}
	var validTxs []types.Tx
	includedStart, invalidStart := -1, -1
	for i, tx := range mem.txs {
		txID := types.TxID(state.ChainID, tx)
		if _, ok := blockTxsMap[string(txID)]; ok {
			startRange(&includedStart, i)           // start counting included txs
			endRange(&invalidStart, i, &ri.Invalid) // stop counting invalid txs
			log.Info("Filter out, already committed", "tx", tx, "txID", txID)
		} else {
			endRange(&includedStart, i, &ri.Included) // stop counting included txs
			err := sm.ExecTx(mem.cache, tx, false, nil)
			if err != nil {
				startRange(&invalidStart, i) // start counting invalid txs
				log.Info("Filter out, no longer valid", "tx", tx, "error", err)
			} else {
				endRange(&invalidStart, i, &ri.Invalid) // stop counting invalid txs
				log.Info("Filter in, new, valid", "tx", tx, "txID", txID)
				validTxs = append(validTxs, tx)
			}
		}
	}
	endRange(&includedStart, len(mem.txs)-1, &ri.Included) // stop counting included txs
	endRange(&invalidStart, len(mem.txs)-1, &ri.Invalid)   // stop counting invalid txs

	// We're done!
	log.Info("New txs", "txs", validTxs, "oldTxs", mem.txs)
	mem.txs = validTxs
	return ri
}
예제 #3
0
// "block" is the new block being committed.
// "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded.
func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) {
	mem.mtx.Lock()
	defer mem.mtx.Unlock()
	mem.state = state.Copy()
	mem.cache = sm.NewBlockCache(mem.state)

	// First, create a lookup map of txns in new block.
	blockTxsMap := make(map[string]struct{})
	for _, tx := range block.Data.Txs {
		blockTxsMap[string(types.TxID(state.ChainID, tx))] = struct{}{}
	}

	// Next, filter all txs from mem.txs that are in blockTxsMap
	txs := []types.Tx{}
	for _, tx := range mem.txs {
		txID := types.TxID(state.ChainID, tx)
		if _, ok := blockTxsMap[string(txID)]; ok {
			log.Debug("Filter out, already committed", "tx", tx, "txID", txID)
			continue
		} else {
			log.Debug("Filter in, still new", "tx", tx, "txID", txID)
			txs = append(txs, tx)
		}
	}

	// Next, filter all txs that aren't valid given new state.
	validTxs := []types.Tx{}
	for _, tx := range txs {
		err := sm.ExecTx(mem.cache, tx, false, nil)
		if err == nil {
			log.Debug("Filter in, valid", "tx", tx)
			validTxs = append(validTxs, tx)
		} else {
			// tx is no longer valid.
			log.Debug("Filter out, no longer valid", "tx", tx, "error", err)
		}
	}

	// We're done!
	log.Debug("New txs", "txs", validTxs, "oldTxs", mem.txs)
	mem.txs = validTxs
}