diff --git a/beacon_chain/spec/eip7594_helpers.nim b/beacon_chain/spec/eip7594_helpers.nim index f28aa90c2..6c381f802 100644 --- a/beacon_chain/spec/eip7594_helpers.nim +++ b/beacon_chain/spec/eip7594_helpers.nim @@ -26,33 +26,33 @@ type CellBytes = array[eip7594.CELLS_PER_EXT_BLOB, Cell] ProofBytes = array[eip7594.CELLS_PER_EXT_BLOB, KzgProof] -proc sortedColumnIndices*(columnsPerSubnet: ColumnIndex, subnetIds: HashSet[uint64]): seq[ColumnIndex] = +proc sortedColumnIndices*(columnsPerSubnet: ColumnIndex, + subnetIds: HashSet[uint64]): + seq[ColumnIndex] = var res: seq[ColumnIndex] = @[] - for i in 0 ..< columnsPerSubnet: + for i in 0'u64 ..< columnsPerSubnet: for subnetId in subnetIds: let index = DATA_COLUMN_SIDECAR_SUBNET_COUNT * i + subnetId res.add(ColumnIndex(index)) - res.sort() + res.sort res proc sortedColumnIndexList*(columnsPerSubnet: ColumnIndex, - subnetIds: HashSet[uint64]): - List[ColumnIndex, NUMBER_OF_COLUMNS] = + subnetIds: HashSet[uint64]): + List[ColumnIndex, NUMBER_OF_COLUMNS] = var res: seq[ColumnIndex] - list: List[ColumnIndex, NUMBER_OF_COLUMNS] - for i in 0 ..< columnsPerSubnet: + for i in 0'u64 ..< columnsPerSubnet: for subnetId in subnetIds: let index = DATA_COLUMN_SIDECAR_SUBNET_COUNT * i + subnetId res.add(ColumnIndex(index)) res.sort() - for elem in res: - discard list.add(ColumnIndex(elem)) + let list = List[ColumnIndex, NUMBER_OF_COLUMNS].init(res) list -proc get_custody_column_subnet*(node_id: NodeId, - custody_subnet_count: uint64): - Result[HashSet[uint64], cstring] = +proc get_custody_column_subnets*(node_id: NodeId, + custody_subnet_count: uint64): + Result[HashSet[uint64], cstring] = # Decouples the custody subnet computation part from # `get_custody_columns`, in order to later use this subnet list @@ -65,55 +65,55 @@ proc get_custody_column_subnet*(node_id: NodeId, subnet_ids: HashSet[uint64] current_id = node_id - while subnet_ids.len < int(custody_subnet_count): - + while subnet_ids.lenu64 < custody_subnet_count: var - current_id_bytes: array[32, byte] hashed_bytes: array[8, byte] - - current_id_bytes = current_id.toBytesBE() - current_id_bytes.reverse() let + current_id_bytes = current_id.toBytesLE() hashed_current_id = eth2digest(current_id_bytes) hashed_bytes[0..7] = hashed_current_id.data.toOpenArray(0,7) - var subnet_id = bytes_to_uint64(hashed_bytes) mod + let subnet_id = bytes_to_uint64(hashed_bytes) mod DATA_COLUMN_SIDECAR_SUBNET_COUNT - if subnet_id notin subnet_ids: - subnet_ids.incl(subnet_id) + subnet_ids.incl(subnet_id) if current_id == UInt256.high.NodeId: - # Overflow prevention - current_id = NodeId(StUint[256].zero) + # Overflow prevention + current_id = NodeId(StUint[256].zero) current_id += NodeId(StUint[256].one) ok(subnet_ids) - -# https://github.com/ethereum/consensus-specs/blob/5f48840f4d768bf0e0a8156a3ed06ec333589007/specs/_features/eip7594/das-core.md#get_custody_columns +# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/_features/eip7594/das-core.md#get_custody_columns proc get_custody_columns*(node_id: NodeId, custody_subnet_count: uint64): - Result[seq[ColumnIndex], cstring] = - - let subnet_ids = get_custody_column_subnet(node_id, custody_subnet_count).get + seq[ColumnIndex] = + let + subnet_ids = + get_custody_column_subnets(node_id, custody_subnet_count).get + const + columns_per_subnet = + NUMBER_OF_COLUMNS div DATA_COLUMN_SIDECAR_SUBNET_COUNT + + sortedColumnIndices(ColumnIndex(columns_per_subnet), subnet_ids) - # columns_per_subnet = NUMBER_OF_COLUMNS // DATA_COLUMN_SIDECAR_SUBNET_COUNT - let columns_per_subnet = NUMBER_OF_COLUMNS div DATA_COLUMN_SIDECAR_SUBNET_COUNT - - ok(sortedColumnIndices(ColumnIndex(columns_per_subnet), subnet_ids)) proc get_custody_column_list*(node_id: NodeId, custody_subnet_count: uint64): - Result[List[ColumnIndex, NUMBER_OF_COLUMNS], cstring] = - - let subnet_ids = get_custody_column_subnet(node_id, custody_subnet_count).get + List[ColumnIndex, NUMBER_OF_COLUMNS] = - # columns_per_subnet = NUMBER_OF_COLUMNS // DATA_COLUMN_SIDECAR_SUBNET_COUNT - let columns_per_subnet = NUMBER_OF_COLUMNS div DATA_COLUMN_SIDECAR_SUBNET_COUNT + # Not in spec in the exact format, but it is useful in sorting custody columns + # before sending, data_column_sidecars_by_range requests + let + subnet_ids = + get_custody_column_subnets(node_id, custody_subnet_count).get + const + columns_per_subnet = + NUMBER_OF_COLUMNS div DATA_COLUMN_SIDECAR_SUBNET_COUNT - ok(sortedColumnIndexList(ColumnIndex(columns_per_subnet), subnet_ids)) + sortedColumnIndexList(ColumnIndex(columns_per_subnet), subnet_ids) # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/_features/eip7594/das-core.md#compute_extended_matrix proc compute_extended_matrix* (blobs: seq[KzgBlob]): Result[seq[MatrixEntry], cstring] = diff --git a/beacon_chain/sync/request_manager.nim b/beacon_chain/sync/request_manager.nim index f401565c3..aef4c7090 100644 --- a/beacon_chain/sync/request_manager.nim +++ b/beacon_chain/sync/request_manager.nim @@ -282,7 +282,7 @@ proc constructValidCustodyPeers(rman: RequestManager, let localNodeId = rman.network.nodeId localCustodyColumns = - localNodeId.get_custody_columns(localCustodySubnetCount).get + localNodeId.get_custody_columns(localCustodySubnetCount) var validPeers: seq[Peer] @@ -296,7 +296,7 @@ proc constructValidCustodyPeers(rman: RequestManager, let remoteNodeId = getNodeIdFromPeer(peer) remoteCustodyColumns = - remoteNodeId.get_custody_columns(remoteCustodySubnetCount).get + remoteNodeId.get_custody_columns(remoteCustodySubnetCount) # If the remote peer custodies less columns than # our local node diff --git a/beacon_chain/validators/message_router.nim b/beacon_chain/validators/message_router.nim index 716b53d96..2e8505a1c 100644 --- a/beacon_chain/validators/message_router.nim +++ b/beacon_chain/validators/message_router.nim @@ -213,7 +213,7 @@ proc routeSignedBeaconBlock*( custody_columns = router[].network.nodeId.get_custody_columns(metadata) for dc in data_columns: - if dc.index in custody_columns.get: + if dc.index in custody_columns: let dataColumnRefs = Opt.some(dataColumnsOpt[].get().mapIt(newClone(it))) let added = await router[].blockProcessor[].addBlock( diff --git a/tests/consensus_spec/test_fixture_networking b/tests/consensus_spec/test_fixture_networking deleted file mode 100755 index 99b1fc0fc..000000000 Binary files a/tests/consensus_spec/test_fixture_networking and /dev/null differ diff --git a/tests/consensus_spec/test_fixture_networking.nim b/tests/consensus_spec/test_fixture_networking.nim index b82020118..9a8c11b77 100644 --- a/tests/consensus_spec/test_fixture_networking.nim +++ b/tests/consensus_spec/test_fixture_networking.nim @@ -11,35 +11,14 @@ import std/[json, streams], yaml, - kzg4844/kzg_ex, + kzg4844/[kzg, kzg_abi], stint, - chronicles, eth/p2p/discoveryv5/[node], - stew/[byteutils, results], ../../beacon_chain/spec/eip7594_helpers, ../testutil, ./fixtures_utils, ./os_ops -from std/sequtils import anyIt, mapIt, toSeq -from std/strutils import rsplit - -func toUInt64(s: SomeInteger): Opt[uint64] = - if s < 0: - return Opt.none uint64 - try: - Opt.some uint64(s) - except ValueError: - Opt.none uint64 - -func fromHex[N: static int](s: string): Opt[array[N, byte]] = - if s.len != 2*(N+1): - # 0x prefix - return Opt.none array[N, byte] - - try: - Opt.some fromHex(array[N, byte], s) - except ValueError: - Opt.none array[N, byte] +from std/sequtils import mapIt proc runGetCustodyColumns(suiteName, path: string) = let relativePathComponent = path.relativeTestPathComponent() @@ -47,7 +26,7 @@ proc runGetCustodyColumns(suiteName, path: string) = type TestMetaYaml = object node_id: string custody_subnet_count: uint64 - result: Option[seq[uint64]] + result: seq[uint64] let meta = block: var s = openFileStream(path/"meta.yaml") @@ -56,20 +35,13 @@ proc runGetCustodyColumns(suiteName, path: string) = yaml.load(s, res) res node_id = UInt256.fromDecimal(meta.node_id) - custody_subnet_count = toUInt64(meta.custody_subnet_count) - reslt = (meta.result.get).mapIt(uint64(it)) + custody_subnet_count = meta.custody_subnet_count + reslt = (meta.result).mapIt(it) - if custody_subnet_count.isNone: - check meta.result.isNone - else: - let columns = get_custody_columns(node_id, custody_subnet_count.get) - if columns.isErr: - check meta.result.isNone - else: - var count = 0 - for column in columns.get: - check column == uint64(reslt[count]) - count = count + 1 + let columns = get_custody_columns(node_id, custody_subnet_count) + + for i in 0..