From 4ace86e9b305519cfa1eff391506e192fb0cc603 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 24 Mar 2021 11:00:35 +0300 Subject: [PATCH] [#58] common: Add getters for alphabet and IR nodes from chain Signed-off-by: Alex Vanin --- common/ir.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/common/ir.go b/common/ir.go index f621fd7..12903a0 100644 --- a/common/ir.go +++ b/common/ir.go @@ -3,6 +3,9 @@ package common import ( "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/contract" + "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger" + "github.com/nspcc-dev/neo-go/pkg/interop/native/neo" + "github.com/nspcc-dev/neo-go/pkg/interop/native/roles" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" ) @@ -71,3 +74,51 @@ func CommitteeMultiAddressViaStorage(ctx storage.Context, key interface{}) []byt func CommitteeMultiAddress(sc interop.Hash160) []byte { return contract.Call(sc, committeeMethod, contract.ReadOnly).([]byte) } + +// InnerRingNodes return list of inner ring nodes from state validator role +// in side chain. +func InnerRingNodes() []IRNode { + blockHeight := ledger.CurrentIndex() + list := roles.GetDesignatedByRole(roles.NeoFSAlphabet, uint32(blockHeight)) + return keysToNodes(list) +} + +// AlphabetNodes return list of alphabet nodes from committee in side chain. +func AlphabetNodes() []IRNode { + list := neo.GetCommittee() + return keysToNodes(list) +} + +// AlphabetAddress returns multi address of alphabet public keys. +func AlphabetAddress() []byte { + alphabet := neo.GetCommittee() + return Multiaddress(alphabet, false) +} + +// Multiaddress returns default multi signature account address for N keys. +// If committee set to true, then it is `M = N/2+1` committee account. +func Multiaddress(n []interop.PublicKey, committee bool) []byte { + threshold := len(n)/3*2 + 1 + if committee { + threshold = len(n)/2 + 1 + } + + keys := []interop.PublicKey{} + for _, key := range n { + keys = append(keys, key) + } + + return contract.CreateMultisigAccount(threshold, keys) +} + +func keysToNodes(list []interop.PublicKey) []IRNode { + result := []IRNode{} + + for i := range list { + result = append(result, IRNode{ + PublicKey: list[i], + }) + } + + return result +}