Merge pull request #3771 from Chubru/feat/add_LoadContract

Add 'neotest.ReadNEF' method
This commit is contained in:
Anna Shaleva 2024-12-20 20:53:13 +03:00 committed by GitHub
commit 992890721a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,9 @@
package neotest
import (
"encoding/json"
"io"
"os"
"testing"
"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
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
}