mirror of
https://github.com/status-im/nim-eth.git
synced 2025-02-16 16:06:35 +00:00
Clean up logging and some exception handling
This commit is contained in:
parent
bb6a3c2ce1
commit
5daaf73d2e
@ -1,6 +1,6 @@
|
|||||||
import std/net
|
import
|
||||||
import types, ../enode
|
std/net,
|
||||||
import eth/trie/db
|
eth/trie/db, types, ../enode
|
||||||
|
|
||||||
type
|
type
|
||||||
DiscoveryDB* = ref object of Database
|
DiscoveryDB* = ref object of Database
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import std/tables
|
import
|
||||||
import types, node, enr, hkdf, ../enode, eth/[rlp, keys], nimcrypto, stint
|
std/tables, nimcrypto, stint, chronicles,
|
||||||
|
types, node, enr, hkdf, ../enode, eth/[rlp, keys]
|
||||||
|
|
||||||
const
|
const
|
||||||
idNoncePrefix = "discovery-id-nonce"
|
idNoncePrefix = "discovery-id-nonce"
|
||||||
@ -179,13 +180,13 @@ proc decodePacketBody(typ: byte, body: openarray[byte], res: var Packet): bool =
|
|||||||
decode(findNode)
|
decode(findNode)
|
||||||
decode(nodes)
|
decode(nodes)
|
||||||
else:
|
else:
|
||||||
echo "unknown packet: ", typ
|
debug "unknown packet type ", typ
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc decodeAuthResp(c: Codec, fromId: NodeId, head: AuthHeader, challenge: Whoareyou, secrets: var HandshakeSecrets, newNode: var Node): bool =
|
proc decodeAuthResp(c: Codec, fromId: NodeId, head: AuthHeader, challenge: Whoareyou, secrets: var HandshakeSecrets, newNode: var Node): bool =
|
||||||
if head.scheme != authSchemeName:
|
if head.scheme != authSchemeName:
|
||||||
echo "Unknown auth scheme"
|
warn "Unknown auth scheme"
|
||||||
return false
|
return false
|
||||||
|
|
||||||
var ephKey: PublicKey
|
var ephKey: PublicKey
|
||||||
@ -207,7 +208,7 @@ proc decodeEncrypted*(c: var Codec, fromId: NodeID, fromAddr: Address, input: se
|
|||||||
var auth: AuthHeader
|
var auth: AuthHeader
|
||||||
var readKey: array[16, byte]
|
var readKey: array[16, byte]
|
||||||
if r.isList:
|
if r.isList:
|
||||||
# Handshake
|
# Handshake - rlp list indicates auth-header
|
||||||
|
|
||||||
# TODO: Auth failure will result in resending whoareyou. Do we really want this?
|
# TODO: Auth failure will result in resending whoareyou. Do we really want this?
|
||||||
auth = r.read(AuthHeader)
|
auth = r.read(AuthHeader)
|
||||||
@ -231,6 +232,7 @@ proc decodeEncrypted*(c: var Codec, fromId: NodeID, fromAddr: Address, input: se
|
|||||||
readKey = sec.readKey
|
readKey = sec.readKey
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
# Message packet or random packet - rlp bytes (size 12) indicates auth-tag
|
||||||
authTag = r.read(array[12, byte])
|
authTag = r.read(array[12, byte])
|
||||||
auth.auth = authTag
|
auth.auth = authTag
|
||||||
var writeKey: array[16, byte]
|
var writeKey: array[16, byte]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import std/[net, endians, hashes]
|
import
|
||||||
import nimcrypto, stint
|
std/[net, endians, hashes], nimcrypto, stint, chronicles,
|
||||||
import types, enr, eth/keys, ../enode
|
types, enr, eth/keys, ../enode
|
||||||
|
|
||||||
type
|
type
|
||||||
Node* = ref object
|
Node* = ref object
|
||||||
@ -34,7 +34,7 @@ proc newNode*(r: Record): Node =
|
|||||||
|
|
||||||
var pk: PublicKey
|
var pk: PublicKey
|
||||||
if recoverPublicKey(r.get("secp256k1", seq[byte]), pk) != EthKeysStatus.Success:
|
if recoverPublicKey(r.get("secp256k1", seq[byte]), pk) != EthKeysStatus.Success:
|
||||||
echo "Could not recover public key"
|
warn "Could not recover public key"
|
||||||
return
|
return
|
||||||
|
|
||||||
let a = Address(ip: IpAddress(family: IpAddressFamily.IPv4,
|
let a = Address(ip: IpAddress(family: IpAddressFamily.IPv4,
|
||||||
|
@ -51,7 +51,7 @@ proc start*(p: Protocol) =
|
|||||||
discard
|
discard
|
||||||
|
|
||||||
proc send(d: Protocol, a: Address, data: seq[byte]) =
|
proc send(d: Protocol, a: Address, data: seq[byte]) =
|
||||||
# echo "Sending ", data.len, " bytes to ", a
|
# debug "Sending bytes", amount = data.len, to = a
|
||||||
let ta = initTAddress(a.ip, a.udpPort)
|
let ta = initTAddress(a.ip, a.udpPort)
|
||||||
let f = d.transp.sendTo(ta, data)
|
let f = d.transp.sendTo(ta, data)
|
||||||
f.callback = proc(data: pointer) {.gcsafe.} =
|
f.callback = proc(data: pointer) {.gcsafe.} =
|
||||||
@ -123,14 +123,13 @@ proc handleFindNode(d: Protocol, fromNode: Node, fn: FindNodePacket, reqId: Requ
|
|||||||
let distance = min(fn.distance, 256)
|
let distance = min(fn.distance, 256)
|
||||||
d.sendNodes(fromNode, reqId, d.routingTable.neighboursAtDistance(distance))
|
d.sendNodes(fromNode, reqId, d.routingTable.neighboursAtDistance(distance))
|
||||||
|
|
||||||
proc receive*(d: Protocol, a: Address, msg: Bytes) {.gcsafe.} =
|
proc receive(d: Protocol, a: Address, msg: Bytes)
|
||||||
## Can raise `DiscProtocolError` and all of `RlpError`
|
{.gcsafe, raises:[RlpError, Exception].} =
|
||||||
# Note: export only needed for testing
|
# TODO: figure out where the general exception comes from and clean it up
|
||||||
if msg.len < 32:
|
if msg.len < 32:
|
||||||
return # Invalid msg
|
return # Invalid msg
|
||||||
|
|
||||||
try:
|
# debug "Packet received: ", length = msg.len
|
||||||
# echo "Packet received: ", msg.len
|
|
||||||
|
|
||||||
if d.isWhoAreYou(msg):
|
if d.isWhoAreYou(msg):
|
||||||
let whoareyou = d.decodeWhoAreYou(msg)
|
let whoareyou = d.decodeWhoAreYou(msg)
|
||||||
@ -155,7 +154,7 @@ proc receive*(d: Protocol, a: Address, msg: Bytes) {.gcsafe.} =
|
|||||||
if node.isNil:
|
if node.isNil:
|
||||||
node = d.routingTable.getNode(sender)
|
node = d.routingTable.getNode(sender)
|
||||||
else:
|
else:
|
||||||
echo "Adding new node to routing table"
|
debug "Adding new node to routing table"
|
||||||
discard d.routingTable.addNode(node)
|
discard d.routingTable.addNode(node)
|
||||||
|
|
||||||
doAssert(not node.isNil, "No node in the routing table (internal error?)")
|
doAssert(not node.isNil, "No node in the routing table (internal error?)")
|
||||||
@ -170,16 +169,12 @@ proc receive*(d: Protocol, a: Address, msg: Bytes) {.gcsafe.} =
|
|||||||
if d.awaitedPackets.take((node, packet.reqId), waiter):
|
if d.awaitedPackets.take((node, packet.reqId), waiter):
|
||||||
waiter.complete(packet.some)
|
waiter.complete(packet.some)
|
||||||
else:
|
else:
|
||||||
echo "TODO: handle packet: ", packet.kind, " from ", node
|
debug "TODO: handle packet: ", packet = packet.kind, origin = node
|
||||||
|
|
||||||
else:
|
else:
|
||||||
echo "Could not decode, respond with whoareyou"
|
debug "Could not decode, respond with whoareyou"
|
||||||
d.sendWhoareyou(a, sender, authTag)
|
d.sendWhoareyou(a, sender, authTag)
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
echo "Exception: ", e.msg
|
|
||||||
echo e.getStackTrace()
|
|
||||||
|
|
||||||
proc waitPacket(d: Protocol, fromNode: Node, reqId: RequestId): Future[Option[Packet]] =
|
proc waitPacket(d: Protocol, fromNode: Node, reqId: RequestId): Future[Option[Packet]] =
|
||||||
result = newFuture[Option[Packet]]("waitPacket")
|
result = newFuture[Option[Packet]]("waitPacket")
|
||||||
let res = result
|
let res = result
|
||||||
@ -229,7 +224,7 @@ proc lookupWorker(p: Protocol, destNode: Node, target: NodeId): Future[seq[Node]
|
|||||||
var i = 0
|
var i = 0
|
||||||
while i < lookupRequestLimit and result.len < findNodeResultLimit:
|
while i < lookupRequestLimit and result.len < findNodeResultLimit:
|
||||||
let r = await p.findNode(destNode, dists[i])
|
let r = await p.findNode(destNode, dists[i])
|
||||||
# TODO: Handle falures
|
# TODO: Handle failures
|
||||||
result.add(r)
|
result.add(r)
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
@ -284,11 +279,12 @@ proc processClient(transp: DatagramTransport,
|
|||||||
var buf = transp.getMessage()
|
var buf = transp.getMessage()
|
||||||
let a = Address(ip: raddr.address, udpPort: raddr.port, tcpPort: raddr.port)
|
let a = Address(ip: raddr.address, udpPort: raddr.port, tcpPort: raddr.port)
|
||||||
proto.receive(a, buf)
|
proto.receive(a, buf)
|
||||||
except RlpError:
|
except RlpError as e:
|
||||||
debug "Receive failed", err = getCurrentExceptionMsg()
|
debug "Receive failed", exception = e.name, msg = e.msg
|
||||||
except:
|
# TODO: what else can be raised? Figure this out and be more restrictive?
|
||||||
debug "Receive failed", err = getCurrentExceptionMsg()
|
except CatchableError as e:
|
||||||
raise
|
debug "Receive failed", exception = e.name, msg = e.msg,
|
||||||
|
stacktrace = e.getStackTrace()
|
||||||
|
|
||||||
proc revalidateNode(p: Protocol, n: Node) {.async.} =
|
proc revalidateNode(p: Protocol, n: Node) {.async.} =
|
||||||
let reqId = newRequestId()
|
let reqId = newRequestId()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import std/[algorithm, times, sequtils, bitops, random, sets]
|
import
|
||||||
import types, node
|
std/[algorithm, times, sequtils, bitops, random, sets], stint, chronicles,
|
||||||
import stint, chronicles
|
types, node
|
||||||
|
|
||||||
type
|
type
|
||||||
RoutingTable* = object
|
RoutingTable* = object
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import hashes
|
import
|
||||||
import ../enode, enr, stint
|
hashes, stint,
|
||||||
|
../enode, enr
|
||||||
|
|
||||||
type
|
type
|
||||||
NodeId* = UInt256
|
NodeId* = UInt256
|
||||||
|
Loading…
x
Reference in New Issue
Block a user