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(pkg/std): ensure files are sorted in a MemPackage #1618

Merged
merged 19 commits into from
Mar 21, 2024
Merged
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
52 changes: 51 additions & 1 deletion gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fmt"
"os"
"regexp"
"sort"
"strings"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
Expand Down Expand Up @@ -174,12 +175,61 @@
if err != nil {
return err
}

// TODO
// enforce sorting msg.Files based on Go conventions for predictability
pkgNames := []string{}

// add std.MemFile.Name to slice -> sort
// create slice of file names
for _, pkgFile := range msg.Package.Files {
pkgNames = append(
pkgNames,
pkgFile.Name, // string
)
}

// sort filenames based on Go conventions
// https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/sort/sort.go;l=39
sort.Strings(pkgNames)

var memPkgSorted std.MemPackage
pFiles := make([]*std.MemFile, 0, len(msg.Package.Files))

// add to new []*std.MemFile to enforce order
for _, pkgName := range pkgNames {
for _, nsFile := range msg.Package.Files {
// bail if not the most immediate pkgName
if nsFile.Name != pkgName {
continue

Check warning on line 204 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L204

Added line #L204 was not covered by tests
}

pFiles = append(
pFiles,
nsFile,
)
}

memPkgSorted = std.MemPackage{
Name: pkgName,
Path: msg.Package.Path,
Files: pFiles,
}
}

// new instance of MsgAddPackage
msgSorted := MsgAddPackage{
Creator: msg.Creator,
Package: &memPkgSorted,
Deposit: msg.Deposit,
}

// Parse and run the files, construct *PV.
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
Msg: msg,
Msg: msgSorted,
OrigCaller: creator.Bech32(),
OrigSend: deposit,
OrigSendSpent: new(std.Coins),
Expand Down
Loading