Michael Sutton 21b82d7efc
Block template cache (#1994)
* minor text fix

* Implement a block template cache with template modification/reuse mechanism

* Fix compilation error

* Address review comments

* Added a through TestModifyBlockTemplate test

* Update header timestamp if possible

* Avoid copying the transactions when only the header changed

* go fmt
2022-03-31 16:37:48 +03:00

85 lines
2.1 KiB
Go

package externalapi
import "math/big"
// DomainBlock represents a Kaspa block
type DomainBlock struct {
Header BlockHeader
Transactions []*DomainTransaction
}
// Clone returns a clone of DomainBlock
func (block *DomainBlock) Clone() *DomainBlock {
transactionClone := make([]*DomainTransaction, len(block.Transactions))
for i, tx := range block.Transactions {
transactionClone[i] = tx.Clone()
}
return &DomainBlock{
Header: block.Header,
Transactions: transactionClone,
}
}
// If this doesn't compile, it means the type definition has been changed, so it's
// an indication to update Equal and Clone accordingly.
var _ = DomainBlock{nil, []*DomainTransaction{}}
// Equal returns whether block equals to other
func (block *DomainBlock) Equal(other *DomainBlock) bool {
if block == nil || other == nil {
return block == other
}
if len(block.Transactions) != len(other.Transactions) {
return false
}
if !block.Header.Equal(other.Header) {
return false
}
for i, tx := range block.Transactions {
if !tx.Equal(other.Transactions[i]) {
return false
}
}
return true
}
// BlockHeader represents an immutable block header.
type BlockHeader interface {
BaseBlockHeader
ToMutable() MutableBlockHeader
}
// BaseBlockHeader represents the header part of a Kaspa block
type BaseBlockHeader interface {
Version() uint16
Parents() []BlockLevelParents
DirectParents() BlockLevelParents
HashMerkleRoot() *DomainHash
AcceptedIDMerkleRoot() *DomainHash
UTXOCommitment() *DomainHash
TimeInMilliseconds() int64
Bits() uint32
Nonce() uint64
DAAScore() uint64
BlueScore() uint64
BlueWork() *big.Int
PruningPoint() *DomainHash
BlockLevel(maxBlockLevel int) int
Equal(other BaseBlockHeader) bool
}
// MutableBlockHeader represents a block header that can be mutated, but only
// the fields that are relevant to mining (Nonce and TimeInMilliseconds).
type MutableBlockHeader interface {
BaseBlockHeader
ToImmutable() BlockHeader
SetNonce(nonce uint64)
SetTimeInMilliseconds(timeInMilliseconds int64)
SetHashMerkleRoot(hashMerkleRoot *DomainHash)
}