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 fdf5ca9
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 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)
// Define lower difficulty to use for solving blocks.
mockPowLimit := new(uint256.Uint256).SetUint64(1).Lsh(255).SubUint64(1)
mockPowLimitBig := mockPowLimit.ToBig()
mockPowLimitBits := standalone.BigToCompact(mockPowLimitBig)

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)

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,
mockPowLimitBig)
return err == nil
}
header := block432100.Header
header.PrevBlock[0] ^= 0x55
header.Bits = mockPowLimitBits
for !isSolved(&header) {
header.Nonce++
}

encoded := hex.EncodeToString(serializeGetWorkData(&header))
return &encoded
}(),
},
mockChainParams: func() *chaincfg.Params {
params := cloneParams(defaultChainParams)
params.PowLimit = mockPowLimitBig
params.PowLimitBits = mockPowLimitBits
return params
}(),
mockMiningState: mine(),
mockSyncManager: func() *testSyncManager {
syncManager := defaultMockSyncManager()
syncManager.submitBlockErr = blockchain.ErrMissingParent
return syncManager
}(),
result: false,
Expand Down

0 comments on commit fdf5ca9

Please sign in to comment.