-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8db272f
commit d6c9f53
Showing
13 changed files
with
226 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.