Open dagurval opened 4 years ago
I have also found myself working around this in my code:
// If we received an array of exactly one block header..
if(Array.isArray(data) && data.length === 1)
{
// Store the block hash and height.
await this.setBlock(data[0].height, Buffer.from(data[0].hex, 'hex'));
}
// ..if we received a single block header..
else
{
// Store the block hash and height.
await this.setBlock(data.height, Buffer.from(data.hex, 'hex'));
}
While we're sharing workarounds, here's mine in Kotlin 😂
try {
try {
// First response is wrapped in 'result', so use HeaderReply
val parsed = json.parse(HeaderReply.serializer(), it)
val header = BlockHeader.fromHex(parsed.result.hex)
header.height = parsed.result.height
callback(header)
}
catch (e: MissingFieldException) {
// Other responses are wrapped in params, use 'HeaderNotification'
val parsed = json.parse(HeaderNotification.serializer(), it)
val header = BlockHeader.fromHex(parsed.params.last().hex)
header.height = parsed.params.last().height
callback(header)
}
}
catch (e: Exception) {
LogIt.warning("Error when handling block header notification: $e")
}
The initial response and notification differ,
The inital response looks something like this
While a notification looks something like this
This is not clear in the documentation.
Electron Cash handles this by "rewriting" the response https://github.com/Electron-Cash/Electron-Cash/blob/master/lib/network.py#L875