github.com/dominant-strategies/go-quai@v0.28.2/core/knot.go (about)

     1  package core
     2  
     3  import (
     4  	"compress/gzip"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/dominant-strategies/go-quai/common"
    10  	"github.com/dominant-strategies/go-quai/core/types"
    11  	"github.com/dominant-strategies/go-quai/log"
    12  	"github.com/dominant-strategies/go-quai/rlp"
    13  	"github.com/dominant-strategies/go-quai/trie"
    14  )
    15  
    16  func ReadKnot(chainfile string) []*types.Block {
    17  	nodeCtx := common.NodeLocation.Context()
    18  	// Load chain.rlp.
    19  	fh, err := os.Open(chainfile)
    20  	if err != nil {
    21  		log.Error("Error in ReadKnot", "Err", err)
    22  		return nil
    23  	}
    24  	defer fh.Close()
    25  	var reader io.Reader = fh
    26  	if strings.HasSuffix(chainfile, ".gz") {
    27  		if reader, err = gzip.NewReader(reader); err != nil {
    28  			log.Error("Error in ReadKnot", "Err", err)
    29  			return nil
    30  		}
    31  	}
    32  	stream := rlp.NewStream(reader, 0)
    33  	var blocks = make([]*types.Block, 0)
    34  	for i := 0; ; i++ {
    35  		b := &types.Block{}
    36  		if err := stream.Decode(b); err == io.EOF {
    37  			break
    38  		} else if err != nil {
    39  			log.Error("Error in ReadKnot", "Err", err)
    40  			return nil
    41  		}
    42  		h := b.Header()
    43  		// If we have a subordinate, we need to rebuild the block with the correct
    44  		// manifest of subordinate blocks
    45  		if nodeCtx < common.ZONE_CTX {
    46  			subManifest := types.BlockManifest{h.ParentHash(nodeCtx + 1)}
    47  			b = types.NewBlock(h, nil, nil, nil, subManifest, nil, trie.NewStackTrie(nil))
    48  		}
    49  		blocks = append(blocks, b)
    50  	}
    51  	return blocks
    52  }