Skip to content

Commit

Permalink
use expect more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed May 19, 2021
1 parent cdc0529 commit 1d60df2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
25 changes: 10 additions & 15 deletions libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ type

proc new*(T: type[SwitchBuilder]): T =

let addrRes = MultiAddress.init("/ip4/127.0.0.1/tcp/0")
if addrRes.isErr:
raise newException(Defect, addrRes.error)
let address = MultiAddress
.init("/ip4/127.0.0.1/tcp/0")
.expect("address should initialize to default")

SwitchBuilder(
privKey: none(PrivateKey),
address: addrRes.get,
address: address,
secureManagers: @[],
tcpTransportOpts: TcpTransportOpts(),
maxConnections: MaxConnections,
Expand Down Expand Up @@ -128,25 +128,20 @@ proc build*(b: SwitchBuilder): Switch =
raise newException(Defect, "Cannot initialize RNG")

let pkRes = PrivateKey.random(b.rng[])
if pkRes.isErr:
raise newException(Defect, "No RNG supplied!")

let
seckey = b.privKey.get(otherwise = pkRes.expect("pk was just set"))
seckey = b.privKey.get(otherwise = pkRes.expect("Should supply a valid RNG"))

var
secureManagerInstances: seq[Secure]
if SecureProtocol.Noise in b.secureManagers:
secureManagerInstances.add(newNoise(b.rng, seckey).Secure)

let
peerInfo = try:
var info = PeerInfo.init(seckey, [b.address])
info.protoVersion = b.protoVersion
info.agentVersion = b.agentVersion
info
except CatchableError as exc:
raiseAssert exc.msg
peerInfo = PeerInfo.init(
seckey,
[b.address],
protoVersion = b.protoVersion,
agentVersion = b.agentVersion)

let
muxers = block:
Expand Down
14 changes: 5 additions & 9 deletions libp2p/protocols/secure/noise.nim
Original file line number Diff line number Diff line change
Expand Up @@ -601,19 +601,15 @@ proc newNoise*(
outgoing: bool = true;
commonPrologue: seq[byte] = @[]): Noise =

let pkRes = privateKey.getKey()
if pkRes.isErr:
raise newException(Defect, "Error in private key: " & $pkRes.error)

let pkByteRes = pkRes.get().getBytes()
if pkByteRes.isErr:
raise newException(Defect, "Error getting PK bytes " & $pkByteRes.error)

let pkBytes = privateKey.getKey()
.expect("Expected valid private key")
.getBytes()
.expect("Couldn't get key bytes")
var noise = Noise(
rng: rng,
outgoing: outgoing,
localPrivateKey: privateKey,
localPublicKey: pkByteRes.get(),
localPublicKey: pkBytes,
noiseKeys: genKeyPair(rng[]),
commonPrologue: commonPrologue,
)
Expand Down
5 changes: 1 addition & 4 deletions libp2p/protocols/secure/secio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,7 @@ proc newSecioConn(conn: Connection,
if conn.peerInfo != nil:
conn.peerInfo
else:
try:
PeerInfo.init(remotePubKey)
except CatchableError as exc:
raise newException(SecioError, exc.msg)
PeerInfo.init(remotePubKey)

result = SecioConn.init(conn, conn.peerInfo, conn.observedAddr)

Expand Down

0 comments on commit 1d60df2

Please sign in to comment.