forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ethereum#56 from lochjin/v1.14.9-qng
V1.14.9 qng
- Loading branch information
Showing
31 changed files
with
2,575 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package txpool | ||
|
||
func (p *TxPool) Subpools() []SubPool { | ||
return p.subpools | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.