diff --git a/cli/vm/cli.go b/cli/vm/cli.go index 36b021ab7..9ee107cf2 100644 --- a/cli/vm/cli.go +++ b/cli/vm/cli.go @@ -27,6 +27,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" + "github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -856,8 +857,7 @@ func handleLoadDeployed(c *cli.Context) error { if err != nil { return fmt.Errorf("failed to parse contract hash, address or ID: %w", err) } - bc := getChainFromContext(c.App) - h, err = bc.GetContractScriptHash(int32(i)) // @fixme: can be improved after #2702 to retrieve historic state of destroyed contract by ID. + h, err = native.GetContractScriptHash(ic.DAO, int32(i)) if err != nil { return fmt.Errorf("failed to retrieve contract hash by ID: %w", err) } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 490e6c699..5dc1a54d4 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1774,7 +1774,7 @@ func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[uti if nativeContract != nil { id = nativeContract.Metadata().ID } else { - assetContract, err := bc.contracts.Management.GetContract(cache, sc) + assetContract, err := native.GetContract(cache, sc) if err != nil { return } @@ -2112,7 +2112,7 @@ func (bc *Blockchain) BlockHeight() uint32 { // GetContractState returns contract by its script hash. func (bc *Blockchain) GetContractState(hash util.Uint160) *state.Contract { - contract, err := bc.contracts.Management.GetContract(bc.dao, hash) + contract, err := native.GetContract(bc.dao, hash) if contract == nil && !errors.Is(err, storage.ErrKeyNotFound) { bc.log.Warn("failed to get contract state", zap.Error(err)) } @@ -2823,7 +2823,7 @@ func (bc *Blockchain) newInteropContext(trigger trigger.Type, d *dao.Simple, blo // changes that were not yet persisted to Blockchain's dao. baseStorageFee = bc.contracts.Policy.GetStoragePriceInternal(d) } - ic := interop.NewContext(trigger, bc, d, baseExecFee, baseStorageFee, bc.contracts.Management.GetContract, bc.contracts.Contracts, contract.LoadToken, block, tx, bc.log) + ic := interop.NewContext(trigger, bc, d, baseExecFee, baseStorageFee, native.GetContract, bc.contracts.Contracts, contract.LoadToken, block, tx, bc.log) ic.Functions = systemInterops switch { case tx != nil: diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index faa2d4e4a..3da8bac1f 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -1,6 +1,8 @@ package native import ( + "context" + "encoding/binary" "encoding/json" "errors" "fmt" @@ -11,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop/contract" + istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" @@ -44,7 +47,8 @@ const ( ManagementContractID = -1 // PrefixContract is a prefix used to store contract states inside Management native contract. - PrefixContract = 8 + PrefixContract = 8 + prefixContractHash = 12 defaultMinimumDeploymentFee = 10_00000000 contractDeployNotificationName = "Deploy" @@ -149,6 +153,15 @@ func newManagement() *Management { md = newMethodAndPrice(m.hasMethod, 1<<15, callflag.ReadStates) m.AddMethod(md, desc) + desc = newDescriptor("getContractById", smartcontract.ArrayType, + manifest.NewParameter("id", smartcontract.IntegerType)) + md = newMethodAndPrice(m.getContractByID, 1<<15, callflag.ReadStates) + m.AddMethod(md, desc) + + desc = newDescriptor("getContractHashes", smartcontract.InteropInterfaceType) + md = newMethodAndPrice(m.getContractHashes, 1<<15, callflag.ReadStates) + m.AddMethod(md, desc) + hashParam := manifest.NewParameter("Hash", smartcontract.Hash160Type) m.AddEvent(contractDeployNotificationName, hashParam) m.AddEvent(contractUpdateNotificationName, hashParam) @@ -172,7 +185,28 @@ func toHash160(si stackitem.Item) util.Uint160 { // VM protections, so it's OK for it to panic instead of returning errors. func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) stackitem.Item { hash := toHash160(args[0]) - ctr, err := m.GetContract(ic.DAO, hash) + ctr, err := GetContract(ic.DAO, hash) + if err != nil { + if errors.Is(err, storage.ErrKeyNotFound) { + return stackitem.Null{} + } + panic(err) + } + return contractToStack(ctr) +} + +// getContractByID is an implementation of public getContractById method, it's run under +// VM protections, so it's OK for it to panic instead of returning errors. +func (m *Management) getContractByID(ic *interop.Context, args []stackitem.Item) stackitem.Item { + idBig, err := args[0].TryInteger() + if err != nil { + panic(err) + } + id := idBig.Int64() + if !idBig.IsInt64() || id < math.MinInt32 || id > math.MaxInt32 { + panic("id is not a correct int32") + } + ctr, err := GetContractByID(ic.DAO, int32(id)) if err != nil { if errors.Is(err, storage.ErrKeyNotFound) { return stackitem.Null{} @@ -183,8 +217,8 @@ func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) sta } // GetContract returns a contract with the given hash from the given DAO. -func (m *Management) GetContract(d *dao.Simple, hash util.Uint160) (*state.Contract, error) { - cache := d.GetROCache(m.ID).(*ManagementCache) +func GetContract(d *dao.Simple, hash util.Uint160) (*state.Contract, error) { + cache := d.GetROCache(ManagementContractID).(*ManagementCache) cs, ok := cache.contracts[hash] if !ok { return nil, storage.ErrKeyNotFound @@ -192,6 +226,21 @@ func (m *Management) GetContract(d *dao.Simple, hash util.Uint160) (*state.Contr return cs, nil } +// GetContractByID returns a contract with the given ID from the given DAO. +func GetContractByID(d *dao.Simple, id int32) (*state.Contract, error) { + key := make([]byte, 5) + key = putHashKey(key, id) + si := d.GetStorageItem(ManagementContractID, key) + if si == nil { + return nil, storage.ErrKeyNotFound + } + hash, err := util.Uint160DecodeBytesBE(si) + if err != nil { + return nil, err + } + return GetContract(d, hash) +} + func getLimitedSlice(arg stackitem.Item, max int) ([]byte, error) { _, isNull := arg.(stackitem.Null) if isNull { @@ -211,6 +260,29 @@ func getLimitedSlice(arg stackitem.Item, max int) ([]byte, error) { return b, nil } +func (m *Management) getContractHashes(ic *interop.Context, _ []stackitem.Item) stackitem.Item { + ctx, cancel := context.WithCancel(context.Background()) + prefix := []byte{prefixContractHash} + seekres := ic.DAO.SeekAsync(ctx, ManagementContractID, storage.SeekRange{Prefix: prefix}) + filteredRes := make(chan storage.KeyValue) + go func() { + for kv := range seekres { + if len(kv.Key) == 4 && binary.BigEndian.Uint32(kv.Key) < math.MaxInt32 { + filteredRes <- kv + } + } + close(filteredRes) + }() + opts := istorage.FindRemovePrefix + item := istorage.NewIterator(filteredRes, prefix, int64(opts)) + ic.RegisterCancelFunc(func() { + cancel() + for range seekres { + } + }) + return stackitem.NewInterop(item) +} + // getNefAndManifestFromItems converts input arguments into NEF and manifest // adding an appropriate deployment GAS price and sanitizing inputs. func (m *Management) getNefAndManifestFromItems(ic *interop.Context, args []stackitem.Item, isDeploy bool) (*nef.File, *manifest.Manifest, error) { @@ -303,7 +375,7 @@ func (m *Management) Deploy(d *dao.Simple, sender util.Uint160, neff *nef.File, if m.Policy.IsBlocked(d, h) { return nil, fmt.Errorf("the contract %s has been blocked", h.StringLE()) } - _, err := m.GetContract(d, h) + _, err := GetContract(d, h) if err == nil { return nil, errors.New("contract already exists") } @@ -362,7 +434,7 @@ func (m *Management) updateWithData(ic *interop.Context, args []stackitem.Item) func (m *Management) Update(d *dao.Simple, hash util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) { var contract state.Contract - oldcontract, err := m.GetContract(d, hash) + oldcontract, err := GetContract(d, hash) if err != nil { return nil, errors.New("contract doesn't exist") } @@ -412,12 +484,14 @@ func (m *Management) destroy(ic *interop.Context, sis []stackitem.Item) stackite // Destroy drops the given contract from DAO along with its storage. It doesn't emit notification. func (m *Management) Destroy(d *dao.Simple, hash util.Uint160) error { - contract, err := m.GetContract(d, hash) + contract, err := GetContract(d, hash) if err != nil { return err } key := MakeContractKey(hash) d.DeleteStorageItem(m.ID, key) + key = putHashKey(key, contract.ID) + d.DeleteStorageItem(ManagementContractID, key) d.DeleteContractID(contract.ID) d.Seek(contract.ID, storage.SeekRange{}, func(k, _ []byte) bool { @@ -476,7 +550,7 @@ func (m *Management) hasMethod(ic *interop.Context, args []stackitem.Item) stack panic(err) } pcount := int(toInt64((args[2]))) - cs, err := m.GetContract(ic.DAO, cHash) + cs, err := GetContract(ic.DAO, cHash) if err != nil { return stackitem.NewBool(false) } @@ -610,10 +684,18 @@ func putContractState(d *dao.Simple, cs *state.Contract, updateCache bool) error if cs.UpdateCounter != 0 { // Update. return nil } + key = putHashKey(key, cs.ID) + d.PutStorageItem(ManagementContractID, key, cs.Hash.BytesBE()) d.PutContractID(cs.ID, cs.Hash) return nil } +func putHashKey(buf []byte, id int32) []byte { + buf[0] = prefixContractHash + binary.BigEndian.PutUint32(buf[1:], uint32(id)) + return buf[:5] +} + func (m *Management) getNextContractID(d *dao.Simple) (int32, error) { si := d.GetStorageItem(m.ID, keyNextAvailableID) if si == nil { diff --git a/pkg/core/native/management_test.go b/pkg/core/native/management_test.go index 2b3a71df5..a4cb7bc87 100644 --- a/pkg/core/native/management_test.go +++ b/pkg/core/native/management_test.go @@ -57,7 +57,11 @@ func TestDeployGetUpdateDestroyContract(t *testing.T) { require.Equal(t, ne, &contract2.NEF) require.Equal(t, *manif, contract2.Manifest) - refContract, err := mgmt.GetContract(d, h) + refContract, err := GetContract(d, h) + require.NoError(t, err) + require.Equal(t, contract, refContract) + + refContract, err = GetContractByID(d, contract.ID) require.NoError(t, err) require.Equal(t, contract, refContract) @@ -68,7 +72,9 @@ func TestDeployGetUpdateDestroyContract(t *testing.T) { err = mgmt.Destroy(d, h) require.NoError(t, err) - _, err = mgmt.GetContract(d, h) + _, err = GetContract(d, h) + require.Error(t, err) + _, err = GetContractByID(d, contract.ID) require.Error(t, err) } @@ -140,11 +146,11 @@ func TestManagement_GetNEP17Contracts(t *testing.T) { // No changes expected in lower store. require.Equal(t, []util.Uint160{c1.Hash}, mgmt.GetNEP17Contracts(d)) - c1Lower, err := mgmt.GetContract(d, c1.Hash) + c1Lower, err := GetContract(d, c1.Hash) require.NoError(t, err) require.Equal(t, 1, len(c1Lower.Manifest.ABI.Methods)) require.Equal(t, []util.Uint160{c1Updated.Hash}, mgmt.GetNEP17Contracts(private)) - c1Upper, err := mgmt.GetContract(private, c1Updated.Hash) + c1Upper, err := GetContract(private, c1Updated.Hash) require.NoError(t, err) require.Equal(t, 2, len(c1Upper.Manifest.ABI.Methods)) @@ -152,7 +158,7 @@ func TestManagement_GetNEP17Contracts(t *testing.T) { _, err = private.Persist() require.NoError(t, err) require.Equal(t, []util.Uint160{c1.Hash}, mgmt.GetNEP17Contracts(d)) - c1Lower, err = mgmt.GetContract(d, c1.Hash) + c1Lower, err = GetContract(d, c1.Hash) require.NoError(t, err) require.Equal(t, 2, len(c1Lower.Manifest.ABI.Methods)) } diff --git a/pkg/core/native/native_test/management_test.go b/pkg/core/native/native_test/management_test.go index 68f808895..22b8669d6 100644 --- a/pkg/core/native/native_test/management_test.go +++ b/pkg/core/native/native_test/management_test.go @@ -8,6 +8,7 @@ import ( "github.com/nspcc-dev/neo-go/internal/contracts" "github.com/nspcc-dev/neo-go/pkg/core/chaindump" + "github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" @@ -556,6 +557,41 @@ func TestManagement_GetContract(t *testing.T) { t.Run("positive", func(t *testing.T) { managementInvoker.Invoke(t, si, "getContract", cs1.Hash.BytesBE()) }) + t.Run("by ID, bad parameter type", func(t *testing.T) { + managementInvoker.InvokeFail(t, "invalid conversion: Array/Integer", "getContractById", []interface{}{int64(1)}) + }) + t.Run("by ID, bad num", func(t *testing.T) { + managementInvoker.InvokeFail(t, "id is not a correct int32", "getContractById", []byte{1, 2, 3, 4, 5}) + }) + t.Run("by ID, positive", func(t *testing.T) { + managementInvoker.Invoke(t, si, "getContractById", cs1.ID) + }) + t.Run("by ID, native", func(t *testing.T) { + csm := managementInvoker.Executor.Chain.GetContractState(managementInvoker.Hash) + require.NotNil(t, csm) + sim, err := csm.ToStackItem() + require.NoError(t, err) + managementInvoker.Invoke(t, sim, "getContractById", -1) + }) + t.Run("by ID, empty", func(t *testing.T) { + managementInvoker.Invoke(t, stackitem.Null{}, "getContractById", -100) + }) + t.Run("contract hashes", func(t *testing.T) { + w := io.NewBufBinWriter() + emit.AppCall(w.BinWriter, managementInvoker.Hash, "getContractHashes", callflag.All) + emit.Opcodes(w.BinWriter, opcode.DUP) // Iterator. + emit.Syscall(w.BinWriter, interopnames.SystemIteratorNext) + emit.Opcodes(w.BinWriter, opcode.ASSERT) // Has one element. + emit.Opcodes(w.BinWriter, opcode.DUP) // Iterator. + emit.Syscall(w.BinWriter, interopnames.SystemIteratorValue) + emit.Opcodes(w.BinWriter, opcode.SWAP) // Iterator to the top. + emit.Syscall(w.BinWriter, interopnames.SystemIteratorNext) + emit.Opcodes(w.BinWriter, opcode.NOT) + emit.Opcodes(w.BinWriter, opcode.ASSERT) // No more elements, single value left on the stack. + require.NoError(t, w.Err) + h := managementInvoker.InvokeScript(t, w.Bytes(), managementInvoker.Signers) + managementInvoker.Executor.CheckHalt(t, h, stackitem.NewStruct([]stackitem.Item{stackitem.Make([]byte{0, 0, 0, 1}), stackitem.Make(cs1.Hash.BytesBE())})) + }) } func TestManagement_ContractDestroy(t *testing.T) { diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 2d854c221..03c16710c 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -82,7 +82,7 @@ const ( faultedTxHashLE = "82279bfe9bada282ca0f8cb8e0bb124b921af36f00c69a518320322c6f4fef60" faultedTxBlock uint32 = 23 invokescriptContractAVM = "VwIADBQBDAMOBQYMDQIODw0DDgcJAAAAAErZMCQE2zBwaEH4J+yMqiYEEUAMFA0PAwIJAAIBAwcDBAUCAQAOBgwJStkwJATbMHFpQfgn7IyqJgQSQBNA" - block20StateRootLE = "b49a045246bf3bb90248ed538dd21e67d782a9242c52f31dfdef3da65ecd87c1" + block20StateRootLE = "13620fef0fb28060523a0b73ce574ee4658fca5d0d24078a73e74a349c37a854" ) var (