2020-10-07 17:22:44 +02:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 20:39:40 +02:00
|
|
|
// This file is part of go-ethereum.
|
2020-10-07 17:22:44 +02:00
|
|
|
//
|
2022-05-24 20:39:40 +02: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 17:22:44 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-10-07 17:22:44 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 20:39:40 +02:00
|
|
|
// GNU General Public License for more details.
|
2020-10-07 17:22:44 +02:00
|
|
|
//
|
2022-05-24 20:39:40 +02: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 17:22:44 +02:00
|
|
|
|
2020-09-23 15:18:17 +02:00
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2023-12-20 09:23:48 -07:00
|
|
|
"bytes"
|
2020-09-23 15:18:17 +02:00
|
|
|
"compress/gzip"
|
2023-12-20 09:23:48 -07:00
|
|
|
"crypto/ecdsa"
|
2020-09-23 15:18:17 +02:00
|
|
|
"encoding/json"
|
2023-05-24 18:21:29 +08:00
|
|
|
"errors"
|
2020-09-23 15:18:17 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2024-03-12 17:00:34 +08:00
|
|
|
"path/filepath"
|
2024-03-25 07:50:18 +01:00
|
|
|
"slices"
|
2023-12-20 09:23:48 -07:00
|
|
|
"sort"
|
2020-09-23 15:18:17 +02:00
|
|
|
"strings"
|
|
|
|
|
2022-02-04 15:24:32 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-12-20 09:23:48 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-09-23 15:18:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
2023-12-20 09:23:48 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2020-09-23 15:18:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-12-20 09:23:48 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2020-09-23 15:18:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// Chain is a lightweight blockchain-like store which can read a hivechain
|
|
|
|
// created chain.
|
2020-09-23 15:18:17 +02:00
|
|
|
type Chain struct {
|
2023-12-20 09:23:48 -07: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 17:00:34 +08:00
|
|
|
gen, err := loadGenesis(filepath.Join(dir, "genesis.json"))
|
2023-12-20 09:23:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gblock := gen.ToBlock()
|
|
|
|
|
2024-03-12 17:00:34 +08:00
|
|
|
blocks, err := blocksFromFile(filepath.Join(dir, "chain.rlp"), gblock)
|
2023-12-20 09:23:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-12 17:00:34 +08:00
|
|
|
state, err := readState(filepath.Join(dir, "headstate.json"))
|
2023-12-20 09:23:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-12 17:00:34 +08:00
|
|
|
accounts, err := readAccounts(filepath.Join(dir, "accounts.json"))
|
2023-12-20 09:23:48 -07: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 {
|
|
|
|
addr := addr
|
|
|
|
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 15:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the chain.
|
|
|
|
func (c *Chain) Len() int {
|
|
|
|
return len(c.blocks)
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07: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 23:09:11 +02:00
|
|
|
// TD calculates the total difficulty of the chain at the
|
|
|
|
// chain head.
|
|
|
|
func (c *Chain) TD() *big.Int {
|
2022-06-14 14:09:48 +02:00
|
|
|
sum := new(big.Int)
|
2021-05-25 23:09:11 +02:00
|
|
|
for _, block := range c.blocks[:c.Len()] {
|
|
|
|
sum.Add(sum, block.Difficulty())
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// GetBlock returns the block at the specified number.
|
|
|
|
func (c *Chain) GetBlock(number int) *types.Block {
|
|
|
|
return c.blocks[number]
|
2020-09-23 15:18:17 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// RootAt returns the state root for the block at the given height.
|
2022-02-04 15:24:32 +01:00
|
|
|
func (c *Chain) RootAt(height int) common.Hash {
|
|
|
|
if height < c.Len() {
|
|
|
|
return c.blocks[height].Root()
|
|
|
|
}
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07: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) {
|
|
|
|
var accounts Addresses
|
|
|
|
for addr := range c.senders {
|
|
|
|
accounts = append(accounts, addr)
|
|
|
|
}
|
|
|
|
sort.Sort(accounts)
|
|
|
|
addr := accounts[idx]
|
|
|
|
return addr, c.senders[addr].Nonce
|
2020-09-23 15:18:17 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07: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 15:18:17 +02:00
|
|
|
|
2023-12-20 09:23:48 -07: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 15:18:17 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
return bal
|
2020-09-23 15:18:17 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07: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 15:18:17 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// GetHeaders returns the headers base on an ethGetPacketHeadersPacket.
|
|
|
|
func (c *Chain) GetHeaders(req *eth.GetBlockHeadersPacket) ([]*types.Header, error) {
|
2020-10-07 17:22:44 +02:00
|
|
|
if req.Amount < 1 {
|
2023-05-24 18:21:29 +08:00
|
|
|
return nil, errors.New("no block headers requested")
|
2020-10-07 17:22:44 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07: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 17:22:44 +02: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 09:23:48 -07: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 11:15:42 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
config := *c.config
|
|
|
|
return &Chain{
|
|
|
|
blocks: blocks,
|
|
|
|
config: &config,
|
2021-04-23 11:15:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadGenesis(genesisFile string) (core.Genesis, error) {
|
2022-05-16 11:59:35 +02:00
|
|
|
chainConfig, err := os.ReadFile(genesisFile)
|
2021-04-23 11:15:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return core.Genesis{}, err
|
|
|
|
}
|
2020-10-23 13:34:44 +02:00
|
|
|
var gen core.Genesis
|
|
|
|
if err := json.Unmarshal(chainConfig, &gen); err != nil {
|
2021-04-23 11:15:42 +02:00
|
|
|
return core.Genesis{}, err
|
2020-10-23 13:34:44 +02:00
|
|
|
}
|
2021-04-23 11:15:42 +02:00
|
|
|
return gen, nil
|
|
|
|
}
|
2020-10-23 13:34:44 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
type Addresses []common.Address
|
|
|
|
|
|
|
|
func (a Addresses) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Addresses) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(a[i][:], a[j][:]) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Addresses) Swap(i, j int) {
|
|
|
|
tmp := a[i]
|
|
|
|
a[i] = a[j]
|
|
|
|
a[j] = tmp
|
|
|
|
}
|
|
|
|
|
2021-04-23 11:15:42 +02:00
|
|
|
func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
|
2020-10-23 13:34:44 +02:00
|
|
|
// Load chain.rlp.
|
2020-09-23 15:18:17 +02: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 13:34:44 +02:00
|
|
|
var blocks = make([]*types.Block, 1)
|
|
|
|
blocks[0] = gblock
|
2020-09-23 15:18:17 +02: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 13:34:44 +02: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 15:18:17 +02:00
|
|
|
}
|
|
|
|
blocks = append(blocks, &b)
|
|
|
|
}
|
2021-04-23 11:15:42 +02:00
|
|
|
return blocks, nil
|
2020-09-23 15:18:17 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07: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
|
|
|
|
}
|