Skip to content

Commit 9ac1e74

Browse files
committed
resolve comments
1 parent 896dde4 commit 9ac1e74

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

certstore/snapshot.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,77 @@ import (
55
"context"
66
"encoding/binary"
77
"errors"
8+
"fmt"
89
"io"
910

1011
"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"
1313
)
1414

15-
var ErrlatestCertificateNil = errors.New("latest certificate is not available")
15+
var ErrUnknownLatestCertificate = errors.New("latest certificate is not known")
1616

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`.
1818
func (cs *Store) ExportLatestSnapshot(ctx context.Context, writer io.Writer) error {
1919
if cs.latestCertificate == nil {
20-
return ErrlatestCertificateNil
20+
return ErrUnknownLatestCertificate
2121
}
2222
return cs.ExportSnapshot(ctx, cs.latestCertificate.GPBFTInstance, writer)
2323
}
2424

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 {
2727
initialPowerTable, err := cs.GetPowerTable(ctx, cs.firstInstance)
2828
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)
3030
}
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)
3434
}
35-
for i := cs.firstInstance; i <= lastInstance; i++ {
35+
for i := cs.firstInstance; i <= latestInstance; i++ {
3636
cert, err := cs.ds.Get(ctx, cs.keyForCert(i))
3737
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)
3939
}
4040
buffer := bytes.NewBuffer(cert)
41-
if err := writeSnapshotBlockBytes(writer, buffer); err != nil {
41+
if _, err := writeSnapshotBlockBytes(writer, buffer); err != nil {
4242
return err
4343
}
4444
}
4545
return nil
4646
}
4747

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-
5548
type SnapshotHeader struct {
5649
Version uint64
5750
FirstInstance uint64
5851
LatestInstance uint64
5952
InitialPowerTable gpbft.PowerEntries
6053
}
6154

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)
6457
}
6558

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) {
6861
var buffer bytes.Buffer
6962
if err := block.MarshalCBOR(&buffer); err != nil {
70-
return err
63+
return 0, err
7164
}
7265
return writeSnapshotBlockBytes(writer, &buffer)
7366
}
7467

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) {
7770
buf := make([]byte, 8)
7871
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
8175
}
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
8479
}
85-
return nil
86-
}
87-
88-
type MarshalCBOR interface {
89-
MarshalCBOR(w io.Writer) error
80+
return len1 + len2, nil
9081
}

certstore/snapshot_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package certstore

f3.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ func (m *F3) GetCert(ctx context.Context, instance uint64) (*certs.FinalityCerti
126126
return nil, ErrF3NotRunning
127127
}
128128

129+
func (m *F3) GetCertStore(ctx context.Context) (*certstore.Store, error) {
130+
if state := m.state.Load(); state != nil {
131+
return state.cs, nil
132+
}
133+
return nil, ErrF3NotRunning
134+
}
135+
129136
// GetPowerTableByInstance returns the power table (committee) used to validate the specified instance.
130137
func (m *F3) GetPowerTableByInstance(ctx context.Context, instance uint64) (gpbft.PowerEntries, error) {
131138
if state := m.state.Load(); state != nil {

0 commit comments

Comments
 (0)