Skip to content

Commit

Permalink
Add legacy backup support
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Oct 6, 2023
1 parent 8db272f commit d6c9f53
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 50 deletions.
32 changes: 4 additions & 28 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ package backup
import (
"context"
"fmt"
"io"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/tx-archive/backup/client"
"github.com/gnolang/tx-archive/backup/writer"
"github.com/gnolang/tx-archive/log"
"github.com/gnolang/tx-archive/log/noop"
"github.com/gnolang/tx-archive/types"
Expand All @@ -18,12 +17,12 @@ import (
// Service is the chain backup service
type Service struct {
client client.Client
writer io.Writer
writer writer.Writer
logger log.Logger
}

// NewService creates a new backup service
func NewService(client client.Client, writer io.Writer, opts ...Option) *Service {
func NewService(client client.Client, writer writer.Writer, opts ...Option) *Service {
s := &Service{
client: client,
writer: writer,
Expand Down Expand Up @@ -76,7 +75,7 @@ func (s *Service) ExecuteBackup(ctx context.Context, cfg Config) error {
}

// Write the tx data to the file
if writeErr := writeTxData(s.writer, data); writeErr != nil {
if writeErr := s.writer.WriteTxData(data); writeErr != nil {
return fmt.Errorf("unable to write tx data, %w", writeErr)
}
}
Expand Down Expand Up @@ -113,29 +112,6 @@ func determineRightBound(
return latestBlockNumber, nil
}

// writeTxData outputs the tx data to the writer
func writeTxData(writer io.Writer, txData *types.TxData) error {
// Marshal tx data into JSON
jsonData, err := amino.MarshalJSON(txData)
if err != nil {
return fmt.Errorf("unable to marshal JSON data, %w", err)
}

// Write the JSON data as a line to the file
_, err = writer.Write(jsonData)
if err != nil {
return fmt.Errorf("unable to write to output, %w", err)
}

// Write a newline character to separate JSON objects
_, err = writer.Write([]byte("\n"))
if err != nil {
return fmt.Errorf("unable to write newline output, %w", err)
}

return nil
}

// logProgress logs the backup progress
func logProgress(logger log.Logger, from, to, current uint64) {
total := to - from
Expand Down
3 changes: 2 additions & 1 deletion backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/tx-archive/backup/writer/standard"
"github.com/gnolang/tx-archive/log/noop"
"github.com/gnolang/tx-archive/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -119,7 +120,7 @@ func TestBackup_ExecuteBackup(t *testing.T) {
cfg.FromBlock = fromBlock
cfg.ToBlock = &toBlock

s := NewService(mockClient, tempFile, WithLogger(noop.New()))
s := NewService(mockClient, standard.NewWriter(tempFile), WithLogger(noop.New()))

// Run the backup procedure
require.NoError(
Expand Down
45 changes: 45 additions & 0 deletions backup/writer/legacy/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package legacy

//nolint:revive // See https://github.com/gnolang/gno/issues/1197
import (
"fmt"
"io"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/tx-archive/types"

_ "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
)

type Writer struct {
writer io.Writer
}

// NewWriter creates a new legacy tx writer
func NewWriter(writer io.Writer) *Writer {
return &Writer{
writer: writer,
}
}

func (w *Writer) WriteTxData(data *types.TxData) error {
// Marshal tx individual tx into JSON, instead of the entire tx data
jsonData, err := amino.MarshalJSON(data.Tx)
if err != nil {
return fmt.Errorf("unable to marshal JSON data, %w", err)
}

// Write the JSON data as a line to the file
_, err = w.writer.Write(jsonData)
if err != nil {
return fmt.Errorf("unable to write to output, %w", err)
}

// Write a newline character to separate JSON objects
_, err = w.writer.Write([]byte("\n"))
if err != nil {
return fmt.Errorf("unable to write newline output, %w", err)
}

return nil
}
40 changes: 40 additions & 0 deletions backup/writer/legacy/legacy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package legacy

import (
"bytes"
"testing"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/tx-archive/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWriter_Legacy(t *testing.T) {
t.Parallel()

var (
b bytes.Buffer

txData = &types.TxData{
Tx: std.Tx{
Memo: "example tx",
},
BlockNum: 10,
}
)

// Create a new standard writer
w := NewWriter(&b)

// Write example tx data
require.NoError(t, w.WriteTxData(txData))

var readTx std.Tx

readErr := amino.UnmarshalJSON(b.Bytes(), &readTx)
require.NoError(t, readErr)

assert.Equal(t, txData.Tx, readTx)
}
45 changes: 45 additions & 0 deletions backup/writer/standard/standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package standard

//nolint:revive // See https://github.com/gnolang/gno/issues/1197
import (
"fmt"
"io"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/tx-archive/types"

_ "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
)

type Writer struct {
writer io.Writer
}

// NewWriter creates a new standard tx data writer
func NewWriter(writer io.Writer) *Writer {
return &Writer{
writer: writer,
}
}

func (w *Writer) WriteTxData(data *types.TxData) error {
// Marshal the entire tx data into JSON
jsonData, err := amino.MarshalJSON(data)
if err != nil {
return fmt.Errorf("unable to marshal JSON data, %w", err)
}

// Write the JSON data as a line to the file
_, err = w.writer.Write(jsonData)
if err != nil {
return fmt.Errorf("unable to write to output, %w", err)
}

// Write a newline character to separate JSON objects
_, err = w.writer.Write([]byte("\n"))
if err != nil {
return fmt.Errorf("unable to write newline output, %w", err)
}

return nil
}
40 changes: 40 additions & 0 deletions backup/writer/standard/standard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package standard

import (
"bytes"
"testing"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/tx-archive/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWriter_Standard(t *testing.T) {
t.Parallel()

var (
b bytes.Buffer

txData = &types.TxData{
Tx: std.Tx{
Memo: "example tx",
},
BlockNum: 10,
}
)

// Create a new standard writer
w := NewWriter(&b)

// Write example tx data
require.NoError(t, w.WriteTxData(txData))

var readTx types.TxData

readErr := amino.UnmarshalJSON(b.Bytes(), &readTx)
require.NoError(t, readErr)

assert.Equal(t, *txData, readTx)
}
10 changes: 10 additions & 0 deletions backup/writer/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package writer

import "github.com/gnolang/tx-archive/types"

// Writer defines the backup writer interface
type Writer interface {
// WriteTxData outputs the given TX data
// to some kind of storage
WriteTxData(*types.TxData) error
}
21 changes: 20 additions & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

"github.com/gnolang/tx-archive/backup"
"github.com/gnolang/tx-archive/backup/client/http"
"github.com/gnolang/tx-archive/backup/writer"
"github.com/gnolang/tx-archive/backup/writer/legacy"
"github.com/gnolang/tx-archive/backup/writer/standard"
"github.com/peterbourgon/ff/v3/ffcli"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -36,6 +39,7 @@ type backupCfg struct {
fromBlock uint64

overwrite bool
legacy bool
}

// newBackupCmd creates the backup command
Expand Down Expand Up @@ -90,6 +94,13 @@ func (c *backupCfg) registerFlags(fs *flag.FlagSet) {
false,
"flag indicating if the output file should be overwritten during backup",
)

fs.BoolVar(
&c.legacy,
"legacy",
false,
"flag indicating if the legacy output format should be used (tx-per-line)",
)
}

// exec executes the backup command
Expand Down Expand Up @@ -162,10 +173,18 @@ func (c *backupCfg) exec(ctx context.Context, _ []string) error {
// Set up the teardown
defer teardown()

var w writer.Writer

if c.legacy {
w = legacy.NewWriter(outputFile)
} else {
w = standard.NewWriter(outputFile)
}

// Create the backup service
service := backup.NewService(
client,
outputFile,
w,
backup.WithLogger(logger),
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (c *restoreCfg) exec(ctx context.Context, _ []string) error {
)

if c.legacyBackup {
src, srcErr = legacy.NewLegacySource(c.inputPath)
src, srcErr = legacy.NewSource(c.inputPath)
} else {
src, srcErr = standard.NewStandardSource(c.inputPath)
src, srcErr = standard.NewSource(c.inputPath)
}

if srcErr != nil {
Expand Down
12 changes: 6 additions & 6 deletions restore/source/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

type Legacy struct {
type Source struct {
file *os.File
scanner *bufio.Scanner
}

// NewLegacySource creates a new legacy amino JSON source
func NewLegacySource(filePath string) (*Legacy, error) {
// NewSource creates a new legacy amino JSON source
func NewSource(filePath string) (*Source, error) {
// Open the file
file, err := os.Open(filePath)
if err != nil {
Expand All @@ -27,13 +27,13 @@ func NewLegacySource(filePath string) (*Legacy, error) {
)
}

return &Legacy{
return &Source{
file: file,
scanner: bufio.NewScanner(file),
}, nil
}

func (l *Legacy) Next(ctx context.Context) (*std.Tx, error) {
func (l *Source) Next(ctx context.Context) (*std.Tx, error) {
for l.scanner.Scan() {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -64,7 +64,7 @@ func (l *Legacy) Next(ctx context.Context) (*std.Tx, error) {
return nil, io.EOF
}

func (l *Legacy) Close() error {
func (l *Source) Close() error {
// Attempt to gracefully close the file
if closeErr := l.file.Close(); closeErr != nil {
return fmt.Errorf(
Expand Down
Loading

0 comments on commit d6c9f53

Please sign in to comment.