2021-06-18 17:20:48 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# https://github.com/ethereum/stateless-ethereum-specs/blob/master/state-network.md#content
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-08-18 07:23:57 +00:00
|
|
|
std/[options, sugar],
|
2021-09-02 12:35:25 +00:00
|
|
|
nimcrypto/[sha2, hash], stew/objects, stint,
|
2021-07-15 13:12:33 +00:00
|
|
|
eth/ssz/ssz_serialization, eth/trie/[hexary, db]
|
2021-07-09 14:15:10 +00:00
|
|
|
|
|
|
|
export ssz_serialization
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ByteList* = List[byte, 2048]
|
|
|
|
|
|
|
|
ContentType* = enum
|
|
|
|
Account = 0x01
|
|
|
|
ContractStorage = 0x02
|
|
|
|
ContractBytecode = 0x03
|
|
|
|
|
|
|
|
NetworkId* = uint16
|
|
|
|
|
2021-07-20 12:04:57 +00:00
|
|
|
NodeHash* = MDigest[32 * 8] # keccak256
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-07-20 12:04:57 +00:00
|
|
|
CodeHash* = MDigest[32 * 8] # keccak256
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-07-20 12:04:57 +00:00
|
|
|
Address* = array[20, byte]
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
ContentKey* = object
|
2021-07-09 14:15:10 +00:00
|
|
|
networkId*: NetworkId
|
|
|
|
contentType*: ContentType
|
2021-06-18 17:20:48 +00:00
|
|
|
# TODO: How shall we deal with the different ContentKey structures?
|
2021-07-09 14:15:10 +00:00
|
|
|
# Lets start with just node hashes for now.
|
|
|
|
# address: Address
|
|
|
|
# triePath: ByteList
|
|
|
|
nodeHash*: NodeHash
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
ContentId* = MDigest[32 * 8]
|
|
|
|
|
2021-07-09 14:15:10 +00:00
|
|
|
KeccakHash* = MDigest[32 * 8] # could also import from either eth common types or trie defs
|
|
|
|
|
|
|
|
template toSszType*(x: ContentType): uint8 =
|
|
|
|
uint8(x)
|
|
|
|
|
2021-06-18 17:20:48 +00:00
|
|
|
template toSszType*(x: auto): auto =
|
2021-07-09 14:15:10 +00:00
|
|
|
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
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-07-09 14:15:10 +00:00
|
|
|
contentType
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-08-18 07:23:57 +00:00
|
|
|
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 =
|
2021-07-13 13:15:33 +00:00
|
|
|
# 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
|
2021-08-18 07:23:57 +00:00
|
|
|
sha2.sha_256.digest(contentKey.asSeq())
|
2021-07-13 13:15:33 +00:00
|
|
|
|
2021-09-02 12:35:25 +00:00
|
|
|
func toContentId*(contentKey: ContentKey): ContentId =
|
|
|
|
toContentId(encodeKeyAsList(contentKey))
|
|
|
|
|
|
|
|
func contentIdAsUint256*(id: ContentId): Uint256 =
|
|
|
|
readUintBE[256](id.data)
|
|
|
|
|
2021-07-13 13:15:33 +00:00
|
|
|
type
|
|
|
|
ContentStorage* = object
|
2021-07-15 13:12:33 +00:00
|
|
|
# TODO: Quick implementation for now where we just use HexaryTrie, current
|
|
|
|
# idea is to move in here a more direct storage of the trie nodes, but have
|
|
|
|
# an `ContentProvider` "interface" that could provide the trie nodes via
|
|
|
|
# this direct storage, via the HexaryTrie (for full nodes), or also without
|
|
|
|
# storage, via json rpc client requesting data from a full eth1 client.
|
|
|
|
trie*: HexaryTrie
|
|
|
|
|
|
|
|
proc getContent*(storage: ContentStorage, key: ContentKey): Option[seq[byte]] =
|
|
|
|
if storage.trie.db == nil: # TODO: for now...
|
|
|
|
return none(seq[byte])
|
2021-07-20 12:04:57 +00:00
|
|
|
let val = storage.trie.db.get(key.nodeHash.data)
|
2021-07-15 13:12:33 +00:00
|
|
|
if val.len > 0:
|
|
|
|
some(val)
|
|
|
|
else:
|
|
|
|
none(seq[byte])
|
2021-08-18 07:23:57 +00:00
|
|
|
|
|
|
|
proc getContent*(storage: ContentStorage, contentKey: ByteList): Option[seq[byte]] =
|
|
|
|
decodeKey(contentKey).flatMap((key: ContentKey) => getContent(storage, key))
|
2021-09-03 08:57:19 +00:00
|
|
|
|
|
|
|
proc newEmptyInMemoryStorage*(): ContentStorage =
|
|
|
|
let trie = initHexaryTrie(newMemoryDb())
|
|
|
|
ContentStorage(trie: trie)
|