github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neotest/compile.go (about)

     1  package neotest
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/cli/smartcontract"
     8  	"github.com/nspcc-dev/neo-go/pkg/compiler"
     9  	"github.com/nspcc-dev/neo-go/pkg/config"
    10  	"github.com/nspcc-dev/neo-go/pkg/core/state"
    11  	"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
    12  	"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
    13  	"github.com/nspcc-dev/neo-go/pkg/util"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // Contract contains contract info for deployment.
    18  type Contract struct {
    19  	Hash     util.Uint160
    20  	NEF      *nef.File
    21  	Manifest *manifest.Manifest
    22  }
    23  
    24  // contracts caches the compiled contracts from FS across multiple tests.
    25  var contracts = make(map[string]*Contract)
    26  
    27  // CompileSource compiles a contract from the reader and returns its NEF, manifest and hash.
    28  func CompileSource(t testing.TB, sender util.Uint160, src io.Reader, opts *compiler.Options) *Contract {
    29  	// nef.NewFile() cares about version a lot.
    30  	config.Version = "neotest"
    31  
    32  	ne, di, err := compiler.CompileWithOptions("contract.go", src, opts)
    33  	require.NoError(t, err)
    34  
    35  	m, err := compiler.CreateManifest(di, opts)
    36  	require.NoError(t, err)
    37  
    38  	return &Contract{
    39  		Hash:     state.CreateContractHash(sender, ne.Checksum, m.Name),
    40  		NEF:      ne,
    41  		Manifest: m,
    42  	}
    43  }
    44  
    45  // CompileFile compiles a contract from the file and returns its NEF, manifest and hash.
    46  func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath string) *Contract {
    47  	if c, ok := contracts[srcPath]; ok {
    48  		return c
    49  	}
    50  
    51  	// nef.NewFile() cares about version a lot.
    52  	config.Version = "neotest"
    53  
    54  	ne, di, err := compiler.CompileWithOptions(srcPath, nil, nil)
    55  	require.NoError(t, err)
    56  
    57  	conf, err := smartcontract.ParseContractConfig(configPath)
    58  	require.NoError(t, err)
    59  
    60  	o := &compiler.Options{}
    61  	o.Name = conf.Name
    62  	o.ContractEvents = conf.Events
    63  	o.DeclaredNamedTypes = conf.NamedTypes
    64  	o.ContractSupportedStandards = conf.SupportedStandards
    65  	o.Permissions = make([]manifest.Permission, len(conf.Permissions))
    66  	for i := range conf.Permissions {
    67  		o.Permissions[i] = manifest.Permission(conf.Permissions[i])
    68  	}
    69  	o.SafeMethods = conf.SafeMethods
    70  	o.Overloads = conf.Overloads
    71  	o.SourceURL = conf.SourceURL
    72  	m, err := compiler.CreateManifest(di, o)
    73  	require.NoError(t, err)
    74  
    75  	c := &Contract{
    76  		Hash:     state.CreateContractHash(sender, ne.Checksum, m.Name),
    77  		NEF:      ne,
    78  		Manifest: m,
    79  	}
    80  	contracts[srcPath] = c
    81  	return c
    82  }