Skip to content

Commit

Permalink
native: drop OnPersistEnd
Browse files Browse the repository at this point in the history
Now that PostPersist is being run for every native contract we can do our
dirty caching tricks right there instead of OnPersistEnd.
  • Loading branch information
roman-khimov committed Dec 14, 2020
1 parent fc10f4e commit 8c0c845
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 64 deletions.
15 changes: 0 additions & 15 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions pkg/core/native/designate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 8 additions & 13 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
15 changes: 5 additions & 10 deletions pkg/core/native/notary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 7 additions & 12 deletions pkg/core/native/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/native_designate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/native_neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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())

Expand Down
1 change: 0 additions & 1 deletion pkg/core/native_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c0c845

Please sign in to comment.