Ori Newman d9bc94a2a8
Replace header finality point with pruning point and enforce finality rules on IBD with headers proof (#1823)
* Replace header finality point with pruning point

* Fix TestTransactionAcceptance

* Fix pruning candidate

* Store all past pruning points

* Pass pruning points on IBD

* Add blue score to block header

* Simplify ArePruningPointsInValidChain

* Fix static check errors

* Fix genesis

* Renames and text fixing

* Use ExpectedHeaderPruningPoint in block builder

* Fix TestCheckPruningPointViolation
2021-08-31 08:01:48 +03:00

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(), header.BlueScore(), blueWork, header.PruningPoint())
}