diff --git a/eth/keyfile/uuid.nim b/eth/keyfile/uuid.nim index 943e3ef..59986be 100644 --- a/eth/keyfile/uuid.nim +++ b/eth/keyfile/uuid.nim @@ -21,7 +21,7 @@ import nimcrypto/utils, endians type - UUIDException = object of Exception + UUIDException = object of CatchableError UUID* = object ## Represents UUID object diff --git a/eth/keys.nim b/eth/keys.nim index 8e41700..dc03690 100644 --- a/eth/keys.nim +++ b/eth/keys.nim @@ -13,7 +13,7 @@ type Success, ## Operation was successful Error ## Operation failed - EthKeysException* = object of Exception + EthKeysException* = object of CatchableError ## Exception generated by this module when not defined(native): diff --git a/eth/keys/libsecp256k1.nim b/eth/keys/libsecp256k1.nim index fe589c4..0d0621f 100644 --- a/eth/keys/libsecp256k1.nim +++ b/eth/keys/libsecp256k1.nim @@ -50,7 +50,7 @@ type SignatureNR* = secp256k1_ecdsa_signature ## Representation of non-recoverable signature - Secp256k1Exception* = object of Exception + Secp256k1Exception* = object of CatchableError ## Exceptions generated by `libsecp256k1` EthKeysContext = ref object diff --git a/eth/p2p/auth.nim b/eth/p2p/auth.nim index 875ffd1..12760c6 100644 --- a/eth/p2p/auth.nim +++ b/eth/p2p/auth.nim @@ -79,7 +79,7 @@ type egressMac*: keccak256 ingressMac*: keccak256 - AuthException* = object of Exception + AuthException* = object of CatchableError template toa(a, b, c: untyped): untyped = toOpenArray((a), (b), (b) + (c) - 1) diff --git a/eth/p2p/ecies.nim b/eth/p2p/ecies.nim index e60703d..0f276d1 100644 --- a/eth/p2p/ecies.nim +++ b/eth/p2p/ecies.nim @@ -16,7 +16,7 @@ const emptyMac* = array[0, byte]([]) type - EciesException* = object of Exception + EciesException* = object of CatchableError EciesStatus* = enum Success, ## Operation was successful BufferOverrun, ## Output buffer size is too small diff --git a/eth/p2p/enode.nim b/eth/p2p/enode.nim index 8d6029e..342e4b0 100644 --- a/eth/p2p/enode.nim +++ b/eth/p2p/enode.nim @@ -34,7 +34,7 @@ type pubkey*: PublicKey ## Node public key address*: Address ## Node address - ENodeException* = object of Exception + ENodeException* = object of CatchableError proc raiseENodeError(status: ENodeStatus) = if status == IncorrectIP: diff --git a/eth/p2p/rlpx_protocols/les/private/les_types.nim b/eth/p2p/rlpx_protocols/les/private/les_types.nim index 5a22453..b84992e 100644 --- a/eth/p2p/rlpx_protocols/les/private/les_types.nim +++ b/eth/p2p/rlpx_protocols/les/private/les_types.nim @@ -95,7 +95,7 @@ type key*: string value*: Blob - HandshakeError* = object of Exception + HandshakeError* = object of CatchableError LesTime* = int # this is in milliseconds BufValueInt* = int diff --git a/eth/rlp/writer.nim b/eth/rlp/writer.nim index 5663cd9..b082f3f 100644 --- a/eth/rlp/writer.nim +++ b/eth/rlp/writer.nim @@ -11,8 +11,6 @@ type pendingLists: seq[tuple[remainingItems, outBytes: int]] output: Bytes - PrematureFinalizationError* = object of Exception - IntLike* = concept x, y type T = type(x) @@ -256,9 +254,7 @@ proc initRlpList*(listSize: int): RlpWriter = # TODO: This should return a lent value proc finish*(self): Bytes = - if pendingLists.len > 0: - raise newException(PrematureFinalizationError, - "Insufficient number of elements written to a started list") + doAssert pendingLists.len == 0, "Insufficient number of elements written to a started list" result = output proc encode*[T](v: T): Bytes = diff --git a/eth/trie/backends/backend_defs.nim b/eth/trie/backends/backend_defs.nim index 6ab3212..63ec446 100644 --- a/eth/trie/backends/backend_defs.nim +++ b/eth/trie/backends/backend_defs.nim @@ -1,5 +1,5 @@ type - StorageError* = object of Exception + StorageError* = object of CatchableError template raiseStorageInitError* = raise newException(StorageError, "failure to initialize storage") diff --git a/eth/trie/binaries.nim b/eth/trie/binaries.nim index 3a81161..a81b40d 100644 --- a/eth/trie/binaries.nim +++ b/eth/trie/binaries.nim @@ -1,6 +1,6 @@ import sequtils, - stew/ranges/[ptr_arith, bitranges], eth/rlp/types + stew/ranges/[ptr_arith, bitranges], eth/rlp/types, trie_defs type TrieNodeKind* = enum @@ -22,8 +22,8 @@ type of LEAF_TYPE: value*: BytesRange - InvalidNode* = object of Exception - ValidationError* = object of Exception + InvalidNode* = object of CorruptedTrieDatabase + ValidationError* = object of CorruptedTrieDatabase # ---------------------------------------------- template sliceToEnd*(r: TrieBitRange, index: int): TrieBitRange = diff --git a/eth/trie/binary.nim b/eth/trie/binary.nim index 9c8e7b8..44518a2 100644 --- a/eth/trie/binary.nim +++ b/eth/trie/binary.nim @@ -12,7 +12,7 @@ type db: DB rootHash: TrieNodeKey - NodeOverrideError* = object of Exception + NodeOverrideError* = object of CatchableError let zeroHash* = zeroBytesRange diff --git a/eth/trie/branches.nim b/eth/trie/branches.nim index bf902a8..8322a2f 100644 --- a/eth/trie/branches.nim +++ b/eth/trie/branches.nim @@ -4,7 +4,9 @@ import type DB = TrieDatabaseRef - InvalidKeyError* = object of Exception + + # TODO: replace the usages of this with regular asserts + InvalidKeyError* = object of Defect template query(db: DB, nodeHash: TrieNodeKey): BytesRange = db.get(nodeHash.toOpenArray).toRange diff --git a/eth/trie/hexary.nim b/eth/trie/hexary.nim index 2f597c3..0bb4481 100644 --- a/eth/trie/hexary.nim +++ b/eth/trie/hexary.nim @@ -19,10 +19,6 @@ type TrieNode = Rlp - TrieError* = object of Exception - CorruptedTrieError* = object of TrieError - PersistenceFailure* = object of TrieError - template len(key: TrieNodeKey): int = key.usedBytes.int @@ -133,7 +129,7 @@ proc getAux(db: DB, nodeRlp: Rlp, path: NibblesRange): BytesRange = let nextLookup = branch.getLookup return getAux(db, nextLookup, path.slice(1)) else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") proc get*(self: HexaryTrie; key: BytesRange): BytesRange = @@ -173,7 +169,7 @@ proc getKeysAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesRange]]) doAssert(path.len mod 2 == 0) return path.getBytes else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") iterator keys*(self: HexaryTrie): BytesRange = @@ -212,7 +208,7 @@ proc getValuesAux(db: DB, stack: var seq[Rlp]): BytesRange = if not lastElem.isEmpty: return lastElem.toBytes else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") iterator values*(self: HexaryTrie): BytesRange = @@ -255,7 +251,7 @@ proc getPairsAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesRange]] doAssert(path.len mod 2 == 0) return (path.getBytes, lastElem.toBytes) else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") iterator pairs*(self: HexaryTrie): (BytesRange, BytesRange) = @@ -311,7 +307,7 @@ iterator replicate*(self: HexaryTrie): (BytesRange, BytesRange) = key.replaceLastNibble(i.byte) pushOrYield(branch) else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") proc getValues*(self: HexaryTrie): seq[BytesRange] = @@ -350,7 +346,7 @@ proc getBranchAux(db: DB, node: BytesRange, path: NibblesRange, output: var seq[ output.add nextLookup getBranchAux(db, nextLookup, path.slice(1), output) else: - raise newException(CorruptedTrieError, + raise newException(CorruptedTrieDatabase, "HexaryTrie node with an unexpected number of children") proc getBranch*(self: HexaryTrie; key: BytesRange): seq[BytesRange] = diff --git a/eth/trie/trie_defs.nim b/eth/trie/trie_defs.nim index e7a3271..72eb2a4 100644 --- a/eth/trie/trie_defs.nim +++ b/eth/trie/trie_defs.nim @@ -8,6 +8,18 @@ type KeccakHash* = MDigest[256] BytesContainer* = ByteRange | Bytes | string + TrieError* = object of CatchableError + # A common base type of all Trie errors. + + PersistenceFailure* = object of TrieError + # The backing database of the trie was not able to carry out + # the storage or retrieval request. + + CorruptedTrieDatabase* = object of Defect + # We consider this a Defect, because the software cannot safely + # operate if its database has been tampered with. A swift crash + # will be a more appropriate response. + # can't be a const: https://github.com/status-im/nim-eth/issues/6 # we can't initialise it here, but since it's already zeroed memory, we don't need to var zeroBytesRange* {.threadvar.}: ByteRange