github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/internal/contracts/contracts.go (about) 1 package contracts 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/nspcc-dev/neo-go/pkg/core/state" 12 "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" 13 "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" 14 "github.com/nspcc-dev/neo-go/pkg/util" 15 "github.com/stretchr/testify/require" 16 ) 17 18 var ( 19 helper1ContractNEFPath = filepath.Join("management_helper", "management_helper1.nef") 20 helper1ContractManifestPath = filepath.Join("management_helper", "management_helper1.manifest.json") 21 helper2ContractNEFPath = filepath.Join("management_helper", "management_helper2.nef") 22 helper2ContractManifestPath = filepath.Join("management_helper", "management_helper2.manifest.json") 23 24 oracleContractModPath = "oracle_contract" 25 oracleContractYAMLPath = filepath.Join(oracleContractModPath, "oracle.yml") 26 oracleContractNEFPath = filepath.Join(oracleContractModPath, "oracle.nef") 27 oracleContractManifestPath = filepath.Join(oracleContractModPath, "oracle.manifest.json") 28 ) 29 30 // GetTestContractState reads 2 pre-compiled contracts generated by 31 // TestGenerateHelperContracts, second of which is allowed to call the first. 32 func GetTestContractState(t *testing.T, pathToInternalContracts string, id1, id2 int32, sender2 util.Uint160) (*state.Contract, *state.Contract) { 33 errNotFound := errors.New("auto-generated oracle contract is not found, use TestGenerateHelperContracts to regenerate") 34 neBytes, err := os.ReadFile(filepath.Join(pathToInternalContracts, helper1ContractNEFPath)) 35 require.NoError(t, err, fmt.Errorf("nef1: %w", errNotFound)) 36 ne, err := nef.FileFromBytes(neBytes) 37 require.NoError(t, err) 38 39 mBytes, err := os.ReadFile(filepath.Join(pathToInternalContracts, helper1ContractManifestPath)) 40 require.NoError(t, err, fmt.Errorf("manifest1: %w", errNotFound)) 41 m := &manifest.Manifest{} 42 err = json.Unmarshal(mBytes, m) 43 require.NoError(t, err) 44 45 cs1 := &state.Contract{ 46 ContractBase: state.ContractBase{ 47 NEF: ne, 48 Manifest: *m, 49 ID: id1, 50 }, 51 } 52 53 neBytes, err = os.ReadFile(filepath.Join(pathToInternalContracts, helper2ContractNEFPath)) 54 require.NoError(t, err, fmt.Errorf("nef2: %w", errNotFound)) 55 ne, err = nef.FileFromBytes(neBytes) 56 require.NoError(t, err) 57 58 mBytes, err = os.ReadFile(filepath.Join(pathToInternalContracts, helper2ContractManifestPath)) 59 require.NoError(t, err, fmt.Errorf("manifest2: %w", errNotFound)) 60 m = &manifest.Manifest{} 61 err = json.Unmarshal(mBytes, m) 62 require.NoError(t, err) 63 64 // Retrieve hash of the first contract from the permissions of the second contract. 65 require.Equal(t, 1, len(m.Permissions)) 66 require.Equal(t, manifest.PermissionHash, m.Permissions[0].Contract.Type) 67 cs1.Hash = m.Permissions[0].Contract.Hash() 68 69 cs2 := &state.Contract{ 70 ContractBase: state.ContractBase{ 71 NEF: ne, 72 Manifest: *m, 73 ID: id2, 74 Hash: state.CreateContractHash(sender2, ne.Checksum, m.Name), 75 }, 76 } 77 78 return cs1, cs2 79 } 80 81 // GetOracleContractState reads pre-compiled oracle contract generated by 82 // TestGenerateHelperContracts and returns its state. 83 func GetOracleContractState(t *testing.T, pathToInternalContracts string, sender util.Uint160, id int32) *state.Contract { 84 errNotFound := errors.New("auto-generated oracle contract is not found, use TestGenerateHelperContracts to regenerate") 85 86 neBytes, err := os.ReadFile(filepath.Join(pathToInternalContracts, oracleContractNEFPath)) 87 require.NoError(t, err, fmt.Errorf("nef: %w", errNotFound)) 88 ne, err := nef.FileFromBytes(neBytes) 89 require.NoError(t, err) 90 91 mBytes, err := os.ReadFile(filepath.Join(pathToInternalContracts, oracleContractManifestPath)) 92 require.NoError(t, err, fmt.Errorf("manifest: %w", errNotFound)) 93 m := &manifest.Manifest{} 94 err = json.Unmarshal(mBytes, m) 95 require.NoError(t, err) 96 97 return &state.Contract{ 98 ContractBase: state.ContractBase{ 99 NEF: ne, 100 Hash: state.CreateContractHash(sender, ne.Checksum, m.Name), 101 Manifest: *m, 102 ID: id, 103 }, 104 } 105 }