kaspad/domain/consensus/consensus.go
stasatdaglabs 7891f73cb0
[NOD-1414] Write domain interfaces and stub implementations for the new kaspadstate architecture (#941)
* [NOD-1414] Add interfaces for Factory and State.

* [NOD-1414] Create interfaces for algorithms and data stores.

* [NOD-1414] Create empty implementations for algorithms and data stores.

* [NOD-1414] Add new functions for all the implementations.

* [NOD-1414] Begin filling in the interfaces.

* [NOD-1414] Fill in the interfaces for the data structures.

* [NOD-1414] Fill in the interfaces for the algorithms.

* [NOD-1414] Fix a bug in package names.

* [NOD-1414] Connect up the various interfaces.

* [NOD-1414] Add stubs to all the implementations.

* [NOD-1414] Create MiningManager and its Factory.

* [NOD-1414] Add interfaces for mempool and blockTemplateBuilder.

* [NOD-1414] Add implementation structs for miningManager.

* [NOD-1414] Add stub implementations for mempool and blockTemplateBuilder.

* [NOD-1414] Rename state to kaspadState.

* [NOD-1414] Restructure where interfaces sit.

* [NOD-1414] Restructure where interfaces sit in the algorithms package as well.

* [NOD-1414] Move remaining models out of models.go.

* [NOD-1414] Modified some interfaces.

* [NOD-1414] Make go vet happy.

* [NOD-1414] Move SerializedUTXOSet into PruningManager.

* [NOD-1414] Modify FindNextPruningPoint to return found and nextPruningPointUTXOSet.

* [NOD-1414] Add IsDAGAncestorOf.

* [NOD-1414] Add PruningPoint().

* [NOD-1414] Add Entry() to ReadOnlyUTXOSet.

* [NOD-1414] Add MergeSet() to BlockGHOSTDAGData.

* [NOD-1414] Write comments for all the exported types and functions in miningmanager.

* [NOD-1414] Add comments to the upper levels of KaspadState.

* [NOD-1414] Replace AddNode with ReachabilityChangeset.

* [NOD-1414] Add payAddress and extraData to GetBlockTemplate.

* [NOD-1414] Add scriptPublicKey and extraData to BuildBlock.

* [NOD-1414] Rename algorithms to processes.

* [NOD-1414] Rename kaspadState to consensus.

* [NOD-1414] Add ValidateAgainstPastUTXO and ValidateFinality.

* [NOD-1414] Add BlockGHOSTDAGData to ReachabilityChangeset.

* [NOD-1414] Fix the comment over Mempool.

* [NOD-1414] Fix the comment over ValidateTransaction.

* [NOD-1414] Fill up the data structures.

* [NOD-1414] Add comments to remaining uncommented items in miningmanager.

* [NOD-1414] Add comments to structs and constructors.

* [NOD-1414] Rename Set to Insert.

* [NOD-1414] Add comments to everything inside datastructures.

* [NOD-1414] Add comments to everything inside models.

* [NOD-1414] Add comments to the interfaces in processes.

* [NOD-1414] Add comments to everything in processes.

* [NOD-1414] Make go vet happy.

* [NOD-1414] Rename scriptPublicKey to coinbaseScriptPublicKey.

* [NOD-1414] Add handlers to the consensus.

* [NOD-1414] Add highHash to blockAtDepth.

* [NOD-1414] Add resolveFinalityConflict.

* [NOD-1414] Reorg BlockValidator.

* [NOD-1414] In ResolveFinalityConflicts, rename blockHash to newFinalityBlockHash.

* [NOD-1414] Fix a comment.

* [NOD-1414] Make reachability structs public.

* [NOD-1414] Make UTXO structs public.
2020-10-06 10:34:04 +03:00

80 lines
3.6 KiB
Go

package consensus
import (
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/processes"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/daghash"
)
// Consensus maintains the current core state of the node
type Consensus interface {
BuildBlock(scriptPublicKey []byte, extraData []byte, transactionSelector model.TransactionSelector) *appmessage.MsgBlock
ValidateAndInsertBlock(block *appmessage.MsgBlock) error
UTXOByOutpoint(outpoint *appmessage.Outpoint) *model.UTXOEntry
ValidateTransaction(transaction *util.Tx, utxoEntries []*model.UTXOEntry) error
ResolveFinalityConflict(newFinalityBlockHash *daghash.Hash)
SetOnBlockAddedToDAGHandler(onBlockAddedToDAGHandler model.OnBlockAddedToDAGHandler)
SetOnChainChangedHandler(onChainChangedHandler model.OnChainChangedHandler)
SetOnFinalityConflictHandler(onFinalityConflictHandler model.OnFinalityConflictHandler)
SetOnFinalityConflictResolvedHandler(onFinalityConflictResolvedHandler model.OnFinalityConflictResolvedHandler)
}
type consensus struct {
blockProcessor processes.BlockProcessor
consensusStateManager processes.ConsensusStateManager
}
// BuildBlock builds a block over the current state, with the transactions
// selected by the given transactionSelector
func (s *consensus) BuildBlock(coinbaseScriptPublicKey []byte, coinbaseExtraData []byte,
transactionSelector model.TransactionSelector) *appmessage.MsgBlock {
return s.blockProcessor.BuildBlock(coinbaseScriptPublicKey, coinbaseExtraData, transactionSelector)
}
// ValidateAndInsertBlock validates the given block and, if valid, applies it
// to the current state
func (s *consensus) ValidateAndInsertBlock(block *appmessage.MsgBlock) error {
return s.blockProcessor.ValidateAndInsertBlock(block)
}
// UTXOByOutpoint returns a UTXOEntry matching the given outpoint
func (s *consensus) UTXOByOutpoint(outpoint *appmessage.Outpoint) *model.UTXOEntry {
return s.consensusStateManager.UTXOByOutpoint(outpoint)
}
// ValidateTransaction validates the given transaction using
// the given utxoEntries
func (s *consensus) ValidateTransaction(transaction *util.Tx, utxoEntries []*model.UTXOEntry) error {
return s.consensusStateManager.ValidateTransaction(transaction, utxoEntries)
}
// ResolveFinalityConflict resolves an existing finality conflict
// using the given finalityBlockHash
func (s *consensus) ResolveFinalityConflict(newFinalityBlockHash *daghash.Hash) {
s.consensusStateManager.ResolveFinalityConflict(newFinalityBlockHash)
}
// SetOnBlockAddedToDAGHandler set the onBlockAddedToDAGHandler for the consensus
func (s *consensus) SetOnBlockAddedToDAGHandler(onBlockAddedToDAGHandler model.OnBlockAddedToDAGHandler) {
s.blockProcessor.SetOnBlockAddedToDAGHandler(onBlockAddedToDAGHandler)
}
// SetOnChainChangedHandler set the onBlockAddedToDAGHandler for the consensus
func (s *consensus) SetOnChainChangedHandler(onChainChangedHandler model.OnChainChangedHandler) {
s.blockProcessor.SetOnChainChangedHandler(onChainChangedHandler)
}
// SetOnFinalityConflictHandler set the onBlockAddedToDAGHandler for the consensus
func (s *consensus) SetOnFinalityConflictHandler(onFinalityConflictHandler model.OnFinalityConflictHandler) {
s.blockProcessor.SetOnFinalityConflictHandler(onFinalityConflictHandler)
}
// SetOnFinalityConflictResolvedHandler set the onBlockAddedToDAGHandler for the consensus
func (s *consensus) SetOnFinalityConflictResolvedHandler(onFinalityConflictResolvedHandler model.OnFinalityConflictResolvedHandler) {
s.consensusStateManager.SetOnFinalityConflictResolvedHandler(onFinalityConflictResolvedHandler)
}