gasLimit (#12)

This commit is contained in:
Moon Shiesty 2023-10-31 01:23:29 -05:00 committed by GitHub
parent 1202a3b64d
commit 7f40bbd7d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 7 deletions

View file

@ -24,6 +24,7 @@ func (p PayloadAttributes) MarshalJSON() ([]byte, error) {
// <specular modification>
Transactions []hexutil.Bytes `json:"transactions,omitempty" gencodec:"optional"`
NoTxPool bool `json:"noTxPool,omitempty" gencodec:"optional"`
GasLimit *hexutil.Uint64 `json:"gasLimit,omitempty" gencodec:"optional"`
// <specular modification/>
}
var enc PayloadAttributes
@ -40,6 +41,7 @@ func (p PayloadAttributes) MarshalJSON() ([]byte, error) {
}
}
enc.NoTxPool = p.NoTxPool
enc.GasLimit = (*hexutil.Uint64)(p.GasLimit)
// <specular modification/>
return json.Marshal(&enc)
}
@ -55,6 +57,7 @@ func (p *PayloadAttributes) UnmarshalJSON(input []byte) error {
// <specular modification>
Transactions []hexutil.Bytes `json:"transactions,omitempty" gencodec:"optional"`
NoTxPool *bool `json:"noTxPool,omitempty" gencodec:"optional"`
GasLimit *hexutil.Uint64 `json:"gasLimit,omitempty" gencodec:"optional"`
// <specular modification/>
}
var dec PayloadAttributes
@ -89,6 +92,9 @@ func (p *PayloadAttributes) UnmarshalJSON(input []byte) error {
if dec.NoTxPool != nil {
p.NoTxPool = *dec.NoTxPool
}
if dec.GasLimit != nil {
p.GasLimit = (*uint64)(dec.GasLimit)
}
// <specular modification/>
return nil
}

View file

@ -42,6 +42,8 @@ type PayloadAttributes struct {
// NoTxPool is a field for L2s: if true, the no transactions are taken out of the tx-pool,
// only transactions from the above Transactions list will be included.
NoTxPool bool `json:"noTxPool,omitempty" gencodec:"optional"`
// GasLimit is a field for L2s: if set, this sets the exact gas limit the block produced with.
GasLimit *uint64 `json:"gasLimit,omitempty" gencodec:"optional"`
// <specular modification//>
}
@ -50,6 +52,7 @@ type payloadAttributesMarshaling struct {
Timestamp hexutil.Uint64
// <specular modification>
Transactions []hexutil.Bytes
GasLimit *hexutil.Uint64
// <specular modification/>
}

View file

