Merge pull request #1432 from nspcc-dev/fix/postpersist
Initialize cache in native contracts properly
This commit is contained in:
commit
c21f699ffc
2 changed files with 33 additions and 12 deletions
|
@ -173,13 +173,10 @@ func (n *NEO) Initialize(ic *interop.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
committee := ic.Chain.GetStandByCommittee()
|
committee := ic.Chain.GetStandByCommittee()
|
||||||
n.committee.Store(committee)
|
err := n.updateCache(committee, ic.Chain)
|
||||||
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n.committeeHash.Store(hash.Hash160(script))
|
|
||||||
n.updateNextValidators(committee, ic.Chain)
|
|
||||||
|
|
||||||
err = ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, &state.StorageItem{Value: committee.Bytes()})
|
err = ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, &state.StorageItem{Value: committee.Bytes()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -207,10 +204,18 @@ func (n *NEO) Initialize(ic *interop.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NEO) updateNextValidators(committee keys.PublicKeys, bc blockchainer.Blockchainer) {
|
func (n *NEO) updateCache(committee keys.PublicKeys, bc blockchainer.Blockchainer) error {
|
||||||
|
n.committee.Store(committee)
|
||||||
|
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n.committeeHash.Store(hash.Hash160(script))
|
||||||
|
|
||||||
nextVals := committee[:bc.GetConfig().ValidatorsCount].Copy()
|
nextVals := committee[:bc.GetConfig().ValidatorsCount].Copy()
|
||||||
sort.Sort(nextVals)
|
sort.Sort(nextVals)
|
||||||
n.nextValidators.Store(nextVals)
|
n.nextValidators.Store(nextVals)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NEO) updateCommittee(ic *interop.Context) error {
|
func (n *NEO) updateCommittee(ic *interop.Context) error {
|
||||||
|
@ -226,13 +231,9 @@ func (n *NEO) updateCommittee(ic *interop.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n.committee.Store(committee)
|
if err := n.updateCache(committee, ic.Chain); err != nil {
|
||||||
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n.committeeHash.Store(hash.Hash160(script))
|
|
||||||
n.updateNextValidators(committee, ic.Chain)
|
|
||||||
n.votesChanged.Store(false)
|
n.votesChanged.Store(false)
|
||||||
si := &state.StorageItem{Value: committee.Bytes()}
|
si := &state.StorageItem{Value: committee.Bytes()}
|
||||||
return ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, si)
|
return ic.DAO.PutStorageItem(n.ContractID, prefixCommittee, si)
|
||||||
|
@ -246,6 +247,25 @@ func shouldUpdateCommittee(h uint32, bc blockchainer.Blockchainer) bool {
|
||||||
|
|
||||||
// OnPersist implements Contract interface.
|
// OnPersist implements Contract interface.
|
||||||
func (n *NEO) OnPersist(ic *interop.Context) error {
|
func (n *NEO) OnPersist(ic *interop.Context) error {
|
||||||
|
gpb := n.gasPerBlockChanged.Load()
|
||||||
|
if gpb == nil {
|
||||||
|
committee := keys.PublicKeys{}
|
||||||
|
si := ic.DAO.GetStorageItem(n.ContractID, prefixCommittee)
|
||||||
|
if err := committee.DecodeBytes(si.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := n.updateCache(committee, ic.Chain); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var gr state.GASRecord
|
||||||
|
si = ic.DAO.GetStorageItem(n.ContractID, []byte{prefixGASPerBlock})
|
||||||
|
if err := gr.FromBytes(si.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n.gasPerBlock.Store(gr)
|
||||||
|
n.gasPerBlockChanged.Store(false)
|
||||||
|
}
|
||||||
if shouldUpdateCommittee(ic.Block.Index, ic.Chain) {
|
if shouldUpdateCommittee(ic.Block.Index, ic.Chain) {
|
||||||
if err := n.updateCommittee(ic); err != nil {
|
if err := n.updateCommittee(ic); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -130,6 +130,9 @@ func newOracle() *Oracle {
|
||||||
md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates)
|
md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates)
|
||||||
o.AddMethod(md, desc, false)
|
o.AddMethod(md, desc, false)
|
||||||
|
|
||||||
|
o.nodes.Store(keys.PublicKeys(nil))
|
||||||
|
o.nodesChanged.Store(false)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,8 +202,6 @@ func (o *Oracle) Initialize(ic *interop.Context) error {
|
||||||
if err := ic.DAO.PutStorageItem(o.ContractID, prefixNodeList, si); err != nil {
|
if err := ic.DAO.PutStorageItem(o.ContractID, prefixNodeList, si); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
o.nodes.Store(keys.PublicKeys(nil))
|
|
||||||
o.nodesChanged.Store(false)
|
|
||||||
si = &state.StorageItem{Value: make([]byte, 8)} // uint64(0) LE
|
si = &state.StorageItem{Value: make([]byte, 8)} // uint64(0) LE
|
||||||
return ic.DAO.PutStorageItem(o.ContractID, prefixRequestID, si)
|
return ic.DAO.PutStorageItem(o.ContractID, prefixRequestID, si)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue