nim-codex/codex/blockexchange/protobuf/presence.nim

43 lines
997 B
Nim
Raw Normal View History

2021-04-26 14:34:04 +00:00
import libp2p
import pkg/stint
import pkg/questionable
import pkg/questionable/results
import pkg/upraises
import ./blockexc
2021-04-26 14:34:04 +00:00
export questionable
export stint
export BlockPresenceType
upraises.push: {.upraises: [].}
type
PresenceMessage* = blockexc.BlockPresence
2021-04-26 14:34:04 +00:00
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 =
2021-05-10 09:49:33 +00:00
without cid =? Cid.init(message.cid) and
price =? UInt256.parse(message.price):
2021-04-26 14:34:04 +00:00
return none Presence
some Presence(
2021-05-10 09:49:33 +00:00
cid: cid,
2021-04-26 14:34:04 +00:00
have: message.`type` == presenceHave,
2021-05-10 09:49:33 +00:00
price: price
2021-04-26 14:34:04 +00:00
)