|
| 1 | +package ethtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "compress/gzip" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "math/big" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/ethereum/go-ethereum/core" |
| 14 | + "github.com/ethereum/go-ethereum/core/forkid" |
| 15 | + "github.com/ethereum/go-ethereum/core/types" |
| 16 | + "github.com/ethereum/go-ethereum/params" |
| 17 | + "github.com/ethereum/go-ethereum/rlp" |
| 18 | +) |
| 19 | + |
| 20 | +type Chain struct { |
| 21 | + blocks []*types.Block |
| 22 | + chainConfig *params.ChainConfig |
| 23 | +} |
| 24 | + |
| 25 | +func (c *Chain) WriteTo(writer io.Writer) error { |
| 26 | + for _, block := range c.blocks { |
| 27 | + if err := rlp.Encode(writer, block); err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +// Len returns the length of the chain. |
| 36 | +func (c *Chain) Len() int { |
| 37 | + return len(c.blocks) |
| 38 | +} |
| 39 | + |
| 40 | +// TD calculates the total difficulty of the chain. |
| 41 | +func (c *Chain) TD(height int) *big.Int { // TODO later on channge scheme so that the height is included in range |
| 42 | + sum := big.NewInt(0) |
| 43 | + for _, block := range c.blocks[:height] { |
| 44 | + sum.Add(sum, block.Difficulty()) |
| 45 | + } |
| 46 | + return sum |
| 47 | +} |
| 48 | + |
| 49 | +// ForkID gets the fork id of the chain. |
| 50 | +func (c *Chain) ForkID() forkid.ID { |
| 51 | + return forkid.NewID(c.chainConfig, c.blocks[0].Hash(), uint64(c.Len())) |
| 52 | +} |
| 53 | + |
| 54 | +// Shorten returns a copy chain of a desired height from the imported |
| 55 | +func (c *Chain) Shorten(height int) *Chain { |
| 56 | + blocks := make([]*types.Block, height) |
| 57 | + copy(blocks, c.blocks[:height]) |
| 58 | + |
| 59 | + config := *c.chainConfig |
| 60 | + return &Chain{ |
| 61 | + blocks: blocks, |
| 62 | + chainConfig: &config, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Head returns the chain head. |
| 67 | +func (c *Chain) Head() *types.Block { |
| 68 | + return c.blocks[c.Len()-1] |
| 69 | +} |
| 70 | + |
| 71 | +// loadChain takes the given chain.rlp file, and decodes and returns |
| 72 | +// the blocks from the file. |
| 73 | +func loadChain(chainfile string, genesis string) (*Chain, error) { |
| 74 | + // Open the file handle and potentially unwrap the gzip stream |
| 75 | + fh, err := os.Open(chainfile) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + defer fh.Close() |
| 80 | + |
| 81 | + var reader io.Reader = fh |
| 82 | + if strings.HasSuffix(chainfile, ".gz") { |
| 83 | + if reader, err = gzip.NewReader(reader); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + } |
| 87 | + stream := rlp.NewStream(reader, 0) |
| 88 | + var blocks []*types.Block |
| 89 | + for i := 0; ; i++ { |
| 90 | + var b types.Block |
| 91 | + if err := stream.Decode(&b); err == io.EOF { |
| 92 | + break |
| 93 | + } else if err != nil { |
| 94 | + return nil, fmt.Errorf("at block %d: %v", i, err) |
| 95 | + } |
| 96 | + blocks = append(blocks, &b) |
| 97 | + } |
| 98 | + |
| 99 | + // Open the file handle and potentially unwrap the gzip stream |
| 100 | + chainConfig, err := ioutil.ReadFile(genesis) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + var gen core.Genesis |
| 105 | + if err := json.Unmarshal(chainConfig, &gen); err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + return &Chain{ |
| 110 | + blocks: blocks, |
| 111 | + chainConfig: gen.Config, |
| 112 | + }, nil |
| 113 | +} |
0 commit comments