native: unbind PutContractState from Management

It doesn't need Management's state, ID can't really change.
This commit is contained in:
Roman Khimov 2022-06-08 16:41:28 +03:00
parent 209b977e9a
commit bb021d0778
2 changed files with 25 additions and 25 deletions

View file

@ -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)

View file

@ -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