@@ -5,86 +5,77 @@ import (
5
5
"context"
6
6
"encoding/binary"
7
7
"errors"
8
+ "fmt"
8
9
"io"
9
10
10
11
"github.com/filecoin-project/go-f3/gpbft"
11
- "github.com/ipfs/go-datastore"
12
- xerrors "golang.org/x/xerrors"
12
+ "github.com/filecoin-project/go-state-types/cbor"
13
13
)
14
14
15
- var ErrlatestCertificateNil = errors .New ("latest certificate is not available " )
15
+ var ErrUnknownLatestCertificate = errors .New ("latest certificate is not known " )
16
16
17
- // Exports an F3 snapshot that includes the finality certificate chain until the current `latestCertificate`.
17
+ // ExportLatestSnapshot exports an F3 snapshot that includes the finality certificate chain until the current `latestCertificate`.
18
18
func (cs * Store ) ExportLatestSnapshot (ctx context.Context , writer io.Writer ) error {
19
19
if cs .latestCertificate == nil {
20
- return ErrlatestCertificateNil
20
+ return ErrUnknownLatestCertificate
21
21
}
22
22
return cs .ExportSnapshot (ctx , cs .latestCertificate .GPBFTInstance , writer )
23
23
}
24
24
25
- // Exports an F3 snapshot that includes the finality certificate chain until the specified `lastInstance`.
26
- func (cs * Store ) ExportSnapshot (ctx context.Context , lastInstance uint64 , writer io.Writer ) error {
25
+ // ExportSnapshot exports an F3 snapshot that includes the finality certificate chain from the `Store.firstInstance` to the specified `lastInstance`.
26
+ func (cs * Store ) ExportSnapshot (ctx context.Context , latestInstance uint64 , writer io.Writer ) error {
27
27
initialPowerTable , err := cs .GetPowerTable (ctx , cs .firstInstance )
28
28
if err != nil {
29
- return xerrors .Errorf ("failed to get initial power table at instance %d: %w" , cs .firstInstance , err )
29
+ return fmt .Errorf ("failed to get initial power table at instance %d: %w" , cs .firstInstance , err )
30
30
}
31
- header := SnapshotHeader {1 , cs .firstInstance , lastInstance , initialPowerTable }
32
- if err := header .WriteToSnapshot (writer ); err != nil {
33
- return xerrors .Errorf ("failed to write snapshot header: %w" , err )
31
+ header := SnapshotHeader {1 , cs .firstInstance , latestInstance , initialPowerTable }
32
+ if _ , err := header .WriteTo (writer ); err != nil {
33
+ return fmt .Errorf ("failed to write snapshot header: %w" , err )
34
34
}
35
- for i := cs .firstInstance ; i <= lastInstance ; i ++ {
35
+ for i := cs .firstInstance ; i <= latestInstance ; i ++ {
36
36
cert , err := cs .ds .Get (ctx , cs .keyForCert (i ))
37
37
if err != nil {
38
- return xerrors .Errorf ("failed to get certificate at instance %d:: %w" , i , err )
38
+ return fmt .Errorf ("failed to get certificate at instance %d:: %w" , i , err )
39
39
}
40
40
buffer := bytes .NewBuffer (cert )
41
- if err := writeSnapshotBlockBytes (writer , buffer ); err != nil {
41
+ if _ , err := writeSnapshotBlockBytes (writer , buffer ); err != nil {
42
42
return err
43
43
}
44
44
}
45
45
return nil
46
46
}
47
47
48
- // Imports an F3 snapshot and opens the certificate store.
49
- //
50
- // The passed Datastore has to be thread safe.
51
- func ImportSnapshotAndOpenStore (ctx context.Context , ds datastore.Datastore ) error {
52
- return xerrors .New ("to be implemented" )
53
- }
54
-
55
48
type SnapshotHeader struct {
56
49
Version uint64
57
50
FirstInstance uint64
58
51
LatestInstance uint64
59
52
InitialPowerTable gpbft.PowerEntries
60
53
}
61
54
62
- func (h * SnapshotHeader ) WriteToSnapshot ( writer io.Writer ) error {
63
- return writeSnapshotCborEncodedBlock (writer , h )
55
+ func (h * SnapshotHeader ) WriteTo ( w io.Writer ) ( int64 , error ) {
56
+ return writeSnapshotCborEncodedBlock (w , h )
64
57
}
65
58
66
- // Writes CBOR-encoded header or data block with a varint-encoded length prefix
67
- func writeSnapshotCborEncodedBlock (writer io.Writer , block MarshalCBOR ) error {
59
+ // writeSnapshotCborEncodedBlock writes CBOR-encoded header or data block with a varint-encoded length prefix
60
+ func writeSnapshotCborEncodedBlock (writer io.Writer , block cbor. Marshaler ) ( int64 , error ) {
68
61
var buffer bytes.Buffer
69
62
if err := block .MarshalCBOR (& buffer ); err != nil {
70
- return err
63
+ return 0 , err
71
64
}
72
65
return writeSnapshotBlockBytes (writer , & buffer )
73
66
}
74
67
75
- // Writes header or data block with a varint-encoded length prefix
76
- func writeSnapshotBlockBytes (writer io.Writer , buffer * bytes.Buffer ) error {
68
+ // writeSnapshotBlockBytes writes header or data block with a varint-encoded length prefix
69
+ func writeSnapshotBlockBytes (writer io.Writer , buffer * bytes.Buffer ) ( int64 , error ) {
77
70
buf := make ([]byte , 8 )
78
71
n := binary .PutUvarint (buf , uint64 (buffer .Len ()))
79
- if _ , err := writer .Write (buf [:n ]); err != nil {
80
- return err
72
+ len1 , err := bytes .NewBuffer (buf [:n ]).WriteTo (writer )
73
+ if err != nil {
74
+ return 0 , err
81
75
}
82
- if _ , err := buffer .WriteTo (writer ); err != nil {
83
- return err
76
+ len2 , err := buffer .WriteTo (writer )
77
+ if err != nil {
78
+ return 0 , err
84
79
}
85
- return nil
86
- }
87
-
88
- type MarshalCBOR interface {
89
- MarshalCBOR (w io.Writer ) error
80
+ return len1 + len2 , nil
90
81
}
0 commit comments