safe tables access (#95)

* safer table access

* use withValue
This commit is contained in:
Dmitriy Ryajov 2022-05-12 14:23:05 -06:00 committed by GitHub
parent cf062670f6
commit 9ca4f90cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 31 deletions

View File

@ -120,14 +120,16 @@ proc broadcastWantList*(
trace "Sending want list to peer", peer = id, `type` = $wantType, len = cids.len trace "Sending want list to peer", peer = id, `type` = $wantType, len = cids.len
let wantList = makeWantList( let
cids, wantList = makeWantList(
priority, cids,
cancel, priority,
wantType, cancel,
full, wantType,
sendDontHave) full,
b.peers[id].broadcast(Message(wantlist: wantList)) sendDontHave)
b.peers.withValue(id, peer):
peer[].broadcast(Message(wantlist: wantList))
proc handleBlocks( proc handleBlocks(
b: BlockExcNetwork, b: BlockExcNetwork,
@ -153,9 +155,7 @@ proc handleBlocks(
b.handlers.onBlocks(peer.id, blks) b.handlers.onBlocks(peer.id, blks)
template makeBlocks*( template makeBlocks*(blocks: seq[bt.Block]): seq[pb.Block] =
blocks: seq[bt.Block]):
seq[pb.Block] =
var blks: seq[pb.Block] var blks: seq[pb.Block]
for blk in blocks: for blk in blocks:
blks.add(pb.Block( blks.add(pb.Block(
@ -176,7 +176,8 @@ proc broadcastBlocks*(
return return
trace "Sending blocks to peer", peer = id, len = blocks.len trace "Sending blocks to peer", peer = id, len = blocks.len
b.peers[id].broadcast(pb.Message(payload: makeBlocks(blocks))) b.peers.withValue(id, peer):
peer[].broadcast(pb.Message(payload: makeBlocks(blocks)))
proc handleBlockPresence( proc handleBlockPresence(
b: BlockExcNetwork, b: BlockExcNetwork,
@ -202,7 +203,8 @@ proc broadcastBlockPresence*(
return return
trace "Sending presence to peer", peer = id trace "Sending presence to peer", peer = id
b.peers[id].broadcast(Message(blockPresences: presence)) b.peers.withValue(id, peer):
peer[].broadcast(Message(blockPresences: @presence))
proc handleAccount(network: BlockExcNetwork, proc handleAccount(network: BlockExcNetwork,
peer: NetworkPeer, peer: NetworkPeer,
@ -218,7 +220,8 @@ proc broadcastAccount*(network: BlockExcNetwork,
return return
let message = Message(account: AccountMessage.init(account)) let message = Message(account: AccountMessage.init(account))
network.peers[id].broadcast(message) network.peers.withValue(id, peer):
peer[].broadcast(message)
proc broadcastPayment*(network: BlockExcNetwork, proc broadcastPayment*(network: BlockExcNetwork,
id: PeerId, id: PeerId,
@ -227,7 +230,8 @@ proc broadcastPayment*(network: BlockExcNetwork,
return return
let message = Message(payment: StateChannelUpdate.init(payment)) let message = Message(payment: StateChannelUpdate.init(payment))
network.peers[id].broadcast(message) network.peers.withValue(id, peer):
peer[].broadcast(message)
proc handlePayment(network: BlockExcNetwork, proc handlePayment(network: BlockExcNetwork,
peer: NetworkPeer, peer: NetworkPeer,
@ -261,13 +265,13 @@ proc getOrCreatePeer(b: BlockExcNetwork, peer: PeerID): NetworkPeer =
## ##
if peer in b.peers: if peer in b.peers:
return b.peers[peer] return b.peers.getOrDefault(peer, nil)
var getConn = proc(): Future[Connection] {.async.} = var getConn = proc(): Future[Connection] {.async.} =
try: try:
return await b.switch.dial(peer, Codec) return await b.switch.dial(peer, Codec)
except CatchableError as exc: except CatchableError as exc:
trace "unable to connect to blockexc peer", exc = exc.msg trace "Unable to connect to blockexc peer", exc = exc.msg
if not isNil(b.getConn): if not isNil(b.getConn):
getConn = b.getConn getConn = b.getConn
@ -277,7 +281,7 @@ proc getOrCreatePeer(b: BlockExcNetwork, peer: PeerID): NetworkPeer =
# create new pubsub peer # create new pubsub peer
let blockExcPeer = NetworkPeer.new(peer, getConn, rpcHandler) let blockExcPeer = NetworkPeer.new(peer, getConn, rpcHandler)
debug "created new blockexc peer", peer debug "Created new blockexc peer", peer
b.peers[peer] = blockExcPeer b.peers[peer] = blockExcPeer
@ -363,8 +367,7 @@ proc new*(
sendBlocks: sendBlocks, sendBlocks: sendBlocks,
sendPresence: sendPresence, sendPresence: sendPresence,
sendAccount: sendAccount, sendAccount: sendAccount,
sendPayment: sendPayment sendPayment: sendPayment)
)
b.init() b.init()
return b return b

View File

@ -13,12 +13,12 @@ export payments, nitro
type type
BlockExcPeerCtx* = ref object of RootObj BlockExcPeerCtx* = ref object of RootObj
id*: PeerID id*: PeerID
peerPrices*: Table[Cid, UInt256] # remote peer have list including price peerPrices*: Table[Cid, UInt256] # remote peer have list including price
peerWants*: seq[Entry] # remote peers want lists peerWants*: seq[Entry] # remote peers want lists
exchanged*: int # times peer has exchanged with us exchanged*: int # times peer has exchanged with us
lastExchange*: Moment # last time peer has exchanged with us lastExchange*: Moment # last time peer has exchanged with us
account*: ?Account # ethereum account of this peer account*: ?Account # ethereum account of this peer
paymentChannel*: ?ChannelId # payment channel id paymentChannel*: ?ChannelId # payment channel id
proc peerHave*(context: BlockExcPeerCtx): seq[Cid] = proc peerHave*(context: BlockExcPeerCtx): seq[Cid] =
toSeq(context.peerPrices.keys) toSeq(context.peerPrices.keys)

View File

@ -10,6 +10,10 @@
import std/tables import std/tables
import std/sequtils import std/sequtils
import pkg/upraises
push: {.upraises: [].}
import pkg/questionable import pkg/questionable
import pkg/chronicles import pkg/chronicles
import pkg/chronos import pkg/chronos
@ -63,11 +67,11 @@ proc resolve*(
for blk in blocks: for blk in blocks:
# resolve any pending blocks # resolve any pending blocks
if blk.cid in p.blocks: if blk.cid in p.blocks:
let pending = p.blocks[blk.cid] p.blocks.withValue(blk.cid, pending):
if not pending.finished: if not pending[].finished:
trace "Resolving block", cid = $blk.cid trace "Resolving block", cid = $blk.cid
pending.complete(blk) pending[].complete(blk)
p.blocks.del(blk.cid) p.blocks.del(blk.cid)
proc pending*( proc pending*(
p: PendingBlocksManager, p: PendingBlocksManager,