Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support tx-archive std format #10

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions extractor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/peterbourgon/ff/v3/ffcli"
"golang.org/x/sync/errgroup"
"io"
Expand All @@ -32,9 +31,9 @@ var (

// Define extractor config
type extractorCfg struct {
fileType string
sourceDir string
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
outputDir string
fileType string
sourcePath string
outputDir string
}

func main() {
Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *extractorCfg) registerFlags(fs *flag.FlagSet) {
)

fs.StringVar(
&c.sourceDir,
&c.sourcePath,
"source-dir",
".",
"the root folder containing transaction data",
Expand All @@ -96,7 +95,7 @@ func execExtract(ctx context.Context, cfg *extractorCfg) error {
}

// Check the source dir is valid
if cfg.sourceDir == "" {
if cfg.sourcePath == "" {
return errInvalidSourceDir
}

Expand All @@ -106,7 +105,7 @@ func execExtract(ctx context.Context, cfg *extractorCfg) error {
}

// Find the files that need to be analyzed
sourceFiles, findErr := findFilePaths(cfg.sourceDir, cfg.fileType)
sourceFiles, findErr := findFilePaths(cfg.sourcePath, cfg.fileType)
if findErr != nil {
return fmt.Errorf("unable to find file paths, %w", findErr)
}
Expand All @@ -119,6 +118,7 @@ func execExtract(ctx context.Context, cfg *extractorCfg) error {
g, ctx := errgroup.WithContext(ctx)

for _, sourceFile := range sourceFiles {
// Redeclare locally for thread safety
sourceFile := sourceFile

g.Go(func() error {
Expand Down Expand Up @@ -212,7 +212,7 @@ func extractAddMessages(filePath string) ([]vm.MsgAddPackage, error) {
tempBuf := make([]byte, 0)

for {
var tx std.Tx
var tx TxData
line, isPrefix, err := reader.ReadLine()

// Exit if no more lines in file
Expand Down Expand Up @@ -248,7 +248,7 @@ func extractAddMessages(filePath string) ([]vm.MsgAddPackage, error) {
tempBuf = nil
}

for _, msg := range tx.Msgs {
for _, msg := range tx.Tx.Msgs {
// Only MsgAddPkg should be parsed
if msg.Type() != "add_package" {
continue
Expand Down
40 changes: 21 additions & 19 deletions extractor/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,36 @@ func TestExtractor_Errors(t *testing.T) {
{
"no source files",
&extractorCfg{
fileType: ".log",
sourceDir: "./",
outputDir: ".",
fileType: ".log",
sourcePath: "./",
outputDir: ".",
},
errNoSourceFilesFound,
},
{
"invalid filetype",
&extractorCfg{
fileType: "",
sourceDir: ".",
outputDir: ".",
fileType: "",
sourcePath: ".",
outputDir: ".",
},
errInvalidFileType,
},
{
"invalid source dir",
&extractorCfg{
fileType: ".log",
sourceDir: "",
outputDir: ".",
fileType: ".log",
sourcePath: "",
outputDir: ".",
},
errInvalidSourceDir,
},
{
"invalid output dir",
&extractorCfg{
fileType: ".log",
sourceDir: ".",
outputDir: "",
fileType: ".log",
sourcePath: ".",
outputDir: "",
},
errInvalidOutputDir,
},
Expand Down Expand Up @@ -106,9 +106,9 @@ func TestValidFlow(t *testing.T) {

// Set correct config
var cfg = &extractorCfg{
fileType: sourceFileType,
sourceDir: sourceDir,
outputDir: outputDir,
fileType: sourceFileType,
sourcePath: sourceDir,
outputDir: outputDir,
}

// Generate mock messages & mock files
Expand Down Expand Up @@ -310,14 +310,16 @@ func generateSourceFiles(t *testing.T, dir string, mockMsgs []std.Msg) []string
t.Helper()

var (
mockTx = make([]std.Tx, numTx)
mockTx = make([]TxData, numTx)
testFiles = make([]string, numSourceFiles)
)

// Generate transactions to wrap messages
for i := range mockTx {
mockTx[i] = std.Tx{
Msgs: mockMsgs[:msgPerTx],
mockTx[i] = TxData{
Tx: std.Tx{
Msgs: mockMsgs[:msgPerTx],
},
}
mockMsgs = mockMsgs[msgPerTx:]
}
Expand Down Expand Up @@ -464,7 +466,7 @@ func randString(t *testing.T, length int) string {
return base64.StdEncoding.EncodeToString(buf)
}

func writeTxToFile(t *testing.T, tx std.Tx, file *os.File) error {
func writeTxToFile(t *testing.T, tx TxData, file *os.File) error {
t.Helper()

data, err := amino.MarshalJSON(tx)
Expand Down
8 changes: 8 additions & 0 deletions extractor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/std"
)

// Metadata defines the metadata info that accompanies
Expand All @@ -11,6 +12,13 @@ type Metadata struct {
Deposit string `json:"deposit"` // the deposit associated with the deployment
}

// TxData contains the single block transaction,
// along with the block information
type TxData struct {
Tx std.Tx `json:"tx"`
BlockNum uint64 `json:"blockNum"`
}

ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
// metadataFromMsg extracts the metadata from a message
func metadataFromMsg(msg vm.MsgAddPackage) Metadata {
return Metadata{
Expand Down