Skip to content

Commit

Permalink
Move cliqueSealBlock functionality into standalone constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
scorbajio committed Aug 8, 2024
1 parent c7dea57 commit 05a84b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
41 changes: 40 additions & 1 deletion packages/block/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createTxFromTxData,
} from '@ethereumjs/tx'
import {
BIGINT_27,
CLRequestFactory,
ConsolidationRequest,
DepositRequest,
Expand All @@ -16,6 +17,8 @@ import {
bigIntToHex,
bytesToHex,
bytesToUtf8,
concatBytes,
ecsign,
equalsBytes,
fetchFromProvider,
getProvider,
Expand All @@ -24,6 +27,7 @@ import {
isHexString,
} from '@ethereumjs/util'

import { _requireClique } from './consensus/clique.js'
import { createBlockFromRpc } from './from-rpc.js'
import {
genRequestsTrieRoot,
Expand All @@ -32,7 +36,14 @@ import {
valuesArrayToHeaderData,
} from './helpers.js'

import { Block, BlockHeader, executionPayloadFromBeaconPayload } from './index.js'
import {
Block,
BlockHeader,
CLIQUE_EXTRA_SEAL,
CLIQUE_EXTRA_VANITY,
cliqueSigHash,
executionPayloadFromBeaconPayload,
} from './index.js'

import type { BeaconPayloadJson } from './from-beacon-payload.js'
import type {
Expand Down Expand Up @@ -515,3 +526,31 @@ export async function createBlockFromBeaconPayloadJson(
const executionPayload = executionPayloadFromBeaconPayload(payload)
return createBlockFromExecutionPayload(executionPayload, opts)
}

/**
* Method to retrieve a sealed block from the header and clique signer key
* @param header block header to be used for creating sealed block
* @param cliqueSigner clique signer key to be used for creating sealed block
* @returns the block constructed block
*/
export function createSealedCliqueBlock(header: BlockHeader, cliqueSigner: Uint8Array): Uint8Array {
// Ensure extraData is at least length CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
const minExtraDataLength = CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
if (header.extraData.length < minExtraDataLength) {
const remainingLength = minExtraDataLength - header.extraData.length
;(header.extraData as any) = concatBytes(header.extraData, new Uint8Array(remainingLength))
}

_requireClique(header, 'cliqueSealBlock')

const ecSignFunction = header.common.customCrypto?.ecsign ?? ecsign
const signature = ecSignFunction(cliqueSigHash(header), cliqueSigner)
const signatureB = concatBytes(signature.r, signature.s, bigIntToBytes(signature.v - BIGINT_27))

const extraDataWithoutSeal = header.extraData.subarray(
0,
header.extraData.length - CLIQUE_EXTRA_SEAL,
)
const extraData = concatBytes(extraDataWithoutSeal, signatureB)
return extraData
}
37 changes: 2 additions & 35 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import {
BIGINT_0,
BIGINT_1,
BIGINT_2,
BIGINT_27,
BIGINT_7,
KECCAK256_RLP,
KECCAK256_RLP_ARRAY,
TypeOutput,
bigIntToBytes,
bigIntToHex,
bigIntToUnpaddedBytes,
bytesToHex,
bytesToUtf8,
concatBytes,
createZeroAddress,
ecsign,
equalsBytes,
hexToBytes,
toType,
Expand All @@ -33,7 +29,7 @@ import {
CLIQUE_EXTRA_SEAL,
CLIQUE_EXTRA_VANITY,
cliqueIsEpochTransition,
cliqueSigHash,
createSealedCliqueBlock,
} from './index.js'

import type { BlockHeaderBytes, BlockOptions, HeaderData, JsonHeader } from './types.js'
Expand Down Expand Up @@ -251,16 +247,7 @@ export class BlockHeader {
}

// If cliqueSigner is provided, seal block with provided privateKey.
if (opts.cliqueSigner) {
// Ensure extraData is at least length CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
const minExtraDataLength = CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
if (this.extraData.length < minExtraDataLength) {
const remainingLength = minExtraDataLength - this.extraData.length
this.extraData = concatBytes(this.extraData, new Uint8Array(remainingLength))
}

this.extraData = this.cliqueSealBlock(opts.cliqueSigner)
}
if (opts.cliqueSigner) this.extraData = createSealedCliqueBlock(this, opts.cliqueSigner)

Check failure on line 250 in packages/block/src/header.ts

View workflow job for this annotation

GitHub Actions / test-block (18)

test/clique.spec.ts > [Header]: Clique PoA Functionality > Signing

TypeError: createSealedCliqueBlock is not a function ❯ new BlockHeader src/header.ts:250:45 ❯ Module.createBlockHeader src/constructors.ts:77:10 ❯ test/clique.spec.ts:100:18

// Validate consensus format after block is sealed (if applicable) so extraData checks will pass
if (skipValidateConsensusFormat === false) this._consensusFormatValidation()
Expand Down Expand Up @@ -745,26 +732,6 @@ export class BlockHeader {
return dif
}

/**
* Seal block with the provided signer.
* Returns the final extraData field to be assigned to `this.extraData`.
* @hidden
*/
private cliqueSealBlock(privateKey: Uint8Array) {
_requireClique(this, 'cliqueSealBlock')

const ecSignFunction = this.common.customCrypto?.ecsign ?? ecsign
const signature = ecSignFunction(cliqueSigHash(this), privateKey)
const signatureB = concatBytes(signature.r, signature.s, bigIntToBytes(signature.v - BIGINT_27))

const extraDataWithoutSeal = this.extraData.subarray(
0,
this.extraData.length - CLIQUE_EXTRA_SEAL,
)
const extraData = concatBytes(extraDataWithoutSeal, signatureB)
return extraData
}

/**
* Returns the rlp encoding of the block header.
*/
Expand Down

0 comments on commit 05a84b5

Please sign in to comment.