diff --git a/domain/dagconfig/consensus_defaults.go b/domain/dagconfig/consensus_defaults.go index 8bc914431..d38265a7f 100644 --- a/domain/dagconfig/consensus_defaults.go +++ b/domain/dagconfig/consensus_defaults.go @@ -84,4 +84,6 @@ const ( defaultDeflationaryPhaseDaaScore = 15778800 - 259200 defaultMergeDepth = 3600 + + defaultDustConst = 1e12 // TODO: Determine the right value ) diff --git a/domain/dagconfig/params.go b/domain/dagconfig/params.go index 0340763ef..5dc6ac5e2 100644 --- a/domain/dagconfig/params.go +++ b/domain/dagconfig/params.go @@ -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 diff --git a/domain/miningmanager/blocktemplatebuilder/blocktemplatebuilder.go b/domain/miningmanager/blocktemplatebuilder/blocktemplatebuilder.go index 564542db8..e21afe127 100644 --- a/domain/miningmanager/blocktemplatebuilder/blocktemplatebuilder.go +++ b/domain/miningmanager/blocktemplatebuilder/blocktemplatebuilder.go @@ -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) { diff --git a/domain/miningmanager/blocktemplatebuilder/policy.go b/domain/miningmanager/blocktemplatebuilder/policy.go index 58980f8ed..8413e36df 100644 --- a/domain/miningmanager/blocktemplatebuilder/policy.go +++ b/domain/miningmanager/blocktemplatebuilder/policy.go @@ -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 } diff --git a/domain/miningmanager/blocktemplatebuilder/txselection.go b/domain/miningmanager/blocktemplatebuilder/txselection.go index 6ebfec689..93079fd07 100644 --- a/domain/miningmanager/blocktemplatebuilder/txselection.go +++ b/domain/miningmanager/blocktemplatebuilder/txselection.go @@ -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 diff --git a/domain/miningmanager/factory.go b/domain/miningmanager/factory.go index 16b50fb29..d1bdd6920 100644 --- a/domain/miningmanager/factory.go +++ b/domain/miningmanager/factory.go @@ -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, diff --git a/version/version.go b/version/version.go index c04cd9643..8224f7faf 100644 --- a/version/version.go +++ b/version/version.go @@ -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