diff --git a/backup/backup_test.go b/backup/backup_test.go index fdbaf54..07266bf 100644 --- a/backup/backup_test.go +++ b/backup/backup_test.go @@ -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)) diff --git a/backup/client/client.go b/backup/client/client.go index abb8768..fb16725 100644 --- a/backup/client/client.go +++ b/backup/client/client.go @@ -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) } diff --git a/backup/config.go b/backup/config.go index 27663cd..d4dadaa 100644 --- a/backup/config.go +++ b/backup/config.go @@ -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 diff --git a/backup/config_test.go b/backup/config_test.go index 528f0ba..fd0476a 100644 --- a/backup/config_test.go +++ b/backup/config_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) // createTempFile creates a temporary file @@ -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() diff --git a/restore/config.go b/restore/config.go new file mode 100644 index 0000000..f070a92 --- /dev/null +++ b/restore/config.go @@ -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 +} diff --git a/restore/config_test.go b/restore/config_test.go new file mode 100644 index 0000000..a585d00 --- /dev/null +++ b/restore/config_test.go @@ -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)) + }) +}