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

* Replace the old parents in the block header with BlockLevelParents. * Begin fixing compilation errors. * Implement database serialization for block level parents. * Implement p2p serialization for block level parents. * Implement rpc serialization for block level parents. * Add DirectParents() to the block header interface. * Use DirectParents() instead of Parents() in some places. * Revert test_block_builder.go. * Add block level parents to hash serialization. * Use the zero hash for genesis finality points. * Fix failing tests. * Fix a variable name. * Update headerEstimatedSerializedSize. * Add comments in blocklevelparents.go. * Fix the rpc-stability stability test. * Change the field number for `parents` fields in p2p.proto and rpc.proto. * Remove MsgBlockHeader::NumParentBlocks.
56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
package consensushashing
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/serialization"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// BlockHash returns the given block's hash
|
|
func BlockHash(block *externalapi.DomainBlock) *externalapi.DomainHash {
|
|
return HeaderHash(block.Header)
|
|
}
|
|
|
|
// HeaderHash returns the given header's hash
|
|
func HeaderHash(header externalapi.BaseBlockHeader) *externalapi.DomainHash {
|
|
// Encode the header and hash everything prior to the number of
|
|
// transactions.
|
|
writer := hashes.NewBlockHashWriter()
|
|
err := serializeHeader(writer, header)
|
|
if err != nil {
|
|
// It seems like this could only happen if the writer returned an error.
|
|
// and this writer should never return an error (no allocations or possible failures)
|
|
// the only non-writer error path here is unknown types in `WriteElement`
|
|
panic(errors.Wrap(err, "this should never happen. Hash digest should never return an error"))
|
|
}
|
|
|
|
return writer.Finalize()
|
|
}
|
|
|
|
func serializeHeader(w io.Writer, header externalapi.BaseBlockHeader) error {
|
|
timestamp := header.TimeInMilliseconds()
|
|
blueWork := header.BlueWork().Bytes()
|
|
|
|
numParents := len(header.Parents())
|
|
if err := serialization.WriteElements(w, header.Version(), uint64(numParents)); err != nil {
|
|
return err
|
|
}
|
|
for _, blockLevelParents := range header.Parents() {
|
|
numBlockLevelParents := len(blockLevelParents)
|
|
if err := serialization.WriteElements(w, uint64(numBlockLevelParents)); err != nil {
|
|
return err
|
|
}
|
|
for _, hash := range blockLevelParents {
|
|
if err := serialization.WriteElement(w, hash); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return serialization.WriteElements(w, header.HashMerkleRoot(), header.AcceptedIDMerkleRoot(), header.UTXOCommitment(), timestamp,
|
|
header.Bits(), header.Nonce(), header.DAAScore(), blueWork, header.FinalityPoint())
|
|
}
|