From f007cca80b13e2d3bb8977688b59ead415e9cd1a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 29 Jun 2020 10:48:35 +0300 Subject: [PATCH] native: hide native contract methods Copy public keys when returning them to the outside and hide unused `GetValidators` method. --- pkg/core/native/native_gas.go | 2 +- pkg/core/native/native_neo.go | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 08683863a..dcc17d27f 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -91,7 +91,7 @@ func (g *GAS) OnPersist(ic *interop.Context) error { absAmount := big.NewInt(int64(tx.SystemFee + tx.NetworkFee)) g.burn(ic, tx.Sender, absAmount) } - validators, err := g.NEO.GetNextBlockValidatorsInternal(ic.Chain, ic.DAO) + validators, err := g.NEO.getNextBlockValidatorsInternal(ic.Chain, ic.DAO) if err != nil { return fmt.Errorf("cannot get block validators: %v", err) } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 677c47266..73388bbdd 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -142,7 +142,7 @@ func (n *NEO) Initialize(ic *interop.Context) error { // OnPersist implements Contract interface. func (n *NEO) OnPersist(ic *interop.Context) error { - pubs, err := n.GetValidatorsInternal(ic.Chain, ic.DAO) + pubs, err := n.getValidatorsInternal(ic.Chain, ic.DAO) if err != nil { return err } @@ -385,8 +385,8 @@ func (n *NEO) getRegisteredValidatorsCall(ic *interop.Context, _ []stackitem.Ite return stackitem.NewArray(arr) } -// GetValidatorsInternal returns a list of current validators. -func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { +// getValidatorsInternal returns a list of current validators. +func (n *NEO) getValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { if vals := n.validators.Load().(keys.PublicKeys); vals != nil { return vals, nil } @@ -442,7 +442,7 @@ func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (ke } func (n *NEO) getValidators(ic *interop.Context, _ []stackitem.Item) stackitem.Item { - result, err := n.GetValidatorsInternal(ic.Chain, ic.DAO) + result, err := n.getValidatorsInternal(ic.Chain, ic.DAO) if err != nil { panic(err) } @@ -450,7 +450,7 @@ func (n *NEO) getValidators(ic *interop.Context, _ []stackitem.Item) stackitem.I } func (n *NEO) getNextBlockValidators(ic *interop.Context, _ []stackitem.Item) stackitem.Item { - result, err := n.GetNextBlockValidatorsInternal(ic.Chain, ic.DAO) + result, err := n.getNextBlockValidatorsInternal(ic.Chain, ic.DAO) if err != nil { panic(err) } @@ -459,9 +459,18 @@ func (n *NEO) getNextBlockValidators(ic *interop.Context, _ []stackitem.Item) st // GetNextBlockValidatorsInternal returns next block validators. func (n *NEO) GetNextBlockValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { + pubs, err := n.getNextBlockValidatorsInternal(bc, d) + if err != nil { + return nil, err + } + return pubs.Copy(), nil +} + +// getNextBlockValidatorsInternal returns next block validators. +func (n *NEO) getNextBlockValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { si := d.GetStorageItem(n.ContractID, nextValidatorsKey) if si == nil { - return n.GetValidatorsInternal(bc, d) + return n.getValidatorsInternal(bc, d) } pubs := keys.PublicKeys{} err := pubs.DecodeBytes(si.Value)