@ -37,9 +37,15 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
if !config.IsLondon(parent.Number) {
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
}
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
return err
// <specular modification>
if !config.EnableL2GasLimitApi { // gasLimit can adjust instantly on L2s
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
return err
}
}
// <specular modification>
// Verify the header is not malformed
if header.BaseFee == nil {
return errors.New("header is missing baseFee")

View file

@ -367,6 +367,9 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
// will replace it arbitrarily many times in between.
if payloadAttributes != nil {
// <specular modification>
if api.eth.BlockChain().Config().EnableL2GasLimitApi && payloadAttributes.GasLimit == nil {
return engine.STATUS_INVALID, engine.InvalidPayloadAttributes.With(errors.New("gasLimit parameter is required"))
}
transactions := make(types.Transactions, 0, len(payloadAttributes.Transactions))
for i, otx := range payloadAttributes.Transactions {
var tx types.Transaction
@ -387,6 +390,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
// <specular modification>
NoTxPool: payloadAttributes.NoTxPool,
Transactions: transactions,
GasLimit: payloadAttributes.GasLimit,
// <specular modification/>
}
id := args.Id()

View file

@ -42,8 +42,9 @@ type BuildPayloadArgs struct {
Withdrawals types.Withdrawals // The provided withdrawals
BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
// <specular modification>
NoTxPool bool // Specular addition: option to disable tx pool contents from being included
Transactions []*types.Transaction // Specular addition: txs forced into the block via engine API
NoTxPool bool // L2 engine api addition: option to disable tx pool contents from being included
Transactions []*types.Transaction // L2 engine api addition: txs forced into the block via engine API
GasLimit *uint64 // L2 engine api addition: override gas limit of the block to build
// <specular modification/>
}
@ -68,6 +69,9 @@ func (args *BuildPayloadArgs) Id() engine.PayloadID {
hasher.Write(h[:])
}
}
if args.GasLimit != nil {
binary.Write(hasher, binary.BigEndian, *args.GasLimit)
}
// <specular modification/>
var out engine.PayloadID
copy(out[:], hasher.Sum(nil)[:8])
@ -193,6 +197,10 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
// Build the initial version with no transaction included. It should be fast
// enough to run. The empty payload can at least make sure there is something
// to deliver for not missing slot.
// <specular modification>
// With L2s, the "empty" block is constructed from provided txs only, i.e. no tx-pool usage.
// <specular modification/>
emptyParams := &generateParams{
timestamp: args.Timestamp,
forceTime: true,
@ -203,7 +211,8 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
beaconRoot: args.BeaconRoot,
noTxs: true,
// <specular modification>
txs: args.Transactions,
txs: args.Transactions,
gasLimit: args.GasLimit,
// <specular modification/>
}
empty := w.getSealingBlock(emptyParams)
@ -245,7 +254,8 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
beaconRoot: args.BeaconRoot,
noTxs: false,
// <specular modification>
txs: args.Transactions,
txs: args.Transactions,
gasLimit: args.GasLimit,
// <specular modification/>
}

View file

@ -902,7 +902,8 @@ type generateParams struct {
beaconRoot *common.Hash // The beacon root (cancun field).
noTxs bool // Flag whether an empty block without any transaction is expected
// <specular modification>
txs types.Transactions // Transactions to include at the start of the block
txs types.Transactions // Transactions to include at the start of the block
gasLimit *uint64 // Optional gas limit override
// <specular modification/>
}
@ -958,6 +959,15 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
header.GasLimit = core.CalcGasLimit(parentGasLimit, w.config.GasCeil)
}
}
// <specular modification>
if genParams.gasLimit != nil { // override gas limit if specified
header.GasLimit = *genParams.gasLimit
} else if w.chain.Config().EnableL2GasLimitApi && w.config.GasCeil != 0 {
// configure the gas limit of pending blocks with the miner gas limit config when L2 gas limit config is enabled
header.GasLimit = w.config.GasCeil
}
// <specular modification/>
// Apply EIP-4844, EIP-4788.
if w.chainConfig.IsCancun(header.Number, header.Time) {
var excessBlobGas uint64
@ -1031,6 +1041,9 @@ func (w *worker) generateWork(params *generateParams) *newPayloadResult {
}
defer work.discard()
// <specular modification>
if work.gasPool == nil {
work.gasPool = new(core.GasPool).AddGas(work.header.GasLimit)
}
for _, tx := range params.txs {
from, _ := types.Sender(work.signer, tx)
work.state.SetTxContext(tx.Hash(), work.tcount)

View file

@ -333,6 +333,10 @@ type ChainConfig struct {
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
IsDevMode bool `json:"isDev,omitempty"`
// <specular modification>
EnableL2GasLimitApi bool `json:"enableL2GasLimitApi,omitempty"`
// <specular modification/>
}
// EthashConfig is the consensus engine configs for proof-of-work based sealing.
@ -365,6 +369,10 @@ func (c *ChainConfig) Description() string {
}
banner += fmt.Sprintf("Chain ID: %v (%s)\n", c.ChainID, network)
switch {
// <specular modification>
case c.EnableL2GasLimitApi:
banner += "Consensus: L2 gas limit API enabled\n"
// <specular modification/>
case c.Ethash != nil:
if c.TerminalTotalDifficulty == nil {
banner += "Consensus: Ethash (proof-of-work)\n"