neotest: Add 'ReadNEF' method

Signed-off-by: Chubru <chubru.alexander@gmail.com>
This commit is contained in:
Chubru 2024-12-20 16:03:03 +03:00
parent 6d20772714
commit 0b21d4caf6
No known key found for this signature in database
GPG key ID: 5922DA1D08C70E3A

View file

@ -1,7 +1,9 @@
package neotest package neotest
import ( import (
"encoding/json"
"io" "io"
"os"
"testing" "testing"
"github.com/nspcc-dev/neo-go/cli/smartcontract" "github.com/nspcc-dev/neo-go/cli/smartcontract"
@ -90,3 +92,37 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s
contracts[cacheKey] = c contracts[cacheKey] = c
return c return c
} }
// ReadNEF loads a contract from the specified NEF and manifest files.
func ReadNEF(t testing.TB, sender util.Uint160, nefPath, manifestPath string) *Contract {
cacheKey := sender.StringLE() + "|" + nefPath + "|" + manifestPath
if c, ok := contracts[cacheKey]; ok {
return c
}
nefBytes, err := os.ReadFile(nefPath)
require.NoError(t, err)
ne, err := nef.FileFromBytes(nefBytes)
require.NoError(t, err)
manifestBytes, err := os.ReadFile(manifestPath)
require.NoError(t, err)
m := new(manifest.Manifest)
err = json.Unmarshal(manifestBytes, m)
require.NoError(t, err)
hash := state.CreateContractHash(sender, ne.Checksum, m.Name)
err = m.IsValid(hash, true)
require.NoError(t, err)
c := &Contract{
Hash: hash,
NEF: &ne,
Manifest: m,
}
contracts[cacheKey] = c
return c
}