Skip to content

Commit

Permalink
feat(txtar): add loadpkg command (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored Feb 23, 2024
1 parent 16c7c2e commit 6ff519f
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 60 deletions.
2 changes: 1 addition & 1 deletion gno.land/cmd/gnoland/testdata/addpkg.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package bar

func Render(path string) string {
return "hello from foo"
}
}
2 changes: 2 additions & 0 deletions gno.land/cmd/gnoland/testdata/append.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
loadpkg gno.land/p/demo/ufmt

# start a new node
gnoland start

Expand Down
5 changes: 2 additions & 3 deletions gno.land/cmd/gnoland/testdata/grc20-registry.txtar
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# example for contract-contract interaction with ownership
# add registry
loadpkg gno.land/r/registry $WORK/registry

## start a new node
gnoland start

# add registry
gnokey maketx addpkg -pkgdir $WORK/registry -pkgpath gno.land/r/registry -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

# we call Transfer with foo20, before it's registered
gnokey maketx call -pkgpath gno.land/r/registry -func TransferByName -args 'foo20' -args 'g123456789' -args '42' -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout 'not found'
Expand Down
1 change: 1 addition & 0 deletions gno.land/cmd/gnoland/testdata/issue-1167.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Reproducible Test for https://github.com/gnolang/gno/issues/1167
loadpkg gno.land/p/demo/avl

gnoland start

Expand Down
5 changes: 2 additions & 3 deletions gno.land/cmd/gnoland/testdata/run.txtar
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
loadpkg gno.land/r/foobar/bar $WORK/bar

## start a new node
gnoland start

## add bar.gno package located in $WORK directory as gno.land/r/foobar/bar
gnokey maketx addpkg -pkgdir $WORK/bar -pkgpath gno.land/r/foobar/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

## execute Render
gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script/script.gno

Expand Down
2 changes: 2 additions & 0 deletions gno.land/cmd/gnoland/testdata/wugnot.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
loadpkg gno.land/r/demo/wugnot

gnoland start

gnokey maketx call -pkgpath gno.land/r/demo/wugnot -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
Expand Down
45 changes: 28 additions & 17 deletions gno.land/pkg/gnoland/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,38 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee, deposit s
nonDraftPkgs := sortedPkgs.GetNonDraftPkgs()
txs := []std.Tx{}
for _, pkg := range nonDraftPkgs {
// Open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name)
if err := memPkg.Validate(); err != nil {
return nil, fmt.Errorf("invalid package: %w", err)
}

// Create transaction
tx := std.Tx{
Fee: fee,
Msgs: []std.Msg{
vmm.MsgAddPackage{
Creator: creator,
Package: memPkg,
Deposit: deposit,
},
},
tx, err := LoadPackage(pkg, creator, fee, deposit)
if err != nil {
return nil, fmt.Errorf("unable to load package %q: %w", pkg.Dir, err)
}

tx.Signatures = make([]std.Signature, len(tx.GetSigners()))
txs = append(txs, tx)
}

return txs, nil
}

// LoadPackage loads a single package into a `std.Tx`
func LoadPackage(pkg gnomod.Pkg, creator bft.Address, fee std.Fee, deposit std.Coins) (std.Tx, error) {
var tx std.Tx

// Open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name)
err := memPkg.Validate()
if err != nil {
return tx, fmt.Errorf("invalid package: %w", err)
}

// Create transaction
tx.Fee = fee
tx.Msgs = []std.Msg{
vmm.MsgAddPackage{
Creator: creator,
Package: memPkg,
Deposit: deposit,
},
}
tx.Signatures = make([]std.Signature, len(tx.GetSigners()))

return tx, nil
}
18 changes: 18 additions & 0 deletions gno.land/pkg/integration/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@
// communicate with the gnoland node.
//
// 3. `adduser`:
// - Must be run before `gnoland start`.
// - Creates a new user in the default keybase directory.
//
// 4. `loadpkg`:
// - Must be run before `gnoland start`.
// - Loads a specific package from the example folder or from the working ($WORK) directory.
// - Can be used to load a single package or all packages within a directory.
// - If the target package has a `gno.mod`, all its dependencies (and their respective
// dependencies) will also be loaded.
// - The command takes either one or two arguments. The first argument is the name of the package(s),
// and the second (optional) argument is the path to the package(s).
// Examples:
// -- # Load a package from the example packages directory:
// -- loadpkg gno.land/p/demo/ufmt
// -- # Load a package `./bar` from the current testscript's working directory with the name `gno.land/r/foobar/bar`:
// -- loadpkg gno.land/r/foobar/bar $WORK/bar
// - If the path is not prefixed with the working directory, it is assumed to be relative to the
// examples directory.
// - It's important to note that the load order is significant when using multiple `loadpkg`
// command; packages should be loaded in the order they are dependent upon.
//
// Logging:
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# test that the example packages directory is loaded and usable.
loadpkg gno.land/p/demo/ufmt
loadpkg $WORK

## start a new node
gnoland start

gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/importtest -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

## execute Render
gnokey maketx call -pkgpath gno.land/r/importtest -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
stdout '("92054" string)'
Expand Down
28 changes: 28 additions & 0 deletions gno.land/pkg/integration/testdata/loadpkg_example.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# load a package from the example packages directory.
loadpkg gno.land/p/demo/ufmt

## start a new node
gnoland start

gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/importtest -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

## execute Render
gnokey maketx call -pkgpath gno.land/r/importtest -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
stdout '("92054" string)'
stdout OK!

-- gno.mod --
module gno.land/r/importtest

-- import.gno --
package importtest

import (
"gno.land/p/demo/ufmt"
)

func Render(_ string) string {
return ufmt.Sprintf("%d", 92054)
}

23 changes: 23 additions & 0 deletions gno.land/pkg/integration/testdata/loadpkg_example_and_work.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# load a package from the example packages directory.
loadpkg gno.land/p/demo/ufmt
loadpkg gno.land/r/importtest $WORK

## start a new node
gnoland start

## execute Render
gnokey maketx call -pkgpath gno.land/r/importtest -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
stdout '("92054" string)'
stdout OK!

-- import.gno --
package importtest

import (
"gno.land/p/demo/ufmt"
)

func Render(_ string) string {
return ufmt.Sprintf("%d", 92054)
}

29 changes: 29 additions & 0 deletions gno.land/pkg/integration/testdata/loadpkg_work.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
loadpkg gno.land/r/foobar/bar $WORK/bar

## start a new node
gnoland start

## execute Render
gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script/script.gno

## compare render
stdout 'main: --- hello from foo ---'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '

-- bar/bar.gno --
package bar

func Render(path string) string {
return "hello from foo"
}

-- script/script.gno --
package main

import "gno.land/r/foobar/bar"

func main() {
println("main: ---", bar.Render(""), "---")
}
Loading

0 comments on commit 6ff519f

Please sign in to comment.