Skip to content

Commit

Permalink
rpcserver: Use solved mock orphan block in tests.
Browse files Browse the repository at this point in the history
Currently, the RPC server getwork test for orphan block submission is
rather brittle because it relies on the specific current implementation
of the handler.

This makes the test more robust by modifying it to solve the mock orphan
block submission so that it reliably tests what it intends to even if
the handler code changes.
  • Loading branch information
davecgh committed Apr 24, 2023
1 parent 20afa86 commit dc9c60e
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions internal/rpcserver/rpcserverhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5617,29 +5617,31 @@ 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)

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:])
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit dc9c60e

Please sign in to comment.