2022-10-13 23:58:57 +00:00
|
|
|
# Protocol of data exchange between Codex nodes
|
|
|
|
# and Protobuf encoder/decoder for these messages.
|
|
|
|
#
|
|
|
|
# Eventually all this code should be auto-generated from message.proto.
|
2023-11-14 12:02:17 +00:00
|
|
|
import std/sugar
|
2022-10-13 23:58:57 +00:00
|
|
|
|
|
|
|
import pkg/libp2p/protobuf/minprotobuf
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/libp2p/cid
|
|
|
|
|
|
|
|
import pkg/questionable
|
2022-10-13 23:58:57 +00:00
|
|
|
|
2023-07-19 13:10:14 +00:00
|
|
|
import ../../units
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../../merkletree
|
|
|
|
import ../../blocktype
|
|
|
|
|
2023-07-19 13:10:14 +00:00
|
|
|
const
|
|
|
|
MaxBlockSize* = 100.MiBs.uint
|
|
|
|
MaxMessageSize* = 100.MiBs.uint
|
2022-10-13 23:58:57 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
WantType* = enum
|
2022-11-15 15:46:21 +00:00
|
|
|
WantBlock = 0,
|
|
|
|
WantHave = 1
|
2022-10-13 23:58:57 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry* = object
|
|
|
|
address*: BlockAddress
|
2022-10-13 23:58:57 +00:00
|
|
|
priority*: int32 # The priority (normalized). default to 1
|
|
|
|
cancel*: bool # Whether this revokes an entry
|
|
|
|
wantType*: WantType # Note: defaults to enum 0, ie Block
|
|
|
|
sendDontHave*: bool # Note: defaults to false
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
WantList* = object
|
|
|
|
entries*: seq[WantListEntry] # A list of wantList entries
|
|
|
|
full*: bool # Whether this is the full wantList. default to false
|
2022-10-13 23:58:57 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
BlockDelivery* = object
|
|
|
|
blk*: Block
|
|
|
|
address*: BlockAddress
|
|
|
|
proof*: ?MerkleProof # Present only if `address.leaf` is true
|
2022-10-13 23:58:57 +00:00
|
|
|
|
|
|
|
BlockPresenceType* = enum
|
2022-11-15 15:46:21 +00:00
|
|
|
Have = 0,
|
|
|
|
DontHave = 1
|
2022-10-13 23:58:57 +00:00
|
|
|
|
|
|
|
BlockPresence* = object
|
2023-11-14 12:02:17 +00:00
|
|
|
address*: BlockAddress
|
2022-10-13 23:58:57 +00:00
|
|
|
`type`*: BlockPresenceType
|
|
|
|
price*: seq[byte] # Amount of assets to pay for the block (UInt256)
|
|
|
|
|
|
|
|
AccountMessage* = object
|
|
|
|
address*: seq[byte] # Ethereum address to which payments should be made
|
|
|
|
|
|
|
|
StateChannelUpdate* = object
|
|
|
|
update*: seq[byte] # Signed Nitro state, serialized as JSON
|
|
|
|
|
|
|
|
Message* = object
|
2023-11-14 12:02:17 +00:00
|
|
|
wantList*: WantList
|
|
|
|
payload*: seq[BlockDelivery]
|
2022-10-13 23:58:57 +00:00
|
|
|
blockPresences*: seq[BlockPresence]
|
|
|
|
pendingBytes*: uint
|
|
|
|
account*: AccountMessage
|
|
|
|
payment*: StateChannelUpdate
|
|
|
|
|
|
|
|
#
|
|
|
|
# Encoding Message into seq[byte] in Protobuf format
|
|
|
|
#
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: BlockAddress) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, value.leaf.uint)
|
|
|
|
if value.leaf:
|
|
|
|
ipb.write(2, value.treeCid.data.buffer)
|
|
|
|
ipb.write(3, value.index.uint64)
|
|
|
|
else:
|
|
|
|
ipb.write(4, value.cid.data.buffer)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: WantListEntry) =
|
2022-10-13 23:58:57 +00:00
|
|
|
var ipb = initProtoBuffer()
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb.write(1, value.address)
|
2022-10-13 23:58:57 +00:00
|
|
|
ipb.write(2, value.priority.uint64)
|
|
|
|
ipb.write(3, value.cancel.uint)
|
|
|
|
ipb.write(4, value.wantType.uint)
|
|
|
|
ipb.write(5, value.sendDontHave.uint)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: WantList) =
|
2022-10-13 23:58:57 +00:00
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
for v in value.entries:
|
|
|
|
ipb.write(1, v)
|
|
|
|
ipb.write(2, value.full.uint)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: BlockDelivery) =
|
2023-07-19 13:10:14 +00:00
|
|
|
var ipb = initProtoBuffer(maxSize = MaxBlockSize)
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb.write(1, value.blk.cid.data.buffer)
|
|
|
|
ipb.write(2, value.blk.data)
|
|
|
|
ipb.write(3, value.address)
|
|
|
|
if value.address.leaf:
|
|
|
|
if proof =? value.proof:
|
|
|
|
ipb.write(4, proof.encode())
|
2022-10-13 23:58:57 +00:00
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: BlockPresence) =
|
|
|
|
var ipb = initProtoBuffer()
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb.write(1, value.address)
|
2022-10-13 23:58:57 +00:00
|
|
|
ipb.write(2, value.`type`.uint)
|
|
|
|
ipb.write(3, value.price)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: AccountMessage) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, value.address)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, value: StateChannelUpdate) =
|
|
|
|
var ipb = initProtoBuffer()
|
|
|
|
ipb.write(1, value.update)
|
|
|
|
ipb.finish()
|
|
|
|
pb.write(field, ipb)
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc protobufEncode*(value: Message): seq[byte] =
|
2023-07-19 13:10:14 +00:00
|
|
|
var ipb = initProtoBuffer(maxSize = MaxMessageSize)
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb.write(1, value.wantList)
|
2022-10-13 23:58:57 +00:00
|
|
|
for v in value.payload:
|
|
|
|
ipb.write(3, v)
|
|
|
|
for v in value.blockPresences:
|
|
|
|
ipb.write(4, v)
|
|
|
|
ipb.write(5, value.pendingBytes)
|
|
|
|
ipb.write(6, value.account)
|
|
|
|
ipb.write(7, value.payment)
|
|
|
|
ipb.finish()
|
|
|
|
ipb.buffer
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Decoding Message from seq[byte] in Protobuf format
|
|
|
|
#
|
2023-11-14 12:02:17 +00:00
|
|
|
proc decode*(_: type BlockAddress, pb: ProtoBuffer): ProtoResult[BlockAddress] =
|
|
|
|
var
|
|
|
|
value: BlockAddress
|
|
|
|
leaf: bool
|
|
|
|
field: uint64
|
|
|
|
cidBuf = newSeq[byte]()
|
|
|
|
|
|
|
|
if ? pb.getField(1, field):
|
|
|
|
leaf = bool(field)
|
|
|
|
|
|
|
|
if leaf:
|
|
|
|
var
|
|
|
|
treeCid: Cid
|
|
|
|
index: Natural
|
|
|
|
if ? pb.getField(2, cidBuf):
|
|
|
|
treeCid = ? Cid.init(cidBuf).mapErr(x => ProtoError.IncorrectBlob)
|
|
|
|
if ? pb.getField(3, field):
|
|
|
|
index = field
|
|
|
|
value = BlockAddress(leaf: true, treeCid: treeCid, index: index)
|
|
|
|
else:
|
|
|
|
var cid: Cid
|
|
|
|
if ? pb.getField(4, cidBuf):
|
|
|
|
cid = ? Cid.init(cidBuf).mapErr(x => ProtoError.IncorrectBlob)
|
|
|
|
value = BlockAddress(leaf: false, cid: cid)
|
2022-10-13 23:58:57 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc decode*(_: type WantListEntry, pb: ProtoBuffer): ProtoResult[WantListEntry] =
|
2022-10-13 23:58:57 +00:00
|
|
|
var
|
2023-11-14 12:02:17 +00:00
|
|
|
value = WantListEntry()
|
2022-10-13 23:58:57 +00:00
|
|
|
field: uint64
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb: ProtoBuffer
|
|
|
|
buf = newSeq[byte]()
|
|
|
|
if ? pb.getField(1, ipb):
|
|
|
|
value.address = ? BlockAddress.decode(ipb)
|
2022-10-13 23:58:57 +00:00
|
|
|
if ? pb.getField(2, field):
|
|
|
|
value.priority = int32(field)
|
|
|
|
if ? pb.getField(3, field):
|
|
|
|
value.cancel = bool(field)
|
|
|
|
if ? pb.getField(4, field):
|
|
|
|
value.wantType = WantType(field)
|
|
|
|
if ? pb.getField(5, field):
|
|
|
|
value.sendDontHave = bool(field)
|
|
|
|
ok(value)
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc decode*(_: type WantList, pb: ProtoBuffer): ProtoResult[WantList] =
|
2022-10-13 23:58:57 +00:00
|
|
|
var
|
2023-11-14 12:02:17 +00:00
|
|
|
value = WantList()
|
2022-10-13 23:58:57 +00:00
|
|
|
field: uint64
|
|
|
|
sublist: seq[seq[byte]]
|
|
|
|
if ? pb.getRepeatedField(1, sublist):
|
|
|
|
for item in sublist:
|
2023-11-14 12:02:17 +00:00
|
|
|
value.entries.add(? WantListEntry.decode(initProtoBuffer(item)))
|
2022-10-13 23:58:57 +00:00
|
|
|
if ? pb.getField(2, field):
|
|
|
|
value.full = bool(field)
|
|
|
|
ok(value)
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc decode*(_: type BlockDelivery, pb: ProtoBuffer): ProtoResult[BlockDelivery] =
|
2022-10-13 23:58:57 +00:00
|
|
|
var
|
2023-11-14 12:02:17 +00:00
|
|
|
value = BlockDelivery()
|
|
|
|
field: uint64
|
|
|
|
dataBuf = newSeq[byte]()
|
|
|
|
cidBuf = newSeq[byte]()
|
|
|
|
cid: Cid
|
|
|
|
ipb: ProtoBuffer
|
|
|
|
|
|
|
|
if ? pb.getField(1, cidBuf):
|
|
|
|
cid = ? Cid.init(cidBuf).mapErr(x => ProtoError.IncorrectBlob)
|
|
|
|
if ? pb.getField(2, dataBuf):
|
|
|
|
value.blk = ? Block.new(cid, dataBuf, verify = true).mapErr(x => ProtoError.IncorrectBlob)
|
|
|
|
if ? pb.getField(3, ipb):
|
|
|
|
value.address = ? BlockAddress.decode(ipb)
|
|
|
|
|
|
|
|
if value.address.leaf:
|
|
|
|
var proofBuf = newSeq[byte]()
|
|
|
|
if ? pb.getField(4, proofBuf):
|
|
|
|
let proof = ? MerkleProof.decode(proofBuf).mapErr(x => ProtoError.IncorrectBlob)
|
|
|
|
value.proof = proof.some
|
|
|
|
else:
|
|
|
|
value.proof = MerkleProof.none
|
|
|
|
else:
|
|
|
|
value.proof = MerkleProof.none
|
|
|
|
|
2022-10-13 23:58:57 +00:00
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc decode*(_: type BlockPresence, pb: ProtoBuffer): ProtoResult[BlockPresence] =
|
|
|
|
var
|
|
|
|
value = BlockPresence()
|
|
|
|
field: uint64
|
2023-11-14 12:02:17 +00:00
|
|
|
ipb: ProtoBuffer
|
|
|
|
if ? pb.getField(1, ipb):
|
|
|
|
value.address = ? BlockAddress.decode(ipb)
|
2022-10-13 23:58:57 +00:00
|
|
|
if ? pb.getField(2, field):
|
|
|
|
value.`type` = BlockPresenceType(field)
|
|
|
|
discard ? pb.getField(3, value.price)
|
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc decode*(_: type AccountMessage, pb: ProtoBuffer): ProtoResult[AccountMessage] =
|
|
|
|
var
|
|
|
|
value = AccountMessage()
|
|
|
|
discard ? pb.getField(1, value.address)
|
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc decode*(_: type StateChannelUpdate, pb: ProtoBuffer): ProtoResult[StateChannelUpdate] =
|
|
|
|
var
|
|
|
|
value = StateChannelUpdate()
|
|
|
|
discard ? pb.getField(1, value.update)
|
|
|
|
ok(value)
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc protobufDecode*(_: type Message, msg: seq[byte]): ProtoResult[Message] =
|
2022-10-13 23:58:57 +00:00
|
|
|
var
|
|
|
|
value = Message()
|
2023-07-19 13:10:14 +00:00
|
|
|
pb = initProtoBuffer(msg, maxSize = MaxMessageSize)
|
2022-10-13 23:58:57 +00:00
|
|
|
ipb: ProtoBuffer
|
|
|
|
sublist: seq[seq[byte]]
|
|
|
|
if ? pb.getField(1, ipb):
|
2023-11-14 12:02:17 +00:00
|
|
|
value.wantList = ? WantList.decode(ipb)
|
2022-10-13 23:58:57 +00:00
|
|
|
if ? pb.getRepeatedField(3, sublist):
|
|
|
|
for item in sublist:
|
2023-11-14 12:02:17 +00:00
|
|
|
value.payload.add(? BlockDelivery.decode(initProtoBuffer(item, maxSize = MaxBlockSize)))
|
2022-10-13 23:58:57 +00:00
|
|
|
if ? pb.getRepeatedField(4, sublist):
|
|
|
|
for item in sublist:
|
|
|
|
value.blockPresences.add(? BlockPresence.decode(initProtoBuffer(item)))
|
|
|
|
discard ? pb.getField(5, value.pendingBytes)
|
|
|
|
if ? pb.getField(6, ipb):
|
|
|
|
value.account = ? AccountMessage.decode(ipb)
|
|
|
|
if ? pb.getField(7, ipb):
|
|
|
|
value.payment = ? StateChannelUpdate.decode(ipb)
|
|
|
|
ok(value)
|