2022-10-05 06:44:10 +00:00
|
|
|
package smartcontract_test
|
2020-10-02 11:16:25 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-11 12:16:15 +00:00
|
|
|
"bytes"
|
2020-10-02 11:16:25 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2021-01-12 12:52:08 +00:00
|
|
|
"strconv"
|
2020-10-02 11:16:25 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-01-14 11:18:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/smartcontract"
|
2020-12-17 12:41:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2022-10-05 06:44:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testcli"
|
2020-10-02 11:16:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-01-12 12:52:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2020-12-17 12:41:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-10-12 09:55:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-03-02 12:43:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-12-17 12:41:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-12-10 11:34:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2021-01-22 09:22:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-12-17 12:41:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-10-02 11:16:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-01-12 12:52:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-03-02 12:43:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-10-02 11:16:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-23 13:50:21 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-10-02 11:16:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-17 12:41:53 +00:00
|
|
|
func TestCalcHash(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2020-12-17 12:41:53 +00:00
|
|
|
|
|
|
|
nefPath := "./testdata/verify.nef"
|
2022-02-22 16:27:32 +00:00
|
|
|
src, err := os.ReadFile(nefPath)
|
2020-12-17 12:41:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
nefF, err := nef.FileFromBytes(src)
|
|
|
|
require.NoError(t, err)
|
2021-01-22 09:22:48 +00:00
|
|
|
manifestPath := "./testdata/verify.manifest.json"
|
2022-02-22 16:27:32 +00:00
|
|
|
manifestBytes, err := os.ReadFile(manifestPath)
|
2021-01-22 09:22:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
manif := &manifest.Manifest{}
|
|
|
|
err = json.Unmarshal(manifestBytes, manif)
|
|
|
|
require.NoError(t, err)
|
2020-12-17 12:41:53 +00:00
|
|
|
sender := random.Uint160()
|
|
|
|
|
|
|
|
cmd := []string{"neo-go", "contract", "calc-hash"}
|
|
|
|
t.Run("no sender", func(t *testing.T) {
|
2021-01-22 09:22:48 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--in", nefPath, "--manifest", manifestPath)...)
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
|
|
|
t.Run("no nef file", func(t *testing.T) {
|
2021-01-22 09:22:48 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--manifest", manifestPath)...)
|
|
|
|
})
|
|
|
|
t.Run("no manifest file", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", nefPath)...)
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("invalid nef path", func(t *testing.T) {
|
2020-12-17 12:41:53 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(),
|
2021-01-22 09:22:48 +00:00
|
|
|
"--in", "./testdata/verify.nef123", "--manifest", manifestPath)...)
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("invalid manifest path", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(),
|
|
|
|
"--in", nefPath, "--manifest", "./testdata/verify.manifest123")...)
|
|
|
|
})
|
|
|
|
t.Run("invalid nef file", func(t *testing.T) {
|
2021-11-17 11:14:22 +00:00
|
|
|
p := filepath.Join(tmpDir, "neogo.calchash.verify.nef")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(p, src[:4], os.ModePerm))
|
2021-08-25 19:17:37 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...)
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("invalid manifest file", func(t *testing.T) {
|
|
|
|
p := filepath.Join(tmpDir, "neogo.calchash.verify.manifest.json")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(p, manifestBytes[:4], os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", nefPath, "--manifest", p)...)
|
|
|
|
})
|
2020-12-17 12:41:53 +00:00
|
|
|
|
2021-01-22 09:22:48 +00:00
|
|
|
cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath)
|
|
|
|
expected := state.CreateContractHash(sender, nefF.Checksum, manif.Name)
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "something")...)
|
|
|
|
})
|
2020-12-17 12:41:53 +00:00
|
|
|
t.Run("valid, uint160", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--sender", sender.StringLE())...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, expected.StringLE())
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
|
|
|
t.Run("valid, uint160 with 0x", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--sender", "0x"+sender.StringLE())...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, expected.StringLE())
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
|
|
|
t.Run("valid, address", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--sender", address.Uint160ToString(sender))...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, expected.StringLE())
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:16:15 +00:00
|
|
|
func TestContractBindings(t *testing.T) {
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "v0.98.1-test"
|
|
|
|
|
|
|
|
// For proper contract init. The actual version as it will be replaced.
|
|
|
|
smartcontract.ModVersion = "v0.0.0"
|
|
|
|
|
|
|
|
tmpDir := t.TempDir()
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2022-02-11 12:16:15 +00:00
|
|
|
|
|
|
|
ctrPath := filepath.Join(tmpDir, "testcontract")
|
|
|
|
e.Run(t, "neo-go", "contract", "init", "--name", ctrPath)
|
|
|
|
|
|
|
|
srcPath := filepath.Join(ctrPath, "main.go")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(srcPath, []byte(`package testcontract
|
2022-02-11 12:16:15 +00:00
|
|
|
import(
|
|
|
|
alias "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
|
|
|
)
|
|
|
|
type MyPair struct {
|
|
|
|
Key int
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
func ToMap(a []MyPair) map[int]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func ToArray(m map[int]string) []MyPair {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func Block() *alias.Block{
|
|
|
|
return alias.GetBlock(1)
|
|
|
|
}
|
|
|
|
func Blocks() []*alias.Block {
|
|
|
|
return []*alias.Block{
|
|
|
|
alias.GetBlock(10),
|
|
|
|
alias.GetBlock(11),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`), os.ModePerm))
|
|
|
|
|
|
|
|
cfgPath := filepath.Join(ctrPath, "neo-go.yml")
|
|
|
|
manifestPath := filepath.Join(tmpDir, "manifest.json")
|
|
|
|
bindingsPath := filepath.Join(tmpDir, "bindings.yml")
|
|
|
|
cmd := []string{"neo-go", "contract", "compile"}
|
|
|
|
|
|
|
|
cmd = append(cmd, "--in", ctrPath, "--bindings", bindingsPath)
|
|
|
|
|
|
|
|
// Replace `pkg/interop` in go.mod to avoid getting an actual module version.
|
|
|
|
goMod := filepath.Join(ctrPath, "go.mod")
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(goMod)
|
2022-02-11 12:16:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
i := bytes.IndexByte(data, '\n')
|
|
|
|
data = append([]byte("module myimport.com/testcontract"), data[i:]...)
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
data = append(data, "\nreplace github.com/nspcc-dev/neo-go/pkg/interop => "...)
|
2022-10-05 06:44:10 +00:00
|
|
|
data = append(data, filepath.Join(wd, "../../pkg/interop")...)
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(goMod, data, os.ModePerm))
|
2022-02-11 12:16:15 +00:00
|
|
|
|
|
|
|
cmd = append(cmd, "--config", cfgPath,
|
|
|
|
"--out", filepath.Join(tmpDir, "out.nef"),
|
|
|
|
"--manifest", manifestPath,
|
|
|
|
"--bindings", bindingsPath)
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "something")...)
|
|
|
|
})
|
2022-02-11 12:16:15 +00:00
|
|
|
e.Run(t, cmd...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckEOF(t)
|
2022-02-11 12:16:15 +00:00
|
|
|
require.FileExists(t, bindingsPath)
|
|
|
|
|
|
|
|
outPath := filepath.Join(t.TempDir(), "binding.go")
|
|
|
|
e.Run(t, "neo-go", "contract", "generate-wrapper",
|
|
|
|
"--config", bindingsPath, "--manifest", manifestPath,
|
|
|
|
"--out", outPath, "--hash", "0x0123456789987654321001234567899876543210")
|
|
|
|
|
2022-02-22 16:27:32 +00:00
|
|
|
bs, err := os.ReadFile(outPath)
|
2022-02-11 12:16:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, `// Package testcontract contains wrappers for testcontract contract.
|
|
|
|
package testcontract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
"myimport.com/testcontract"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hash contains contract hash in big-endian form.
|
|
|
|
const Hash = "\x10\x32\x54\x76\x98\x89\x67\x45\x23\x01\x10\x32\x54\x76\x98\x89\x67\x45\x23\x01"
|
|
|
|
|
|
|
|
// Block invokes `+"`block`"+` method of contract.
|
|
|
|
func Block() *ledger.Block {
|
|
|
|
return neogointernal.CallWithToken(Hash, "block", int(contract.All)).(*ledger.Block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocks invokes `+"`blocks`"+` method of contract.
|
|
|
|
func Blocks() []*ledger.Block {
|
|
|
|
return neogointernal.CallWithToken(Hash, "blocks", int(contract.All)).([]*ledger.Block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToArray invokes `+"`toArray`"+` method of contract.
|
|
|
|
func ToArray(m map[int]string) []testcontract.MyPair {
|
|
|
|
return neogointernal.CallWithToken(Hash, "toArray", int(contract.All), m).([]testcontract.MyPair)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap invokes `+"`toMap`"+` method of contract.
|
|
|
|
func ToMap(a []testcontract.MyPair) map[int]string {
|
|
|
|
return neogointernal.CallWithToken(Hash, "toMap", int(contract.All), a).(map[int]string)
|
|
|
|
}
|
|
|
|
`, string(bs))
|
|
|
|
}
|
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
func TestContractInitAndCompile(t *testing.T) {
|
2022-01-14 11:18:33 +00:00
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "v0.98.1-test"
|
|
|
|
|
|
|
|
// For proper contract init. The actual version as it will be replaced.
|
|
|
|
smartcontract.ModVersion = "v0.0.0"
|
|
|
|
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2020-12-03 11:57:45 +00:00
|
|
|
|
|
|
|
t.Run("no path is provided", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "init")
|
|
|
|
})
|
|
|
|
t.Run("invalid path", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "init", "--name", "\x00")
|
|
|
|
})
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
ctrPath := filepath.Join(tmpDir, "testcontract")
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "init", "--name", ctrPath, "something")
|
|
|
|
})
|
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "init", "--name", ctrPath)
|
|
|
|
|
|
|
|
t.Run("don't rewrite existing directory", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "init", "--name", ctrPath)
|
|
|
|
})
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
srcPath := filepath.Join(ctrPath, "main.go")
|
|
|
|
cfgPath := filepath.Join(ctrPath, "neo-go.yml")
|
|
|
|
nefPath := filepath.Join(tmpDir, "testcontract.nef")
|
|
|
|
manifestPath := filepath.Join(tmpDir, "testcontract.manifest.json")
|
2020-12-03 11:57:45 +00:00
|
|
|
cmd := []string{"neo-go", "contract", "compile"}
|
|
|
|
t.Run("missing source", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd = append(cmd, "--in", srcPath, "--out", nefPath, "--manifest", manifestPath)
|
|
|
|
t.Run("missing config, but require manifest", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("provided non-existent config", func(t *testing.T) {
|
2021-11-17 11:14:22 +00:00
|
|
|
cfgName := filepath.Join(ctrPath, "notexists.yml")
|
2020-12-03 11:57:45 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--config", cfgName)...)
|
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("provided corrupted config", func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(cfgPath)
|
2022-01-31 13:24:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
badCfg := filepath.Join(tmpDir, "bad.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(badCfg, data[:len(data)-5], os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--config", badCfg)...)
|
|
|
|
})
|
2020-12-03 11:57:45 +00:00
|
|
|
|
2022-01-14 11:18:33 +00:00
|
|
|
// Replace `pkg/interop` in go.mod to avoid getting an actual module version.
|
|
|
|
goMod := filepath.Join(ctrPath, "go.mod")
|
2022-02-22 16:27:32 +00:00
|
|
|
data, err := os.ReadFile(goMod)
|
2022-01-14 11:18:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
data = append(data, "\nreplace github.com/nspcc-dev/neo-go/pkg/interop => "...)
|
2022-10-05 06:44:10 +00:00
|
|
|
data = append(data, filepath.Join(wd, "../../pkg/interop")...)
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(goMod, data, os.ModePerm))
|
2021-12-02 14:44:53 +00:00
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
cmd = append(cmd, "--config", cfgPath)
|
2022-08-05 10:32:37 +00:00
|
|
|
|
|
|
|
t.Run("excessive parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "something")...)
|
|
|
|
})
|
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
e.Run(t, cmd...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckEOF(t)
|
2020-12-03 11:57:45 +00:00
|
|
|
require.FileExists(t, nefPath)
|
|
|
|
require.FileExists(t, manifestPath)
|
|
|
|
|
|
|
|
t.Run("output hex script with --verbose", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--verbose")...)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, "^[0-9a-hA-H]+$")
|
2020-12-03 11:57:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:34:46 +00:00
|
|
|
// Checks that error is returned if GAS available for test-invoke exceeds
|
|
|
|
// GAS needed to be consumed.
|
|
|
|
func TestDeployBigContract(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutorWithConfig(t, true, true, func(c *config.Config) {
|
2020-12-10 11:34:46 +00:00
|
|
|
c.ApplicationConfiguration.RPC.MaxGasInvoke = fixedn.Fixed8(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-12-10 11:34:46 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2020-12-10 11:34:46 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", "testdata/deploy/main.go", // compile single file
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
e.In.WriteString(testcli.ValidatorPass + "\r")
|
2020-12-10 11:34:46 +00:00
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2020-12-10 11:34:46 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName)
|
|
|
|
}
|
|
|
|
|
2021-04-16 15:04:19 +00:00
|
|
|
func TestContractDeployWithData(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
eCompile := testcli.NewExecutor(t, false)
|
2021-10-12 09:55:56 +00:00
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2021-10-12 09:55:56 +00:00
|
|
|
eCompile.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", "testdata/deploy/main.go", // compile single file
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
|
|
|
deployContract := func(t *testing.T, haveData bool, scope string) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2021-10-12 09:55:56 +00:00
|
|
|
cmd := []string{
|
|
|
|
"neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2021-10-12 09:55:56 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
|
|
|
"--force",
|
|
|
|
}
|
|
|
|
|
|
|
|
if haveData {
|
|
|
|
cmd = append(cmd, "[", "key1", "12", "key2", "take_me_to_church", "]")
|
|
|
|
}
|
|
|
|
if scope != "" {
|
2022-10-05 06:44:10 +00:00
|
|
|
cmd = append(cmd, "--", testcli.ValidatorAddr+":"+scope)
|
2021-10-12 09:55:56 +00:00
|
|
|
} else {
|
|
|
|
scope = "CalledByEntry"
|
|
|
|
}
|
2022-10-05 06:44:10 +00:00
|
|
|
e.In.WriteString(testcli.ValidatorPass + "\r")
|
2021-10-12 09:55:56 +00:00
|
|
|
e.Run(t, cmd...)
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
tx, _ := e.CheckTxPersisted(t, "Sent invocation transaction ")
|
2021-10-12 09:55:56 +00:00
|
|
|
require.Equal(t, scope, tx.Signers[0].Scopes.String())
|
|
|
|
if !haveData {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
line, err := e.Out.ReadString('\n')
|
|
|
|
require.NoError(t, err)
|
|
|
|
line = strings.TrimSpace(strings.TrimPrefix(line, "Contract: "))
|
|
|
|
h, err := util.Uint160DecodeStringLE(line)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
h.StringLE(),
|
|
|
|
"getValueWithKey", "key1",
|
|
|
|
)
|
|
|
|
|
|
|
|
res := new(result.Invoke)
|
|
|
|
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Halt.String(), res.State, res.FaultException)
|
2021-10-12 09:55:56 +00:00
|
|
|
require.Len(t, res.Stack, 1)
|
|
|
|
require.Equal(t, []byte{12}, res.Stack[0].Value())
|
|
|
|
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
h.StringLE(),
|
|
|
|
"getValueWithKey", "key2",
|
|
|
|
)
|
|
|
|
|
|
|
|
res = new(result.Invoke)
|
|
|
|
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Halt.String(), res.State, res.FaultException)
|
2021-10-12 09:55:56 +00:00
|
|
|
require.Len(t, res.Stack, 1)
|
|
|
|
require.Equal(t, []byte("take_me_to_church"), res.Stack[0].Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
deployContract(t, true, "")
|
|
|
|
deployContract(t, false, "Global")
|
|
|
|
deployContract(t, true, "Global")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeployWithSigners(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2021-04-16 15:04:19 +00:00
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2021-04-16 15:04:19 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2021-04-16 15:04:19 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
2021-10-12 09:55:56 +00:00
|
|
|
"--in", "testdata/deploy/main.go",
|
2021-04-16 15:04:19 +00:00
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("missing nef", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", "", "--manifest", manifestName)
|
|
|
|
})
|
|
|
|
t.Run("missing manifest", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", nefName, "--manifest", "")
|
|
|
|
})
|
|
|
|
t.Run("corrupted data", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
|
|
|
"[", "str1")
|
|
|
|
})
|
|
|
|
t.Run("invalid data", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
|
|
|
"str1", "str2")
|
|
|
|
})
|
|
|
|
t.Run("missing wallet", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
|
|
|
"[", "str1", "str2", "]")
|
|
|
|
})
|
|
|
|
t.Run("missing RPC", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "deploy",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
|
|
|
"[", "str1", "str2", "]")
|
|
|
|
})
|
2022-10-05 06:44:10 +00:00
|
|
|
e.In.WriteString(testcli.ValidatorPass + "\r")
|
2021-04-16 15:04:19 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2021-04-16 15:04:19 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName,
|
2021-09-15 09:40:30 +00:00
|
|
|
"--force",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--", testcli.ValidatorAddr+":Global")
|
|
|
|
tx, _ := e.CheckTxPersisted(t, "Sent invocation transaction ")
|
2021-10-12 09:55:56 +00:00
|
|
|
require.Equal(t, transaction.Global, tx.Signers[0].Scopes)
|
2021-04-16 15:04:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 08:10:26 +00:00
|
|
|
func TestContractManifestGroups(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2021-08-02 08:10:26 +00:00
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2021-08-02 08:10:26 +00:00
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
_, err := wallet.NewWalletFromFile(testcli.TestWalletPath)
|
2021-08-02 08:10:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2021-08-02 08:10:26 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", "testdata/deploy/main.go", // compile single file
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("missing wallet", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group")
|
|
|
|
})
|
|
|
|
t.Run("invalid wallet", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
|
|
|
"--wallet", t.TempDir())
|
|
|
|
})
|
|
|
|
t.Run("invalid sender", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", testcli.TestWalletAccount,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--sender", "not-a-sender")
|
|
|
|
})
|
|
|
|
t.Run("invalid NEF file", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", testcli.TestWalletAccount,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--nef", tmpDir)
|
2022-01-31 13:24:15 +00:00
|
|
|
})
|
|
|
|
t.Run("corrupted NEF file", func(t *testing.T) {
|
|
|
|
f := filepath.Join(tmpDir, "invalid.nef")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(f, []byte{1, 2, 3}, os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", testcli.TestWalletAccount,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--nef", f)
|
2022-01-31 13:24:15 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid manifest file", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", testcli.TestWalletAccount,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--nef", nefName,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--manifest", tmpDir)
|
|
|
|
})
|
|
|
|
t.Run("corrupted manifest file", func(t *testing.T) {
|
|
|
|
f := filepath.Join(tmpDir, "invalid.manifest.json")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(f, []byte{1, 2, 3}, os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", testcli.TestWalletAccount,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--nef", nefName,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--manifest", f)
|
|
|
|
})
|
|
|
|
t.Run("unknown account", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "manifest", "add-group",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.TestWalletPath, "--address", util.Uint160{}.StringLE(),
|
|
|
|
"--sender", testcli.TestWalletAccount, "--nef", nefName,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--manifest", manifestName)
|
|
|
|
})
|
2021-08-02 08:10:26 +00:00
|
|
|
cmd := []string{"neo-go", "contract", "manifest", "add-group",
|
|
|
|
"--nef", nefName, "--manifest", manifestName}
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive parameters", func(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--wallet", testcli.TestWalletPath,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--address", testcli.TestWalletAccount, "something")...)
|
2022-08-05 10:32:37 +00:00
|
|
|
})
|
2021-08-02 08:10:26 +00:00
|
|
|
e.In.WriteString("testpass\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, "--wallet", testcli.TestWalletPath,
|
|
|
|
"--sender", testcli.TestWalletAccount, "--address", testcli.TestWalletAccount)...)
|
2021-08-02 08:10:26 +00:00
|
|
|
|
|
|
|
e.In.WriteString("testpass\r") // should override signature with the previous sender
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, "--wallet", testcli.TestWalletPath,
|
|
|
|
"--sender", testcli.ValidatorAddr, "--address", testcli.TestWalletAccount)...)
|
2021-08-02 08:10:26 +00:00
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
e.In.WriteString(testcli.ValidatorPass + "\r")
|
2021-08-02 08:10:26 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "deploy",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", nefName, "--manifest", manifestName,
|
2021-09-15 09:40:30 +00:00
|
|
|
"--force",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr)
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
func deployVerifyContract(t *testing.T, e *testcli.Executor) util.Uint160 {
|
|
|
|
return testcli.DeployContract(t, e, "testdata/verify.go", "testdata/verify.yml", testcli.ValidatorWallet, testcli.ValidatorAddr, testcli.ValidatorPass)
|
2021-04-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:24:15 +00:00
|
|
|
func TestContract_TestInvokeScript(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2022-01-31 13:24:15 +00:00
|
|
|
tmpDir := t.TempDir()
|
|
|
|
badNef := filepath.Join(tmpDir, "invalid.nef")
|
|
|
|
goodNef := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", "testdata/deploy/main.go", // compile single file
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", goodNef, "--manifest", manifestName)
|
|
|
|
|
|
|
|
t.Run("missing in", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr)
|
|
|
|
})
|
|
|
|
t.Run("unexisting in", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", badNef)
|
|
|
|
})
|
|
|
|
t.Run("invalid nef", func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(badNef, []byte("qwer"), os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", badNef)
|
|
|
|
})
|
|
|
|
t.Run("invalid signers", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", goodNef, "--", "not-a-valid-signer")
|
|
|
|
})
|
|
|
|
t.Run("no RPC endpoint", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://123456789",
|
|
|
|
"--in", goodNef)
|
|
|
|
})
|
2022-09-08 16:05:32 +00:00
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", goodNef)
|
|
|
|
})
|
|
|
|
t.Run("good with hashed signer", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", goodNef, "--", util.Uint160{1, 2, 3}.StringLE())
|
|
|
|
})
|
|
|
|
t.Run("good with addressed signer", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--in", goodNef, "--", address.Uint160ToString(util.Uint160{1, 2, 3}))
|
|
|
|
})
|
|
|
|
t.Run("historic, invalid", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", "bad",
|
|
|
|
"--in", goodNef)
|
|
|
|
})
|
|
|
|
t.Run("historic, index", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", "0",
|
|
|
|
"--in", goodNef)
|
|
|
|
})
|
|
|
|
t.Run("historic, hash", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokescript",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", e.Chain.GetHeaderHash(0).StringLE(),
|
|
|
|
"--in", goodNef)
|
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 11:16:25 +00:00
|
|
|
func TestComlileAndInvokeFunction(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2020-10-02 11:16:25 +00:00
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-12-03 11:57:45 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2020-10-02 11:16:25 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", "testdata/deploy/main.go", // compile single file
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
2022-06-23 13:50:21 +00:00
|
|
|
tmp := t.TempDir()
|
|
|
|
configPath := filepath.Join(tmp, "config.yaml")
|
|
|
|
cfg := config.Wallet{
|
2022-10-05 06:44:10 +00:00
|
|
|
Path: testcli.ValidatorWallet,
|
|
|
|
Password: testcli.ValidatorPass,
|
2022-06-23 13:50:21 +00:00
|
|
|
}
|
|
|
|
yml, err := yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, os.WriteFile(configPath, yml, 0666))
|
2020-10-02 11:16:25 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "deploy",
|
2021-09-15 09:40:30 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr, "--force",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet-config", configPath, "--address", testcli.ValidatorAddr,
|
2020-10-02 11:16:25 +00:00
|
|
|
"--in", nefName, "--manifest", manifestName)
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckTxPersisted(t, "Sent invocation transaction ")
|
2020-10-02 11:16:25 +00:00
|
|
|
line, err := e.Out.ReadString('\n')
|
|
|
|
require.NoError(t, err)
|
|
|
|
line = strings.TrimSpace(strings.TrimPrefix(line, "Contract: "))
|
|
|
|
h, err := util.Uint160DecodeStringLE(line)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-17 12:41:53 +00:00
|
|
|
t.Run("check calc hash", func(t *testing.T) {
|
2021-04-16 09:09:59 +00:00
|
|
|
// missing sender
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "calc-hash",
|
|
|
|
"--in", nefName,
|
|
|
|
"--manifest", manifestName)
|
|
|
|
|
2020-12-17 12:41:53 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "calc-hash",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--sender", testcli.ValidatorAddr, "--in", nefName,
|
2021-01-22 09:22:48 +00:00
|
|
|
"--manifest", manifestName)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, h.StringLE())
|
2020-12-17 12:41:53 +00:00
|
|
|
})
|
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
cmd := []string{"neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr}
|
|
|
|
t.Run("missing hash", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("invalid hash", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "notahash")...)
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd = append(cmd, h.StringLE())
|
|
|
|
t.Run("missing method", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd = append(cmd, "getValue")
|
|
|
|
t.Run("invalid params", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "[")...)
|
|
|
|
})
|
|
|
|
t.Run("invalid cosigner", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--", "notahash")...)
|
|
|
|
})
|
|
|
|
t.Run("missing RPC address", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
})
|
|
|
|
|
|
|
|
e.Run(t, cmd...)
|
2020-10-02 11:16:25 +00:00
|
|
|
|
2022-09-08 16:05:32 +00:00
|
|
|
checkGetValueOut := func(str string) {
|
|
|
|
res := new(result.Invoke)
|
|
|
|
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
|
|
|
|
require.Equal(t, vmstate.Halt.String(), res.State, res.FaultException)
|
|
|
|
require.Len(t, res.Stack, 1)
|
|
|
|
require.Equal(t, []byte(str), res.Stack[0].Value())
|
|
|
|
}
|
|
|
|
checkGetValueOut("on create|sub create")
|
2020-10-02 11:16:25 +00:00
|
|
|
|
2021-04-20 09:04:25 +00:00
|
|
|
// deploy verification contract
|
2021-04-21 09:50:04 +00:00
|
|
|
hVerify := deployVerifyContract(t, e)
|
2021-04-20 09:04:25 +00:00
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
t.Run("real invoke", func(t *testing.T) {
|
|
|
|
cmd := []string{"neo-go", "contract", "invokefunction",
|
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr}
|
|
|
|
t.Run("missing wallet", func(t *testing.T) {
|
|
|
|
cmd := append(cmd, h.StringLE(), "getValue")
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("non-existent wallet", func(t *testing.T) {
|
2021-11-17 11:14:22 +00:00
|
|
|
cmd := append(cmd, "--wallet", filepath.Join(tmpDir, "not.exists"),
|
2020-12-03 11:57:45 +00:00
|
|
|
h.StringLE(), "getValue")
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
2022-01-31 13:24:15 +00:00
|
|
|
t.Run("corrupted wallet", func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
tmp := t.TempDir()
|
2022-01-31 13:24:15 +00:00
|
|
|
tmpPath := filepath.Join(tmp, "wallet.json")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(tmpPath, []byte("{"), os.ModePerm))
|
2022-01-31 13:24:15 +00:00
|
|
|
|
|
|
|
cmd := append(cmd, "--wallet", tmpPath,
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("non-existent address", func(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
cmd := append(cmd, "--wallet", testcli.ValidatorWallet,
|
2022-01-31 13:24:15 +00:00
|
|
|
"--address", random.Uint160().StringLE(),
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("invalid password", func(t *testing.T) {
|
|
|
|
e.In.WriteString("invalid_password\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
cmd := append(cmd, "--wallet", testcli.ValidatorWallet,
|
2022-01-31 13:24:15 +00:00
|
|
|
h.StringLE(), "getValue")
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("good: default address", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.In.WriteString("y\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, "--wallet", testcli.ValidatorWallet, h.StringLE(), "getValue")...)
|
2022-01-31 13:24:15 +00:00
|
|
|
})
|
2022-06-23 13:50:21 +00:00
|
|
|
t.Run("good: from wallet config", func(t *testing.T) {
|
|
|
|
e.In.WriteString("y\r")
|
|
|
|
e.Run(t, append(cmd, "--wallet-config", configPath, h.StringLE(), "getValue")...)
|
|
|
|
})
|
2020-12-03 11:57:45 +00:00
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
cmd = append(cmd, "--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr)
|
2021-09-15 09:40:30 +00:00
|
|
|
t.Run("cancelled", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.In.WriteString("n\r")
|
|
|
|
e.RunWithError(t, append(cmd, h.StringLE(), "getValue")...)
|
|
|
|
})
|
|
|
|
t.Run("confirmed", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.In.WriteString("y\r")
|
|
|
|
e.Run(t, append(cmd, h.StringLE(), "getValue")...)
|
|
|
|
})
|
2020-12-03 11:57:45 +00:00
|
|
|
|
|
|
|
t.Run("failind method", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
2021-09-15 09:40:30 +00:00
|
|
|
e.In.WriteString("y\r")
|
2020-12-03 11:57:45 +00:00
|
|
|
e.RunWithError(t, append(cmd, h.StringLE(), "fail")...)
|
|
|
|
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, append(cmd, "--force", h.StringLE(), "fail")...)
|
|
|
|
})
|
2021-04-20 09:04:25 +00:00
|
|
|
|
|
|
|
t.Run("cosigner is deployed contract", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
2021-09-15 09:40:30 +00:00
|
|
|
e.In.WriteString("y\r")
|
2021-04-20 09:04:25 +00:00
|
|
|
e.Run(t, append(cmd, h.StringLE(), "getValue",
|
2022-10-05 06:44:10 +00:00
|
|
|
"--", testcli.ValidatorAddr, hVerify.StringLE())...)
|
2021-04-20 09:04:25 +00:00
|
|
|
})
|
2020-12-03 11:57:45 +00:00
|
|
|
})
|
|
|
|
|
2021-03-02 12:43:09 +00:00
|
|
|
t.Run("real invoke and save tx", func(t *testing.T) {
|
2021-11-17 11:14:22 +00:00
|
|
|
txout := filepath.Join(tmpDir, "test_contract_tx.json")
|
2021-03-02 12:43:09 +00:00
|
|
|
|
|
|
|
cmd = []string{"neo-go", "contract", "invokefunction",
|
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
|
|
|
"--out", txout,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2021-03-02 12:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("without cosigner", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, append(cmd, hVerify.StringLE(), "verify")...)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with cosigner", func(t *testing.T) {
|
2021-11-22 09:46:42 +00:00
|
|
|
t.Run("cosigner is sender (none)", func(t *testing.T) {
|
2021-03-02 12:43:09 +00:00
|
|
|
e.In.WriteString("one\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.RunWithError(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", testcli.ValidatorAddr+":None")...)
|
2021-11-22 09:46:42 +00:00
|
|
|
})
|
|
|
|
t.Run("cosigner is sender (customcontract)", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", testcli.ValidatorAddr+":CustomContracts:"+h.StringLE())...)
|
2021-11-22 09:46:42 +00:00
|
|
|
})
|
|
|
|
t.Run("cosigner is sender (global)", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, h.StringLE(), "checkSenderWitness", "--", testcli.ValidatorAddr+":Global")...)
|
2021-03-02 12:43:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
2022-09-01 14:52:44 +00:00
|
|
|
err = acc.ConvertMultisig(2, keys.PublicKeys{acc.PublicKey(), pk.PublicKey()})
|
2021-05-12 18:34:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-02 12:43:09 +00:00
|
|
|
|
|
|
|
t.Run("cosigner is multisig account", func(t *testing.T) {
|
|
|
|
t.Run("missing in the wallet", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.RunWithError(t, append(cmd, hVerify.StringLE(), "verify", "--", acc.Address)...)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
2022-10-05 06:44:10 +00:00
|
|
|
e.Run(t, append(cmd, hVerify.StringLE(), "verify", "--", testcli.MultisigAddr)...)
|
2021-03-02 12:43:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cosigner is deployed contract", func(t *testing.T) {
|
|
|
|
t.Run("missing in the wallet", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.RunWithError(t, append(cmd, hVerify.StringLE(), "verify", "--", h.StringLE())...)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, append(cmd, hVerify.StringLE(), "verify", "--", hVerify.StringLE())...)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-12 12:52:08 +00:00
|
|
|
t.Run("test Storage.Find", func(t *testing.T) {
|
|
|
|
cmd := []string{"neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
|
|
|
h.StringLE(), "testFind"}
|
|
|
|
|
|
|
|
t.Run("keys only", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, strconv.FormatInt(storage.FindKeysOnly, 10))...)
|
|
|
|
res := new(result.Invoke)
|
|
|
|
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Halt.String(), res.State)
|
2021-01-12 12:52:08 +00:00
|
|
|
require.Len(t, res.Stack, 1)
|
|
|
|
require.Equal(t, []stackitem.Item{
|
|
|
|
stackitem.Make("findkey1"),
|
|
|
|
stackitem.Make("findkey2"),
|
|
|
|
}, res.Stack[0].Value())
|
|
|
|
})
|
|
|
|
t.Run("both", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, strconv.FormatInt(storage.FindDefault, 10))...)
|
|
|
|
res := new(result.Invoke)
|
|
|
|
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
|
2022-07-08 14:28:29 +00:00
|
|
|
require.Equal(t, vmstate.Halt.String(), res.State)
|
2021-01-12 12:52:08 +00:00
|
|
|
require.Len(t, res.Stack, 1)
|
|
|
|
|
|
|
|
arr, ok := res.Stack[0].Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Len(t, arr, 2)
|
|
|
|
require.Equal(t, []stackitem.Item{
|
|
|
|
stackitem.Make("findkey1"), stackitem.Make("value1"),
|
|
|
|
}, arr[0].Value())
|
|
|
|
require.Equal(t, []stackitem.Item{
|
|
|
|
stackitem.Make("findkey2"), stackitem.Make("value2"),
|
|
|
|
}, arr[1].Value())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-08 16:05:32 +00:00
|
|
|
var (
|
|
|
|
hashBeforeUpdate util.Uint256
|
|
|
|
indexBeforeUpdate uint32
|
|
|
|
indexAfterUpdate uint32
|
|
|
|
stateBeforeUpdate util.Uint256
|
|
|
|
)
|
2020-10-02 11:16:25 +00:00
|
|
|
t.Run("Update", func(t *testing.T) {
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "updated.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "updated.manifest.json")
|
2020-10-02 11:16:25 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--in", "testdata/deploy/", // compile all files in dir
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
2021-03-01 11:14:15 +00:00
|
|
|
t.Cleanup(func() {
|
2020-10-02 11:16:25 +00:00
|
|
|
os.Remove(nefName)
|
|
|
|
os.Remove(manifestName)
|
2021-03-01 11:14:15 +00:00
|
|
|
})
|
2020-10-02 11:16:25 +00:00
|
|
|
|
2022-02-22 16:27:32 +00:00
|
|
|
rawNef, err := os.ReadFile(nefName)
|
2020-10-02 11:16:25 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-22 16:27:32 +00:00
|
|
|
rawManifest, err := os.ReadFile(manifestName)
|
2020-10-02 11:16:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-08 16:05:32 +00:00
|
|
|
indexBeforeUpdate = e.Chain.BlockHeight()
|
|
|
|
hashBeforeUpdate = e.Chain.CurrentHeaderHash()
|
|
|
|
mptBeforeUpdate, err := e.Chain.GetStateRoot(indexBeforeUpdate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
stateBeforeUpdate = mptBeforeUpdate.Root
|
2020-10-02 11:16:25 +00:00
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "contract", "invokefunction",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet, "--address", testcli.ValidatorAddr,
|
2021-09-15 09:40:30 +00:00
|
|
|
"--force",
|
2020-10-02 11:16:25 +00:00
|
|
|
h.StringLE(), "update",
|
2020-11-18 20:10:48 +00:00
|
|
|
"bytes:"+hex.EncodeToString(rawNef),
|
2020-10-02 11:16:25 +00:00
|
|
|
"bytes:"+hex.EncodeToString(rawManifest),
|
|
|
|
)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckTxPersisted(t, "Sent invocation transaction ")
|
2020-10-02 11:16:25 +00:00
|
|
|
|
2022-09-08 16:05:32 +00:00
|
|
|
indexAfterUpdate = e.Chain.BlockHeight()
|
2020-10-02 11:16:25 +00:00
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokefunction",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2020-11-18 20:10:48 +00:00
|
|
|
h.StringLE(), "getValue")
|
2022-09-08 16:05:32 +00:00
|
|
|
checkGetValueOut("on update|sub update")
|
|
|
|
})
|
|
|
|
t.Run("historic", func(t *testing.T) {
|
|
|
|
t.Run("bad ref", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", "bad",
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
})
|
|
|
|
for name, ref := range map[string]string{
|
|
|
|
"by index": strconv.FormatUint(uint64(indexBeforeUpdate), 10),
|
|
|
|
"by block hash": hashBeforeUpdate.StringLE(),
|
|
|
|
"by state hash": stateBeforeUpdate.StringLE(),
|
|
|
|
} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", ref,
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
})
|
|
|
|
checkGetValueOut("on create|sub create")
|
|
|
|
}
|
|
|
|
t.Run("updated historic", func(t *testing.T) {
|
|
|
|
e.Run(t, "neo-go", "contract", "testinvokefunction",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--historic", strconv.FormatUint(uint64(indexAfterUpdate), 10),
|
|
|
|
h.StringLE(), "getValue")
|
|
|
|
checkGetValueOut("on update|sub update")
|
|
|
|
})
|
2020-10-02 11:16:25 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-24 10:38:24 +00:00
|
|
|
|
2020-12-03 11:57:45 +00:00
|
|
|
func TestContractInspect(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2020-12-03 11:57:45 +00:00
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
|
|
|
const srcPath = "testdata/deploy/main.go"
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-12-03 11:57:45 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
nefName := filepath.Join(tmpDir, "deploy.nef")
|
|
|
|
manifestName := filepath.Join(tmpDir, "deploy.manifest.json")
|
2020-12-03 11:57:45 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "compile",
|
|
|
|
"--in", srcPath,
|
|
|
|
"--config", "testdata/deploy/neo-go.yml",
|
|
|
|
"--out", nefName, "--manifest", manifestName)
|
|
|
|
|
|
|
|
cmd := []string{"neo-go", "contract", "inspect"}
|
|
|
|
t.Run("missing input", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, cmd...)
|
|
|
|
})
|
|
|
|
t.Run("with raw '.go'", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--in", srcPath)...)
|
|
|
|
e.Run(t, append(cmd, "--in", srcPath, "--compile")...)
|
|
|
|
require.True(t, strings.Contains(e.Out.String(), "SYSCALL"))
|
|
|
|
})
|
|
|
|
t.Run("with nef", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmd, "--in", nefName, "--compile")...)
|
2021-11-17 11:14:22 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--in", filepath.Join(tmpDir, "not.exists"))...)
|
2022-08-05 10:32:37 +00:00
|
|
|
e.RunWithError(t, append(cmd, "--in", nefName, "something")...)
|
2020-12-03 11:57:45 +00:00
|
|
|
e.Run(t, append(cmd, "--in", nefName)...)
|
|
|
|
require.True(t, strings.Contains(e.Out.String(), "SYSCALL"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:38:24 +00:00
|
|
|
func TestCompileExamples(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2022-10-05 06:44:10 +00:00
|
|
|
const examplePath = "../../examples"
|
2022-02-22 16:27:32 +00:00
|
|
|
infos, err := os.ReadDir(examplePath)
|
2020-11-24 10:38:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// For proper nef generation.
|
|
|
|
config.Version = "0.90.0-test"
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2020-11-24 10:38:24 +00:00
|
|
|
|
|
|
|
for _, info := range infos {
|
2021-03-22 18:13:29 +00:00
|
|
|
if !info.IsDir() {
|
|
|
|
// example smart contracts are located in the `/examples` subdirectories, but
|
|
|
|
// there are also a couple of files inside the `/examples` which doesn't need to be compiled
|
|
|
|
continue
|
|
|
|
}
|
2020-11-24 10:38:24 +00:00
|
|
|
t.Run(info.Name(), func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
infos, err := os.ReadDir(filepath.Join(examplePath, info.Name()))
|
2020-11-24 10:38:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
outF := filepath.Join(tmpDir, info.Name()+".nef")
|
|
|
|
manifestF := filepath.Join(tmpDir, info.Name()+".manifest.json")
|
2022-11-09 09:50:57 +00:00
|
|
|
bindingF := filepath.Join(tmpDir, info.Name()+".binding.yml")
|
|
|
|
wrapperF := filepath.Join(tmpDir, info.Name()+".go")
|
|
|
|
rpcWrapperF := filepath.Join(tmpDir, info.Name()+".rpc.go")
|
2020-11-24 10:38:24 +00:00
|
|
|
|
|
|
|
cfgName := filterFilename(infos, ".yml")
|
|
|
|
opts := []string{
|
|
|
|
"neo-go", "contract", "compile",
|
2021-11-17 11:14:22 +00:00
|
|
|
"--in", filepath.Join(examplePath, info.Name()),
|
2021-08-25 19:17:37 +00:00
|
|
|
"--out", outF,
|
|
|
|
"--manifest", manifestF,
|
2021-11-17 11:14:22 +00:00
|
|
|
"--config", filepath.Join(examplePath, info.Name(), cfgName),
|
2022-11-09 09:50:57 +00:00
|
|
|
"--bindings", bindingF,
|
2020-11-24 10:38:24 +00:00
|
|
|
}
|
|
|
|
e.Run(t, opts...)
|
2021-10-16 11:10:17 +00:00
|
|
|
|
|
|
|
if info.Name() == "storage" {
|
2022-02-22 16:27:32 +00:00
|
|
|
rawM, err := os.ReadFile(manifestF)
|
2021-10-16 11:10:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := new(manifest.Manifest)
|
|
|
|
require.NoError(t, json.Unmarshal(rawM, m))
|
|
|
|
|
|
|
|
require.Nil(t, m.ABI.GetMethod("getDefault", 0))
|
|
|
|
require.NotNil(t, m.ABI.GetMethod("get", 0))
|
|
|
|
require.NotNil(t, m.ABI.GetMethod("get", 1))
|
|
|
|
|
|
|
|
require.Nil(t, m.ABI.GetMethod("putDefault", 1))
|
|
|
|
require.NotNil(t, m.ABI.GetMethod("put", 1))
|
|
|
|
require.NotNil(t, m.ABI.GetMethod("put", 2))
|
|
|
|
}
|
2022-11-09 09:50:57 +00:00
|
|
|
e.Run(t, "neo-go", "contract", "generate-wrapper",
|
|
|
|
"--manifest", manifestF,
|
|
|
|
"--config", bindingF,
|
|
|
|
"--out", wrapperF,
|
|
|
|
"--hash", "0x00112233445566778899aabbccddeeff00112233")
|
|
|
|
e.Run(t, "neo-go", "contract", "generate-rpcwrapper",
|
|
|
|
"--manifest", manifestF,
|
|
|
|
"--config", bindingF,
|
|
|
|
"--out", rpcWrapperF,
|
|
|
|
"--hash", "0x00112233445566778899aabbccddeeff00112233")
|
2020-11-24 10:38:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-24 13:36:35 +00:00
|
|
|
|
2021-09-23 21:19:37 +00:00
|
|
|
t.Run("invalid manifest", func(t *testing.T) {
|
2020-11-24 13:36:35 +00:00
|
|
|
const dir = "./testdata/"
|
2021-09-23 21:19:37 +00:00
|
|
|
for _, name := range []string{"invalid1", "invalid2", "invalid3", "invalid4"} {
|
2021-11-17 11:14:22 +00:00
|
|
|
outF := filepath.Join(tmpDir, name+".nef")
|
|
|
|
manifestF := filepath.Join(tmpDir, name+".manifest.json")
|
2020-11-24 13:36:35 +00:00
|
|
|
e.RunWithError(t, "neo-go", "contract", "compile",
|
2021-11-17 11:14:22 +00:00
|
|
|
"--in", filepath.Join(dir, name),
|
2021-08-25 19:17:37 +00:00
|
|
|
"--out", outF,
|
|
|
|
"--manifest", manifestF,
|
2021-11-17 11:14:22 +00:00
|
|
|
"--config", filepath.Join(dir, name, "invalid.yml"),
|
2020-11-24 13:36:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-11-24 10:38:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:27:32 +00:00
|
|
|
func filterFilename(infos []os.DirEntry, ext string) string {
|
2020-11-24 10:38:24 +00:00
|
|
|
for _, info := range infos {
|
|
|
|
if !info.IsDir() {
|
|
|
|
name := info.Name()
|
|
|
|
if strings.HasSuffix(name, ext) {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|