Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash integrity #2376

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import (
"github.com/dominant-strategies/go-quai/rlp"
)

const (
NonceLength = 8
)

var (
EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
EmptyUncleHash = RlpHash([]*Header(nil))
Expand All @@ -48,7 +52,7 @@ var (
// A BlockNonce is a 64-bit hash which proves (combined with the
// mix-hash) that a sufficient amount of computation has been carried
// out on a block.
type BlockNonce [8]byte
type BlockNonce [NonceLength]byte

// EncodeNonce converts the given integer to a block nonce.
func EncodeNonce(i uint64) BlockNonce {
Expand Down Expand Up @@ -1230,10 +1234,12 @@ func (m BlockManifest) ProtoEncode() (*ProtoManifest, error) {

// ProtoDecode deserializes th ProtoManifest into the BlockManifest format
func (m *BlockManifest) ProtoDecode(protoManifest *ProtoManifest) error {
for _, protoHash := range protoManifest.Manifest {
hash := &common.Hash{}
hash.ProtoDecode(protoHash)
*m = append(*m, *hash)
if protoManifest != nil {
for _, protoHash := range protoManifest.Manifest {
hash := &common.Hash{}
hash.ProtoDecode(protoHash)
*m = append(*m, *hash)
}
}
return nil
}
Expand Down
16 changes: 9 additions & 7 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,14 +799,16 @@ func (s Transactions) ProtoEncode() (*ProtoTransactions, error) {

// ProtoDecode decodes the ProtoTransactions into the Transactions format
func (s *Transactions) ProtoDecode(transactions *ProtoTransactions, location common.Location) error {
*s = make(Transactions, 0, len(transactions.Transactions))
for _, protoTx := range transactions.Transactions {
tx := &Transaction{}
err := tx.ProtoDecode(protoTx, location)
if err != nil {
return err
if transactions != nil {
*s = make(Transactions, 0, len(transactions.Transactions))
for _, protoTx := range transactions.Transactions {
tx := &Transaction{}
err := tx.ProtoDecode(protoTx, location)
if err != nil {
return err
}
*s = append(*s, tx)
}
*s = append(*s, tx)
}
return nil
}
Expand Down
17 changes: 12 additions & 5 deletions core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,12 @@ func (wo *WorkObject) WithBody(header *Header, txs []*Transaction, etxs []*Trans

func EmptyWorkObjectBody() *WorkObjectBody {
woBody := &WorkObjectBody{}
woBody.SetHeader(EmptyHeader())
woBody.SetTransactions([]*Transaction{})
woBody.SetOutboundEtxs([]*Transaction{})
woBody.SetUncles([]*WorkObjectHeader{})
woBody.SetManifest(BlockManifest{})
woBody.SetInterlinkHashes(common.Hashes{})
return woBody
}

Expand Down Expand Up @@ -1079,12 +1083,15 @@ func (wh *WorkObjectHeader) RPCMarshalWorkObjectHeader() map[string]interface{}

func (wh *WorkObjectHeader) Hash() (hash common.Hash) {
sealHash := wh.SealHash().Bytes()
mixHash := wh.MixHash().Bytes()
nonce := wh.Nonce().Bytes()
hasherMu.Lock()
defer hasherMu.Unlock()
hasher.Reset()
var hData [40]byte
copy(hData[:], wh.Nonce().Bytes())
copy(hData[len(wh.nonce):], sealHash)
var hData [common.HashLength + common.HashLength + NonceLength]byte
copy(hData[:], mixHash)
copy(hData[common.HashLength:], sealHash)
copy(hData[common.HashLength+common.HashLength:], nonce)
sum := blake3.Sum256(hData[:])
hash.SetBytes(sum[:])
return hash
Expand All @@ -1106,7 +1113,7 @@ func (wh *WorkObjectHeader) SealHash() (hash common.Hash) {

func (wh *WorkObjectHeader) SealEncode() *ProtoWorkObjectHeader {
// Omit MixHash and PowHash
hash := common.ProtoHash{Value: wh.HeaderHash().Bytes()}
headerHash := common.ProtoHash{Value: wh.HeaderHash().Bytes()}
parentHash := common.ProtoHash{Value: wh.ParentHash().Bytes()}
txHash := common.ProtoHash{Value: wh.TxHash().Bytes()}
number := wh.Number().Bytes()
Expand All @@ -1118,7 +1125,7 @@ func (wh *WorkObjectHeader) SealEncode() *ProtoWorkObjectHeader {
coinbase := common.ProtoAddress{Value: wh.PrimaryCoinbase().Bytes()}

return &ProtoWorkObjectHeader{
HeaderHash: &hash,
HeaderHash: &headerHash,
ParentHash: &parentHash,
Number: number,
Difficulty: difficulty,
Expand Down
Loading
Loading