From bb021d077840cf81abd60172c1fd71941ac6b7ee Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 8 Jun 2022 16:41:28 +0300 Subject: [PATCH] native: unbind PutContractState from Management It doesn't need Management's state, ID can't really change. --- pkg/core/interop_system_core_test.go | 28 ++++++++++++++-------------- pkg/core/native/management.go | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pkg/core/interop_system_core_test.go b/pkg/core/interop_system_core_test.go index 09e38c788..4c9f3ae73 100644 --- a/pkg/core/interop_system_core_test.go +++ b/pkg/core/interop_system_core_test.go @@ -133,10 +133,10 @@ func TestRuntimeGetNotifications(t *testing.T) { } func TestRuntimeGetInvocationCounter(t *testing.T) { - v, ic, bc := createVM(t) + v, ic, _ := createVM(t) cs, _ := contracts.GetTestContractState(t, pathToInternalContracts, 4, 5, random.Uint160()) // sender and IDs are not important for the test - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) + require.NoError(t, native.PutContractState(ic.DAO, cs)) ic.Invocations[hash.Hash160([]byte{2})] = 42 @@ -161,9 +161,9 @@ func TestRuntimeGetInvocationCounter(t *testing.T) { } func TestStoragePut(t *testing.T) { - _, cs, ic, bc := createVMAndContractState(t) + _, cs, ic, _ := createVMAndContractState(t) - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) + require.NoError(t, native.PutContractState(ic.DAO, cs)) initVM := func(t *testing.T, key, value []byte, gas int64) { v := ic.SpawnVM() @@ -218,9 +218,9 @@ func TestStoragePut(t *testing.T) { } func TestStorageDelete(t *testing.T) { - v, cs, ic, bc := createVMAndContractState(t) + v, cs, ic, _ := createVMAndContractState(t) - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) + require.NoError(t, native.PutContractState(ic.DAO, cs)) v.LoadScriptWithHash(cs.NEF.Script, cs.Hash, callflag.All) put := func(key, value string, flag int) { v.Estack().PushVal(value) @@ -254,7 +254,7 @@ func BenchmarkStorageFind(b *testing.B) { for count := 10; count <= 10000; count *= 10 { b.Run(fmt.Sprintf("%dElements", count), func(b *testing.B) { v, contractState, context, chain := createVMAndContractState(b) - require.NoError(b, chain.contracts.Management.PutContractState(chain.dao, contractState)) + require.NoError(b, native.PutContractState(chain.dao, contractState)) items := make(map[string]state.StorageItem) for i := 0; i < count; i++ { @@ -298,7 +298,7 @@ func BenchmarkStorageFindIteratorNext(b *testing.B) { for name, last := range cases { b.Run(name, func(b *testing.B) { v, contractState, context, chain := createVMAndContractState(b) - require.NoError(b, chain.contracts.Management.PutContractState(chain.dao, contractState)) + require.NoError(b, native.PutContractState(chain.dao, contractState)) items := make(map[string]state.StorageItem) for i := 0; i < count; i++ { @@ -381,7 +381,7 @@ func TestStorageFind(t *testing.T) { []byte{222}, } - require.NoError(t, chain.contracts.Management.PutContractState(chain.dao, contractState)) + require.NoError(t, native.PutContractState(chain.dao, contractState)) id := contractState.ID @@ -577,11 +577,11 @@ func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Ui } func TestContractCall(t *testing.T) { - _, ic, bc := createVM(t) + _, ic, _ := createVM(t) cs, currCs := contracts.GetTestContractState(t, pathToInternalContracts, 4, 5, random.Uint160()) // sender and IDs are not important for the test - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, currCs)) + require.NoError(t, native.PutContractState(ic.DAO, cs)) + require.NoError(t, native.PutContractState(ic.DAO, currCs)) currScript := currCs.NEF.Script h := cs.Hash @@ -707,7 +707,7 @@ func TestContractCall(t *testing.T) { } func TestRuntimeCheckWitness(t *testing.T) { - _, ic, bc := createVM(t) + _, ic, _ := createVM(t) script := []byte{byte(opcode.RET)} scriptHash := hash.Hash160(script) @@ -878,7 +878,7 @@ func TestRuntimeCheckWitness(t *testing.T) { }, }, } - require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, contractState)) + require.NoError(t, native.PutContractState(ic.DAO, contractState)) loadScriptWithHashAndFlags(ic, contractScript, contractScriptHash, callflag.All) ic.Tx = tx check(t, ic, targetHash.BytesBE(), false, true) diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index eee25bef6..549289b8d 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -272,8 +272,8 @@ func (m *Management) deployWithData(ic *interop.Context, args []stackitem.Item) return contractToStack(newcontract) } -func (m *Management) markUpdated(d *dao.Simple, hash util.Uint160, cs *state.Contract) { - cache := d.GetRWCache(m.ID).(*ManagementCache) +func markUpdated(d *dao.Simple, hash util.Uint160, cs *state.Contract) { + cache := d.GetRWCache(ManagementContractID).(*ManagementCache) delete(cache.nep11, hash) delete(cache.nep17, hash) if cs == nil { @@ -314,7 +314,7 @@ func (m *Management) Deploy(d *dao.Simple, sender util.Uint160, neff *nef.File, Manifest: *manif, }, } - err = m.PutContractState(d, newcontract) + err = PutContractState(d, newcontract) if err != nil { return nil, err } @@ -378,7 +378,7 @@ func (m *Management) Update(d *dao.Simple, hash util.Uint160, neff *nef.File, ma return nil, err } contract.UpdateCounter++ - err = m.PutContractState(d, &contract) + err = PutContractState(d, &contract) if err != nil { return nil, err } @@ -412,7 +412,7 @@ func (m *Management) Destroy(d *dao.Simple, hash util.Uint160) error { return true }) m.Policy.blockAccountInternal(d, hash) - m.markUpdated(d, hash, nil) + markUpdated(d, hash, nil) return nil } @@ -489,7 +489,7 @@ func (m *Management) OnPersist(ic *interop.Context) error { if err := native.Initialize(ic); err != nil { return fmt.Errorf("initializing %s native contract: %w", md.Name, err) } - err := m.putContractState(ic.DAO, cs, false) // Perform cache update manually. + err := putContractState(ic.DAO, cs, false) // Perform cache update manually. if err != nil { return err } @@ -573,18 +573,18 @@ func (m *Management) Initialize(ic *interop.Context) error { } // PutContractState saves given contract state into given DAO. -func (m *Management) PutContractState(d *dao.Simple, cs *state.Contract) error { - return m.putContractState(d, cs, true) +func PutContractState(d *dao.Simple, cs *state.Contract) error { + return putContractState(d, cs, true) } // putContractState is an internal PutContractState representation. -func (m *Management) putContractState(d *dao.Simple, cs *state.Contract, updateCache bool) error { +func putContractState(d *dao.Simple, cs *state.Contract, updateCache bool) error { key := MakeContractKey(cs.Hash) - if err := putConvertibleToDAO(m.ID, d, key, cs); err != nil { + if err := putConvertibleToDAO(ManagementContractID, d, key, cs); err != nil { return err } if updateCache { - m.markUpdated(d, cs.Hash, cs) + markUpdated(d, cs.Hash, cs) } if cs.UpdateCounter != 0 { // Update. return nil