Fix format of content message (#803)
This commit is contained in:
parent
b09ad5cacb
commit
f4d9421836
|
@ -10,6 +10,7 @@
|
|||
{.push raises: [Defect].}
|
||||
|
||||
import
|
||||
std/[options, sugar],
|
||||
nimcrypto/[sha2, hash], stew/objects,
|
||||
eth/ssz/ssz_serialization, eth/trie/[hexary, db]
|
||||
|
||||
|
@ -61,11 +62,24 @@ func fromSszBytes*(T: type ContentType, data: openArray[byte]):
|
|||
|
||||
contentType
|
||||
|
||||
func toContentId*(contentKey: ContentKey): ContentId =
|
||||
func encodeKey*(contentKey: ContentKey): seq[byte] =
|
||||
SSZ.encode(contentKey)
|
||||
|
||||
# TODO consider if more powerfull error handling is necessary here.
|
||||
func decodeKey*(contentKey: ByteList): Option[ContentKey] =
|
||||
try:
|
||||
some(SSZ.decode(contentKey.asSeq(), ContentKey))
|
||||
except SszError:
|
||||
return none[ContentKey]()
|
||||
|
||||
func encodeKeyAsList*(contentKey: ContentKey): ByteList =
|
||||
List.init(encodeKey(contentKey), 2048)
|
||||
|
||||
func toContentId*(contentKey: ByteList): ContentId =
|
||||
# TODO: Hash function to be defined, sha256 used now, might be confusing
|
||||
# with keccak256 that is used for the actual nodes:
|
||||
# https://github.com/ethereum/stateless-ethereum-specs/blob/master/state-network.md#content
|
||||
sha2.sha_256.digest(SSZ.encode(contentKey))
|
||||
sha2.sha_256.digest(contentKey.asSeq())
|
||||
|
||||
type
|
||||
ContentStorage* = object
|
||||
|
@ -84,3 +98,6 @@ proc getContent*(storage: ContentStorage, key: ContentKey): Option[seq[byte]] =
|
|||
some(val)
|
||||
else:
|
||||
none(seq[byte])
|
||||
|
||||
proc getContent*(storage: ContentStorage, contentKey: ByteList): Option[seq[byte]] =
|
||||
decodeKey(contentKey).flatMap((key: ContentKey) => getContent(storage, key))
|
||||
|
|
|
@ -48,10 +48,7 @@ type
|
|||
# also be limited to 300 bytes instead of 2048
|
||||
|
||||
FindContentMessage* = object
|
||||
contentKey*: ContentKey
|
||||
# TODO: According to the specification, this is actually a ByteList from the
|
||||
# serialized ContentKey container, which will result in a different
|
||||
# serialization
|
||||
contentKey*: ByteList
|
||||
|
||||
FoundContentMessage* = object
|
||||
enrs*: List[ByteList, 32]
|
||||
|
|
|
@ -176,9 +176,12 @@ proc findNode*(p: PortalProtocol, dst: Node, distances: List[uint16, 256]):
|
|||
# TODO Add nodes validation
|
||||
return await reqResponse[FindNodeMessage, NodesMessage](p, dst, PortalProtocolId, fn)
|
||||
|
||||
# TODO It maybe better to accept bytelist, to not tie network layer to particular
|
||||
# content
|
||||
proc findContent*(p: PortalProtocol, dst: Node, contentKey: ContentKey):
|
||||
Future[PortalResult[FoundContentMessage]] {.async.} =
|
||||
let fc = FindContentMessage(contentKey: contentKey)
|
||||
let encKey = encodeKeyAsList(contentKey)
|
||||
let fc = FindContentMessage(contentKey: encKey)
|
||||
|
||||
trace "Send message request", dstId = dst.id, kind = MessageKind.findcontent
|
||||
return await reqResponse[FindContentMessage, FoundContentMessage](p, dst, PortalProtocolId, fc)
|
||||
|
|
|
@ -113,18 +113,21 @@ suite "Portal Protocol Message Encodings":
|
|||
networkId: 0'u16,
|
||||
contentType: ContentType.Account,
|
||||
nodeHash: nodeHash)
|
||||
fn = FindContentMessage(contentKey: contentKey)
|
||||
|
||||
contentEncoded: ByteList = encodeKeyAsList(contentKey)
|
||||
|
||||
fn = FindContentMessage(contentKey: contentEncoded)
|
||||
|
||||
let encoded = encodeMessage(fn)
|
||||
check encoded.toHex == "050000010000000000000000000000000000000000000000000000000000000000000000"
|
||||
check encoded.toHex == "05040000000000010000000000000000000000000000000000000000000000000000000000000000"
|
||||
|
||||
let decoded = decodeMessage(encoded)
|
||||
check decoded.isOk()
|
||||
|
||||
|
||||
let message = decoded.get()
|
||||
check:
|
||||
message.kind == findcontent
|
||||
message.findcontent.contentKey == contentKey
|
||||
message.findcontent.contentKey == contentEncoded
|
||||
|
||||
test "FoundContent Response - payload":
|
||||
let
|
||||
|
|
Loading…
Reference in New Issue