Use go-car for genesis file

This commit is contained in:
Łukasz Magiera
2019-07-25 13:46:48 +02:00
parent 99acdbade0
commit 515fbd41c2
4 changed files with 84 additions and 14 deletions
+17 -3
View File
@@ -1,6 +1,7 @@
package modules
import (
"bytes"
"context"
"crypto/rand"
"io"
@@ -11,6 +12,7 @@ import (
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-filestore"
@@ -195,10 +197,22 @@ func ErrorGenesis() Genesis {
}
}
func LoadGenesis(genBytes []byte) func() Genesis {
return func() Genesis {
func LoadGenesis(genBytes []byte) func(blockstore.Blockstore) Genesis {
return func(bs blockstore.Blockstore) Genesis {
return func() (header *chain.BlockHeader, e error) {
return chain.DecodeBlock(genBytes)
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
if err != nil {
return nil, err
}
if len(c.Roots) != 1 {
return nil, xerrors.New("expected genesis file to have one root")
}
root, err := bs.Get(c.Roots[0])
if err != nil {
return &chain.BlockHeader{}, err
}
return chain.DecodeBlock(root.RawData())
}
}
}
+14 -11
View File
@@ -1,11 +1,17 @@
package testing
import (
"context"
"io"
"os"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-merkledag"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/node/modules"
@@ -21,13 +27,11 @@ func MakeGenesisMem(out io.Writer) func(bs blockstore.Blockstore, w *chain.Walle
if err != nil {
return nil, err
}
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
if _, err := out.Write(genBytes); err != nil {
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, out); err != nil {
return nil, err
}
@@ -50,12 +54,11 @@ func MakeGenesis(outFile string) func(bs blockstore.Blockstore, w *chain.Wallet)
return nil, err
}
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
if _, err := f.Write(genBytes); err != nil {
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, f); err != nil {
return nil, err
}