Apply new dust prevention KIP to miner's selection policy

This commit is contained in:
Ori Newman 2023-12-01 09:20:22 +02:00
parent 387fade044
commit 11ca88933e
7 changed files with 38 additions and 9 deletions

View File

@ -84,4 +84,6 @@ const (
defaultDeflationaryPhaseDaaScore = 15778800 - 259200
defaultMergeDepth = 3600
defaultDustConst = 1e12 // TODO: Determine the right value
)

View File

@ -188,6 +188,8 @@ type Params struct {
MaxBlockLevel int
MergeDepth uint64
DustConst uint64
}
// NormalizeRPCServerAddress returns addr with the current network default
@ -288,6 +290,7 @@ var MainnetParams = Params{
// This means that any block that has a level lower or equal to genesis will be level 0.
MaxBlockLevel: 225,
MergeDepth: defaultMergeDepth,
DustConst: defaultDustConst,
}
// TestnetParams defines the network parameters for the test Kaspa network.
@ -354,6 +357,7 @@ var TestnetParams = Params{
MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
DustConst: defaultDustConst,
}
// SimnetParams defines the network parameters for the simulation test Kaspa
@ -420,6 +424,7 @@ var SimnetParams = Params{
MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
DustConst: defaultDustConst,
}
// DevnetParams defines the network parameters for the development Kaspa network.
@ -482,6 +487,7 @@ var DevnetParams = Params{
MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
DustConst: defaultDustConst,
}
// ErrDuplicateNet describes an error where the parameters for a Kaspa

View File

@ -22,6 +22,7 @@ type candidateTx struct {
*consensusexternalapi.DomainTransaction
txValue float64
gasLimit uint64
mass uint64
p float64
start float64
@ -41,11 +42,11 @@ type blockTemplateBuilder struct {
// New creates a new blockTemplateBuilder
func New(consensusReference consensusreference.ConsensusReference, mempool miningmanagerapi.Mempool,
blockMaxMass uint64, coinbasePayloadScriptPublicKeyMaxLength uint8) miningmanagerapi.BlockTemplateBuilder {
blockMaxMass uint64, coinbasePayloadScriptPublicKeyMaxLength uint8, dustConst uint64) miningmanagerapi.BlockTemplateBuilder {
return &blockTemplateBuilder{
consensusReference: consensusReference,
mempool: mempool,
policy: policy{BlockMaxMass: blockMaxMass},
policy: policy{BlockMaxMass: blockMaxMass, DustConst: dustConst},
coinbasePayloadScriptPublicKeyMaxLength: coinbasePayloadScriptPublicKeyMaxLength,
}
@ -130,6 +131,7 @@ func (btb *blockTemplateBuilder) BuildBlockTemplate(
DomainTransaction: tx,
txValue: btb.calcTxValue(tx),
gasLimit: gasLimit,
mass: btb.calcMass(tx),
})
}
@ -172,6 +174,23 @@ func (btb *blockTemplateBuilder) BuildBlockTemplate(
return blockTemplate, nil
}
func (btb *blockTemplateBuilder) calcMass(tx *consensusexternalapi.DomainTransaction) uint64 {
// TODO: Insert link for relevant KIP
addedMass := uint64(0)
sumOutsValue := uint64(0)
for _, output := range tx.Outputs {
addedMass += btb.policy.DustConst / output.Value
sumOutsValue += output.Value
}
reducedMass := uint64(len(tx.Inputs)*len(tx.Inputs)) * btb.policy.DustConst / sumOutsValue
if addedMass < reducedMass {
return tx.Mass
}
return tx.Mass + addedMass - reducedMass
}
// ModifyBlockTemplate modifies an existing block template to the requested coinbase data and updates the timestamp
func (btb *blockTemplateBuilder) ModifyBlockTemplate(newCoinbaseData *consensusexternalapi.DomainCoinbaseData,
blockTemplateToModify *consensusexternalapi.DomainBlockTemplate) (*consensusexternalapi.DomainBlockTemplate, error) {

View File

@ -11,4 +11,6 @@ type policy struct {
// BlockMaxMass is the maximum block mass to be used when generating a
// block template.
BlockMaxMass uint64
DustConst uint64
}

View File

@ -101,8 +101,8 @@ func (btb *blockTemplateBuilder) selectTransactions(candidateTxs []*candidateTx)
// Enforce maximum transaction mass per block. Also check
// for overflow.
if txsForBlockTemplate.totalMass+selectedTx.Mass < txsForBlockTemplate.totalMass ||
txsForBlockTemplate.totalMass+selectedTx.Mass > btb.policy.BlockMaxMass {
if txsForBlockTemplate.totalMass+selectedTx.mass < txsForBlockTemplate.totalMass ||
txsForBlockTemplate.totalMass+selectedTx.mass > btb.policy.BlockMaxMass {
log.Tracef("Tx %s would exceed the max block mass. "+
"As such, stopping.", consensushashing.TransactionID(tx))
break
@ -143,11 +143,11 @@ func (btb *blockTemplateBuilder) selectTransactions(candidateTxs []*candidateTx)
// save the masses, fees, and signature operation counts to the
// result.
selectedTxs = append(selectedTxs, selectedTx)
txsForBlockTemplate.totalMass += selectedTx.Mass
txsForBlockTemplate.totalMass += selectedTx.mass
txsForBlockTemplate.totalFees += selectedTx.Fee
log.Tracef("Adding tx %s (feePerMegaGram %d)",
consensushashing.TransactionID(tx), selectedTx.Fee*1e6/selectedTx.Mass)
consensushashing.TransactionID(tx), selectedTx.Fee*1e6/selectedTx.mass)
markCandidateTxForDeletion(selectedTx)
}
@ -157,7 +157,7 @@ func (btb *blockTemplateBuilder) selectTransactions(candidateTxs []*candidateTx)
})
for _, selectedTx := range selectedTxs {
txsForBlockTemplate.selectedTxs = append(txsForBlockTemplate.selectedTxs, selectedTx.DomainTransaction)
txsForBlockTemplate.txMasses = append(txsForBlockTemplate.txMasses, selectedTx.Mass)
txsForBlockTemplate.txMasses = append(txsForBlockTemplate.txMasses, selectedTx.mass)
txsForBlockTemplate.txFees = append(txsForBlockTemplate.txFees, selectedTx.Fee)
}
return txsForBlockTemplate

View File

@ -21,7 +21,7 @@ func (f *factory) NewMiningManager(consensusReference consensusreference.Consens
mempoolConfig *mempoolpkg.Config) MiningManager {
mempool := mempoolpkg.New(mempoolConfig, consensusReference)
blockTemplateBuilder := blocktemplatebuilder.New(consensusReference, mempool, params.MaxBlockMass, params.CoinbasePayloadScriptPublicKeyMaxLength)
blockTemplateBuilder := blocktemplatebuilder.New(consensusReference, mempool, params.MaxBlockMass, params.CoinbasePayloadScriptPublicKeyMaxLength, params.DustConst)
return &miningManager{
consensusReference: consensusReference,

View File

@ -11,7 +11,7 @@ const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
const (
appMajor uint = 0
appMinor uint = 12
appPatch uint = 13
appPatch uint = 15
)
// appBuild is defined as a variable so it can be overridden during the build