native: cache committee script hash

This commit is contained in:
Evgenii Stratonikov 2020-09-24 15:36:14 +03:00
parent ca1b8a7df0
commit 5b205ffa7d
2 changed files with 19 additions and 18 deletions

View file

@ -35,6 +35,8 @@ type NEO struct {
// (every 28 blocks for mainnet). It's value // (every 28 blocks for mainnet). It's value
// is always equal to value stored by `prefixCommittee`. // is always equal to value stored by `prefixCommittee`.
committee atomic.Value committee atomic.Value
// committeeHash contains script hash of the committee.
committeeHash atomic.Value
} }
// keyWithVotes is a serialized key with votes balance. It's not deserialized // keyWithVotes is a serialized key with votes balance. It's not deserialized
@ -99,6 +101,7 @@ func NewNEO() *NEO {
n.nextValidators.Store(keys.PublicKeys(nil)) n.nextValidators.Store(keys.PublicKeys(nil))
n.validators.Store(keys.PublicKeys(nil)) n.validators.Store(keys.PublicKeys(nil))
n.committee.Store(keys.PublicKeys(nil)) n.committee.Store(keys.PublicKeys(nil))
n.committeeHash.Store(util.Uint160{})
onp := n.Methods["onPersist"] onp := n.Methods["onPersist"]
onp.Func = getOnPersistWrapper(n.onPersist) onp.Func = getOnPersistWrapper(n.onPersist)
@ -166,9 +169,14 @@ func (n *NEO) Initialize(ic *interop.Context) error {
committee := ic.Chain.GetStandByCommittee() committee := ic.Chain.GetStandByCommittee()
n.committee.Store(committee) n.committee.Store(committee)
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
if err != nil {
return err
}
n.committeeHash.Store(hash.Hash160(script))
n.updateNextValidators(committee, ic.Chain) 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 {
return err return err
} }
@ -212,6 +220,11 @@ func (n *NEO) updateCommittee(ic *interop.Context) error {
return err return err
} }
n.committee.Store(committee) n.committee.Store(committee)
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(committee.Copy())
if err != nil {
return err
}
n.committeeHash.Store(hash.Hash160(script))
n.updateNextValidators(committee, ic.Chain) 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()}
@ -332,13 +345,8 @@ func (n *NEO) GetGASPerBlock(ic *interop.Context, index uint32) (*big.Int, error
} }
// GetCommitteeAddress returns address of the committee. // GetCommitteeAddress returns address of the committee.
func (n *NEO) GetCommitteeAddress(bc blockchainer.Blockchainer, d dao.DAO) (util.Uint160, error) { func (n *NEO) GetCommitteeAddress() util.Uint160 {
pubs := n.GetCommitteeMembers() return n.committeeHash.Load().(util.Uint160)
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(pubs)
if err != nil {
return util.Uint160{}, err
}
return hash.Hash160(script), nil
} }
func (n *NEO) setGASPerBlock(ic *interop.Context, args []stackitem.Item) stackitem.Item { func (n *NEO) setGASPerBlock(ic *interop.Context, args []stackitem.Item) stackitem.Item {
@ -355,10 +363,7 @@ func (n *NEO) SetGASPerBlock(ic *interop.Context, index uint32, gas *big.Int) (b
if gas.Sign() == -1 || gas.Cmp(big.NewInt(10*GASFactor)) == 1 { if gas.Sign() == -1 || gas.Cmp(big.NewInt(10*GASFactor)) == 1 {
return false, errors.New("invalid value for GASPerBlock") return false, errors.New("invalid value for GASPerBlock")
} }
h, err := n.GetCommitteeAddress(ic.Chain, ic.DAO) h := n.GetCommitteeAddress()
if err != nil {
return false, err
}
ok, err := runtime.CheckHashedWitness(ic, h) ok, err := runtime.CheckHashedWitness(ic, h)
if err != nil || !ok { if err != nil || !ok {
return ok, err return ok, err

View file

@ -128,9 +128,7 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx) ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
ic.VM = vm.New() ic.VM = vm.New()
h, err := neo.GetCommitteeAddress(bc, bc.dao) h := neo.GetCommitteeAddress()
require.NoError(t, err)
t.Run("Default", func(t *testing.T) { t.Run("Default", func(t *testing.T) {
g, err := neo.GetGASPerBlock(ic, 0) g, err := neo.GetGASPerBlock(ic, 0)
require.NoError(t, err) require.NoError(t, err)
@ -189,9 +187,7 @@ func TestNEO_CalculateBonus(t *testing.T) {
require.EqualValues(t, 0, res.Int64()) require.EqualValues(t, 0, res.Int64())
}) })
t.Run("ManyBlocks", func(t *testing.T) { t.Run("ManyBlocks", func(t *testing.T) {
h, err := neo.GetCommitteeAddress(bc, bc.dao) setSigner(tx, neo.GetCommitteeAddress())
require.NoError(t, err)
setSigner(tx, h)
ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(1*native.GASFactor)) ok, err := neo.SetGASPerBlock(ic, 10, big.NewInt(1*native.GASFactor))
require.NoError(t, err) require.NoError(t, err)
require.True(t, ok) require.True(t, ok)