2020-10-07 10:22:44 -05:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 13:39:40 -05:00
|
|
|
// This file is part of go-ethereum.
|
2020-10-07 10:22:44 -05:00
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2020-10-07 10:22:44 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-10-07 10:22:44 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 13:39:40 -05:00
|
|
|
// GNU General Public License for more details.
|
2020-10-07 10:22:44 -05:00
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2020-10-07 10:22:44 -05:00
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2023-12-20 10:23:48 -06:00
|
|
|
"bytes"
|
2020-09-23 08:18:17 -05:00
|
|
|
"compress/gzip"
|
2023-12-20 10:23:48 -06:00
|
|
|
"crypto/ecdsa"
|
2020-09-23 08:18:17 -05:00
|
|
|
"encoding/json"
|
2023-05-24 05:21:29 -05:00
|
|
|
"errors"
|
2020-09-23 08:18:17 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2024-03-12 04:00:34 -05:00
|
|
|
"path/filepath"
|
2024-03-25 01:50:18 -05:00
|
|
|
"slices"
|
2020-09-23 08:18:17 -05:00
|
|
|
"strings"
|
|
|
|
|
2022-02-04 08:24:32 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2025-01-13 11:00:25 -06:00
|
|
|
"golang.org/x/exp/maps"
|
2020-09-23 08:18:17 -05:00
|
|
|
)
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Chain is a lightweight blockchain-like store which can read a hivechain
|
|
|
|
// created chain.
|
2020-09-23 08:18:17 -05:00
|
|
|
type Chain struct {
|
2023-12-20 10:23:48 -06:00
|
|
|
genesis core.Genesis
|
|
|
|
blocks []*types.Block
|
|
|
|
state map[common.Address]state.DumpAccount // state of head block
|
|
|
|
senders map[common.Address]*senderInfo
|
|
|
|
config *params.ChainConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChain takes the given chain.rlp file, and decodes and returns
|
|
|
|
// the blocks from the file.
|
|
|
|
func NewChain(dir string) (*Chain, error) {
|
2024-03-12 04:00:34 -05:00
|
|
|
gen, err := loadGenesis(filepath.Join(dir, "genesis.json"))
|
2023-12-20 10:23:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gblock := gen.ToBlock()
|
|
|
|
|
2024-03-12 04:00:34 -05:00
|
|
|
blocks, err := blocksFromFile(filepath.Join(dir, "chain.rlp"), gblock)
|
2023-12-20 10:23:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-12 04:00:34 -05:00
|
|
|
state, err := readState(filepath.Join(dir, "headstate.json"))
|
2023-12-20 10:23:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-12 04:00:34 -05:00
|
|
|
accounts, err := readAccounts(filepath.Join(dir, "accounts.json"))
|
2023-12-20 10:23:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Chain{
|
|
|
|
genesis: gen,
|
|
|
|
blocks: blocks,
|
|
|
|
state: state,
|
|
|
|
senders: accounts,
|
|
|
|
config: gen.Config,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// senderInfo is an account record as output in the "accounts.json" file from
|
|
|
|
// hivechain.
|
|
|
|
type senderInfo struct {
|
|
|
|
Key *ecdsa.PrivateKey `json:"key"`
|
|
|
|
Nonce uint64 `json:"nonce"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head returns the chain head.
|
|
|
|
func (c *Chain) Head() *types.Block {
|
|
|
|
return c.blocks[c.Len()-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountsInHashOrder returns all accounts of the head state, ordered by hash of address.
|
|
|
|
func (c *Chain) AccountsInHashOrder() []state.DumpAccount {
|
|
|
|
list := make([]state.DumpAccount, len(c.state))
|
|
|
|
i := 0
|
|
|
|
for addr, acc := range c.state {
|
|
|
|
list[i] = acc
|
|
|
|
list[i].Address = &addr
|
|
|
|
if len(acc.AddressHash) != 32 {
|
|
|
|
panic(fmt.Errorf("missing/invalid SecureKey in dump account %v", addr))
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
slices.SortFunc(list, func(x, y state.DumpAccount) int {
|
|
|
|
return bytes.Compare(x.AddressHash, y.AddressHash)
|
|
|
|
})
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
// CodeHashes returns all bytecode hashes contained in the head state.
|
|
|
|
func (c *Chain) CodeHashes() []common.Hash {
|
|
|
|
var hashes []common.Hash
|
|
|
|
seen := make(map[common.Hash]struct{})
|
|
|
|
seen[types.EmptyCodeHash] = struct{}{}
|
|
|
|
for _, acc := range c.state {
|
|
|
|
h := common.BytesToHash(acc.CodeHash)
|
|
|
|
if _, ok := seen[h]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hashes = append(hashes, h)
|
|
|
|
seen[h] = struct{}{}
|
|
|
|
}
|
|
|
|
slices.SortFunc(hashes, (common.Hash).Cmp)
|
|
|
|
return hashes
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the chain.
|
|
|
|
func (c *Chain) Len() int {
|
|
|
|
return len(c.blocks)
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// ForkID gets the fork id of the chain.
|
|
|
|
func (c *Chain) ForkID() forkid.ID {
|
|
|
|
return forkid.NewID(c.config, c.blocks[0], uint64(c.Len()), c.blocks[c.Len()-1].Time())
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:09:11 -05:00
|
|
|
// TD calculates the total difficulty of the chain at the
|
|
|
|
// chain head.
|
|
|
|
func (c *Chain) TD() *big.Int {
|
all: nuke total difficulty (#30744)
The total difficulty is the sum of all block difficulties from genesis
to a certain block. This value was used in PoW for deciding which chain
is heavier, and thus which chain to select. Since PoS has a different
fork selection algorithm, all blocks since the merge have a difficulty
of 0, and all total difficulties are the same for the past 2 years.
Whilst the TDs are mostly useless nowadays, there was never really a
reason to mess around removing them since they are so tiny. This
reasoning changes when we go down the path of pruned chain history. In
order to reconstruct any TD, we **must** retrieve all the headers from
chain head to genesis and then iterate all the difficulties to compute
the TD.
In a world where we completely prune past chain segments (bodies,
receipts, headers), it is not possible to reconstruct the TD at all. In
a world where we still keep chain headers and prune only the rest,
reconstructing it possible as long as we process (or download) the chain
forward from genesis, but trying to snap sync the head first and
backfill later hits the same issue, the TD becomes impossible to
calculate until genesis is backfilled.
All in all, the TD is a messy out-of-state, out-of-consensus computed
field that is overall useless nowadays, but code relying on it forces
the client into certain modes of operation and prevents other modes or
other optimizations. This PR completely nukes out the TD from the node.
It doesn't compute it, it doesn't operate on it, it's as if it didn't
even exist.
Caveats:
- Whenever we have APIs that return TD (devp2p handshake, tracer, etc.)
we return a TD of 0.
- For era files, we recompute the TD during export time (fairly quick)
to retain the format content.
- It is not possible to "verify" the merge point (i.e. with TD gone, TTD
is useless). Since we're not verifying PoW any more, just blindly trust
it, not verifying but blindly trusting the many year old merge point
seems just the same trust model.
- Our tests still need to be able to generate pre and post merge blocks,
so they need a new way to split the merge without TTD. The PR introduces
a settable ttdBlock field on the consensus object which is used by tests
as the block where originally the TTD happened. This is not needed for
live nodes, we never want to generate old blocks.
- One merge transition consensus test was disabled. With a
non-operational TD, testing how the client reacts to TTD is useless, it
cannot react.
Questions:
- Should we also drop total terminal difficulty from the genesis json?
It's a number we cannot react on any more, so maybe it would be cleaner
to get rid of even more concepts.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2025-01-28 11:55:41 -06:00
|
|
|
return new(big.Int)
|
2021-05-25 16:09:11 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// GetBlock returns the block at the specified number.
|
|
|
|
func (c *Chain) GetBlock(number int) *types.Block {
|
|
|
|
return c.blocks[number]
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// RootAt returns the state root for the block at the given height.
|
2022-02-04 08:24:32 -06:00
|
|
|
func (c *Chain) RootAt(height int) common.Hash {
|
|
|
|
if height < c.Len() {
|
|
|
|
return c.blocks[height].Root()
|
|
|
|
}
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// GetSender returns the address associated with account at the index in the
|
|
|
|
// pre-funded accounts list.
|
|
|
|
func (c *Chain) GetSender(idx int) (common.Address, uint64) {
|
2025-01-13 11:00:25 -06:00
|
|
|
accounts := maps.Keys(c.senders)
|
|
|
|
slices.SortFunc(accounts, common.Address.Cmp)
|
2023-12-20 10:23:48 -06:00
|
|
|
addr := accounts[idx]
|
|
|
|
return addr, c.senders[addr].Nonce
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// IncNonce increases the specified signing account's pending nonce.
|
|
|
|
func (c *Chain) IncNonce(addr common.Address, amt uint64) {
|
|
|
|
if _, ok := c.senders[addr]; !ok {
|
|
|
|
panic("nonce increment for non-signer")
|
|
|
|
}
|
|
|
|
c.senders[addr].Nonce += amt
|
|
|
|
}
|
2020-09-23 08:18:17 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Balance returns the balance of an account at the head of the chain.
|
|
|
|
func (c *Chain) Balance(addr common.Address) *big.Int {
|
|
|
|
bal := new(big.Int)
|
|
|
|
if acc, ok := c.state[addr]; ok {
|
|
|
|
bal, _ = bal.SetString(acc.Balance, 10)
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
return bal
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// SignTx signs a transaction for the specified from account, so long as that
|
|
|
|
// account was in the hivechain accounts dump.
|
|
|
|
func (c *Chain) SignTx(from common.Address, tx *types.Transaction) (*types.Transaction, error) {
|
|
|
|
signer := types.LatestSigner(c.config)
|
|
|
|
acc, ok := c.senders[from]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("account not available for signing: %s", from)
|
|
|
|
}
|
|
|
|
return types.SignTx(tx, signer, acc.Key)
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// GetHeaders returns the headers base on an ethGetPacketHeadersPacket.
|
|
|
|
func (c *Chain) GetHeaders(req *eth.GetBlockHeadersPacket) ([]*types.Header, error) {
|
2020-10-07 10:22:44 -05:00
|
|
|
if req.Amount < 1 {
|
2023-05-24 05:21:29 -05:00
|
|
|
return nil, errors.New("no block headers requested")
|
2020-10-07 10:22:44 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
var (
|
|
|
|
headers = make([]*types.Header, req.Amount)
|
|
|
|
blockNumber uint64
|
|
|
|
)
|
|
|
|
// Range over blocks to check if our chain has the requested header.
|
2020-10-07 10:22:44 -05:00
|
|
|
for _, block := range c.blocks {
|
|
|
|
if block.Hash() == req.Origin.Hash || block.Number().Uint64() == req.Origin.Number {
|
|
|
|
headers[0] = block.Header()
|
|
|
|
blockNumber = block.Number().Uint64()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if headers[0] == nil {
|
|
|
|
return nil, fmt.Errorf("no headers found for given origin number %v, hash %v", req.Origin.Number, req.Origin.Hash)
|
|
|
|
}
|
|
|
|
if req.Reverse {
|
|
|
|
for i := 1; i < int(req.Amount); i++ {
|
|
|
|
blockNumber -= (1 - req.Skip)
|
|
|
|
headers[i] = c.blocks[blockNumber].Header()
|
|
|
|
}
|
|
|
|
return headers, nil
|
|
|
|
}
|
|
|
|
for i := 1; i < int(req.Amount); i++ {
|
|
|
|
blockNumber += (1 + req.Skip)
|
|
|
|
headers[i] = c.blocks[blockNumber].Header()
|
|
|
|
}
|
|
|
|
return headers, nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Shorten returns a copy chain of a desired height from the imported
|
|
|
|
func (c *Chain) Shorten(height int) *Chain {
|
|
|
|
blocks := make([]*types.Block, height)
|
|
|
|
copy(blocks, c.blocks[:height])
|
2021-04-23 04:15:42 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
config := *c.config
|
|
|
|
return &Chain{
|
|
|
|
blocks: blocks,
|
|
|
|
config: &config,
|
2021-04-23 04:15:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadGenesis(genesisFile string) (core.Genesis, error) {
|
2022-05-16 04:59:35 -05:00
|
|
|
chainConfig, err := os.ReadFile(genesisFile)
|
2021-04-23 04:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return core.Genesis{}, err
|
|
|
|
}
|
2020-10-23 06:34:44 -05:00
|
|
|
var gen core.Genesis
|
|
|
|
if err := json.Unmarshal(chainConfig, &gen); err != nil {
|
2021-04-23 04:15:42 -05:00
|
|
|
return core.Genesis{}, err
|
2020-10-23 06:34:44 -05:00
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
return gen, nil
|
|
|
|
}
|
2020-10-23 06:34:44 -05:00
|
|
|
|
2021-04-23 04:15:42 -05:00
|
|
|
func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
|
2020-10-23 06:34:44 -05:00
|
|
|
// Load chain.rlp.
|
2020-09-23 08:18:17 -05:00
|
|
|
fh, err := os.Open(chainfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
var reader io.Reader = fh
|
|
|
|
if strings.HasSuffix(chainfile, ".gz") {
|
|
|
|
if reader, err = gzip.NewReader(reader); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream := rlp.NewStream(reader, 0)
|
2020-10-23 06:34:44 -05:00
|
|
|
var blocks = make([]*types.Block, 1)
|
|
|
|
blocks[0] = gblock
|
2020-09-23 08:18:17 -05:00
|
|
|
for i := 0; ; i++ {
|
|
|
|
var b types.Block
|
|
|
|
if err := stream.Decode(&b); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2020-10-23 06:34:44 -05:00
|
|
|
return nil, fmt.Errorf("at block index %d: %v", i, err)
|
|
|
|
}
|
|
|
|
if b.NumberU64() != uint64(i+1) {
|
|
|
|
return nil, fmt.Errorf("block at index %d has wrong number %d", i, b.NumberU64())
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
blocks = append(blocks, &b)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
return blocks, nil
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
|
|
|
|
func readState(file string) (map[common.Address]state.DumpAccount, error) {
|
|
|
|
f, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read state: %v", err)
|
|
|
|
}
|
|
|
|
var dump state.Dump
|
|
|
|
if err := json.Unmarshal(f, &dump); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := make(map[common.Address]state.DumpAccount)
|
|
|
|
for key, acct := range dump.Accounts {
|
|
|
|
var addr common.Address
|
|
|
|
if err := addr.UnmarshalText([]byte(key)); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid address %q", key)
|
|
|
|
}
|
|
|
|
state[addr] = acct
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAccounts(file string) (map[common.Address]*senderInfo, error) {
|
|
|
|
f, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read state: %v", err)
|
|
|
|
}
|
|
|
|
type account struct {
|
|
|
|
Key hexutil.Bytes `json:"key"`
|
|
|
|
}
|
|
|
|
keys := make(map[common.Address]account)
|
|
|
|
if err := json.Unmarshal(f, &keys); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal accounts: %v", err)
|
|
|
|
}
|
|
|
|
accounts := make(map[common.Address]*senderInfo)
|
|
|
|
for addr, acc := range keys {
|
|
|
|
pk, err := crypto.HexToECDSA(common.Bytes2Hex(acc.Key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read private key for %s: %v", err, addr)
|
|
|
|
}
|
|
|
|
accounts[addr] = &senderInfo{Key: pk, Nonce: 0}
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|