Add/update ssz calls for fluffy (#750)

This commit is contained in:
Kim De Mey 2021-07-09 16:15:10 +02:00 committed by GitHub
parent 424fd405e9
commit c7569c2247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 15 deletions

View File

@ -10,7 +10,9 @@
{.push raises: [Defect].} {.push raises: [Defect].}
import import
nimcrypto/[sha2, hash], eth/ssz/ssz_serialization nimcrypto/[sha2, hash], stew/objects, eth/ssz/ssz_serialization
export ssz_serialization
type type
ByteList* = List[byte, 2048] ByteList* = List[byte, 2048]
@ -22,27 +24,41 @@ type
NetworkId* = uint16 NetworkId* = uint16
NodeHash* = List[byte, 32] # MDigest[32 * 8] - sha256 NodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256
CodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256 CodeHash* = List[byte, 32] # MDigest[32 * 8] - keccak256
Address* = List[byte, 20] Address* = List[byte, 20]
ContentKey* = object ContentKey* = object
networkId*: NetworkId
contentType*: ContentType
# TODO: How shall we deal with the different ContentKey structures? # TODO: How shall we deal with the different ContentKey structures?
networkId: NetworkId # Lets start with just node hashes for now.
contentType: ContentType # address: Address
address: Address # triePath: ByteList
triePath: ByteList nodeHash*: NodeHash
nodeHash: NodeHash
ContentId* = MDigest[32 * 8] ContentId* = MDigest[32 * 8]
template toSszType*(x: auto): auto = KeccakHash* = MDigest[32 * 8] # could also import from either eth common types or trie defs
mixin toSszType
when x is ContentType: uint8(x) template toSszType*(x: ContentType): uint8 =
else: x uint8(x)
template toSszType*(x: auto): auto =
x
func fromSszBytes*(T: type ContentType, data: openArray[byte]):
T {.raises: [MalformedSszError, Defect].} =
if data.len != sizeof(uint8):
raiseIncorrectSize T
var contentType: T
if not checkedEnumAssign(contentType, data[0]):
raiseIncorrectSize T
contentType
func toContentId*(contentKey: ContentKey): ContentId = func toContentId*(contentKey: ContentKey): ContentId =
sha2.sha_256.digest(SSZ.encode(contentKey)) sha2.sha_256.digest(SSZ.encode(contentKey))

View File

@ -101,11 +101,11 @@ template messageKind*(T: typedesc[SomeMessage]): MessageKind =
elif T is AdvertiseMessage: advertise elif T is AdvertiseMessage: advertise
elif T is RequestProofsMessage: requestproofs elif T is RequestProofsMessage: requestproofs
template toSszType*(x: auto): auto = template toSszType*(x: UInt256): array[32, byte] =
mixin toSszType toBytesLE(x)
when x is UInt256: toBytesLE(x) template toSszType*(x: auto): auto =
else: x x
func fromSszBytes*(T: type UInt256, data: openArray[byte]): func fromSszBytes*(T: type UInt256, data: openArray[byte]):
T {.raises: [MalformedSszError, Defect].} = T {.raises: [MalformedSszError, Defect].} =