forked from TrueCloudLab/neoneo-go
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:
parent
938be298f0
commit
cb5ecaefe7
9 changed files with 21 additions and 64 deletions
|
@ -724,21 +724,6 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
|
||||||
bc.lock.Unlock()
|
bc.lock.Unlock()
|
||||||
return err
|
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()
|
bc.dao.MPT.Flush()
|
||||||
// Every persist cycle we also compact our in-memory MPT.
|
// Every persist cycle we also compact our in-memory MPT.
|
||||||
persistedHeight := atomic.LoadUint32(&bc.persistedHeight)
|
persistedHeight := atomic.LoadUint32(&bc.persistedHeight)
|
||||||
|
|
|
@ -540,7 +540,6 @@ func TestVerifyTx(t *testing.T) {
|
||||||
ic.SpawnVM()
|
ic.SpawnVM()
|
||||||
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
||||||
require.NoError(t, bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, oraclePubs))
|
require.NoError(t, bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, oraclePubs))
|
||||||
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
|
|
||||||
_, err = ic.DAO.Persist()
|
_, err = ic.DAO.Persist()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -747,7 +746,6 @@ func TestVerifyTx(t *testing.T) {
|
||||||
ic.SpawnVM()
|
ic.SpawnVM()
|
||||||
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
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.DesignateAsRole(ic, native.RoleP2PNotary, keys.PublicKeys{notary.PrivateKey().PublicKey()}))
|
||||||
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
|
|
||||||
_, err = ic.DAO.Persist()
|
_, err = ic.DAO.Persist()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
getNotaryAssistedTx := func(signaturesCount uint8, serviceFee int64) *transaction.Transaction {
|
getNotaryAssistedTx := func(signaturesCount uint8, serviceFee int64) *transaction.Transaction {
|
||||||
|
|
|
@ -102,16 +102,11 @@ func (s *Designate) OnPersist(ic *interop.Context) error {
|
||||||
|
|
||||||
// PostPersist implements Contract interface.
|
// PostPersist implements Contract interface.
|
||||||
func (s *Designate) PostPersist(ic *interop.Context) error {
|
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() {
|
if !s.rolesChanged() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeKeys, height, err := s.GetDesignatedByRole(d, RoleOracle, math.MaxUint32)
|
nodeKeys, height, err := s.GetDesignatedByRole(ic.DAO, RoleOracle, math.MaxUint32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,18 +354,6 @@ func (n *NEO) getGASPerVote(d dao.DAO, key []byte, index ...uint32) []big.Int {
|
||||||
return reward
|
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 {
|
func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int) error {
|
||||||
acc, err := state.NEOBalanceStateFromBytes(si.Value)
|
acc, err := state.NEOBalanceStateFromBytes(si.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -158,24 +158,19 @@ func (n *Notary) OnPersist(ic *interop.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnPersistEnd updates cached Policy values if they've been changed
|
// PostPersist implements Contract interface.
|
||||||
func (n *Notary) OnPersistEnd(dao dao.DAO) error {
|
func (n *Notary) PostPersist(ic *interop.Context) error {
|
||||||
|
n.lock.Lock()
|
||||||
|
defer n.lock.Unlock()
|
||||||
if n.isValid {
|
if n.isValid {
|
||||||
return nil
|
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
|
n.isValid = true
|
||||||
return nil
|
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
|
// onPayment records deposited amount as belonging to "from" address with a lock
|
||||||
// till the specified chain's height.
|
// till the specified chain's height.
|
||||||
func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||||
|
|
|
@ -152,25 +152,20 @@ func (p *Policy) OnPersist(ic *interop.Context) error {
|
||||||
|
|
||||||
// PostPersist implements Contract interface.
|
// PostPersist implements Contract interface.
|
||||||
func (p *Policy) PostPersist(ic *interop.Context) error {
|
func (p *Policy) PostPersist(ic *interop.Context) error {
|
||||||
return nil
|
p.lock.Lock()
|
||||||
}
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
// OnPersistEnd updates cached Policy values if they've been changed
|
|
||||||
func (p *Policy) OnPersistEnd(dao dao.DAO) error {
|
|
||||||
if p.isValid {
|
if p.isValid {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p.lock.Lock()
|
|
||||||
defer p.lock.Unlock()
|
|
||||||
|
|
||||||
p.maxTransactionsPerBlock = getUint32WithKey(p.ContractID, dao, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
|
p.maxTransactionsPerBlock = getUint32WithKey(p.ContractID, ic.DAO, maxTransactionsPerBlockKey, defaultMaxTransactionsPerBlock)
|
||||||
p.maxBlockSize = getUint32WithKey(p.ContractID, dao, maxBlockSizeKey, defaultMaxBlockSize)
|
p.maxBlockSize = getUint32WithKey(p.ContractID, ic.DAO, maxBlockSizeKey, defaultMaxBlockSize)
|
||||||
p.feePerByte = getInt64WithKey(p.ContractID, dao, feePerByteKey, defaultFeePerByte)
|
p.feePerByte = getInt64WithKey(p.ContractID, ic.DAO, feePerByteKey, defaultFeePerByte)
|
||||||
p.maxBlockSystemFee = getInt64WithKey(p.ContractID, dao, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
|
p.maxBlockSystemFee = getInt64WithKey(p.ContractID, ic.DAO, maxBlockSystemFeeKey, defaultMaxBlockSystemFee)
|
||||||
p.maxVerificationGas = defaultMaxVerificationGas
|
p.maxVerificationGas = defaultMaxVerificationGas
|
||||||
|
|
||||||
p.blockedAccounts = make([]util.Uint160, 0)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get blocked accounts from storage: %w", err)
|
return fmt.Errorf("failed to get blocked accounts from storage: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
|
||||||
setSigner(tx, testchain.CommitteeScriptHash())
|
setSigner(tx, testchain.CommitteeScriptHash())
|
||||||
err = des.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
|
err = des.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, des.OnPersistEnd(ic.DAO))
|
|
||||||
|
|
||||||
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, bl.Index+1)
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, bl.Index+1)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -152,7 +151,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
|
||||||
pub1 := priv.PublicKey()
|
pub1 := priv.PublicKey()
|
||||||
err = des.DesignateAsRole(ic, native.RoleStateValidator, keys.PublicKeys{pub1})
|
err = des.DesignateAsRole(ic, native.RoleStateValidator, keys.PublicKeys{pub1})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, des.OnPersistEnd(ic.DAO))
|
|
||||||
|
|
||||||
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, 255)
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleOracle, 255)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -172,7 +170,6 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
|
||||||
|
|
||||||
err = des.DesignateAsRole(ic, native.RoleP2PNotary, keys.PublicKeys{pub1})
|
err = des.DesignateAsRole(ic, native.RoleP2PNotary, keys.PublicKeys{pub1})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, des.OnPersistEnd(ic.DAO))
|
|
||||||
|
|
||||||
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleP2PNotary, 255)
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, native.RoleP2PNotary, 255)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
@ -225,7 +225,6 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
|
||||||
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor*2))
|
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(native.GASFactor*2))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
neo.OnPersistEnd(ic.DAO)
|
|
||||||
_, err = ic.DAO.Persist()
|
_, err = ic.DAO.Persist()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -242,7 +241,6 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
neo.OnPersistEnd(ic.DAO)
|
|
||||||
g := neo.GetGASPerBlock(ic.DAO, 9)
|
g := neo.GetGASPerBlock(ic.DAO, 9)
|
||||||
require.EqualValues(t, 5*native.GASFactor, g.Int64())
|
require.EqualValues(t, 5*native.GASFactor, g.Int64())
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,6 @@ func TestOracle_Request(t *testing.T) {
|
||||||
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
||||||
err = bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
|
err = bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, bc.contracts.Designate.OnPersistEnd(ic.DAO))
|
|
||||||
|
|
||||||
tx = transaction.New(netmode.UnitTestNet, native.GetOracleResponseScript(), 0)
|
tx = transaction.New(netmode.UnitTestNet, native.GetOracleResponseScript(), 0)
|
||||||
ic.Tx = tx
|
ic.Tx = tx
|
||||||
|
|
Loading…
Reference in a new issue