forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gen_genesis.go: wip on generating knot
- Loading branch information
1 parent
5e203aa
commit a8b9677
Showing
7 changed files
with
265 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spruce-solutions/go-quai/consensus/blake3" | ||
"github.com/spruce-solutions/go-quai/core" | ||
"github.com/spruce-solutions/go-quai/core/rawdb" | ||
"github.com/spruce-solutions/go-quai/params" | ||
) | ||
|
||
func main() { | ||
var ( | ||
testdb = rawdb.NewMemoryDatabase() | ||
genesis = core.RopstenPrimeGenesisBlock().MustCommit(testdb) | ||
fn = "test_knot.rlp" | ||
) | ||
|
||
blake3Config := blake3.Config{ | ||
MiningThreads: 0, | ||
NotifyFull: true, | ||
} | ||
|
||
blake3Engine, err := blake3.New(blake3Config, nil, false) | ||
if nil != err { | ||
log.Fatal("Failed to create Blake3 engine: ", err) | ||
} | ||
|
||
blocks, _ := core.GenerateKnot(params.TestChainConfig, genesis, blake3Engine, testdb, 9, nil) | ||
|
||
// Open the file handle and potentially wrap with a gzip stream | ||
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) | ||
if err != nil { | ||
log.Panic("error opening file") | ||
} | ||
defer fh.Close() | ||
|
||
var writer io.Writer = fh | ||
if strings.HasSuffix(fn, ".gz") { | ||
writer = gzip.NewWriter(writer) | ||
defer writer.(*gzip.Writer).Close() | ||
} | ||
|
||
for _, block := range blocks { | ||
|
||
if err := block.EncodeRLP(writer); err != nil { | ||
log.Panic("error writing") | ||
} | ||
} | ||
|
||
log.Println("Exported blockchain", "file", fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package core | ||
|
||
import ( | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spruce-solutions/go-quai/core/types" | ||
"github.com/spruce-solutions/go-quai/rlp" | ||
) | ||
|
||
func ReadKnot(chainfile string) []*types.Block { | ||
// Load chain.rlp. | ||
fmt.Println("Loading ReadKnot") | ||
fh, err := os.Open(chainfile) | ||
if err != nil { | ||
fmt.Println("OPEN ERR 1", err) | ||
return nil | ||
} | ||
defer fh.Close() | ||
var reader io.Reader = fh | ||
if strings.HasSuffix(chainfile, ".gz") { | ||
if reader, err = gzip.NewReader(reader); err != nil { | ||
fmt.Println("READER ERR 1", err) | ||
return nil | ||
} | ||
} | ||
stream := rlp.NewStream(reader, 0) | ||
var blocks = make([]*types.Block, 1) | ||
for i := 0; ; i++ { | ||
var b types.Block | ||
if err := stream.Decode(&b); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
fmt.Println("DECODE ERR 1", err) | ||
return nil | ||
} | ||
if b.NumberU64() != uint64(i+1) { | ||
fmt.Println("DECODE ERR 2", err) | ||
return nil | ||
} | ||
blocks = append(blocks, &b) | ||
} | ||
for _, block := range blocks { | ||
fmt.Println(block) | ||
} | ||
return blocks | ||
} |
Oops, something went wrong.