Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename datagas to blobgas - eip 4844 pr 7354 #2919

Merged
merged 10 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ export class Block {
*/
getTransactionsValidationErrors(): string[] {
const errors: string[] = []
let dataGasUsed = BigInt(0)
const dataGasLimit = this.common.param('gasConfig', 'maxDataGasPerBlock')
const dataGasPerBlob = this.common.param('gasConfig', 'dataGasPerBlob')
let blobGasUsed = BigInt(0)
const blobGasLimit = this.common.param('gasConfig', 'maxblobGasPerBlock')
const blobGasPerBlob = this.common.param('gasConfig', 'blobGasPerBlob')

// eslint-disable-next-line prefer-const
for (let [i, tx] of this.transactions.entries()) {
Expand All @@ -490,10 +490,10 @@ export class Block {
}
if (this.common.isActivatedEIP(4844) === true) {
if (tx instanceof BlobEIP4844Transaction) {
dataGasUsed += BigInt(tx.numBlobs()) * dataGasPerBlob
if (dataGasUsed > dataGasLimit) {
blobGasUsed += BigInt(tx.numBlobs()) * blobGasPerBlob
if (blobGasUsed > blobGasLimit) {
errs.push(
`tx causes total data gas of ${dataGasUsed} to exceed maximum data gas per block of ${dataGasLimit}`
`tx causes total blob gas of ${blobGasUsed} to exceed maximum blob gas per block of ${blobGasLimit}`
)
}
}
Expand All @@ -504,8 +504,8 @@ export class Block {
}

if (this.common.isActivatedEIP(4844) === true) {
if (dataGasUsed !== this.header.dataGasUsed) {
errors.push(`invalid dataGasUsed expected=${this.header.dataGasUsed} actual=${dataGasUsed}`)
if (blobGasUsed !== this.header.blobGasUsed) {
errors.push(`invalid blobGasUsed expected=${this.header.blobGasUsed} actual=${blobGasUsed}`)
}
}

Expand Down Expand Up @@ -559,48 +559,48 @@ export class Block {
}

/**
* Validates that data gas fee for each transaction is greater than or equal to the
* dataGasPrice for the block and that total data gas in block is less than maximum
* data gas per block
* Validates that blob gas fee for each transaction is greater than or equal to the
* blobGasPrice for the block and that total blob gas in block is less than maximum
* blob gas per block
* @param parentHeader header of parent block
*/
validateBlobTransactions(parentHeader: BlockHeader) {
if (this.common.isActivatedEIP(4844)) {
const dataGasLimit = this.common.param('gasConfig', 'maxDataGasPerBlock')
const dataGasPerBlob = this.common.param('gasConfig', 'dataGasPerBlob')
let dataGasUsed = BigInt(0)
const blobGasLimit = this.common.param('gasConfig', 'maxblobGasPerBlock')
const blobGasPerBlob = this.common.param('gasConfig', 'blobGasPerBlob')
let blobGasUsed = BigInt(0)

for (const tx of this.transactions) {
if (tx instanceof BlobEIP4844Transaction) {
const dataGasPrice = this.header.getDataGasPrice()
if (tx.maxFeePerDataGas < dataGasPrice) {
const blobGasPrice = this.header.getblobGasPrice()
if (tx.maxFeePerblobGas < blobGasPrice) {
throw new Error(
`blob transaction maxFeePerDataGas ${
tx.maxFeePerDataGas
} < than block data gas price ${dataGasPrice} - ${this.errorStr()}`
`blob transaction maxFeePerblobGas ${
tx.maxFeePerblobGas
} < than block blob gas price ${blobGasPrice} - ${this.errorStr()}`
)
}

dataGasUsed += BigInt(tx.versionedHashes.length) * dataGasPerBlob
blobGasUsed += BigInt(tx.versionedHashes.length) * blobGasPerBlob

if (dataGasUsed > dataGasLimit) {
if (blobGasUsed > blobGasLimit) {
throw new Error(
`tx causes total data gas of ${dataGasUsed} to exceed maximum data gas per block of ${dataGasLimit}`
`tx causes total blob gas of ${blobGasUsed} to exceed maximum blob gas per block of ${blobGasLimit}`
)
}
}
}

if (this.header.dataGasUsed !== dataGasUsed) {
if (this.header.blobGasUsed !== blobGasUsed) {
throw new Error(
`block dataGasUsed mismatch: have ${this.header.dataGasUsed}, want ${dataGasUsed}`
`block blobGasUsed mismatch: have ${this.header.blobGasUsed}, want ${blobGasUsed}`
)
}

const expectedExcessDataGas = parentHeader.calcNextExcessDataGas()
if (this.header.excessDataGas !== expectedExcessDataGas) {
const expectedExcessBlobGas = parentHeader.calcNextExcessBlobGas()
if (this.header.excessBlobGas !== expectedExcessBlobGas) {
throw new Error(
`block excessDataGas mismatch: have ${this.header.excessDataGas}, want ${expectedExcessDataGas}`
`block excessBlobGas mismatch: have ${this.header.excessBlobGas}, want ${expectedExcessBlobGas}`
)
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/block/src/from-beacon-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export type BeaconPayloadJson = {
block_hash: string
transactions: string[]
withdrawals?: BeaconWithdrawal[]
data_gas_used?: string
excess_data_gas?: string
blob_gas_used?: string
excess_blob_gas?: string
parent_beacon_block_root?: string
}

Expand Down Expand Up @@ -63,11 +63,11 @@ export function executionPayloadFromBeaconPayload(payload: BeaconPayloadJson): E
}))
}

if (payload.data_gas_used !== undefined && payload.data_gas_used !== null) {
executionPayload.dataGasUsed = bigIntToHex(BigInt(payload.data_gas_used))
if (payload.blob_gas_used !== undefined && payload.blob_gas_used !== null) {
executionPayload.blobGasUsed = bigIntToHex(BigInt(payload.blob_gas_used))
}
if (payload.excess_data_gas !== undefined && payload.excess_data_gas !== null) {
executionPayload.excessDataGas = bigIntToHex(BigInt(payload.excess_data_gas))
if (payload.excess_blob_gas !== undefined && payload.excess_blob_gas !== null) {
executionPayload.excessBlobGas = bigIntToHex(BigInt(payload.excess_blob_gas))
}
if (payload.parent_beacon_block_root !== undefined && payload.parent_beacon_block_root !== null) {
executionPayload.parentBeaconBlockRoot = payload.parent_beacon_block_root
Expand Down
8 changes: 4 additions & 4 deletions packages/block/src/header-from-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function blockHeaderFromRpc(blockParams: JsonRpcBlock, options?: BlockOpt
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
blobGasUsed,
excessBlobGas,
} = blockParams

const blockHeader = BlockHeader.fromHeaderData(
Expand All @@ -51,8 +51,8 @@ export function blockHeaderFromRpc(blockParams: JsonRpcBlock, options?: BlockOpt
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
blobGasUsed,
excessBlobGas,
},
options
)
Expand Down
86 changes: 43 additions & 43 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class BlockHeader {
public readonly nonce: Uint8Array
public readonly baseFeePerGas?: bigint
public readonly withdrawalsRoot?: Uint8Array
public readonly dataGasUsed?: bigint
public readonly excessDataGas?: bigint
public readonly blobGasUsed?: bigint
public readonly excessBlobGas?: bigint
public readonly parentBeaconBlockRoot?: Uint8Array

public readonly common: Common
Expand Down Expand Up @@ -109,7 +109,7 @@ export class BlockHeader {
*/
public static fromValuesArray(values: BlockHeaderBytes, opts: BlockOptions = {}) {
const headerData = valuesArrayToHeaderData(values)
const { number, baseFeePerGas, excessDataGas, dataGasUsed, parentBeaconBlockRoot } = headerData
const { number, baseFeePerGas, excessBlobGas, blobGasUsed, parentBeaconBlockRoot } = headerData
const header = BlockHeader.fromHeaderData(headerData, opts)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (header.common.isActivatedEIP(1559) && baseFeePerGas === undefined) {
Expand All @@ -121,10 +121,10 @@ export class BlockHeader {
}
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (header.common.isActivatedEIP(4844)) {
if (excessDataGas === undefined) {
throw new Error('invalid header. excessDataGas should be provided')
} else if (dataGasUsed === undefined) {
throw new Error('invalid header. dataGasUsed should be provided')
if (excessBlobGas === undefined) {
throw new Error('invalid header. excessBlobGas should be provided')
} else if (blobGasUsed === undefined) {
throw new Error('invalid header. blobGasUsed should be provided')
}
}
if (header.common.isActivatedEIP(4788) && parentBeaconBlockRoot === undefined) {
Expand Down Expand Up @@ -210,19 +210,19 @@ export class BlockHeader {
: BigInt(7)
: undefined,
withdrawalsRoot: this.common.isActivatedEIP(4895) ? KECCAK256_RLP : undefined,
dataGasUsed: this.common.isActivatedEIP(4844) ? BigInt(0) : undefined,
excessDataGas: this.common.isActivatedEIP(4844) ? BigInt(0) : undefined,
blobGasUsed: this.common.isActivatedEIP(4844) ? BigInt(0) : undefined,
excessBlobGas: this.common.isActivatedEIP(4844) ? BigInt(0) : undefined,
parentBeaconBlockRoot: this.common.isActivatedEIP(4788) ? zeros(32) : undefined,
}

const baseFeePerGas =
toType(headerData.baseFeePerGas, TypeOutput.BigInt) ?? hardforkDefaults.baseFeePerGas
const withdrawalsRoot =
toType(headerData.withdrawalsRoot, TypeOutput.Uint8Array) ?? hardforkDefaults.withdrawalsRoot
const dataGasUsed =
toType(headerData.dataGasUsed, TypeOutput.BigInt) ?? hardforkDefaults.dataGasUsed
const excessDataGas =
toType(headerData.excessDataGas, TypeOutput.BigInt) ?? hardforkDefaults.excessDataGas
const blobGasUsed =
toType(headerData.blobGasUsed, TypeOutput.BigInt) ?? hardforkDefaults.blobGasUsed
const excessBlobGas =
toType(headerData.excessBlobGas, TypeOutput.BigInt) ?? hardforkDefaults.excessBlobGas
const parentBeaconBlockRoot =
toType(headerData.parentBeaconBlockRoot, TypeOutput.Uint8Array) ??
hardforkDefaults.parentBeaconBlockRoot
Expand All @@ -238,12 +238,12 @@ export class BlockHeader {
}

if (!this.common.isActivatedEIP(4844)) {
if (headerData.dataGasUsed !== undefined) {
throw new Error('data gas used can only be provided with EIP4844 activated')
if (headerData.blobGasUsed !== undefined) {
throw new Error('blob gas used can only be provided with EIP4844 activated')
}

if (headerData.excessDataGas !== undefined) {
throw new Error('excess data gas can only be provided with EIP4844 activated')
if (headerData.excessBlobGas !== undefined) {
throw new Error('excess blob gas can only be provided with EIP4844 activated')
}
}

Expand All @@ -270,8 +270,8 @@ export class BlockHeader {
this.nonce = nonce
this.baseFeePerGas = baseFeePerGas
this.withdrawalsRoot = withdrawalsRoot
this.dataGasUsed = dataGasUsed
this.excessDataGas = excessDataGas
this.blobGasUsed = blobGasUsed
this.excessBlobGas = excessBlobGas
this.parentBeaconBlockRoot = parentBeaconBlockRoot
this._genericFormatValidation()
this._validateDAOExtraData()
Expand Down Expand Up @@ -576,46 +576,46 @@ export class BlockHeader {
}

/**
* Returns the price per unit of data gas for a blob transaction in the current/pending block
* @returns the price in gwei per unit of data gas spent
* Returns the price per unit of blob gas for a blob transaction in the current/pending block
* @returns the price in gwei per unit of blob gas spent
*/
getDataGasPrice(): bigint {
if (this.excessDataGas === undefined) {
throw new Error('header must have excessDataGas field populated')
getblobGasPrice(): bigint {
if (this.excessBlobGas === undefined) {
throw new Error('header must have excessBlobGas field populated')
}
return fakeExponential(
this.common.param('gasPrices', 'minDataGasPrice'),
this.excessDataGas,
this.common.param('gasConfig', 'dataGasPriceUpdateFraction')
this.common.param('gasPrices', 'minblobGasPrice'),
this.excessBlobGas,
this.common.param('gasConfig', 'blobGasPriceUpdateFraction')
)
}

/**
* Returns the total fee for data gas spent for including blobs in block.
* Returns the total fee for blob gas spent for including blobs in block.
*
* @param numBlobs number of blobs in the transaction/block
* @returns the total data gas fee for numBlobs blobs
* @returns the total blob gas fee for numBlobs blobs
*/
calcDataFee(numBlobs: number): bigint {
const dataGasPerBlob = this.common.param('gasConfig', 'dataGasPerBlob')
const dataGasUsed = dataGasPerBlob * BigInt(numBlobs)
const blobGasPerBlob = this.common.param('gasConfig', 'blobGasPerBlob')
const blobGasUsed = blobGasPerBlob * BigInt(numBlobs)

const dataGasPrice = this.getDataGasPrice()
return dataGasUsed * dataGasPrice
const blobGasPrice = this.getblobGasPrice()
return blobGasUsed * blobGasPrice
}

/**
* Calculates the excess data gas for next (hopefully) post EIP 4844 block.
* Calculates the excess blob gas for next (hopefully) post EIP 4844 block.
*/
public calcNextExcessDataGas(): bigint {
public calcNextExcessBlobGas(): bigint {
// The validation of the fields and 4844 activation is already taken care in BlockHeader constructor
const targetGasConsumed = (this.excessDataGas ?? BigInt(0)) + (this.dataGasUsed ?? BigInt(0))
const targetDataGasPerBlock = this.common.param('gasConfig', 'targetDataGasPerBlock')
const targetGasConsumed = (this.excessBlobGas ?? BigInt(0)) + (this.blobGasUsed ?? BigInt(0))
const targetBlobGasPerBlock = this.common.param('gasConfig', 'targetBlobGasPerBlock')

if (targetGasConsumed <= targetDataGasPerBlock) {
if (targetGasConsumed <= targetBlobGasPerBlock) {
return BigInt(0)
} else {
return targetGasConsumed - targetDataGasPerBlock
return targetGasConsumed - targetBlobGasPerBlock
}
}

Expand Down Expand Up @@ -649,8 +649,8 @@ export class BlockHeader {
rawItems.push(this.withdrawalsRoot!)
}
if (this.common.isActivatedEIP(4844) === true) {
rawItems.push(bigIntToUnpaddedBytes(this.dataGasUsed!))
rawItems.push(bigIntToUnpaddedBytes(this.excessDataGas!))
rawItems.push(bigIntToUnpaddedBytes(this.blobGasUsed!))
rawItems.push(bigIntToUnpaddedBytes(this.excessBlobGas!))
}
if (this.common.isActivatedEIP(4788) === true) {
rawItems.push(this.parentBeaconBlockRoot!)
Expand Down Expand Up @@ -922,8 +922,8 @@ export class BlockHeader {
jsonDict.baseFeePerGas = bigIntToHex(this.baseFeePerGas!)
}
if (this.common.isActivatedEIP(4844) === true) {
jsonDict.dataGasUsed = bigIntToHex(this.dataGasUsed!)
jsonDict.excessDataGas = bigIntToHex(this.excessDataGas!)
jsonDict.blobGasUsed = bigIntToHex(this.blobGasUsed!)
jsonDict.excessBlobGas = bigIntToHex(this.excessBlobGas!)
}
if (this.common.isActivatedEIP(4788) === true) {
jsonDict.parentBeaconBlockRoot = bytesToHex(this.parentBeaconBlockRoot!)
Expand Down
8 changes: 4 additions & 4 deletions packages/block/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function valuesArrayToHeaderData(values: BlockHeaderBytes): HeaderData {
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
] = values

Expand Down Expand Up @@ -70,8 +70,8 @@ export function valuesArrayToHeaderData(values: BlockHeaderBytes): HeaderData {
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export interface HeaderData {
nonce?: BytesLike
baseFeePerGas?: BigIntLike
withdrawalsRoot?: BytesLike
dataGasUsed?: BigIntLike
excessDataGas?: BigIntLike
blobGasUsed?: BigIntLike
excessBlobGas?: BigIntLike
parentBeaconBlockRoot?: BytesLike
}

Expand Down Expand Up @@ -157,8 +157,8 @@ export interface JsonHeader {
nonce?: string
baseFeePerGas?: string
withdrawalsRoot?: string
dataGasUsed?: string
excessDataGas?: string
blobGasUsed?: string
excessBlobGas?: string
parentBeaconBlockRoot?: string
}

Expand Down Expand Up @@ -189,8 +189,8 @@ export interface JsonRpcBlock {
baseFeePerGas?: string // If EIP-1559 is enabled for this block, returns the base fee per gas
withdrawals?: Array<JsonRpcWithdrawal> // If EIP-4895 is enabled for this block, array of withdrawals
withdrawalsRoot?: string // If EIP-4895 is enabled for this block, the root of the withdrawal trie of the block.
dataGasUsed?: string // If EIP-4844 is enabled for this block, returns the data gas used for the block
excessDataGas?: string // If EIP-4844 is enabled for this block, returns the excess data gas for the block
blobGasUsed?: string // If EIP-4844 is enabled for this block, returns the blob gas used for the block
excessBlobGas?: string // If EIP-4844 is enabled for this block, returns the excess blob gas for the block
parentBeaconBlockRoot?: string // If EIP-4788 is enabled for this block, returns parent beacon block root
}

Expand Down Expand Up @@ -219,7 +219,7 @@ export type ExecutionPayload = {
blockHash: PrefixedHexString // DATA, 32 Bytes
transactions: PrefixedHexString[] // Array of DATA - Array of transaction rlp strings,
withdrawals?: WithdrawalV1[] // Array of withdrawal objects
dataGasUsed?: PrefixedHexString // QUANTITY, 64 Bits
excessDataGas?: PrefixedHexString // QUANTITY, 64 Bits
blobGasUsed?: PrefixedHexString // QUANTITY, 64 Bits
excessBlobGas?: PrefixedHexString // QUANTITY, 64 Bits
parentBeaconBlockRoot?: PrefixedHexString // QUANTITY, 64 Bits
}
Loading