2020-10-01 15:17:09 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:34:48 +00:00
|
|
|
"encoding/binary"
|
2020-10-01 15:17:09 +00:00
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"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/runtime"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-11-05 16:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-11-05 16:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Designate represents designation contract.
|
|
|
|
type Designate struct {
|
|
|
|
interop.ContractMD
|
|
|
|
NEO *NEO
|
|
|
|
|
|
|
|
rolesChangedFlag atomic.Value
|
2020-11-05 16:34:48 +00:00
|
|
|
oracles atomic.Value
|
2020-11-18 08:59:34 +00:00
|
|
|
|
|
|
|
// p2pSigExtensionsEnabled defines whether the P2P signature extensions logic is relevant.
|
|
|
|
p2pSigExtensionsEnabled bool
|
2020-11-05 16:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type oraclesData struct {
|
|
|
|
nodes keys.PublicKeys
|
|
|
|
addr util.Uint160
|
|
|
|
height uint32
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
designateContractID = -5
|
2020-10-21 09:50:26 +00:00
|
|
|
|
|
|
|
// maxNodeCount is the maximum number of nodes to set the role for.
|
|
|
|
maxNodeCount = 32
|
2020-10-01 15:17:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Role represents type of participant.
|
|
|
|
type Role byte
|
|
|
|
|
|
|
|
// Role enumeration.
|
|
|
|
const (
|
|
|
|
RoleStateValidator Role = 4
|
|
|
|
RoleOracle Role = 8
|
2020-11-18 08:59:34 +00:00
|
|
|
RoleP2PNotary Role = 128
|
2020-10-01 15:17:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Various errors.
|
|
|
|
var (
|
2020-11-05 16:34:48 +00:00
|
|
|
ErrAlreadyDesignated = errors.New("already designated given role at current block")
|
|
|
|
ErrEmptyNodeList = errors.New("node list is empty")
|
|
|
|
ErrInvalidIndex = errors.New("invalid index")
|
|
|
|
ErrInvalidRole = errors.New("invalid role")
|
|
|
|
ErrLargeNodeList = errors.New("node list is too large")
|
|
|
|
ErrNoBlock = errors.New("no persisting block in the context")
|
2020-10-01 15:17:09 +00:00
|
|
|
)
|
|
|
|
|
2020-11-18 08:59:34 +00:00
|
|
|
func (s *Designate) isValidRole(r Role) bool {
|
|
|
|
return r == RoleOracle || r == RoleStateValidator || (s.p2pSigExtensionsEnabled && r == RoleP2PNotary)
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 08:59:34 +00:00
|
|
|
func newDesignate(p2pSigExtensionsEnabled bool) *Designate {
|
2020-12-13 18:25:04 +00:00
|
|
|
s := &Designate{ContractMD: *interop.NewContractMD(nativenames.Designation)}
|
2020-10-01 15:17:09 +00:00
|
|
|
s.ContractID = designateContractID
|
2020-11-18 08:59:34 +00:00
|
|
|
s.p2pSigExtensionsEnabled = p2pSigExtensionsEnabled
|
2020-10-01 15:17:09 +00:00
|
|
|
|
|
|
|
desc := newDescriptor("getDesignatedByRole", smartcontract.ArrayType,
|
2020-11-05 16:34:48 +00:00
|
|
|
manifest.NewParameter("role", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("index", smartcontract.IntegerType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md := newMethodAndPrice(s.getDesignatedByRole, 1000000, smartcontract.ReadStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
s.AddMethod(md, desc)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("designateAsRole", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("role", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("nodes", smartcontract.ArrayType))
|
2020-12-11 07:34:01 +00:00
|
|
|
md = newMethodAndPrice(s.designateAsRole, 0, smartcontract.WriteStates)
|
2020-12-08 10:27:41 +00:00
|
|
|
s.AddMethod(md, desc)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes Oracle contract.
|
|
|
|
func (s *Designate) Initialize(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (s *Designate) OnPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPersist implements Contract interface.
|
|
|
|
func (s *Designate) PostPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
// OnPersistEnd updates cached values if they've been changed.
|
|
|
|
func (s *Designate) OnPersistEnd(d dao.DAO) error {
|
|
|
|
if !s.rolesChanged() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:34:48 +00:00
|
|
|
nodeKeys, height, err := s.GetDesignatedByRole(d, RoleOracle, math.MaxUint32)
|
2020-10-01 15:17:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:34:48 +00:00
|
|
|
od := &oraclesData{
|
|
|
|
nodes: nodeKeys,
|
|
|
|
addr: oracleHashFromNodes(nodeKeys),
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
s.oracles.Store(od)
|
2020-10-01 15:17:09 +00:00
|
|
|
s.rolesChangedFlag.Store(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns contract metadata.
|
|
|
|
func (s *Designate) Metadata() *interop.ContractMD {
|
|
|
|
return &s.ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Designate) getDesignatedByRole(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-11-18 08:59:34 +00:00
|
|
|
r, ok := s.getRole(args[0])
|
2020-10-01 15:17:09 +00:00
|
|
|
if !ok {
|
|
|
|
panic(ErrInvalidRole)
|
|
|
|
}
|
2020-11-05 16:34:48 +00:00
|
|
|
ind, err := args[1].TryInteger()
|
|
|
|
if err != nil || !ind.IsUint64() {
|
|
|
|
panic(ErrInvalidIndex)
|
|
|
|
}
|
|
|
|
index := ind.Uint64()
|
|
|
|
if index > uint64(ic.Chain.BlockHeight()+1) {
|
|
|
|
panic(ErrInvalidIndex)
|
|
|
|
}
|
|
|
|
pubs, _, err := s.GetDesignatedByRole(ic.DAO, r, uint32(index))
|
2020-10-01 15:17:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pubsToArray(pubs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Designate) rolesChanged() bool {
|
|
|
|
rc := s.rolesChangedFlag.Load()
|
|
|
|
return rc == nil || rc.(bool)
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:34:48 +00:00
|
|
|
func oracleHashFromNodes(nodes keys.PublicKeys) util.Uint160 {
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return util.Uint160{}
|
|
|
|
}
|
|
|
|
script, _ := smartcontract.CreateMajorityMultiSigRedeemScript(nodes.Copy())
|
|
|
|
return hash.Hash160(script)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Designate) getLastDesignatedHash(d dao.DAO, r Role) (util.Uint160, error) {
|
2020-11-18 08:59:34 +00:00
|
|
|
if !s.isValidRole(r) {
|
2020-11-05 16:34:48 +00:00
|
|
|
return util.Uint160{}, ErrInvalidRole
|
|
|
|
}
|
|
|
|
if r == RoleOracle && !s.rolesChanged() {
|
|
|
|
odVal := s.oracles.Load()
|
|
|
|
if odVal != nil {
|
|
|
|
od := odVal.(*oraclesData)
|
|
|
|
return od.addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes, _, err := s.GetDesignatedByRole(d, r, math.MaxUint32)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
// We only have hashing defined for oracles now.
|
|
|
|
return oracleHashFromNodes(nodes), nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
// GetDesignatedByRole returns nodes for role r.
|
2020-11-05 16:34:48 +00:00
|
|
|
func (s *Designate) GetDesignatedByRole(d dao.DAO, r Role, index uint32) (keys.PublicKeys, uint32, error) {
|
2020-11-18 08:59:34 +00:00
|
|
|
if !s.isValidRole(r) {
|
2020-11-05 16:34:48 +00:00
|
|
|
return nil, 0, ErrInvalidRole
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
if r == RoleOracle && !s.rolesChanged() {
|
2020-11-05 16:34:48 +00:00
|
|
|
odVal := s.oracles.Load()
|
|
|
|
if odVal != nil {
|
|
|
|
od := odVal.(*oraclesData)
|
|
|
|
if od.height <= index {
|
|
|
|
return od.nodes, od.height, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kvs, err := d.GetStorageItemsWithPrefix(s.ContractID, []byte{byte(r)})
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
var ns NodeList
|
2020-11-05 16:34:48 +00:00
|
|
|
var bestIndex uint32
|
|
|
|
var resSi *state.StorageItem
|
|
|
|
for k, si := range kvs {
|
|
|
|
if len(k) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
siInd := binary.BigEndian.Uint32([]byte(k))
|
|
|
|
if (resSi == nil || siInd > bestIndex) && siInd <= index {
|
|
|
|
bestIndex = siInd
|
|
|
|
resSi = si
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resSi != nil {
|
|
|
|
reader := io.NewBinReaderFromBuf(resSi.Value)
|
|
|
|
ns.DecodeBinary(reader)
|
|
|
|
if reader.Err != nil {
|
|
|
|
return nil, 0, reader.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys.PublicKeys(ns), bestIndex, err
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Designate) designateAsRole(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-11-18 08:59:34 +00:00
|
|
|
r, ok := s.getRole(args[0])
|
2020-10-01 15:17:09 +00:00
|
|
|
if !ok {
|
|
|
|
panic(ErrInvalidRole)
|
|
|
|
}
|
|
|
|
var ns NodeList
|
|
|
|
if err := ns.fromStackItem(args[1]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.DesignateAsRole(ic, r, keys.PublicKeys(ns))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pubsToArray(keys.PublicKeys(ns))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DesignateAsRole sets nodes for role r.
|
|
|
|
func (s *Designate) DesignateAsRole(ic *interop.Context, r Role, pubs keys.PublicKeys) error {
|
2020-10-21 09:50:26 +00:00
|
|
|
length := len(pubs)
|
|
|
|
if length == 0 {
|
2020-10-01 15:17:09 +00:00
|
|
|
return ErrEmptyNodeList
|
|
|
|
}
|
2020-10-21 09:50:26 +00:00
|
|
|
if length > maxNodeCount {
|
|
|
|
return ErrLargeNodeList
|
|
|
|
}
|
2020-11-18 08:59:34 +00:00
|
|
|
if !s.isValidRole(r) {
|
2020-10-01 15:17:09 +00:00
|
|
|
return ErrInvalidRole
|
|
|
|
}
|
|
|
|
h := s.NEO.GetCommitteeAddress()
|
|
|
|
if ok, err := runtime.CheckHashedWitness(ic, h); err != nil || !ok {
|
|
|
|
return ErrInvalidWitness
|
|
|
|
}
|
2020-11-05 16:34:48 +00:00
|
|
|
if ic.Block == nil {
|
|
|
|
return ErrNoBlock
|
|
|
|
}
|
|
|
|
var key = make([]byte, 5)
|
|
|
|
key[0] = byte(r)
|
|
|
|
binary.BigEndian.PutUint32(key[1:], ic.Block.Index+1)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2020-11-05 16:34:48 +00:00
|
|
|
si := ic.DAO.GetStorageItem(s.ContractID, key)
|
|
|
|
if si != nil {
|
|
|
|
return ErrAlreadyDesignated
|
|
|
|
}
|
2020-10-01 15:17:09 +00:00
|
|
|
sort.Sort(pubs)
|
|
|
|
s.rolesChangedFlag.Store(true)
|
2020-11-05 16:34:48 +00:00
|
|
|
si = &state.StorageItem{Value: NodeList(pubs).Bytes()}
|
|
|
|
return ic.DAO.PutStorageItem(s.ContractID, key, si)
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 08:59:34 +00:00
|
|
|
func (s *Designate) getRole(item stackitem.Item) (Role, bool) {
|
2020-10-01 15:17:09 +00:00
|
|
|
bi, err := item.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
if !bi.IsUint64() {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
u := bi.Uint64()
|
2020-11-18 08:59:34 +00:00
|
|
|
return Role(u), u <= math.MaxUint8 && s.isValidRole(Role(u))
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|