Skip to content

Commit

Permalink
Commit to MixHash in header, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Nov 21, 2024
1 parent 82e490f commit 3e04fc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion 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
12 changes: 7 additions & 5 deletions core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ func (wo *WorkObject) WithBody(header *Header, txs []*Transaction, etxs []*Trans

func EmptyWorkObjectBody() *WorkObjectBody {
woBody := &WorkObjectBody{}
woBody.SetHeader(EmptyHeader())
woBody.SetTransactions([]*Transaction{})
woBody.SetOutboundEtxs([]*Transaction{})
return woBody
Expand Down Expand Up @@ -1082,9 +1083,10 @@ func (wh *WorkObjectHeader) Hash() (hash common.Hash) {
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[:], wh.MixHash().Bytes())
copy(hData[common.HashLength:], sealHash)
copy(hData[common.HashLength+common.HashLength:], wh.Nonce().Bytes())
sum := blake3.Sum256(hData[:])
hash.SetBytes(sum[:])
return hash
Expand All @@ -1106,7 +1108,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 +1120,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
22 changes: 18 additions & 4 deletions core/types/wo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func woTestData() (*WorkObject, common.Hash) {
wo := &WorkObject{
woHeader: &WorkObjectHeader{
headerHash: common.HexToHash("0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"),
parentHash: common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1"),
parentHash: common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef12"),
number: big.NewInt(1),
difficulty: big.NewInt(123456789),
primeTerminusNumber: big.NewInt(42),
Expand All @@ -24,16 +24,24 @@ func woTestData() (*WorkObject, common.Hash) {
nonce: EncodeNonce(uint64(1)),
lock: 0,
},
woBody: EmptyWorkObjectBody(),
}
return wo, wo.Hash()
}

func TestWoHash(t *testing.T) {
_, actualHash := woTestData()
expectedHash := common.HexToHash("0x572273f26629ca2a02c329cd21dc3def6e1d1934465ea11fca393c7a9ce3c89b")
expectedHash := common.HexToHash("0x5699348bb74873668408253dc3c518e1570eec73516dced58cbddcf294d2f477")
require.Equal(t, expectedHash, actualHash, "Hash not equal to expected hash")
}

func TestWoSealHash(t *testing.T) {
testWo, _ := woTestData()
actualHash := testWo.SealHash()
expectedHash := common.HexToHash("0x8ea659d6d9e1da464ae1a505cc19b1fe5f3df1e7013168227be1c290b3edaf01")
require.Equal(t, expectedHash, actualHash, "Seal hash not equal to expected hash")
}

func FuzzHeaderHash(f *testing.F) {
fuzzHash(f,
func(woh *WorkObjectHeader) common.Hash { return woh.headerHash },
Expand All @@ -60,8 +68,14 @@ func FuzzNumberHash(f *testing.F) {

func FuzzTxHash(f *testing.F) {
fuzzHash(f,
func(woh *WorkObjectHeader) common.Hash { return woh.txHash },
func(woh *WorkObjectHeader, hash common.Hash) { woh.txHash = hash })
func(woh *WorkObjectHeader) common.Hash { return woh.TxHash() },
func(woh *WorkObjectHeader, hash common.Hash) { woh.SetTxHash(hash)})
}

func FuzzMixHash(f *testing.F) {
fuzzHash(f,
func(woh *WorkObjectHeader) common.Hash { return woh.MixHash() },
func(woh *WorkObjectHeader, hash common.Hash) { woh.SetMixHash(hash) })
}

func TestLocationHash(t *testing.T) {
Expand Down

0 comments on commit 3e04fc6

Please sign in to comment.