forked from TrueCloudLab/neoneo-go
native: unbind PutContractState from Management
It doesn't need Management's state, ID can't really change.
This commit is contained in:
parent
209b977e9a
commit
bb021d0778
2 changed files with 25 additions and 25 deletions
|
@ -133,10 +133,10 @@ func TestRuntimeGetNotifications(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRuntimeGetInvocationCounter(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
|
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
|
ic.Invocations[hash.Hash160([]byte{2})] = 42
|
||||||
|
|
||||||
|
@ -161,9 +161,9 @@ func TestRuntimeGetInvocationCounter(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStoragePut(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) {
|
initVM := func(t *testing.T, key, value []byte, gas int64) {
|
||||||
v := ic.SpawnVM()
|
v := ic.SpawnVM()
|
||||||
|
@ -218,9 +218,9 @@ func TestStoragePut(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStorageDelete(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)
|
v.LoadScriptWithHash(cs.NEF.Script, cs.Hash, callflag.All)
|
||||||
put := func(key, value string, flag int) {
|
put := func(key, value string, flag int) {
|
||||||
v.Estack().PushVal(value)
|
v.Estack().PushVal(value)
|
||||||
|
@ -254,7 +254,7 @@ func BenchmarkStorageFind(b *testing.B) {
|
||||||
for count := 10; count <= 10000; count *= 10 {
|
for count := 10; count <= 10000; count *= 10 {
|
||||||
b.Run(fmt.Sprintf("%dElements", count), func(b *testing.B) {
|
b.Run(fmt.Sprintf("%dElements", count), func(b *testing.B) {
|
||||||
v, contractState, context, chain := createVMAndContractState(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)
|
items := make(map[string]state.StorageItem)
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
@ -298,7 +298,7 @@ func BenchmarkStorageFindIteratorNext(b *testing.B) {
|
||||||
for name, last := range cases {
|
for name, last := range cases {
|
||||||
b.Run(name, func(b *testing.B) {
|
b.Run(name, func(b *testing.B) {
|
||||||
v, contractState, context, chain := createVMAndContractState(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)
|
items := make(map[string]state.StorageItem)
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
@ -381,7 +381,7 @@ func TestStorageFind(t *testing.T) {
|
||||||
[]byte{222},
|
[]byte{222},
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, chain.contracts.Management.PutContractState(chain.dao, contractState))
|
require.NoError(t, native.PutContractState(chain.dao, contractState))
|
||||||
|
|
||||||
id := contractState.ID
|
id := contractState.ID
|
||||||
|
|
||||||
|
@ -577,11 +577,11 @@ func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Ui
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContractCall(t *testing.T) {
|
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
|
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, native.PutContractState(ic.DAO, cs))
|
||||||
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, currCs))
|
require.NoError(t, native.PutContractState(ic.DAO, currCs))
|
||||||
|
|
||||||
currScript := currCs.NEF.Script
|
currScript := currCs.NEF.Script
|
||||||
h := cs.Hash
|
h := cs.Hash
|
||||||
|
@ -707,7 +707,7 @@ func TestContractCall(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRuntimeCheckWitness(t *testing.T) {
|
func TestRuntimeCheckWitness(t *testing.T) {
|
||||||
_, ic, bc := createVM(t)
|
_, ic, _ := createVM(t)
|
||||||
|
|
||||||
script := []byte{byte(opcode.RET)}
|
script := []byte{byte(opcode.RET)}
|
||||||
scriptHash := hash.Hash160(script)
|
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)
|
loadScriptWithHashAndFlags(ic, contractScript, contractScriptHash, callflag.All)
|
||||||
ic.Tx = tx
|
ic.Tx = tx
|
||||||
check(t, ic, targetHash.BytesBE(), false, true)
|
check(t, ic, targetHash.BytesBE(), false, true)
|
||||||
|
|
|
@ -272,8 +272,8 @@ func (m *Management) deployWithData(ic *interop.Context, args []stackitem.Item)
|
||||||
return contractToStack(newcontract)
|
return contractToStack(newcontract)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Management) markUpdated(d *dao.Simple, hash util.Uint160, cs *state.Contract) {
|
func markUpdated(d *dao.Simple, hash util.Uint160, cs *state.Contract) {
|
||||||
cache := d.GetRWCache(m.ID).(*ManagementCache)
|
cache := d.GetRWCache(ManagementContractID).(*ManagementCache)
|
||||||
delete(cache.nep11, hash)
|
delete(cache.nep11, hash)
|
||||||
delete(cache.nep17, hash)
|
delete(cache.nep17, hash)
|
||||||
if cs == nil {
|
if cs == nil {
|
||||||
|
@ -314,7 +314,7 @@ func (m *Management) Deploy(d *dao.Simple, sender util.Uint160, neff *nef.File,
|
||||||
Manifest: *manif,
|
Manifest: *manif,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err = m.PutContractState(d, newcontract)
|
err = PutContractState(d, newcontract)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -378,7 +378,7 @@ func (m *Management) Update(d *dao.Simple, hash util.Uint160, neff *nef.File, ma
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
contract.UpdateCounter++
|
contract.UpdateCounter++
|
||||||
err = m.PutContractState(d, &contract)
|
err = PutContractState(d, &contract)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -412,7 +412,7 @@ func (m *Management) Destroy(d *dao.Simple, hash util.Uint160) error {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
m.Policy.blockAccountInternal(d, hash)
|
m.Policy.blockAccountInternal(d, hash)
|
||||||
m.markUpdated(d, hash, nil)
|
markUpdated(d, hash, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -489,7 +489,7 @@ func (m *Management) OnPersist(ic *interop.Context) error {
|
||||||
if err := native.Initialize(ic); err != nil {
|
if err := native.Initialize(ic); err != nil {
|
||||||
return fmt.Errorf("initializing %s native contract: %w", md.Name, err)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -573,18 +573,18 @@ func (m *Management) Initialize(ic *interop.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutContractState saves given contract state into given DAO.
|
// PutContractState saves given contract state into given DAO.
|
||||||
func (m *Management) PutContractState(d *dao.Simple, cs *state.Contract) error {
|
func PutContractState(d *dao.Simple, cs *state.Contract) error {
|
||||||
return m.putContractState(d, cs, true)
|
return putContractState(d, cs, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// putContractState is an internal PutContractState representation.
|
// 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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
if updateCache {
|
if updateCache {
|
||||||
m.markUpdated(d, cs.Hash, cs)
|
markUpdated(d, cs.Hash, cs)
|
||||||
}
|
}
|
||||||
if cs.UpdateCounter != 0 { // Update.
|
if cs.UpdateCounter != 0 { // Update.
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue