cmd/evm/internal: add Codeword and CodeLength on block header struct

This commit is contained in:
Your Name 2024-04-23 13:02:08 +09:00
parent e5a24bdf69
commit fc1e51c4cb
3 changed files with 66 additions and 4 deletions

View file

@ -55,6 +55,8 @@ type header struct {
MixDigest common.Hash `json:"mixHash"` MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"` Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
Codeword []byte `json:"codeword" rlp:"optional"`
CodeLength uint64 `json:"codelength" rlp:"optional"`
} }
type headerMarshaling struct { type headerMarshaling struct {
@ -78,6 +80,9 @@ type bbInput struct {
PowMode ethash.Mode `json:"-"` PowMode ethash.Mode `json:"-"`
Txs []*types.Transaction `json:"-"` Txs []*types.Transaction `json:"-"`
Ommers []*types.Header `json:"-"` Ommers []*types.Header `json:"-"`
//eccpow
Eccpow bool `json:"-"`
} }
type cliqueInput struct { type cliqueInput struct {
@ -154,6 +159,13 @@ func (i *bbInput) ToBlock() *types.Block {
if header.Difficulty != nil { if header.Difficulty != nil {
header.Difficulty = i.Header.Difficulty header.Difficulty = i.Header.Difficulty
} }
if header.Codeword != nil {
header.Codeword = i.Header.Codeword
}
if header.CodeLength != 0 {
header.CodeLength = i.Header.CodeLength
}
return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers) return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers)
} }
@ -162,6 +174,8 @@ func (i *bbInput) SealBlock(block *types.Block) (*types.Block, error) {
switch { switch {
case i.Ethash: case i.Ethash:
return i.sealEthash(block) return i.sealEthash(block)
case i.Eccpow:
return i.sealEccpow(block)
case i.Clique != nil: case i.Clique != nil:
return i.sealClique(block) return i.sealClique(block)
default: default:
@ -196,6 +210,29 @@ func (i *bbInput) sealEthash(block *types.Block) (*types.Block, error) {
return block.WithSeal(found.Header()), nil return block.WithSeal(found.Header()), nil
} }
// sealEccpow seals the given block using eccpow !!to update
func (i *bbInput) sealEccpow(block *types.Block) (*types.Block, error) {
if i.Header.Nonce != nil {
return nil, NewError(ErrorConfig, fmt.Errorf("sealing with eccpow will overwrite provided nonce"))
}
//!!true or false? // eccpow
engine := eccpow.New(eccpow.Config{}, nil, true)
defer engine.Close()
// Use a buffered chan for results.
// If the testmode is used, the sealer will return quickly, and complain
// "Sealing result is not read by miner" if it cannot write the result.
results := make(chan *types.Block, 1)
if err := engine.Seal(nil, block, results, nil); err != nil {
panic(fmt.Sprintf("failed to seal block: %v", err))
}
found := <-results
//copy codeword?
return block.WithSeal(found.Header()), nil
}
// sealClique seals the given block using clique. // sealClique seals the given block using clique.
func (i *bbInput) sealClique(block *types.Block) (*types.Block, error) { func (i *bbInput) sealClique(block *types.Block) (*types.Block, error) {
// If any clique value overwrites an explicit header value, fail // If any clique value overwrites an explicit header value, fail
@ -264,13 +301,19 @@ func readInput(ctx *cli.Context) (*bbInput, error) {
ommersStr = ctx.String(InputOmmersFlag.Name) ommersStr = ctx.String(InputOmmersFlag.Name)
txsStr = ctx.String(InputTxsRlpFlag.Name) txsStr = ctx.String(InputTxsRlpFlag.Name)
cliqueStr = ctx.String(SealCliqueFlag.Name) cliqueStr = ctx.String(SealCliqueFlag.Name)
eccpowOn = ctx.Bool(SealEccpowFlag.Name)
ethashOn = ctx.Bool(SealEthashFlag.Name) ethashOn = ctx.Bool(SealEthashFlag.Name)
ethashDir = ctx.String(SealEthashDirFlag.Name) ethashDir = ctx.String(SealEthashDirFlag.Name)
ethashMode = ctx.String(SealEthashModeFlag.Name) ethashMode = ctx.String(SealEthashModeFlag.Name)
inputData = &bbInput{} inputData = &bbInput{}
) )
if ethashOn && cliqueStr != "" {
return nil, NewError(ErrorConfig, fmt.Errorf("both ethash and clique sealing specified, only one may be chosen")) //need to update.
if ethashOn && eccpowOn && cliqueStr != "" {
return nil, NewError(ErrorConfig, fmt.Errorf("ethash and clique, eccpow sealing specified, only one may be chosen"))
}
if eccpowOn {
inputData.Eccpow = eccpowOn
} }
if ethashOn { if ethashOn {
inputData.Ethash = ethashOn inputData.Ethash = ethashOn
@ -292,6 +335,8 @@ func readInput(ctx *cli.Context) (*bbInput, error) {
return nil, NewError(ErrorJson, fmt.Errorf("failed unmarshaling stdin: %v", err)) return nil, NewError(ErrorJson, fmt.Errorf("failed unmarshaling stdin: %v", err))
} }
} }
if cliqueStr != stdinSelector && cliqueStr != "" { if cliqueStr != stdinSelector && cliqueStr != "" {
var clique cliqueInput var clique cliqueInput
if err := readFile(cliqueStr, "clique", &clique); err != nil { if err := readFile(cliqueStr, "clique", &clique); err != nil {

View file

@ -121,6 +121,10 @@ var (
Name: "seal.clique", Name: "seal.clique",
Usage: "Seal block with Clique. `stdin` or file name of where to find the Clique sealing data.", Usage: "Seal block with Clique. `stdin` or file name of where to find the Clique sealing data.",
} }
SealEccpowFlag = &cli.StringFlag{
Name: "seal.eccpow",
Usage: "Seal block with eccpow.",
}
SealEthashFlag = &cli.BoolFlag{ SealEthashFlag = &cli.BoolFlag{
Name: "seal.ethash", Name: "seal.ethash",
Usage: "Seal block with ethash.", Usage: "Seal block with ethash.",

View file

@ -34,6 +34,9 @@ func (h header) MarshalJSON() ([]byte, error) {
MixDigest common.Hash `json:"mixHash"` MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"` Nonce *types.BlockNonce `json:"nonce"`
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas" rlp:"optional"` BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas" rlp:"optional"`
Codeword hexutil.Bytes `json:"codeword" rlp:"optional"`
CodeLength math.HexOrDecimal64 `json:"codelength" rlp:"optional"`
} }
var enc header var enc header
enc.ParentHash = h.ParentHash enc.ParentHash = h.ParentHash
@ -52,6 +55,8 @@ func (h header) MarshalJSON() ([]byte, error) {
enc.MixDigest = h.MixDigest enc.MixDigest = h.MixDigest
enc.Nonce = h.Nonce enc.Nonce = h.Nonce
enc.BaseFee = (*math.HexOrDecimal256)(h.BaseFee) enc.BaseFee = (*math.HexOrDecimal256)(h.BaseFee)
enc.Codeword = h.Codeword
enc.CodeLength = math.HexOrDecimal64(h.CodeLength)
return json.Marshal(&enc) return json.Marshal(&enc)
} }
@ -74,6 +79,8 @@ func (h *header) UnmarshalJSON(input []byte) error {
MixDigest *common.Hash `json:"mixHash"` MixDigest *common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"` Nonce *types.BlockNonce `json:"nonce"`
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas" rlp:"optional"` BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas" rlp:"optional"`
Codeword *hexutil.Bytes `json:"codeword" rlp:"optional"`
CodeLength *math.HexOrDecimal64 `json:"codelength" rlp:"optional"`
} }
var dec header var dec header
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {
@ -131,5 +138,11 @@ func (h *header) UnmarshalJSON(input []byte) error {
if dec.BaseFee != nil { if dec.BaseFee != nil {
h.BaseFee = (*big.Int)(dec.BaseFee) h.BaseFee = (*big.Int)(dec.BaseFee)
} }
if dec.Codeword != nil {
h.Codeword = *dec.Codeword
}
if dec.CodeLength != nil {
h.CodeLength = uint64(*dec.CodeLength)
}
return nil return nil
} }