2021-04-26 14:34:04 +00:00
|
|
|
import libp2p
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/upraises
|
2021-08-30 19:25:20 +00:00
|
|
|
import ./blockexc
|
2021-04-26 14:34:04 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../../blocktype
|
|
|
|
|
2021-04-26 14:34:04 +00:00
|
|
|
export questionable
|
|
|
|
export stint
|
|
|
|
export BlockPresenceType
|
|
|
|
|
|
|
|
upraises.push: {.upraises: [].}
|
|
|
|
|
|
|
|
type
|
2021-08-30 19:25:20 +00:00
|
|
|
PresenceMessage* = blockexc.BlockPresence
|
2021-04-26 14:34:04 +00:00
|
|
|
Presence* = object
|
2023-11-14 12:02:17 +00:00
|
|
|
address*: BlockAddress
|
2021-04-26 14:34:04 +00:00
|
|
|
have*: bool
|
|
|
|
price*: UInt256
|
|
|
|
|
|
|
|
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 =
|
2023-11-14 12:02:17 +00:00
|
|
|
without price =? UInt256.parse(message.price):
|
2021-04-26 14:34:04 +00:00
|
|
|
return none Presence
|
|
|
|
|
|
|
|
some Presence(
|
2023-11-14 12:02:17 +00:00
|
|
|
address: message.address,
|
2022-11-15 15:46:21 +00:00
|
|
|
have: message.`type` == BlockPresenceType.Have,
|
2021-05-10 09:49:33 +00:00
|
|
|
price: price
|
2021-04-26 14:34:04 +00:00
|
|
|
)
|
2022-11-15 15:46:21 +00:00
|
|
|
|
|
|
|
func init*(_: type PresenceMessage, presence: Presence): PresenceMessage =
|
|
|
|
PresenceMessage(
|
2023-11-14 12:02:17 +00:00
|
|
|
address: presence.address,
|
2022-11-15 15:46:21 +00:00
|
|
|
`type`: if presence.have:
|
|
|
|
BlockPresenceType.Have
|
|
|
|
else:
|
|
|
|
BlockPresenceType.DontHave,
|
|
|
|
price: @(presence.price.toBytesBE)
|
|
|
|
)
|