fix: NPE when viewing the latest block header - #139
Merged
Conversation
Tapping "View latest block" calls the getblockheader RPC, which Floresta answers in verbose form by default (a serialized corepc_types::v29::GetBlockHeaderVerbose). Its JSON keys are `merkleroot` and `previousblockhash`, and `bits` is a hex string (e.g. "1702905c") — not `merkle_root`/`prev_blockhash` and a Long as BlockHeaderResult expected. Gson populates non-null Kotlin fields by reflection without honoring nullability, so the mismatched keys left merkleRoot/prevBlockhash null and BlockHeaderCard dereferenced them -> NullPointerException. When `bits` contained a-f hex digits, Gson instead threw NumberFormatException, surfacing as a "Failed to parse response" snackbar. Map the correct JSON keys, type `bits` as String, make prevBlockhash nullable (genesis has none), and skip the "Previous block" row when it is absent. Add GetBlockHeaderResponseParsingTest covering hex bits, all-digit bits, and the genesis (no previousblockhash) case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A user reported a NullPointerException when tapping the header of the last block ("View latest block" on the Blockchain screen).
Tapping the button calls the
getblockheaderRPC, which Floresta answers in verbose form by default (verbositydefaults totrueinfloresta-node'sserver.rs). The verbose body is a serializedcorepc_types::v29::GetBlockHeaderVerbose, whose JSON keys aremerkleroot/previousblockhashand whosebitsis a hex string (e.g."1702905c").BlockHeaderResultexpected@SerializedName("merkle_root")/@SerializedName("prev_blockhash")andbits: Long. Since Gson populates non-null Kotlin fields by reflection without honoring nullability:merkleRoot/prevBlockhashnull, andBlockHeaderCarddereferenced them → NPE. This happens wheneverbitsis all digits (e.g."170300000"), so parsing succeeds.bitscontainsa–fhex digits (the common case), Gson instead throwsNumberFormatException→ "Failed to parse response" snackbar.The intermittency (crash depends on the tip block's
nBits) matches a "sometimes crashes" report.Fix
merkleroot,previousblockhash).bitsasString.prevBlockhashnullable (Option<String>in Floresta — genesis has none) and skip the "Previous block" row when absent.Tests
GetBlockHeaderResponseParsingTest— deserializes realistic verbosegetblockheaderresponses (hex bits, all-digit bits, and genesis with nopreviousblockhash) using the app's defaultGson(). Fails against the old model, passes with the fix../gradlew detekt lintDebug :app:testDebugUnitTestgreen.Manual verification
Reproduced and verified on an x86_64 emulator: tapping View latest block now renders the full Block Header (Merkle Root, Previous Block, Bits, Time, Version, Nonce) with real values and no crash / no parse-error snackbar.
🤖 Generated with Claude Code