Skip to content

Commit

Permalink
Fluffy state offer validation (#2170)
Browse files Browse the repository at this point in the history
* Implemented offer validation interface.

* Implement high level offer validation steps.

* Completed state validation tests.

* Update validation to use result type.

* Update state proof verification tests to test offer verification.

* Query history network to get state root by block hash.

* Fix state network test and remove usage of CoreDb.

* Fix state network gossip test and PR comment updates.

* Add trieproof state validation tests and fix for short nodes.
  • Loading branch information
web3-developer committed May 16, 2024
1 parent 8767bbd commit 2891b9a
Show file tree
Hide file tree
Showing 22 changed files with 31,580 additions and 492 deletions.
29 changes: 15 additions & 14 deletions fluffy/fluffy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,6 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
)
streamManager = StreamManager.new(d)

stateNetwork =
if Network.state in config.networks:
Opt.some(
StateNetwork.new(
d,
db,
streamManager,
bootstrapRecords = bootstrapRecords,
portalConfig = portalConfig,
)
)
else:
Opt.none(StateNetwork)

accumulator =
# Building an accumulator from header epoch files takes > 2m30s and is
# thus not really a viable option at start-up.
Expand Down Expand Up @@ -234,6 +220,21 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
else:
Opt.none(HistoryNetwork)

stateNetwork =
if Network.state in config.networks:
Opt.some(
StateNetwork.new(
d,
db,
streamManager,
bootstrapRecords = bootstrapRecords,
portalConfig = portalConfig,
historyNetwork = historyNetwork,
)
)
else:
Opt.none(StateNetwork)

beaconLightClient =
# TODO: Currently disabled by default as it is not sufficiently polished.
# Eventually this should be always-on functionality.
Expand Down
22 changes: 0 additions & 22 deletions fluffy/network/state/experimental/state_proof_generation.nim

This file was deleted.

30 changes: 0 additions & 30 deletions fluffy/network/state/experimental/state_proof_types.nim

This file was deleted.

69 changes: 0 additions & 69 deletions fluffy/network/state/experimental/state_proof_verification.nim

This file was deleted.

41 changes: 30 additions & 11 deletions fluffy/network/state/state_content.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import
nimcrypto/[hash, sha2, keccak],
stew/results,
results,
stint,
eth/common/eth_types,
ssz_serialization,
Expand Down Expand Up @@ -208,16 +208,35 @@ func encode*(content: RetrievalContentValue): seq[byte] =
of contractCode:
SSZ.encode(content.contractCode)

func packNibbles*(nibbles: seq[byte]): Nibbles =
doAssert(nibbles.len() <= MAX_UNPACKED_NIBBLES_LEN, "Can't pack more than 64 nibbles")
func init*(T: type Nibbles, packed: openArray[byte], isEven: bool): T =
doAssert(packed.len() <= MAX_PACKED_NIBBLES_LEN)

if nibbles.len() == 0:
var output = newSeqOfCap[byte](packed.len() + 1)
if isEven:
output.add(0x00)
else:
doAssert(packed.len() > 0)
# set the first nibble to 1 and copy the second nibble from the input
output.add((packed[0] and 0x0F) or 0x10)

let startIdx = if isEven: 0 else: 1
for i in startIdx ..< packed.len():
output.add(packed[i])

Nibbles(output)

func packNibbles*(unpacked: openArray[byte]): Nibbles =
doAssert(
unpacked.len() <= MAX_UNPACKED_NIBBLES_LEN, "Can't pack more than 64 nibbles"
)

if unpacked.len() == 0:
return Nibbles(@[byte(0x00)])

let isEvenLength = nibbles.len() mod 2 == 0
let isEvenLength = unpacked.len() mod 2 == 0

var
output = newSeqOfCap[byte](nibbles.len() div 2 + 1)
output = newSeqOfCap[byte](unpacked.len() div 2 + 1)
highNibble = isEvenLength
currentByte: byte = 0

Expand All @@ -226,7 +245,7 @@ func packNibbles*(nibbles: seq[byte]): Nibbles =
else:
currentByte = 0x10

for i, nibble in nibbles:
for i, nibble in unpacked:
if highNibble:
currentByte = nibble shl 4
else:
Expand All @@ -236,12 +255,12 @@ func packNibbles*(nibbles: seq[byte]): Nibbles =

Nibbles(output)

func unpackNibbles*(nibbles: Nibbles): seq[byte] =
doAssert(nibbles.len() <= MAX_PACKED_NIBBLES_LEN, "Packed nibbles length is too long")
func unpackNibbles*(packed: Nibbles): seq[byte] =
doAssert(packed.len() <= MAX_PACKED_NIBBLES_LEN, "Packed nibbles length is too long")

var output = newSeqOfCap[byte](nibbles.len() * 2)
var output = newSeqOfCap[byte](packed.len() * 2)

for i, pair in nibbles:
for i, pair in packed:
if i == 0 and pair == 0x00:
continue

Expand Down
Loading

0 comments on commit 2891b9a

Please sign in to comment.