github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/cmd/devp2p/internal/ethtest/chain.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethtest 18 19 import ( 20 "compress/gzip" 21 "encoding/json" 22 "fmt" 23 "io" 24 "io/ioutil" 25 "math/big" 26 "os" 27 "strings" 28 29 "github.com/fff-chain/go-fff/core" 30 "github.com/fff-chain/go-fff/core/forkid" 31 "github.com/fff-chain/go-fff/core/types" 32 "github.com/fff-chain/go-fff/params" 33 "github.com/fff-chain/go-fff/rlp" 34 ) 35 36 type Chain struct { 37 genesis core.Genesis 38 blocks []*types.Block 39 chainConfig *params.ChainConfig 40 } 41 42 func (c *Chain) WriteTo(writer io.Writer) error { 43 for _, block := range c.blocks { 44 if err := rlp.Encode(writer, block); err != nil { 45 return err 46 } 47 } 48 49 return nil 50 } 51 52 // Len returns the length of the chain. 53 func (c *Chain) Len() int { 54 return len(c.blocks) 55 } 56 57 // TD calculates the total difficulty of the chain. 58 func (c *Chain) TD(height int) *big.Int { // TODO later on channge scheme so that the height is included in range 59 sum := big.NewInt(0) 60 for _, block := range c.blocks[:height] { 61 sum.Add(sum, block.Difficulty()) 62 } 63 return sum 64 } 65 66 // ForkID gets the fork id of the chain. 67 func (c *Chain) ForkID() forkid.ID { 68 return forkid.NewID(c.chainConfig, c.blocks[0].Hash(), uint64(c.Len())) 69 } 70 71 // Shorten returns a copy chain of a desired height from the imported 72 func (c *Chain) Shorten(height int) *Chain { 73 blocks := make([]*types.Block, height) 74 copy(blocks, c.blocks[:height]) 75 76 config := *c.chainConfig 77 return &Chain{ 78 blocks: blocks, 79 chainConfig: &config, 80 } 81 } 82 83 // Head returns the chain head. 84 func (c *Chain) Head() *types.Block { 85 return c.blocks[c.Len()-1] 86 } 87 88 func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) { 89 if req.Amount < 1 { 90 return nil, fmt.Errorf("no block headers requested") 91 } 92 93 headers := make(BlockHeaders, req.Amount) 94 var blockNumber uint64 95 96 // range over blocks to check if our chain has the requested header 97 for _, block := range c.blocks { 98 if block.Hash() == req.Origin.Hash || block.Number().Uint64() == req.Origin.Number { 99 headers[0] = block.Header() 100 blockNumber = block.Number().Uint64() 101 } 102 } 103 if headers[0] == nil { 104 return nil, fmt.Errorf("no headers found for given origin number %v, hash %v", req.Origin.Number, req.Origin.Hash) 105 } 106 107 if req.Reverse { 108 for i := 1; i < int(req.Amount); i++ { 109 blockNumber -= (1 - req.Skip) 110 headers[i] = c.blocks[blockNumber].Header() 111 112 } 113 114 return headers, nil 115 } 116 117 for i := 1; i < int(req.Amount); i++ { 118 blockNumber += (1 + req.Skip) 119 headers[i] = c.blocks[blockNumber].Header() 120 } 121 122 return headers, nil 123 } 124 125 // loadChain takes the given chain.rlp file, and decodes and returns 126 // the blocks from the file. 127 func loadChain(chainfile string, genesis string) (*Chain, error) { 128 gen, err := loadGenesis(genesis) 129 if err != nil { 130 return nil, err 131 } 132 gblock := gen.ToBlock(nil) 133 134 blocks, err := blocksFromFile(chainfile, gblock) 135 if err != nil { 136 return nil, err 137 } 138 139 c := &Chain{genesis: gen, blocks: blocks, chainConfig: gen.Config} 140 return c, nil 141 } 142 143 func loadGenesis(genesisFile string) (core.Genesis, error) { 144 chainConfig, err := ioutil.ReadFile(genesisFile) 145 if err != nil { 146 return core.Genesis{}, err 147 } 148 var gen core.Genesis 149 if err := json.Unmarshal(chainConfig, &gen); err != nil { 150 return core.Genesis{}, err 151 } 152 return gen, nil 153 } 154 155 func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) { 156 // Load chain.rlp. 157 fh, err := os.Open(chainfile) 158 if err != nil { 159 return nil, err 160 } 161 defer fh.Close() 162 var reader io.Reader = fh 163 if strings.HasSuffix(chainfile, ".gz") { 164 if reader, err = gzip.NewReader(reader); err != nil { 165 return nil, err 166 } 167 } 168 stream := rlp.NewStream(reader, 0) 169 var blocks = make([]*types.Block, 1) 170 blocks[0] = gblock 171 for i := 0; ; i++ { 172 var b types.Block 173 if err := stream.Decode(&b); err == io.EOF { 174 break 175 } else if err != nil { 176 return nil, fmt.Errorf("at block index %d: %v", i, err) 177 } 178 if b.NumberU64() != uint64(i+1) { 179 return nil, fmt.Errorf("block at index %d has wrong number %d", i, b.NumberU64()) 180 } 181 blocks = append(blocks, &b) 182 } 183 return blocks, nil 184 }