diff --git a/internal/rpcserver/rpcserverhandlers_test.go b/internal/rpcserver/rpcserverhandlers_test.go index 74ee1d550b..85f135ed12 100644 --- a/internal/rpcserver/rpcserverhandlers_test.go +++ b/internal/rpcserver/rpcserverhandlers_test.go @@ -5617,15 +5617,18 @@ func TestHandleVerifyChain(t *testing.T) { func TestHandleGetWork(t *testing.T) { t.Parallel() - data := make([]byte, 0, getworkDataLen) - buf := bytes.NewBuffer(data) - err := block432100.Header.Serialize(buf) - if err != nil { - t.Fatalf("unexpected serialize error: %v", err) + serializeGetWorkData := func(header *wire.BlockHeader) []byte { + data := make([]byte, 0, getworkDataLen) + buf := bytes.NewBuffer(data) + err := header.Serialize(buf) + if err != nil { + t.Fatalf("unexpected serialize error: %v", err) + } + data = data[:getworkDataLen] + copy(data[wire.MaxBlockHeaderPayload:], blake256Pad) + return data } - - data = data[:getworkDataLen] - copy(data[wire.MaxBlockHeaderPayload:], blake256Pad) + data := serializeGetWorkData(&block432100.Header) submissionB := make([]byte, hex.EncodedLen(len(data))) hex.Encode(submissionB, data) @@ -5633,13 +5636,12 @@ func TestHandleGetWork(t *testing.T) { submission := string(submissionB) lessThanGetWorkDataLen := submission[10:] - // Create an orphan block by mutating the prevblock field of the data. - orphanData := make([]byte, len(data)) - copy(orphanData, data) - orphanData[4] ^= 0x55 - orphanSubmission := hex.EncodeToString(orphanData) + // Define lower difficulty to use for solving orphan block submission. + orphanPowLimit := new(uint256.Uint256).SetUint64(1).Lsh(255).SubUint64(1) + orphanPowLimitBig := orphanPowLimit.ToBig() + orphanPowLimitBits := standalone.BigToCompact(orphanPowLimitBig) - buf = &bytes.Buffer{} + var buf bytes.Buffer buf.Write(submissionB[:10]) buf.WriteRune('g') buf.Write(submissionB[10:]) @@ -5812,11 +5814,36 @@ func TestHandleGetWork(t *testing.T) { name: "handleGetWork: submission is an orphan", handler: handleGetWork, cmd: &types.GetWorkCmd{ - Data: &orphanSubmission, + Data: func() *string { + // Create an orphan block by mutating the previous block field + // and solving the block. + isSolved := func(header *wire.BlockHeader) bool { + powHash := header.BlockHash() + err := standalone.CheckProofOfWork(&powHash, header.Bits, + orphanPowLimitBig) + return err == nil + } + header := block432100.Header + header.PrevBlock[0] ^= 0x55 + header.Bits = orphanPowLimitBits + for !isSolved(&header) { + header.Nonce++ + } + + encoded := hex.EncodeToString(serializeGetWorkData(&header)) + return &encoded + }(), }, + mockChainParams: func() *chaincfg.Params { + params := cloneParams(defaultChainParams) + params.PowLimit = orphanPowLimitBig + params.PowLimitBits = orphanPowLimitBits + return params + }(), mockMiningState: mine(), mockSyncManager: func() *testSyncManager { syncManager := defaultMockSyncManager() + syncManager.submitBlockErr = blockchain.ErrMissingParent return syncManager }(), result: false,