Skip to content

Commit

Permalink
Merge pull request ethereum#56 from lochjin/v1.14.9-qng
Browse files Browse the repository at this point in the history
V1.14.9 qng
  • Loading branch information
dindinw authored Sep 26, 2024
2 parents c350d3a + 2e86de6 commit cf23a50
Show file tree
Hide file tree
Showing 31 changed files with 2,575 additions and 19 deletions.
4 changes: 3 additions & 1 deletion beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type ExecutableData struct {
ExcessBlobGas *uint64 `json:"excessBlobGas"`
Deposits types.Deposits `json:"depositRequests"`
ExecutionWitness *types.ExecutionWitness `json:"executionWitness,omitempty"`
Difficulty *big.Int
}

// JSON type overrides for executableData.
Expand Down Expand Up @@ -253,7 +254,7 @@ func ExecutableDataToBlock(data ExecutableData, versionedHashes []common.Hash, b
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
ReceiptHash: data.ReceiptsRoot,
Bloom: types.BytesToBloom(data.LogsBloom),
Difficulty: common.Big0,
Difficulty: data.Difficulty,
Number: new(big.Int).SetUint64(data.Number),
GasLimit: data.GasLimit,
GasUsed: data.GasUsed,
Expand Down Expand Up @@ -298,6 +299,7 @@ func BlockToExecutableData(block *types.Block, fees *big.Int, sidecars []*types.
BlobGasUsed: block.BlobGasUsed(),
ExcessBlobGas: block.ExcessBlobGas(),
ExecutionWitness: block.ExecutionWitness(),
Difficulty: block.Difficulty(),
}
bundle := BlobsBundleV1{
Commitments: make([]hexutil.Bytes, 0),
Expand Down
69 changes: 69 additions & 0 deletions beacon/engine/types_qng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package engine

import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
"math/big"
)

func ExecutableDataToBlockQng(params ExecutableData, versionedHashes []common.Hash, beaconRoot *common.Hash) (*types.Block, error) {
txs, err := decodeTransactions(params.Transactions)
if err != nil {
return nil, err
}
if len(params.LogsBloom) != 256 {
return nil, fmt.Errorf("invalid logsBloom length: %v", len(params.LogsBloom))
}
// Check that baseFeePerGas is not negative or too big
if params.BaseFeePerGas != nil && (params.BaseFeePerGas.Sign() == -1 || params.BaseFeePerGas.BitLen() > 256) {
return nil, fmt.Errorf("invalid baseFeePerGas: %v", params.BaseFeePerGas)
}
var blobHashes []common.Hash
for _, tx := range txs {
blobHashes = append(blobHashes, tx.BlobHashes()...)
}
if len(blobHashes) != len(versionedHashes) {
return nil, fmt.Errorf("invalid number of versionedHashes: %v blobHashes: %v", versionedHashes, blobHashes)
}
for i := 0; i < len(blobHashes); i++ {
if blobHashes[i] != versionedHashes[i] {
return nil, fmt.Errorf("invalid versionedHash at %v: %v blobHashes: %v", i, versionedHashes, blobHashes)
}
}
// Only set withdrawalsRoot if it is non-nil. This allows CLs to use
// ExecutableData before withdrawals are enabled by marshaling
// Withdrawals as the json null value.
var withdrawalsRoot *common.Hash
if params.Withdrawals != nil {
h := types.DeriveSha(types.Withdrawals(params.Withdrawals), trie.NewStackTrie(nil))
withdrawalsRoot = &h
}
header := &types.Header{
ParentHash: params.ParentHash,
UncleHash: types.EmptyUncleHash,
Coinbase: params.FeeRecipient,
Root: params.StateRoot,
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
ReceiptHash: params.ReceiptsRoot,
Bloom: types.BytesToBloom(params.LogsBloom),
Difficulty: params.Difficulty,
Number: new(big.Int).SetUint64(params.Number),
GasLimit: params.GasLimit,
GasUsed: params.GasUsed,
Time: params.Timestamp,
BaseFee: params.BaseFeePerGas,
Extra: params.ExtraData,
MixDigest: params.Random,
WithdrawalsHash: withdrawalsRoot,
ExcessBlobGas: params.ExcessBlobGas,
BlobGasUsed: params.BlobGasUsed,
ParentBeaconRoot: beaconRoot,
}
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals})
if block.Hash() != params.BlockHash {
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
}
return block, nil
}
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
if err != nil {
Fatalf("%v", err)
}
engine, err := ethconfig.CreateConsensusEngine(config, chainDb)
engine, err := ethconfig.CreateDefaultConsensusEngine(config, chainDb)
if err != nil {
Fatalf("%v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,14 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ

// noState represents if the target state requested for search
// is unavailable and impossible to be recovered.
noState = !bc.HasState(root) && !bc.stateRecoverable(root)
noState = root == common.Hash{}

start = time.Now() // Timestamp the rewinding is restarted
logged = time.Now() // Timestamp last progress log was printed
)
if !noState {
noState = !bc.HasState(root) && !bc.stateRecoverable(root)
}
// Rewind the head block tag until an available state is found.
for {
logger := log.Trace
Expand Down
20 changes: 20 additions & 0 deletions core/txpool/legacypool/legacypool_qng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package legacypool

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

func (pool *LegacyPool) RemoveTx(hash common.Hash, outofbound bool) {
pool.mu.Lock()
defer pool.mu.Unlock()
pool.removeTx(hash, outofbound, true)
}

func (pool *LegacyPool) All() *lookup {
return pool.all
}

func (pool *LegacyPool) AddRemotesSync(txs []*types.Transaction) []error {
return pool.addRemotesSync(txs)
}
5 changes: 5 additions & 0 deletions core/txpool/txpool_qng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package txpool

func (p *TxPool) Subpools() []SubPool {
return p.subpools
}
112 changes: 112 additions & 0 deletions core/types/transaction_qng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package types

import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
)

type PKSigner interface {
GetPublicKey(tx *Transaction) ([]byte, error)
}

func NewPKSigner(chainId *big.Int) PKSigner {
return cancunSigner{londonSigner{eip2930Signer{NewEIP155Signer(chainId)}}}
}

func (s cancunSigner) GetPublicKey(tx *Transaction) ([]byte, error) {
if tx.Type() != BlobTxType {
return s.londonSigner.GetPublicKey(tx)
}
V, R, S := tx.RawSignatureValues()
// Blob txs are defined to use 0 and 1 as their recovery
// id, add 27 to become equivalent to unprotected Homestead signatures.
V = new(big.Int).Add(V, big.NewInt(27))
if tx.ChainId().Cmp(s.chainId) != 0 {
return nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
}
return recoverPlainForPubK(s.Hash(tx), R, S, V, true)
}

func (s londonSigner) GetPublicKey(tx *Transaction) ([]byte, error) {
if tx.Type() != DynamicFeeTxType {
return s.eip2930Signer.GetPublicKey(tx)
}
V, R, S := tx.RawSignatureValues()
// DynamicFee txs are defined to use 0 and 1 as their recovery
// id, add 27 to become equivalent to unprotected Homestead signatures.
V = new(big.Int).Add(V, big.NewInt(27))
if tx.ChainId().Cmp(s.chainId) != 0 {
return nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
}
return recoverPlainForPubK(s.Hash(tx), R, S, V, true)
}

func (s eip2930Signer) GetPublicKey(tx *Transaction) ([]byte, error) {
V, R, S := tx.RawSignatureValues()
switch tx.Type() {
case LegacyTxType:
return s.EIP155Signer.GetPublicKey(tx)
case AccessListTxType:
// AL txs are defined to use 0 and 1 as their recovery
// id, add 27 to become equivalent to unprotected Homestead signatures.
V = new(big.Int).Add(V, big.NewInt(27))
default:
return nil, ErrTxTypeNotSupported
}
if tx.ChainId().Cmp(s.chainId) != 0 {
return nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
}
return recoverPlainForPubK(s.Hash(tx), R, S, V, true)
}

func (s EIP155Signer) GetPublicKey(tx *Transaction) ([]byte, error) {
if tx.Type() != LegacyTxType {
return nil, ErrTxTypeNotSupported
}
if !tx.Protected() {
return HomesteadSigner{}.GetPublicKey(tx)
}
if tx.ChainId().Cmp(s.chainId) != 0 {
return nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
}
V, R, S := tx.RawSignatureValues()
V = new(big.Int).Sub(V, s.chainIdMul)
V.Sub(V, big8)
return recoverPlainForPubK(s.Hash(tx), R, S, V, true)
}

func (hs HomesteadSigner) GetPublicKey(tx *Transaction) ([]byte, error) {
if tx.Type() != LegacyTxType {
return nil, ErrTxTypeNotSupported
}
v, r, s := tx.RawSignatureValues()
return recoverPlainForPubK(hs.Hash(tx), r, s, v, true)
}

func recoverPlainForPubK(sighash common.Hash, R, S, Vb *big.Int, homestead bool) ([]byte, error) {
if Vb.BitLen() > 8 {
return nil, ErrInvalidSig
}
V := byte(Vb.Uint64() - 27)
if !crypto.ValidateSignatureValues(V, R, S, homestead) {
return nil, ErrInvalidSig
}
// encode the signature in uncompressed format
r, s := R.Bytes(), S.Bytes()
sig := make([]byte, crypto.SignatureLength)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = V
// recover the public key from the signature
pub, err := crypto.Ecrecover(sighash[:], sig)
if err != nil {
return nil, err
}
if len(pub) == 0 || pub[0] != 4 {
return nil, errors.New("invalid public key")
}
return pub, nil
}
42 changes: 42 additions & 0 deletions core/types/transaction_qng_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/crypto"
)

