native: drop OnPersistEnd

Now that PostPersist is being run for every native contract we can do our
dirty caching tricks right there instead of OnPersistEnd.
This commit is contained in:
Roman Khimov 2020-12-13 23:30:21 +03:00
parent 938be298f0
commit cb5ecaefe7
9 changed files with 21 additions and 64 deletions

View file

@ -724,21 +724,6 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
bc.lock.Unlock()
return err
}
if err := bc.contracts.Policy.OnPersistEnd(bc.dao); err != nil {
bc.lock.Unlock()
return fmt.Errorf("failed to call OnPersistEnd for Policy native contract: %w", err)
}
if bc.P2PSigExtensionsEnabled() {
err := bc.contracts.Notary.OnPersistEnd(bc.dao)
if err != nil {
bc.lock.Unlock()
return fmt.Errorf("failed to call OnPersistEnd for Notary native contract: %w", err)
}
}
if err := bc.contracts.Designate.OnPersistEnd(bc.dao); err != nil {
bc.lock.Unlock()
return err
}
bc.dao.MPT.Flush()
// Every persist cycle we also compact our in-memory MPT.
persistedHeight := atomic.LoadUint32(&bc.persistedHeight)

View file

@ -540,7 +540,6 @@ func TestVerifyTx(t *testing.T) {
ic.SpawnVM()
ic.VM.LoadScript([]byte{byte(opcode.RET)})
require.NoError(t, bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, oraclePubs))
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
_, err = ic.DAO.Persist()
require.NoError(t, err)
@ -747,7 +746,6 @@ func TestVerifyTx(t *testing.T) {
ic.SpawnVM()
ic.VM.LoadScript([]byte{byte(opcode.RET)})
require.NoError(t, bc.contracts.Designate.DesignateAsRole(ic, native.RoleP2PNotary, keys.PublicKeys{notary.PrivateKey().PublicKey()}))
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
_, err = ic.DAO.Persist()
require.NoError(t, err)
getNotaryAssistedTx := func(signaturesCount uint8, serviceFee int64) *transaction.Transaction {

View file

@ -102,16 +102,11 @@ func (s *Designate) OnPersist(ic *interop.Context) error {
// PostPersist implements Contract interface.
func (s *Designate) PostPersist(ic *interop.Context) error {
return nil
}
// OnPersistEnd updates cached values if they've been changed.
func (s *Designate) OnPersistEnd(d dao.DAO) error {
if !s.rolesChanged() {
return nil
}
nodeKeys, height, err := s.GetDesignatedByRole(d, RoleOracle, math.MaxUint32)
nodeKeys, height, err := s.GetDesignatedByRole(ic.DAO, RoleOracle, math.MaxUint32)
if err != nil {
return err
}

View file

@ -320,7 +320,14 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
}
}
n.OnPersistEnd(ic.DAO)
if n.gasPerBlockChanged.Load().(bool) {
gr, err := n.getSortedGASRecordFromDAO(ic.DAO)
if err != nil {
panic(err)
}
n.gasPerBlock.Store(gr)
n.gasPerBlockChanged.Store(false)
}
return nil
}
@ -347,18 +354,6 @@ func (n *NEO) getGASPerVote(d dao.DAO, key []byte, index ...uint32) []big.Int {
return reward
}
// OnPersistEnd updates cached values if they've been changed.
func (n *NEO) OnPersistEnd(d dao.DAO) {
if n.gasPerBlockChanged.Load().(bool) {
gr, err := n.getSortedGASRecordFromDAO(d)
if err != nil {
panic(err)
}
n.gasPerBlock.Store(gr)
n.gasPerBlockChanged.Store(false)
}
}
func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int) error {
acc, err := state.NEOBalanceStateFromBytes(si.Value)
if err != nil {

View file

@ -158,24 +158,19 @@ func (n *Notary) OnPersist(ic *interop.Context) error {
return nil
}
// OnPersistEnd updates cached Policy values if they've been changed
func (n *Notary) OnPersistEnd(dao dao.DAO) error {
// PostPersist implements Contract interface.
func (n *Notary) PostPersist(ic *interop.Context) error {
n.lock.Lock()
defer n.lock.Unlock()
if n.isValid {
return nil
}
n.lock.Lock()
defer n.lock.Unlock()
n.maxNotValidBeforeDelta = getUint32WithKey(n.ContractID, dao, maxNotValidBeforeDeltaKey, defaultMaxNotValidBeforeDelta)
n.maxNotValidBeforeDelta = getUint32WithKey(n.ContractID, ic.DAO, maxNotValidBeforeDeltaKey, defaultMaxNotValidBeforeDelta)
n.isValid = true
return nil
}
// PostPersist implements Contract interface.
func (n *Notary) PostPersist(ic *interop.Context) error {
return nil
}
// onPayment records deposited amount as belonging to "from" address with a lock
// till the specified chain's height.
func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem.Item {

View file

@ -152,25 +152,20 @@ func (p *Policy) OnPersist(ic *interop.Context) error {
// PostPersist implements Contract interface.
func (p *Policy) PostPersist(ic *interop.Context) error {
return nil
}
// OnPersistEnd updates cached Policy values if they've been changed
func (p *Policy) OnPersistEnd(dao dao.DAO) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.isValid {
return nil
}
p.lock.Lock()
defer p.lock.Unlock()
p.maxTransactionsPerBlock = getUint32WithKey(p.ContractID, dao, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
p.maxBlockSize = getUint32WithKey(p.ContractID, dao, maxBlockSizeKey, defaultMaxBlockSize)
p.feePerByte = getInt64WithKey(p.ContractID, dao, feePerByteKey, defaultFeePerByte)
p.maxBlockSystemFee = getInt64WithKey(p.ContractID, dao, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
p.maxTransactionsPerBlock = getUint32WithKey(p.ContractID, ic.DAO, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
p.maxBlockSize = getUint32WithKey(p.ContractID, ic.DAO, maxBlockSizeKey, defaultMaxBlockSize)
p.feePerByte = getInt64WithKey(p.ContractID, ic.DAO, feePerByteKey, defaultFeePerByte)
p.maxBlockSystemFee = getInt64WithKey(p.ContractID, ic.DAO, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
p.maxVerificationGas = defaultMaxVerificationGas
p.blockedAccounts = make([]util.Uint160, 0)
siMap, err := dao.GetStorageItemsWithPrefix(p.ContractID, []byte{blockedAccountPrefix})
siMap, err := ic.DAO.GetStorageItemsWithPrefix(p.ContractID, []byte{blockedAccountPrefix})
if err != nil {
return fmt.Errorf("failed to get blocked accounts from storage: %w", err)
}

View file

@ -134,7 +134,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
setSigner(tx, testchain.CommitteeScriptHash())
err = des.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
require.NoError(t, err)
require.NoError(t, des.OnPersistEnd(ic.DAO))
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, bl.Index+1)
require.NoError(t, err)
@ -152,7 +151,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
pub1 := priv.PublicKey()
err = des.DesignateAsRole(ic, native.RoleStateValidator, keys.PublicKeys{pub1})
require.NoError(t, err)
require.NoError(t, des.OnPersistEnd(ic.DAO))
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, 255)
require.NoError(t, err)
@ -172,7 +170,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
err = des.DesignateAsRole(ic, native.RoleP2PNotary, keys.PublicKeys{pub1})
require.NoError(t, err)
require.NoError(t, des.OnPersistEnd(ic.DAO))
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleP2PNotary, 255)
require.NoError(t, err)

View file

@ -225,7 +225,6 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor*2))
require.NoError(t, err)
require.True(t, ok)
neo.OnPersistEnd(ic.DAO)
_, err = ic.DAO.Persist()
require.NoError(t, err)
@ -242,7 +241,6 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
})
})
neo.OnPersistEnd(ic.DAO)
g := neo.GetGASPerBlock(ic.DAO, 9)
require.EqualValues(t, 5*native.GASFactor, g.Int64())

View file

@ -144,7 +144,6 @@ func TestOracle_Request(t *testing.T) {
ic.VM.LoadScript([]byte{byte(opcode.RET)})
err = bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
require.NoError(t, err)
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
tx = transaction.New(netmode.UnitTestNet, native.GetOracleResponseScript(), 0)
ic.Tx = tx