Skip to content

Commit

Permalink
fix: adjust the timing of initializing Feynman system contracts (#2203)
Browse files Browse the repository at this point in the history
* fix: adjust the timing of initializing Feynman system contracts

* fix test case
  • Loading branch information
pythonberg1997 authored Feb 4, 2024
1 parent e7cd04b commit fd0ee7d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func doLint(cmdline []string) {

// downloadLinter downloads and unpacks golangci-lint.
func downloadLinter(cachedir string) string {
const version = "1.52.2"
const version = "1.55.2"

csdb := build.MustLoadChecksums("build/checksums.txt")
arch := runtime.GOARCH
Expand Down
34 changes: 12 additions & 22 deletions cmd/geth/blsaccountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,29 +633,19 @@ func blsAccountDelete(ctx *cli.Context) error {

// blsAccountGenerateProof generate ownership proof for a selected BLS account.
func blsAccountGenerateProof(ctx *cli.Context) error {
if ctx.Args().Len() == 0 {
utils.Fatalf("No BLS account specified.")
pubkeyString := ctx.Args().First()
if pubkeyString == "" {
utils.Fatalf("BLS account must be given as argument.")
}
var filteredPubKeys []bls.PublicKey
for _, str := range ctx.Args().Slice() {
pkString := str
if strings.Contains(pkString, "0x") {
pkString = pkString[2:]
}
pubKeyBytes, err := hex.DecodeString(pkString)
if err != nil {
utils.Fatalf("Could not decode string %s as hex.", pkString)
}
blsPublicKey, err := bls.PublicKeyFromBytes(pubKeyBytes)
if err != nil {
utils.Fatalf("%#x is not a valid BLS public key.", pubKeyBytes)
}
filteredPubKeys = append(filteredPubKeys, blsPublicKey)
pubkeyBz, err := hex.DecodeString(strings.TrimPrefix(pubkeyString, "0x"))
if err != nil {
utils.Fatalf("Could not decode string %s as hex.", pubkeyString)
}
if len(filteredPubKeys) > 1 {
utils.Fatalf("Only support one BLS account specified.")
blsPublicKey, err := bls.PublicKeyFromBytes(pubkeyBz)
if err != nil {
utils.Fatalf("%#x is not a valid BLS public key.", pubkeyBz)
}
pubkeyBz := filteredPubKeys[0].Marshal()
blsPublicKeyBz := blsPublicKey.Marshal()

cfg := gethConfig{Node: defaultNodeConfig()}
// Load config file.
Expand Down Expand Up @@ -692,10 +682,10 @@ func blsAccountGenerateProof(ctx *cli.Context) error {
chainId := new(big.Int).SetInt64(chainIdInt64)
paddedChainIdBytes := make([]byte, 32)
copy(paddedChainIdBytes[32-len(chainId.Bytes()):], chainId.Bytes())
msgHash := crypto.Keccak256(append(pubkeyBz, paddedChainIdBytes...))
msgHash := crypto.Keccak256(append(blsPublicKeyBz, paddedChainIdBytes...))

req := &validatorpb.SignRequest{
PublicKey: pubkeyBz,
PublicKey: blsPublicKeyBz,
SigningRoot: msgHash,
}
sig, err := km.Sign(context.Background(), req)
Expand Down
28 changes: 14 additions & 14 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,13 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
systemcontracts.UpgradeBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state)
}

if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, txs, receipts, systemTxs, usedGas, false)
if err != nil {
log.Error("init feynman contract failed", "error", err)
}
}

// No block rewards in PoA, so the state remains as is and uncles are dropped
if header.Number.Cmp(common.Big1) == 0 {
err := p.initContract(state, header, cx, txs, receipts, systemTxs, usedGas, false)
Expand Down Expand Up @@ -1178,13 +1185,6 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
}
}

if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, txs, receipts, systemTxs, usedGas, false)
if err != nil {
log.Error("init feynman contract failed", "error", err)
}
}

// update validators every day
if p.chainConfig.IsFeynman(header.Number, header.Time) && isBreatheBlock(parent.Time, header.Time) {
// we should avoid update validators in the Feynman upgrade block
Expand Down Expand Up @@ -1223,6 +1223,13 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
systemcontracts.UpgradeBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state)
}

if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
if err != nil {
log.Error("init feynman contract failed", "error", err)
}
}

if header.Number.Cmp(common.Big1) == 0 {
err := p.initContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
if err != nil {
Expand Down Expand Up @@ -1267,13 +1274,6 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
}
}

if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
err := p.initializeFeynmanContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
if err != nil {
log.Error("init feynman contract failed", "error", err)
}
}

// update validators every day
if p.chainConfig.IsFeynman(header.Number, header.Time) && isBreatheBlock(parent.Time, header.Time) {
// we should avoid update validators in the Feynman upgrade block
Expand Down
2 changes: 1 addition & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/systemcontracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/core/systemcontracts"
)

// noopReleaser is returned in case there is no operation expected
Expand Down
14 changes: 8 additions & 6 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,12 +1038,14 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
defer release()

// upgrade build-in system contract before tracing if Feynman is not enabled
parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
if err != nil {
return nil, err
}
if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) {
systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb)
if block.NumberU64() > 0 {
parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
if err != nil {
return nil, err
}
if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) {
systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb)
}
}

vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
Expand Down
8 changes: 4 additions & 4 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestTraceCall(t *testing.T) {
},
config: nil,
expectErr: fmt.Errorf("block #%d not found", genBlocks+1),
//expect: nil,
// expect: nil,
},
// Standard JSON trace upon the latest block
{
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestTracingWithOverrides(t *testing.T) {
Data: newRPCBytes(common.Hex2Bytes("8381f58a")), // call number()
},
config: &TraceCallConfig{
//Tracer: &tracer,
// Tracer: &tracer,
StateOverrides: &ethapi.StateOverride{
randomAccounts[2].addr: ethapi.OverrideAccount{
Code: newRPCBytes(common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80638381f58a14602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea2646970667358221220eab35ffa6ab2adfe380772a48b8ba78e82a1b820a18fcb6f59aa4efb20a5f60064736f6c63430007040033")),
Expand All @@ -563,7 +563,7 @@ func TestTracingWithOverrides(t *testing.T) {
From: &accounts[0].addr,
// BLOCKNUMBER PUSH1 MSTORE
Input: newRPCBytes(common.Hex2Bytes("4360005260206000f3")),
//&hexutil.Bytes{0x43}, // blocknumber
// &hexutil.Bytes{0x43}, // blocknumber
},
config: &TraceCallConfig{
BlockOverrides: &ethapi.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
Expand Down Expand Up @@ -639,7 +639,7 @@ func TestTracingWithOverrides(t *testing.T) {
},
},
},
//want: `{"gas":46900,"failed":false,"returnValue":"0000000000000000000000000000000000000000000000000000000000000539"}`,
// want: `{"gas":46900,"failed":false,"returnValue":"0000000000000000000000000000000000000000000000000000000000000539"}`,
want: `{"gas":44100,"failed":false,"returnValue":"0000000000000000000000000000000000000000000000000000000000000001"}`,
},
{ // No state override
Expand Down

0 comments on commit fd0ee7d

Please sign in to comment.