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