core: drop GetStandBy* methods

They're misleading now that we have variable number of committee
members/validators. The standby list can be seen in the configuration and the
appropriate numbers can be received from it also.
This commit is contained in:
Roman Khimov 2022-01-24 18:36:31 +03:00
parent e621f746a7
commit cda1c75db3
11 changed files with 69 additions and 72 deletions

View file

@ -300,16 +300,6 @@ func (chain *FakeChain) GetValidators() ([]*keys.PublicKey, error) {
panic("TODO") panic("TODO")
} }
// GetStandByCommittee implements Blockchainer interface.
func (chain *FakeChain) GetStandByCommittee() keys.PublicKeys {
panic("TODO")
}
// GetStandByValidators implements Blockchainer interface.
func (chain *FakeChain) GetStandByValidators() keys.PublicKeys {
panic("TODO")
}
// GetEnrollments implements Blockchainer interface. // GetEnrollments implements Blockchainer interface.
func (chain *FakeChain) GetEnrollments() ([]state.Validator, error) { func (chain *FakeChain) GetEnrollments() ([]state.Validator, error) {
panic("TODO") panic("TODO")

View file

@ -152,8 +152,6 @@ type Blockchain struct {
// Block's transactions are passed via mempool. // Block's transactions are passed via mempool.
postBlock []func(func(*transaction.Transaction, *mempool.Pool, bool) bool, *mempool.Pool, *block.Block) postBlock []func(func(*transaction.Transaction, *mempool.Pool, bool) bool, *mempool.Pool, *block.Block)
sbCommittee keys.PublicKeys
log *zap.Logger log *zap.Logger
lastBatch *storage.MemBatch lastBatch *storage.MemBatch
@ -247,10 +245,6 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
zap.Int("StateSyncInterval", cfg.StateSyncInterval)) zap.Int("StateSyncInterval", cfg.StateSyncInterval))
} }
} }
committee, err := committeeFromConfig(cfg)
if err != nil {
return nil, err
}
if len(cfg.NativeUpdateHistories) == 0 { if len(cfg.NativeUpdateHistories) == 0 {
cfg.NativeUpdateHistories = map[string][]uint32{} cfg.NativeUpdateHistories = map[string][]uint32{}
log.Info("NativeActivations are not set, using default values") log.Info("NativeActivations are not set, using default values")
@ -262,7 +256,6 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
stopCh: make(chan struct{}), stopCh: make(chan struct{}),
runToExitCh: make(chan struct{}), runToExitCh: make(chan struct{}),
memPool: mempool.New(cfg.MemPoolSize, 0, false), memPool: mempool.New(cfg.MemPoolSize, 0, false),
sbCommittee: committee,
log: log, log: log,
events: make(chan bcEvent), events: make(chan bcEvent),
subCh: make(chan interface{}), subCh: make(chan interface{}),
@ -2089,16 +2082,6 @@ func (bc *Blockchain) PoolTxWithData(t *transaction.Transaction, data interface{
return bc.verifyAndPoolTx(t, mp, feer, data) return bc.verifyAndPoolTx(t, mp, feer, data)
} }
// GetStandByValidators returns validators from the configuration.
func (bc *Blockchain) GetStandByValidators() keys.PublicKeys {
return bc.sbCommittee[:bc.config.GetNumOfCNs(bc.BlockHeight())].Copy()
}
// GetStandByCommittee returns standby committee from the configuration.
func (bc *Blockchain) GetStandByCommittee() keys.PublicKeys {
return bc.sbCommittee.Copy()
}
// GetCommittee returns the sorted list of public keys of nodes in committee. // GetCommittee returns the sorted list of public keys of nodes in committee.
func (bc *Blockchain) GetCommittee() (keys.PublicKeys, error) { func (bc *Blockchain) GetCommittee() (keys.PublicKeys, error) {
pubs := bc.contracts.NEO.GetCommitteeMembers() pubs := bc.contracts.NEO.GetCommitteeMembers()

View file

@ -56,8 +56,6 @@ type Blockchainer interface {
GetNotaryContractScriptHash() util.Uint160 GetNotaryContractScriptHash() util.Uint160
GetNotaryBalance(acc util.Uint160) *big.Int GetNotaryBalance(acc util.Uint160) *big.Int
GetValidators() ([]*keys.PublicKey, error) GetValidators() ([]*keys.PublicKey, error)
GetStandByCommittee() keys.PublicKeys
GetStandByValidators() keys.PublicKeys
GetStateModule() StateRoot GetStateModule() StateRoot
GetStorageItem(id int32, key []byte) state.StorageItem GetStorageItem(id int32, key []byte) state.StorageItem
GetStorageItems(id int32) ([]state.StorageItemWithKey, error) GetStorageItems(id int32) ([]state.StorageItemWithKey, error)

View file

@ -15,7 +15,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
@ -42,8 +41,6 @@ type Ledger interface {
GetBlock(hash util.Uint256) (*block.Block, error) GetBlock(hash util.Uint256) (*block.Block, error)
GetConfig() config.ProtocolConfiguration GetConfig() config.ProtocolConfiguration
GetHeaderHash(int) util.Uint256 GetHeaderHash(int) util.Uint256
GetStandByCommittee() keys.PublicKeys
GetStandByValidators() keys.PublicKeys
GetStoragePrice() int64 GetStoragePrice() int64
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
) )
@ -135,7 +136,12 @@ func (g *GAS) BalanceOf(d dao.DAO, acc util.Uint160) *big.Int {
} }
func getStandbyValidatorsHash(ic *interop.Context) (util.Uint160, error) { func getStandbyValidatorsHash(ic *interop.Context) (util.Uint160, error) {
s, err := smartcontract.CreateDefaultMultiSigRedeemScript(ic.Chain.GetStandByValidators()) cfg := ic.Chain.GetConfig()
committee, err := keys.NewPublicKeysFromStrings(cfg.StandbyCommittee)
if err != nil {
return util.Uint160{}, err
}
s, err := smartcontract.CreateDefaultMultiSigRedeemScript(committee[:cfg.GetNumOfCNs(0)])
if err != nil { if err != nil {
return util.Uint160{}, err return util.Uint160{}, err
} }

View file

@ -10,6 +10,7 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
@ -54,6 +55,10 @@ type NEO struct {
// It is set in state-modifying methods only and read in `PostPersist` thus is not protected // It is set in state-modifying methods only and read in `PostPersist` thus is not protected
// by any mutex. // by any mutex.
gasPerVoteCache map[string]big.Int gasPerVoteCache map[string]big.Int
// Configuration and standby keys are set during initialization and then
// only read from.
cfg config.ProtocolConfiguration
standbyKeys keys.PublicKeys
} }
const ( const (
@ -190,6 +195,10 @@ func newNEO() *NEO {
// Initialize initializes NEO contract. // Initialize initializes NEO contract.
func (n *NEO) Initialize(ic *interop.Context) error { func (n *NEO) Initialize(ic *interop.Context) error {
err := n.initConfigCache(ic.Chain)
if err != nil {
return nil
}
if err := n.nep17TokenNative.Initialize(ic); err != nil { if err := n.nep17TokenNative.Initialize(ic); err != nil {
return err return err
} }
@ -199,9 +208,9 @@ func (n *NEO) Initialize(ic *interop.Context) error {
return errors.New("already initialized") return errors.New("already initialized")
} }
committee := ic.Chain.GetStandByCommittee() committee0 := n.standbyKeys[:n.cfg.GetCommitteeSize(ic.Block.Index)]
cvs := toKeysWithVotes(committee) cvs := toKeysWithVotes(committee0)
err := n.updateCache(cvs, ic.Chain) err = n.updateCache(cvs, ic.Chain)
if err != nil { if err != nil {
return err return err
} }
@ -244,6 +253,10 @@ func (n *NEO) Initialize(ic *interop.Context) error {
// Cache initialisation should be done apart from Initialize because Initialize is // Cache initialisation should be done apart from Initialize because Initialize is
// called only when deploying native contracts. // called only when deploying native contracts.
func (n *NEO) InitializeCache(bc interop.Ledger, d dao.DAO) error { func (n *NEO) InitializeCache(bc interop.Ledger, d dao.DAO) error {
err := n.initConfigCache(bc)
if err != nil {
return nil
}
var committee = keysWithVotes{} var committee = keysWithVotes{}
si := d.GetStorageItem(n.ID, prefixCommittee) si := d.GetStorageItem(n.ID, prefixCommittee)
if err := committee.DecodeBytes(si); err != nil { if err := committee.DecodeBytes(si); err != nil {
@ -263,6 +276,14 @@ func (n *NEO) InitializeCache(bc interop.Ledger, d dao.DAO) error {
return nil return nil
} }
func (n *NEO) initConfigCache(bc interop.Ledger) error {
var err error
n.cfg = bc.GetConfig()
n.standbyKeys, err = keys.NewPublicKeysFromStrings(n.cfg.StandbyCommittee)
return err
}
func (n *NEO) updateCache(cvs keysWithVotes, bc interop.Ledger) error { func (n *NEO) updateCache(cvs keysWithVotes, bc interop.Ledger) error {
n.committee.Store(cvs) n.committee.Store(cvs)
@ -273,8 +294,7 @@ func (n *NEO) updateCache(cvs keysWithVotes, bc interop.Ledger) error {
} }
n.committeeHash.Store(hash.Hash160(script)) n.committeeHash.Store(hash.Hash160(script))
cfg := bc.GetConfig() nextVals := committee[:n.cfg.GetNumOfCNs(bc.BlockHeight()+1)].Copy()
nextVals := committee[:cfg.GetNumOfCNs(bc.BlockHeight()+1)].Copy()
sort.Sort(nextVals) sort.Sort(nextVals)
n.nextValidators.Store(nextVals) n.nextValidators.Store(nextVals)
return nil return nil
@ -301,12 +321,11 @@ func (n *NEO) updateCommittee(ic *interop.Context) error {
// OnPersist implements Contract interface. // OnPersist implements Contract interface.
func (n *NEO) OnPersist(ic *interop.Context) error { func (n *NEO) OnPersist(ic *interop.Context) error {
cfg := ic.Chain.GetConfig() if n.cfg.ShouldUpdateCommitteeAt(ic.Block.Index) {
if cfg.ShouldUpdateCommitteeAt(ic.Block.Index) {
oldKeys := n.nextValidators.Load().(keys.PublicKeys) oldKeys := n.nextValidators.Load().(keys.PublicKeys)
oldCom := n.committee.Load().(keysWithVotes) oldCom := n.committee.Load().(keysWithVotes)
if cfg.GetNumOfCNs(ic.Block.Index) != len(oldKeys) || if n.cfg.GetNumOfCNs(ic.Block.Index) != len(oldKeys) ||
cfg.GetCommitteeSize(ic.Block.Index) != len(oldCom) { n.cfg.GetCommitteeSize(ic.Block.Index) != len(oldCom) {
n.votesChanged.Store(true) n.votesChanged.Store(true)
} }
if err := n.updateCommittee(ic); err != nil { if err := n.updateCommittee(ic); err != nil {
@ -320,17 +339,16 @@ func (n *NEO) OnPersist(ic *interop.Context) error {
func (n *NEO) PostPersist(ic *interop.Context) error { func (n *NEO) PostPersist(ic *interop.Context) error {
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index) gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
pubs := n.GetCommitteeMembers() pubs := n.GetCommitteeMembers()
cfg := ic.Chain.GetConfig() committeeSize := n.cfg.GetCommitteeSize(ic.Block.Index)
committeeSize := cfg.GetCommitteeSize(ic.Block.Index)
index := int(ic.Block.Index) % committeeSize index := int(ic.Block.Index) % committeeSize
committeeReward := new(big.Int).Mul(gas, bigCommitteeRewardRatio) committeeReward := new(big.Int).Mul(gas, bigCommitteeRewardRatio)
n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big100), false) n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big100), false)
if cfg.ShouldUpdateCommitteeAt(ic.Block.Index) { if n.cfg.ShouldUpdateCommitteeAt(ic.Block.Index) {
var voterReward = new(big.Int).Set(bigVoterRewardRatio) var voterReward = new(big.Int).Set(bigVoterRewardRatio)
voterReward.Mul(voterReward, gas) voterReward.Mul(voterReward, gas)
voterReward.Mul(voterReward, big.NewInt(voterRewardFactor*int64(committeeSize))) voterReward.Mul(voterReward, big.NewInt(voterRewardFactor*int64(committeeSize)))
var validatorsCount = cfg.GetNumOfCNs(ic.Block.Index) var validatorsCount = n.cfg.GetNumOfCNs(ic.Block.Index)
voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount))) voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount)))
voterReward.Div(voterReward, big100) voterReward.Div(voterReward, big100)
@ -940,8 +958,7 @@ func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stacki
// ComputeNextBlockValidators returns an actual list of current validators. // ComputeNextBlockValidators returns an actual list of current validators.
func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, error) { func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, error) {
cfg := bc.GetConfig() numOfCNs := n.cfg.GetNumOfCNs(bc.BlockHeight() + 1)
numOfCNs := cfg.GetNumOfCNs(bc.BlockHeight() + 1)
if vals := n.validators.Load().(keys.PublicKeys); vals != nil && numOfCNs == len(vals) { if vals := n.validators.Load().(keys.PublicKeys); vals != nil && numOfCNs == len(vals) {
return vals.Copy(), nil return vals.Copy(), nil
} }
@ -1010,10 +1027,9 @@ func (n *NEO) computeCommitteeMembers(bc interop.Ledger, d dao.DAO) (keys.Public
_, totalSupply := n.getTotalSupply(d) _, totalSupply := n.getTotalSupply(d)
voterTurnout := votersCount.Div(votersCount, totalSupply) voterTurnout := votersCount.Div(votersCount, totalSupply)
sbVals := bc.GetStandByCommittee() count := n.cfg.GetCommitteeSize(bc.BlockHeight() + 1)
cfg := bc.GetConfig() // Can be sorted and/or returned to outside users, thus needs to be copied.
count := cfg.GetCommitteeSize(bc.BlockHeight() + 1) sbVals := keys.PublicKeys(n.standbyKeys[:count]).Copy()
sbVals = sbVals[:count]
cs, err := n.getCandidates(d, false) cs, err := n.getCandidates(d, false)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err

View file

@ -50,8 +50,9 @@ func TestNEO_Vote(t *testing.T) {
neoValidatorsInvoker := neoCommitteeInvoker.WithSigners(neoCommitteeInvoker.Validator) neoValidatorsInvoker := neoCommitteeInvoker.WithSigners(neoCommitteeInvoker.Validator)
e := neoCommitteeInvoker.Executor e := neoCommitteeInvoker.Executor
committeeSize := len(neoValidatorsInvoker.Chain.GetConfig().StandbyCommittee) cfg := e.Chain.GetConfig()
validatorsCount := neoCommitteeInvoker.Chain.GetConfig().ValidatorsCount committeeSize := cfg.GetCommitteeSize(0)
validatorsCount := cfg.GetNumOfCNs(0)
freq := validatorsCount + committeeSize freq := validatorsCount + committeeSize
advanceChain := func(t *testing.T) { advanceChain := func(t *testing.T) {
for i := 0; i < freq; i++ { for i := 0; i < freq; i++ {
@ -59,7 +60,9 @@ func TestNEO_Vote(t *testing.T) {
} }
} }
standBySorted := e.Chain.GetStandByValidators() standBySorted, err := keys.NewPublicKeysFromStrings(e.Chain.GetConfig().StandbyCommittee)
require.NoError(t, err)
standBySorted = standBySorted[:validatorsCount]
sort.Sort(standBySorted) sort.Sort(standBySorted)
pubs, err := e.Chain.GetValidators() pubs, err := e.Chain.GetValidators()
require.NoError(t, err) require.NoError(t, err)
@ -250,7 +253,8 @@ func TestNEO_CommitteeBountyOnPersist(t *testing.T) {
neoCommitteeInvoker := newNeoCommitteeClient(t, 0) neoCommitteeInvoker := newNeoCommitteeClient(t, 0)
e := neoCommitteeInvoker.Executor e := neoCommitteeInvoker.Executor
hs := e.Chain.GetStandByCommittee() hs, err := keys.NewPublicKeysFromStrings(e.Chain.GetConfig().StandbyCommittee)
require.NoError(t, err)
committeeSize := len(hs) committeeSize := len(hs)
const singleBounty = 50000000 const singleBounty = 50000000

View file

@ -49,25 +49,13 @@ func createGenesisBlock(cfg config.ProtocolConfiguration) (*block.Block, error)
} }
func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) { func validatorsFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
vs, err := committeeFromConfig(cfg) vs, err := keys.NewPublicKeysFromStrings(cfg.StandbyCommittee)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return vs[:cfg.GetNumOfCNs(0)], nil return vs[:cfg.GetNumOfCNs(0)], nil
} }
func committeeFromConfig(cfg config.ProtocolConfiguration) ([]*keys.PublicKey, error) {
validators := make([]*keys.PublicKey, len(cfg.StandbyCommittee))
for i := range validators {
pubKey, err := keys.NewPublicKeyFromString(cfg.StandbyCommittee[i])
if err != nil {
return nil, err
}
validators[i] = pubKey
}
return validators, nil
}
func getNextConsensusAddress(validators []*keys.PublicKey) (val util.Uint160, err error) { func getNextConsensusAddress(validators []*keys.PublicKey) (val util.Uint160, err error) {
raw, err := smartcontract.CreateDefaultMultiSigRedeemScript(validators) raw, err := smartcontract.CreateDefaultMultiSigRedeemScript(validators)
if err != nil { if err != nil {

View file

@ -32,6 +32,20 @@ type PublicKeys []*PublicKey
var big0 = big.NewInt(0) var big0 = big.NewInt(0)
var big3 = big.NewInt(3) var big3 = big.NewInt(3)
// NewPublicKeysFromStrings converts an array of string-encoded P256 public keys
// into an array of PublicKeys.
func NewPublicKeysFromStrings(ss []string) (PublicKeys, error) {
arr := make([]*PublicKey, len(ss))
for i := range ss {
pubKey, err := NewPublicKeyFromString(ss[i])
if err != nil {
return nil, err
}
arr[i] = pubKey
}
return PublicKeys(arr), nil
}
func (keys PublicKeys) Len() int { return len(keys) } func (keys PublicKeys) Len() int { return len(keys) }
func (keys PublicKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] } func (keys PublicKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] }
func (keys PublicKeys) Less(i, j int) bool { func (keys PublicKeys) Less(i, j int) bool {

View file

@ -699,8 +699,7 @@ var rpcTestCases = map[string][]rpcTestCase{
{ {
params: "[]", params: "[]",
result: func(e *executor) interface{} { result: func(e *executor) interface{} {
// it's a test chain, so committee is a sorted standby committee expected, _ := e.chain.GetCommittee()
expected := e.chain.GetStandByCommittee()
sort.Sort(expected) sort.Sort(expected)
return &expected return &expected
}, },

View file

@ -64,7 +64,9 @@ func main() {
bc, err := newChain() bc, err := newChain()
handleError("can't initialize blockchain", err) handleError("can't initialize blockchain", err)
valScript, err := smartcontract.CreateDefaultMultiSigRedeemScript(bc.GetStandByValidators()) nbVals, err := bc.GetNextBlockValidators()
handleError("can't get next block validators", err)
valScript, err := smartcontract.CreateDefaultMultiSigRedeemScript(nbVals)
handleError("can't create verification script", err) handleError("can't create verification script", err)
lastBlock, err := bc.GetBlock(bc.GetHeaderHash(int(bc.BlockHeight()))) lastBlock, err := bc.GetBlock(bc.GetHeaderHash(int(bc.BlockHeight())))
handleError("can't fetch last block", err) handleError("can't fetch last block", err)