func TestGetPubkeyFromTx(t *testing.T) {
key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)

signer := NewEIP155Signer(big.NewInt(18))
tx, err := SignTx(NewTransaction(0, addr, new(big.Int), 0, new(big.Int), nil), signer, key)
if err != nil {
t.Fatal(err)
}

from, err := Sender(signer, tx)
if err != nil {
t.Fatal(err)
}
if from != addr {
t.Errorf("expected from and address to be equal. Got %x want %x", from, addr)
}

var gs PKSigner
gs = &signer
pkb, err := gs.GetPublicKey(tx)
if err != nil {
t.Fatal(err)
}
pk, err := crypto.UnmarshalPubkey(pkb)
if err != nil {
t.Fatal(err)
}
pka := crypto.PubkeyToAddress(*pk)
if pka != addr {
t.Errorf("expected pubkey-address and address to be equal. Got %x want %x", pka, addr)
}
}
2 changes: 1 addition & 1 deletion crypto/signature_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"errors"
"fmt"

secp256k1 "github.com/Qitmeer/go-secp256k1"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)

// Ecrecover returns the uncompressed public key that created the given signature.
Expand Down
13 changes: 11 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if err != nil {
return nil, err
}
engine, err := ethconfig.CreateConsensusEngine(chainConfig, chainDb)
if config.ConsensusEngine == nil {
config.ConsensusEngine = ethconfig.CreateDefaultConsensusEngine
}
engine, err := config.ConsensusEngine(chainConfig, chainDb)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -343,7 +346,13 @@ func (s *Ethereum) BloomIndexer() *core.ChainIndexer { return s.bloomIndexer }
// Protocols returns all the currently configured
// network protocols to start.
func (s *Ethereum) Protocols() []p2p.Protocol {
protos := eth.MakeProtocols((*ethHandler)(s.handler), s.networkID, s.discmix)
var backend eth.Backend
if s.config.Genesis != nil && params.IsAmanaNetwork(s.config.Genesis.Config.ChainID) {
backend = (*qngHandler)(s.handler)
} else {
backend = (*ethHandler)(s.handler)
}
protos := eth.MakeProtocols(backend, s.networkID, s.discmix)
if s.config.SnapshotCache > 0 {
protos = append(protos, snap.MakeProtocols((*snapHandler)(s.handler))...)
}
Expand Down
Loading

0 comments on commit cf23a50

Please sign in to comment.