-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add data column to types repo and network
- Loading branch information
Showing
23 changed files
with
510 additions
and
10 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/beacon-node/src/chain/errors/dataColumnSidecarError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {Slot} from "@lodestar/types"; | ||
import {GossipActionError} from "./gossipValidation.js"; | ||
|
||
export enum DataColumnSidecarErrorCode { | ||
INVALID_INDEX = "BLOB_SIDECAR_ERROR_INVALID_INDEX", | ||
|
||
// following errors are adapted from the block errors | ||
FUTURE_SLOT = "BLOB_SIDECAR_ERROR_FUTURE_SLOT", | ||
} | ||
|
||
export type DataColumnSidecarErrorType = | ||
| {code: DataColumnSidecarErrorCode.INVALID_INDEX; columnIndex: number; gossipIndex: number} | ||
| {code: DataColumnSidecarErrorCode.FUTURE_SLOT; blockSlot: Slot; currentSlot: Slot}; | ||
|
||
export class DataColumnSidecarGossipError extends GossipActionError<DataColumnSidecarErrorType> {} |
51 changes: 51 additions & 0 deletions
51
packages/beacon-node/src/chain/validation/dataColumnSidecar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH, | ||
KZG_COMMITMENTS_SUBTREE_INDEX, | ||
DATA_COLUMN_SIDECAR_SUBNET_COUNT, | ||
} from "lodestar/params"; | ||
import {electra, ssz} from "@lodestar/types"; | ||
import {verifyMerkleBranch} from "@lodestar/utils"; | ||
|
||
import {DataColumnSidecarGossipError, DataColumnSidecarErrorCode} from "../errors/dataColumnSidecarError.js"; | ||
import {GossipAction} from "../errors/gossipValidation.js"; | ||
import {IBeaconChain} from "../interface.js"; | ||
|
||
export async function validateGossipDataColumnSidecar( | ||
chain: IBeaconChain, | ||
dataColumnSideCar: electra.DataColumnSidecar, | ||
gossipIndex: number | ||
): Promise<void> { | ||
const dataColumnSlot = dataColumnSideCar.signedBlockHeader.message.slot; | ||
|
||
if (dataColumnSideCar.index % DATA_COLUMN_SIDECAR_SUBNET_COUNT !== gossipIndex) { | ||
throw new DataColumnSidecarGossipError(GossipAction.REJECT, { | ||
code: DataColumnSidecarErrorCode.INVALID_INDEX, | ||
columnIndex: dataColumnSideCar.index, | ||
gossipIndex, | ||
}); | ||
} | ||
|
||
// [IGNORE] The sidecar is not from a future slot (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- | ||
// i.e. validate that sidecar.slot <= current_slot (a client MAY queue future blocks for processing at | ||
// the appropriate slot). | ||
const currentSlotWithGossipDisparity = chain.clock.currentSlotWithGossipDisparity; | ||
if (currentSlotWithGossipDisparity < dataColumnSlot) { | ||
throw new DataColumnSidecarGossipError(GossipAction.IGNORE, { | ||
code: DataColumnSidecarErrorCode.FUTURE_SLOT, | ||
currentSlot: currentSlotWithGossipDisparity, | ||
blockSlot: dataColumnSlot, | ||
}); | ||
} | ||
|
||
validateInclusionProof(dataColumnSideCar); | ||
} | ||
|
||
function validateInclusionProof(dataColumnSideCar: electra.DataColumnSidecar): boolean { | ||
return verifyMerkleBranch( | ||
ssz.deneb.BlobKzgCommitments.hashTreeRoot(dataColumnSideCar.kzgCommitments), | ||
dataColumnSideCar.kzgCommitmentsInclusionProof, | ||
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH, | ||
KZG_COMMITMENTS_SUBTREE_INDEX, | ||
dataColumnSideCar.signedBlockHeader.message.bodyRoot | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/beacon-node/src/db/repositories/dataColumnSidecars.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {ValueOf, ContainerType, ByteVectorType} from "@chainsafe/ssz"; | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {Db, Repository} from "@lodestar/db"; | ||
import {ssz} from "@lodestar/types"; | ||
import {MAX_BLOB_COMMITMENTS_PER_BLOCK, NUMBER_OF_COLUMNS} from "@lodestar/params"; | ||
|
||
import {Bucket, getBucketNameByValue} from "../buckets.js"; | ||
|
||
export const dataColumnSidecarsWrapperSsz = new ContainerType( | ||
{ | ||
blockRoot: ssz.Root, | ||
slot: ssz.Slot, | ||
columnsSize: ssz.Uint8, | ||
// each byte[i] tells what index (1 based) the column i is stored, 0 means not custodied | ||
custodyColumns: new ByteVectorType(NUMBER_OF_COLUMNS), | ||
dataColumnSidecars: ssz.electra.DataColumnSidecars, | ||
}, | ||
{typeName: "DataColumnSidecarsWrapper", jsonCase: "eth2"} | ||
); | ||
|
||
export type DataColumnSidecarsWrapper = ValueOf<typeof dataColumnSidecarsWrapperSsz>; | ||
export const COLUMN_SIZE_IN_WRAPPER_INDEX = 44; | ||
export const CUSTODY_COLUMNS_IN_IN_WRAPPER_INDEX = 45; | ||
export const DATA_COLUMN_SIDECARS_IN_WRAPPER_INDEX = 46 + MAX_BLOB_COMMITMENTS_PER_BLOCK; | ||
|
||
/** | ||
* dataColumnSidecarsWrapper by block root (= hash_tree_root(SignedBeaconBlock.message)) | ||
* | ||
* Used to store unfinalized DataColumnSidecars | ||
*/ | ||
export class DataColumnSidecarsRepository extends Repository<Uint8Array, DataColumnSidecarsWrapper> { | ||
constructor(config: ChainForkConfig, db: Db) { | ||
const bucket = Bucket.allForks_dataColumnSidecars; | ||
super(config, db, bucket, dataColumnSidecarsWrapperSsz, getBucketNameByValue(bucket)); | ||
} | ||
|
||
/** | ||
* Id is hashTreeRoot of unsigned BeaconBlock | ||
*/ | ||
getId(value: DataColumnSidecarsWrapper): Uint8Array { | ||
const {blockRoot} = value; | ||
return blockRoot; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/beacon-node/src/db/repositories/dataColumnSidecarsArchive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {Db, Repository} from "@lodestar/db"; | ||
import {Slot} from "@lodestar/types"; | ||
import {bytesToInt} from "@lodestar/utils"; | ||
import {Bucket, getBucketNameByValue} from "../buckets.js"; | ||
import {dataColumnSidecarsWrapperSsz, DataColumnSidecarsWrapper} from "./dataColumnSidecars.js"; | ||
|
||
/** | ||
* dataColumnSidecarsWrapper by slot | ||
* | ||
* Used to store finalized DataColumnSidecars | ||
*/ | ||
export class BlobSidecarsArchiveRepository extends Repository<Slot, DataColumnSidecarsWrapper> { | ||
constructor(config: ChainForkConfig, db: Db) { | ||
const bucket = Bucket.allForks_dataColumnSidecarsArchive; | ||
super(config, db, bucket, dataColumnSidecarsWrapperSsz, getBucketNameByValue(bucket)); | ||
} | ||
|
||
// Handle key as slot | ||
|
||
getId(value: DataColumnSidecarsWrapper): Slot { | ||
return value.slot; | ||
} | ||
|
||
decodeKey(data: Uint8Array): number { | ||
return bytesToInt(super.decodeKey(data) as unknown as Uint8Array, "be"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.