move sampler to own dir

This commit is contained in:
Dmitriy Ryajov 2024-01-15 13:59:55 -06:00
parent 877e19cfcf
commit 76181407e9
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
6 changed files with 48 additions and 114 deletions

View File

@ -1,13 +0,0 @@
func floorLog2* (x : int) : int =
var k = -1
var y = x
while (y > 0):
k += 1
y = y shr 1
return k
func ceilingLog2* (x : int) : int =
if (x==0):
return -1
else:
return (floorLog2(x-1) + 1)

View File

@ -1,70 +0,0 @@
import std/bitops
import std/sugar
import pkg/chronos
import pkg/chronicles
import pkg/libp2p
import pkg/questionable
import pkg/questionable/results
import ../contracts/requests
import ../stores/blockstore
import ../manifest
type
SlotBlocks* = ref object of RootObj
slot: Slot
blockStore: BlockStore
manifest: Manifest
proc getManifestForSlot(slot: Slot, blockStore: BlockStore): Future[?!Manifest] {.async.} =
without manifestBlockCid =? Cid.init(slot.request.content.cid).mapFailure, err:
error "Unable to init CID from slot.content.cid"
return failure err
without manifestBlock =? await blockStore.getBlock(manifestBlockCid), err:
error "Failed to fetch manifest block", cid = manifestBlockCid
return failure err
without manifest =? Manifest.decode(manifestBlock):
error "Unable to decode manifest"
return failure("Unable to decode manifest")
return success(manifest)
proc new*(
T: type SlotBlocks,
slot: Slot,
blockStore: BlockStore
): Future[?!SlotBlocks] {.async.} =
# Create a SlotBlocks object for a slot.
# SlotBlocks lets you get the manifest of a slot and blocks by slotBlockIndex for a slot.
without manifest =? await getManifestForSlot(slot, blockStore):
error "Failed to get manifest for slot"
return failure("Failed to get manifest for slot")
success(SlotBlocks(
slot: slot,
blockStore: blockStore,
manifest: manifest
))
proc manifest*(self: SlotBlocks): Manifest =
self.manifest
proc getDatasetBlockIndexForSlotBlockIndex*(self: SlotBlocks, slotBlockIndex: uint64): uint64 =
let
slotSize = self.slot.request.ask.slotSize.truncate(uint64)
blocksInSlot = slotSize div self.manifest.blockSize.uint64
datasetSlotIndex = self.slot.slotIndex.truncate(uint64)
return (datasetSlotIndex * blocksInSlot) + slotBlockIndex
proc getSlotBlock*(self: SlotBlocks, slotBlockIndex: uint64): Future[?!Block] {.async.} =
let
blocksInManifest = (self.manifest.datasetSize div self.manifest.blockSize).uint64
datasetBlockIndex = self.getDatasetBlockIndexForSlotBlockIndex(slotBlockIndex)
if datasetBlockIndex >= blocksInManifest:
return failure("Found datasetBlockIndex that is out-of-range: " & $datasetBlockIndex)
return await self.blockStore.getBlock(self.manifest.treeCid, datasetBlockIndex)

5
codex/slots/sampler.nim Normal file
View File

@ -0,0 +1,5 @@
import ./sampler/sampler
import ./sampler/types
import ./sampler/utils
export sampler, types, utils

View File

@ -32,34 +32,16 @@ type
DataSampler* = ref object of RootObj
slot: Slot
blockStore: BlockStore
slotBlocks: SlotBlocks
# The following data is invariant over time for a given slot:
datasetRoot: FieldElement
slotRootHash: FieldElement
slotPoseidonTree: MerkleTree
datasetToSlotProof: MerkleProof
blockSize: uint64
numberOfCellsInSlot: uint64
datasetSlotIndex: uint64
numberOfCellsPerBlock: uint64
proc getNumberOfCellsInSlot*(slot: Slot): uint64 =
(slot.request.ask.slotSize.truncate(uint64) div CellSize)
builder: SlotsBuilder
proc new*(
T: type DataSampler,
slot: Slot,
blockStore: BlockStore,
datasetRoot: FieldElement,
slotPoseidonTree: MerkleTree,
datasetToSlotProof: MerkleProof
): Future[?!DataSampler] {.async.} =
builder: SlotsBuilder): Future[?!DataSampler] {.async.} =
# Create a DataSampler for a slot.
# A DataSampler can create the input required for the proving circuit.
without slotBlocks =? await SlotBlocks.new(slot, blockStore), err:
error "Failed to create SlotBlocks object for slot"
return failure(err)
let
numberOfCellsInSlot = getNumberOfCellsInSlot(slot)
blockSize = slotBlocks.manifest.blockSize.uint64
@ -78,18 +60,22 @@ proc new*(
numberOfCellsPerBlock: blockSize div CellSize
))
func extractLowBits*[n: static int](A: BigInt[n], k: int): uint64 =
assert(k > 0 and k <= 64)
var r: uint64 = 0
for i in 0..<k:
# A is big-endian. Run index backwards: n-1-i
#let b = bit[n](A, n-1-i)
let b = bit[n](A, i)
proc getDatasetBlockIndexForSlotBlockIndex*(self: DataSampler, slotBlockIndex: uint64): uint64 =
let
slotSize = self.slot.request.ask.slotSize.truncate(uint64)
blocksInSlot = slotSize div self.manifest.blockSize.uint64
datasetSlotIndex = self.slot.slotIndex.truncate(uint64)
return (datasetSlotIndex * blocksInSlot) + slotBlockIndex
let y = uint64(b)
if (y != 0):
r = bitor(r, 1'u64 shl i)
return r
proc getSlotBlock*(self: DataSampler, slotBlockIndex: uint64): Future[?!Block] {.async.} =
let
blocksInManifest = (self.manifest.datasetSize div self.manifest.blockSize).uint64
datasetBlockIndex = self.getDatasetBlockIndexForSlotBlockIndex(slotBlockIndex)
if datasetBlockIndex >= blocksInManifest:
return failure("Found datasetBlockIndex that is out-of-range: " & $datasetBlockIndex)
return await self.blockStore.getBlock(self.manifest.treeCid, datasetBlockIndex)
proc convertToSlotCellIndex(self: DataSampler, fe: FieldElement): uint64 =
let

View File

@ -0,0 +1,26 @@
func extractLowBits*[n: static int](A: BigInt[n], k: int): uint64 =
assert(k > 0 and k <= 64)
var r: uint64 = 0
for i in 0..<k:
# A is big-endian. Run index backwards: n-1-i
#let b = bit[n](A, n-1-i)
let b = bit[n](A, i)
let y = uint64(b)
if (y != 0):
r = bitor(r, 1'u64 shl i)
return r
func floorLog2* (x : int) : int =
var k = -1
var y = x
while (y > 0):
k += 1
y = y shr 1
return k
func ceilingLog2* (x : int) : int =
if (x==0):
return -1
else:
return (floorLog2(x-1) + 1)