mirror of
https://github.com/status-im/nim-codex.git
synced 2025-01-27 19:16:54 +00:00
2fb39ca4a3
* use PeerInfo in event handlers * use CidV1 and raw multicodec as default * add block stream abstraction * raises defect * adding dataset abstraction * move blockstream into own dir * reorg files and fix tests * rename dataset to blockset * wip * wip * adding basic test for treehash algo * run blockset tests along with with the rest * remove obsolete contents * fix chunker tests * rename bitswap and move to stores * rename bitwsap to blockexc and move to stores * moare project structure reorg
43 lines
997 B
Nim
43 lines
997 B
Nim
import libp2p
|
|
import pkg/stint
|
|
import pkg/questionable
|
|
import pkg/questionable/results
|
|
import pkg/upraises
|
|
import ./blockexc
|
|
|
|
export questionable
|
|
export stint
|
|
export BlockPresenceType
|
|
|
|
upraises.push: {.upraises: [].}
|
|
|
|
type
|
|
PresenceMessage* = blockexc.BlockPresence
|
|
Presence* = object
|
|
cid*: Cid
|
|
have*: bool
|
|
price*: UInt256
|
|
|
|
func init*(_: type PresenceMessage, presence: Presence): PresenceMessage =
|
|
PresenceMessage(
|
|
cid: presence.cid.data.buffer,
|
|
`type`: if presence.have: presenceHave else: presenceDontHave,
|
|
price: @(presence.price.toBytesBE)
|
|
)
|
|
|
|
func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 =
|
|
if bytes.len > 32:
|
|
return UInt256.none
|
|
UInt256.fromBytesBE(bytes).some
|
|
|
|
func init*(_: type Presence, message: PresenceMessage): ?Presence =
|
|
without cid =? Cid.init(message.cid) and
|
|
price =? UInt256.parse(message.price):
|
|
return none Presence
|
|
|
|
some Presence(
|
|
cid: cid,
|
|
have: message.`type` == presenceHave,
|
|
price: price
|
|
)
|