mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1579] Rename AcceptedTxIDs to AcceptedTransactionIDs. * [NOD-1579] Add InsertBlockResult to ValidateAndInsertBlock results. * [NOD-1593] Rename InsertBlockResult to BlockInsertionResult. * [NOD-1593] Add SelectedParentChainChanges to AddBlockToVirtual's result. * [NOD-1593] Implement findSelectedParentChainChanges. * [NOD-1593] Implement TestFindSelectedParentChainChanges. * [NOD-1593] Fix a string. * [NOD-1593] Finish implementing TestFindSelectedParentChainChanges. * [NOD-1593] Fix merge errors. * [NOD-1597] Begin implementing UTXOIndex. * [NOD-1597] Connect UTXOIndex to RPC. * [NOD-1597] Connect Consensus to UTXOIndex. * [NOD-1597] Add AcceptanceData to BlockInfo. * [NOD-1597] Implement UTXOIndex.Update(). * [NOD-1597] Implement add(), remove(), and discard() in utxoIndexStore. * [NOD-1597] Add error cases to add() and remove(). * [NOD-1597] Add special cases to add() and remove(). * [NOD-1597] Implement commit. * [NOD-1597] Add a mutex around UTXOIndex.Update(). * [NOD-1597] Return changes to the UTXO from Update(). * [NOD-1597] Add NotifyUTXOsChangedRequestMessage and related structs. * [NOD-1597] Implement HandleNotifyUTXOsChanged. * [NOD-1597] Begin implementing TestUTXOIndex. * [NOD-1597] Implement RegisterForUTXOsChangedNotifications. * [NOD-1597] Fix bad transaction.ID usage. * [NOD-1597] Implement convertUTXOChangesToUTXOsChangedNotification. * [NOD-1597] Make UTXOsChangedNotificationMessage.Removed UTXOsByAddressesEntry instead of just RPCOutpoint so that the client can discern which address was the UTXO removed for. * [NOD-1597] Collect outpoints in TestUTXOIndex. * [NOD-1597] Rename RPC stuff. * [NOD-1597] Add messages for GetUTXOsByAddresses. * [NOD-1597] Implement HandleGetUTXOsByAddresses. * [NOD-1597] Implement GetUTXOsByAddresses. * [NOD-1597] Implement UTXOs(). * [NOD-1597] Implement getUTXOOutpointEntryPairs(). * [NOD-1597] Expand TestUTXOIndex. * [NOD-1597] Convert SubmitTransaction to use RPCTransaction instead of MsgTx. * [NOD-1597] Finish implementing TestUTXOIndex. * [NOD-1597] Add messages for GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement HandleGetVirtualSelectedParentBlueScore and GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement NotifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Expand TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement notifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Make go lint happy. * [NOD-1593] Fix merge errors. * [NOD-1593] Rename findSelectedParentChainChanges to calculateSelectedParentChainChanges. * [NOD-1593] Expand TestCalculateSelectedParentChainChanges. * [NOD-1597] Add logs to utxoindex.go. * [NOD-1597] Add logs to utxoindex/store.go. * [NOD-1597] Add logs to RPCManager.NotifyXXX functions. * Implement notifySelectedParentChainChanged. * Implement TestSelectedParentChain. * Rename NotifyChainChanged to NotifyVirtualSelectedParentChainChanged. * Rename GetChainFromBlock to GetVirtualSelectedParentChainFromBlock. * Remove AcceptanceIndex from the config. * Implement HandleGetVirtualSelectedParentChainFromBlock. * Expand TestVirtualSelectedParentChain. * Fix merge errors. * Add a comment. * Move a comment.
139 lines
4.4 KiB
Go
139 lines
4.4 KiB
Go
package flowcontext
|
|
|
|
import (
|
|
"sync/atomic"
|
|
|
|
"github.com/kaspanet/kaspad/app/protocol/blocklogger"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/app/protocol/flows/blockrelay"
|
|
)
|
|
|
|
// OnNewBlock updates the mempool after a new block arrival, and
|
|
// relays newly unorphaned transactions and possibly rebroadcast
|
|
// manually added transactions when not in IBD.
|
|
func (f *FlowContext) OnNewBlock(block *externalapi.DomainBlock,
|
|
blockInsertionResult *externalapi.BlockInsertionResult) error {
|
|
|
|
hash := consensushashing.BlockHash(block)
|
|
log.Debugf("OnNewBlock start for block %s", hash)
|
|
defer log.Debugf("OnNewBlock end for block %s", hash)
|
|
unorphaningResults, err := f.UnorphanBlocks(block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Debugf("OnNewBlock: block %s unorphaned %d blocks", hash, len(unorphaningResults))
|
|
|
|
newBlocks := []*externalapi.DomainBlock{block}
|
|
newBlockInsertionResults := []*externalapi.BlockInsertionResult{blockInsertionResult}
|
|
for _, unorphaningResult := range unorphaningResults {
|
|
newBlocks = append(newBlocks, unorphaningResult.block)
|
|
newBlockInsertionResults = append(newBlockInsertionResults, unorphaningResult.blockInsertionResult)
|
|
}
|
|
|
|
for i, newBlock := range newBlocks {
|
|
blocklogger.LogBlock(block)
|
|
|
|
log.Tracef("OnNewBlock: passing block %s transactions to mining manager", hash)
|
|
_ = f.Domain().MiningManager().HandleNewBlockTransactions(newBlock.Transactions)
|
|
|
|
if f.onBlockAddedToDAGHandler != nil {
|
|
log.Tracef("OnNewBlock: calling f.onBlockAddedToDAGHandler for block %s", hash)
|
|
blockInsertionResult = newBlockInsertionResults[i]
|
|
err := f.onBlockAddedToDAGHandler(newBlock, blockInsertionResult)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *FlowContext) broadcastTransactionsAfterBlockAdded(
|
|
block *externalapi.DomainBlock, transactionsAcceptedToMempool []*externalapi.DomainTransaction) error {
|
|
|
|
f.updateTransactionsToRebroadcast(block)
|
|
|
|
// Don't relay transactions when in IBD.
|
|
if f.IsIBDRunning() {
|
|
return nil
|
|
}
|
|
|
|
var txIDsToRebroadcast []*externalapi.DomainTransactionID
|
|
if f.shouldRebroadcastTransactions() {
|
|
txIDsToRebroadcast = f.txIDsToRebroadcast()
|
|
}
|
|
|
|
txIDsToBroadcast := make([]*externalapi.DomainTransactionID, len(transactionsAcceptedToMempool)+len(txIDsToRebroadcast))
|
|
for i, tx := range transactionsAcceptedToMempool {
|
|
txIDsToBroadcast[i] = consensushashing.TransactionID(tx)
|
|
}
|
|
offset := len(transactionsAcceptedToMempool)
|
|
for i, txID := range txIDsToRebroadcast {
|
|
txIDsToBroadcast[offset+i] = txID
|
|
}
|
|
|
|
if len(txIDsToBroadcast) == 0 {
|
|
return nil
|
|
}
|
|
if len(txIDsToBroadcast) > appmessage.MaxInvPerTxInvMsg {
|
|
txIDsToBroadcast = txIDsToBroadcast[:appmessage.MaxInvPerTxInvMsg]
|
|
}
|
|
inv := appmessage.NewMsgInvTransaction(txIDsToBroadcast)
|
|
return f.Broadcast(inv)
|
|
}
|
|
|
|
// SharedRequestedBlocks returns a *blockrelay.SharedRequestedBlocks for sharing
|
|
// data about requested blocks between different peers.
|
|
func (f *FlowContext) SharedRequestedBlocks() *blockrelay.SharedRequestedBlocks {
|
|
return f.sharedRequestedBlocks
|
|
}
|
|
|
|
// AddBlock adds the given block to the DAG and propagates it.
|
|
func (f *FlowContext) AddBlock(block *externalapi.DomainBlock) error {
|
|
blockInsertionResult, err := f.Domain().Consensus().ValidateAndInsertBlock(block)
|
|
if err != nil {
|
|
if errors.As(err, &ruleerrors.RuleError{}) {
|
|
log.Infof("Validation failed for block %s: %s", consensushashing.BlockHash(block), err)
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
err = f.OnNewBlock(block, blockInsertionResult)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return f.Broadcast(appmessage.NewMsgInvBlock(consensushashing.BlockHash(block)))
|
|
}
|
|
|
|
// IsIBDRunning returns true if IBD is currently marked as running
|
|
func (f *FlowContext) IsIBDRunning() bool {
|
|
return atomic.LoadUint32(&f.isInIBD) != 0
|
|
}
|
|
|
|
// TrySetIBDRunning attempts to set `isInIBD`. Returns false
|
|
// if it is already set
|
|
func (f *FlowContext) TrySetIBDRunning() bool {
|
|
succeeded := atomic.CompareAndSwapUint32(&f.isInIBD, 0, 1)
|
|
if succeeded {
|
|
log.Infof("IBD started")
|
|
}
|
|
return succeeded
|
|
}
|
|
|
|
// UnsetIBDRunning unsets isInIBD
|
|
func (f *FlowContext) UnsetIBDRunning() {
|
|
succeeded := atomic.CompareAndSwapUint32(&f.isInIBD, 1, 0)
|
|
if !succeeded {
|
|
panic("attempted to unset isInIBD when it was not set to begin with")
|
|
}
|
|
log.Infof("IBD finished")
|
|
}
|