2021-10-23 11:28:03 +00:00
|
|
|
package neotest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Contract contains contract info for deployment.
|
|
|
|
type Contract struct {
|
2024-08-19 11:39:18 +00:00
|
|
|
Hash util.Uint160
|
|
|
|
NEF *nef.File
|
|
|
|
Manifest *manifest.Manifest
|
|
|
|
DebugInfo *compiler.DebugInfo
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// contracts caches the compiled contracts from FS across multiple tests.
|
2021-10-23 11:28:03 +00:00
|
|
|
var contracts = make(map[string]*Contract)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CompileSource compiles a contract from the reader and returns its NEF, manifest and hash.
|
2022-03-18 15:11:24 +00:00
|
|
|
func CompileSource(t testing.TB, sender util.Uint160, src io.Reader, opts *compiler.Options) *Contract {
|
2021-10-23 11:28:03 +00:00
|
|
|
// nef.NewFile() cares about version a lot.
|
|
|
|
config.Version = "neotest"
|
|
|
|
|
2021-12-02 14:44:53 +00:00
|
|
|
ne, di, err := compiler.CompileWithOptions("contract.go", src, opts)
|
2021-10-23 11:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := compiler.CreateManifest(di, opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &Contract{
|
2024-08-19 11:39:18 +00:00
|
|
|
Hash: state.CreateContractHash(sender, ne.Checksum, m.Name),
|
|
|
|
NEF: ne,
|
|
|
|
Manifest: m,
|
|
|
|
DebugInfo: di,
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CompileFile compiles a contract from the file and returns its NEF, manifest and hash.
|
2022-03-18 15:11:24 +00:00
|
|
|
func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath string) *Contract {
|
2021-10-23 11:28:03 +00:00
|
|
|
if c, ok := contracts[srcPath]; ok {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// nef.NewFile() cares about version a lot.
|
|
|
|
config.Version = "neotest"
|
|
|
|
|
2021-07-26 12:34:07 +00:00
|
|
|
ne, di, err := compiler.CompileWithOptions(srcPath, nil, nil)
|
2021-10-23 11:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
conf, err := smartcontract.ParseContractConfig(configPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
o := &compiler.Options{}
|
|
|
|
o.Name = conf.Name
|
|
|
|
o.ContractEvents = conf.Events
|
2023-05-08 18:58:28 +00:00
|
|
|
o.DeclaredNamedTypes = conf.NamedTypes
|
2021-10-23 11:28:03 +00:00
|
|
|
o.ContractSupportedStandards = conf.SupportedStandards
|
|
|
|
o.Permissions = make([]manifest.Permission, len(conf.Permissions))
|
|
|
|
for i := range conf.Permissions {
|
|
|
|
o.Permissions[i] = manifest.Permission(conf.Permissions[i])
|
|
|
|
}
|
|
|
|
o.SafeMethods = conf.SafeMethods
|
2022-03-09 10:34:39 +00:00
|
|
|
o.Overloads = conf.Overloads
|
|
|
|
o.SourceURL = conf.SourceURL
|
2021-10-23 11:28:03 +00:00
|
|
|
m, err := compiler.CreateManifest(di, o)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c := &Contract{
|
2024-08-19 11:39:18 +00:00
|
|
|
Hash: state.CreateContractHash(sender, ne.Checksum, m.Name),
|
|
|
|
NEF: ne,
|
|
|
|
Manifest: m,
|
|
|
|
DebugInfo: di,
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
contracts[srcPath] = c
|
|
|
|
return c
|
|
|
|
}
|