2020-10-07 18:22:44 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 21:39:40 +03:00
|
|
|
// This file is part of go-ethereum.
|
2020-10-07 18:22:44 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03: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 18:22:44 +03:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 21:39:40 +03:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-10-07 18:22:44 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 21:39:40 +03:00
|
|
|
// GNU General Public License for more details.
|
2020-10-07 18:22:44 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03: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 18:22:44 +03:00
|
|
|
|
2020-09-23 16:18:17 +03:00
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-04 17:24:32 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-09-23 16:18:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Chain struct {
|
2021-04-23 12:15:42 +03:00
|
|
|
genesis core.Genesis
|
2020-09-23 16:18:17 +03:00
|
|
|
blocks []*types.Block
|
|
|
|
chainConfig *params.ChainConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the chain.
|
|
|
|
func (c *Chain) Len() int {
|
|
|
|
return len(c.blocks)
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// TD calculates the total difficulty of the chain at the
|
|
|
|
// chain head.
|
|
|
|
func (c *Chain) TD() *big.Int {
|
2022-06-14 15:09:48 +03:00
|
|
|
sum := new(big.Int)
|
2021-05-26 00:09:11 +03:00
|
|
|
for _, block := range c.blocks[:c.Len()] {
|
|
|
|
sum.Add(sum, block.Difficulty())
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalDifficultyAt calculates the total difficulty of the chain
|
|
|
|
// at the given block height.
|
|
|
|
func (c *Chain) TotalDifficultyAt(height int) *big.Int {
|
2022-06-14 15:09:48 +03:00
|
|
|
sum := new(big.Int)
|
2021-05-26 00:09:11 +03:00
|
|
|
if height >= c.Len() {
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
for _, block := range c.blocks[:height+1] {
|
2020-09-23 16:18:17 +03:00
|
|
|
sum.Add(sum, block.Difficulty())
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2022-02-04 17:24:32 +03:00
|
|
|
func (c *Chain) RootAt(height int) common.Hash {
|
|
|
|
if height < c.Len() {
|
|
|
|
return c.blocks[height].Root()
|
|
|
|
}
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2020-09-23 16:18:17 +03:00
|
|
|
// ForkID gets the fork id of the chain.
|
|
|
|
func (c *Chain) ForkID() forkid.ID {
|
|
|
|
return forkid.NewID(c.chainConfig, c.blocks[0].Hash(), uint64(c.Len()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
|
|
|
|
config := *c.chainConfig
|
|
|
|
return &Chain{
|
|
|
|
blocks: blocks,
|
|
|
|
chainConfig: &config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head returns the chain head.
|
|
|
|
func (c *Chain) Head() *types.Block {
|
|
|
|
return c.blocks[c.Len()-1]
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
func (c *Chain) GetHeaders(req *GetBlockHeaders) ([]*types.Header, error) {
|
2020-10-07 18:22:44 +03:00
|
|
|
if req.Amount < 1 {
|
|
|
|
return nil, fmt.Errorf("no block headers requested")
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
headers := make([]*types.Header, req.Amount)
|
2020-10-07 18:22:44 +03:00
|
|
|
var blockNumber uint64
|
|
|
|
|
|
|
|
// range over blocks to check if our chain has the requested header
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-23 16:18:17 +03:00
|
|
|
// loadChain takes the given chain.rlp file, and decodes and returns
|
|
|
|
// the blocks from the file.
|
|
|
|
func loadChain(chainfile string, genesis string) (*Chain, error) {
|
2021-04-23 12:15:42 +03:00
|
|
|
gen, err := loadGenesis(genesis)
|
2020-10-23 14:34:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
gblock := gen.ToBlock(nil)
|
|
|
|
|
|
|
|
blocks, err := blocksFromFile(chainfile, gblock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Chain{genesis: gen, blocks: blocks, chainConfig: gen.Config}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadGenesis(genesisFile string) (core.Genesis, error) {
|
2022-05-16 12:59:35 +03:00
|
|
|
chainConfig, err := os.ReadFile(genesisFile)
|
2021-04-23 12:15:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return core.Genesis{}, err
|
|
|
|
}
|
2020-10-23 14:34:44 +03:00
|
|
|
var gen core.Genesis
|
|
|
|
if err := json.Unmarshal(chainConfig, &gen); err != nil {
|
2021-04-23 12:15:42 +03:00
|
|
|
return core.Genesis{}, err
|
2020-10-23 14:34:44 +03:00
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
return gen, nil
|
|
|
|
}
|
2020-10-23 14:34:44 +03:00
|
|
|
|
2021-04-23 12:15:42 +03:00
|
|
|
func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
|
2020-10-23 14:34:44 +03:00
|
|
|
// Load chain.rlp.
|
2020-09-23 16:18:17 +03: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 14:34:44 +03:00
|
|
|
var blocks = make([]*types.Block, 1)
|
|
|
|
blocks[0] = gblock
|
2020-09-23 16:18:17 +03: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 14:34:44 +03: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 16:18:17 +03:00
|
|
|
}
|
|
|
|
blocks = append(blocks, &b)
|
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
return blocks, nil
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|