From 546a3073f58186380a2013e96729a4b6c63f366e Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu Date: Wed, 6 Nov 2024 17:09:08 +0200 Subject: [PATCH] chore: Block's optional totalDifficulty parsing refactoring; --- Sources/Web3Core/Structure/Block/Block.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/Web3Core/Structure/Block/Block.swift b/Sources/Web3Core/Structure/Block/Block.swift index 8752954a..587458bc 100644 --- a/Sources/Web3Core/Structure/Block/Block.swift +++ b/Sources/Web3Core/Structure/Block/Block.swift @@ -85,7 +85,12 @@ extension Block: Decodable { } self.difficulty = try container.decodeHex(BigUInt.self, forKey: .difficulty) - self.totalDifficulty = (try? container.decodeHex(BigUInt.self, forKey: .totalDifficulty)) ?? .zero + if (container.contains(.totalDifficulty)) { + // Must throw if value is set but it is invalid + self.totalDifficulty = try container.decodeHex(BigUInt.self, forKey: .totalDifficulty) + } else { + self.totalDifficulty = .zero + } self.extraData = try container.decodeHex(Data.self, forKey: .extraData) self.size = try container.decodeHex(BigUInt.self, forKey: .size) self.gasLimit = try container.decodeHex(BigUInt.self, forKey: .gasLimit)