Skip to content

Commit

Permalink
Add restore config
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Oct 4, 2023
1 parent c902e89 commit 6285f51
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 77 deletions.
2 changes: 0 additions & 2 deletions backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ func TestBackup_ExecuteBackup(t *testing.T) {
// Set the config
cfg.FromBlock = fromBlock
cfg.ToBlock = &toBlock
cfg.OutputFile = tempFile.Name()
cfg.Overwrite = true

// Run the backup procedure
require.NoError(t, ExecuteBackup(mockClient, tempFile, noop.New(), cfg))
Expand Down
6 changes: 6 additions & 0 deletions backup/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package client

import "github.com/gnolang/gno/tm2/pkg/std"

// Client defines the client interface for fetching
// chain data
type Client interface {
// GetLatestBlockNumber returns the latest block height from the chain
GetLatestBlockNumber() (uint64, error)

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
GetBlockTransactions(uint64) ([]std.Tx, error)
}
34 changes: 5 additions & 29 deletions backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,26 @@ package backup

import (
"errors"
"os"
)

const (
DefaultOutputFileLocation = "./backup.json"
)

var (
errInvalidOutputLocation = errors.New("invalid output file location")
errOutputFileExists = errors.New("output file already exists")
errInvalidRange = errors.New("invalid backup block range")
)
var errInvalidRange = errors.New("invalid backup block range")

// Config is the base chain backup configuration
type Config struct {
ToBlock *uint64 // the right bound for the block range; latest if not specified
OutputFile string // the output file path
FromBlock uint64 // the left bound for the block range
Overwrite bool // flag indicating if the output file should be overwritten
ToBlock *uint64 // the right bound for the block range; latest if not specified
FromBlock uint64 // the left bound for the block range
}

// DefaultConfig returns the default backup configuration
func DefaultConfig() Config {
return Config{
OutputFile: DefaultOutputFileLocation,
Overwrite: false, // no overwrites by default
ToBlock: nil, // to latest block by default
FromBlock: 0, // from genesis by default
ToBlock: nil, // to latest block by default
FromBlock: 0, // from genesis by default
}
}

// ValidateConfig validates the base backup configuration
func ValidateConfig(cfg Config) error {
// Make sure the output file path is valid
if cfg.OutputFile == "" {
return errInvalidOutputLocation
}

// Make sure the output file can be overwritten, if it exists
if _, err := os.Stat(cfg.OutputFile); err == nil && !cfg.Overwrite {
// File already exists, and the overwrite flag is not set
return errOutputFileExists
}

// Make sure the backup limits are correct
if cfg.ToBlock != nil && *cfg.ToBlock < cfg.FromBlock {
return errInvalidRange
Expand Down
46 changes: 0 additions & 46 deletions backup/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// createTempFile creates a temporary file
Expand All @@ -23,51 +22,6 @@ func createTempFile(t *testing.T) *os.File {
func TestConfig_ValidateConfig(t *testing.T) {
t.Parallel()

t.Run("invalid output file", func(t *testing.T) {
t.Parallel()

cfg := DefaultConfig()
cfg.OutputFile = "" // invalid output file location

assert.ErrorIs(t, ValidateConfig(cfg), errInvalidOutputLocation)
})

t.Run("output file already exists, no overwrite", func(t *testing.T) {
t.Parallel()

// Create temp file
tempFile := createTempFile(t)

t.Cleanup(func() {
require.NoError(t, tempFile.Close())
require.NoError(t, os.Remove(tempFile.Name()))
})

cfg := DefaultConfig()
cfg.OutputFile = tempFile.Name()
cfg.Overwrite = false

assert.ErrorIs(t, ValidateConfig(cfg), errOutputFileExists)
})

t.Run("output file already exists, with overwrite", func(t *testing.T) {
t.Parallel()

// Create temp file
tempFile := createTempFile(t)

t.Cleanup(func() {
require.NoError(t, tempFile.Close())
require.NoError(t, os.Remove(tempFile.Name()))
})

cfg := DefaultConfig()
cfg.OutputFile = tempFile.Name()
cfg.Overwrite = true

assert.NoError(t, ValidateConfig(cfg))
})

t.Run("invalid block range", func(t *testing.T) {
t.Parallel()

Expand Down
30 changes: 30 additions & 0 deletions restore/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package restore

import "errors"

const (
DefaultRemote = "http://127.0.0.1:26657"
)

var errInvalidRemote = errors.New("invalid remote address")

// Config is the base chain restore config
type Config struct {
Remote string // the remote JSON-RPC URL of the chain
}

// DefaultConfig returns the default restore configuration
func DefaultConfig() Config {
return Config{
Remote: DefaultRemote,
}
}

// ValidateConfig validates the base restore configuration
func ValidateConfig(cfg Config) error {
if cfg.Remote == "" {
return errInvalidRemote
}

return nil
}
28 changes: 28 additions & 0 deletions restore/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package restore

import (
"testing"

"github.com/stretchr/testify/assert"
)

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

t.Run("invalid remote address", func(t *testing.T) {
t.Parallel()

cfg := DefaultConfig()
cfg.Remote = ""

assert.ErrorIs(t, ValidateConfig(cfg), errInvalidRemote)
})

t.Run("valid configuration", func(t *testing.T) {
t.Parallel()

cfg := DefaultConfig()

assert.NoError(t, ValidateConfig(cfg))
})
}

0 comments on commit 6285f51

Please sign in to comment.