From 05b45270c8ea6ca5ab407c4fd872c958b5e4917a Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 8 Apr 2021 17:40:49 +0300 Subject: [PATCH] [#468] Update neo-go to v0.94.1 New neo-go version provides: - new type for roles in `RoleManagement` contract, - methods to get keys from `RoleManagement` contract, - new way to sign notary transaction. Signed-off-by: Alex Vanin --- go.mod | 2 +- go.sum | 7 +--- pkg/morph/client/client.go | 68 +++------------------------------ pkg/morph/client/constructor.go | 6 --- pkg/morph/client/notary.go | 10 ++--- 5 files changed, 13 insertions(+), 80 deletions(-) diff --git a/go.mod b/go.mod index 7be2a9521..944ca72ff 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2 github.com/multiformats/go-multihash v0.0.13 // indirect github.com/nspcc-dev/hrw v1.0.9 - github.com/nspcc-dev/neo-go v0.94.0 + github.com/nspcc-dev/neo-go v0.94.1 github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210402125759-771f395d9d4e github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/tzhash v1.4.0 diff --git a/go.sum b/go.sum index a2f93c73d..59f1cd464 100644 --- a/go.sum +++ b/go.sum @@ -24,10 +24,8 @@ github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM= github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8= github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I= github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8= -github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk= github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY= github.com/Workiva/go-datastructures v1.0.50 h1:slDmfW6KCHcC7U+LP3DDBbm4fqTwZGn1beOFPfGaLvo= github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA= @@ -121,7 +119,6 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= -github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -276,8 +273,8 @@ github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y= github.com/nspcc-dev/hrw v1.0.9/go.mod h1:l/W2vx83vMQo6aStyx2AuZrJ+07lGv2JQGlVkPG06MU= github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= -github.com/nspcc-dev/neo-go v0.94.0 h1:2eafoyEnueqEMGDZF06HZJQN0MHgYxFzlcre5YUOP1M= -github.com/nspcc-dev/neo-go v0.94.0/go.mod h1:IrBT/UG3/Slhqgjge8r6zni5tNRsWmwwG9OnVdASheI= +github.com/nspcc-dev/neo-go v0.94.1 h1:AHUnADrzJ3JW2wO8xSp9dH8/Lo5UwTojkpuNqHuulSw= +github.com/nspcc-dev/neo-go v0.94.1/go.mod h1:IrBT/UG3/Slhqgjge8r6zni5tNRsWmwwG9OnVdASheI= github.com/nspcc-dev/neofs-api-go v1.24.0/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210402125759-771f395d9d4e h1:yAsO+uJDClvttFwhStZgnV2xdYP+TWGyk0ib5ceTaIo= github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210402125759-771f395d9d4e/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= diff --git a/pkg/morph/client/client.go b/pkg/morph/client/client.go index d1d9fea04..6587d5dec 100644 --- a/pkg/morph/client/client.go +++ b/pkg/morph/client/client.go @@ -2,10 +2,9 @@ package client import ( "context" - "crypto/elliptic" "time" - "github.com/nspcc-dev/neo-go/pkg/core/native" + "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" @@ -35,8 +34,6 @@ type Client struct { gas util.Uint160 // native gas script-hash - neo util.Uint160 // native neo script-hash - designate util.Uint160 // native designate script-hash waitInterval time.Duration @@ -51,11 +48,6 @@ var ErrNilClient = errors.New("client is nil") // HaltState returned if TestInvoke function processed without panic. const HaltState = "HALT" -const ( - committeeList = "getCommittee" - designateList = "getDesignatedByRole" -) - var errEmptyInvocationScript = errors.New("got empty invocation script from neo node") var errScriptDecode = errors.New("could not decode invocation script from neo node") @@ -204,24 +196,14 @@ func (c *Client) GasBalance() (int64, error) { // Committee returns keys of chain committee from neo native contract. func (c *Client) Committee() (keys.PublicKeys, error) { - items, err := c.TestInvoke(c.neo, committeeList) - if err != nil { - return nil, err - } - - roleKeys, err := keysFromStack(items) - if err != nil { - return nil, errors.Wrap(err, "can't get committee keys") - } - - return roleKeys, nil + return c.client.GetCommittee() } // NeoFSAlphabetList returns keys that stored in NeoFS Alphabet role. Main chain // stores alphabet node keys of inner ring there, however side chain stores both // alphabet and non alphabet node keys of inner ring. func (c *Client) NeoFSAlphabetList() (keys.PublicKeys, error) { - list, err := c.roleList(native.RoleNeoFSAlphabet) + list, err := c.roleList(noderoles.NeoFSAlphabet) if err != nil { return nil, errors.Wrap(err, "can't get alphabet nodes role list") } @@ -229,23 +211,13 @@ func (c *Client) NeoFSAlphabetList() (keys.PublicKeys, error) { return list, nil } -func (c *Client) roleList(r native.Role) (keys.PublicKeys, error) { +func (c *Client) roleList(r noderoles.Role) (keys.PublicKeys, error) { height, err := c.client.GetBlockCount() if err != nil { return nil, errors.Wrap(err, "can't get chain height") } - items, err := c.TestInvoke(c.designate, designateList, r, int64(height)) - if err != nil { - return nil, err - } - - roleKeys, err := keysFromStack(items) - if err != nil { - return nil, errors.Wrap(err, "can't get role keys") - } - - return roleKeys, nil + return c.client.GetDesignatedByRole(r, height) } func toStackParameter(value interface{}) (sc.Parameter, error) { @@ -277,7 +249,7 @@ func toStackParameter(value interface{}) (sc.Parameter, error) { case util.Uint160: result.Type = sc.ByteArrayType result.Value = v.BytesBE() - case native.Role: + case noderoles.Role: result.Type = sc.IntegerType result.Value = int64(v) case keys.PublicKeys: @@ -294,34 +266,6 @@ func toStackParameter(value interface{}) (sc.Parameter, error) { return result, nil } -func keysFromStack(data []stackitem.Item) (keys.PublicKeys, error) { - if len(data) == 0 { - return nil, nil - } - - arr, err := ArrayFromStackItem(data[0]) - if err != nil { - return nil, errors.Wrap(err, "non array element on stack") - } - - res := make([]*keys.PublicKey, 0, len(arr)) - for i := range arr { - rawKey, err := BytesFromStackItem(arr[i]) - if err != nil { - return nil, errors.Wrap(err, "key is not slice of bytes") - } - - key, err := keys.NewPublicKeyFromBytes(rawKey, elliptic.P256()) - if err != nil { - return nil, errors.Wrap(err, "can't parse key") - } - - res = append(res, key) - } - - return res, nil -} - // MagicNumber returns the magic number of the network // to which the underlying RPC node client is connected. func (c *Client) MagicNumber() uint64 { diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 573d81a96..10915a219 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -100,11 +100,6 @@ func New(key *ecdsa.PrivateKey, endpoint string, opts ...Option) (*Client, error return nil, err } - neo, err := cli.GetNativeContractHash(nativenames.Neo) - if err != nil { - return nil, err - } - designate, err := cli.GetNativeContractHash(nativenames.Designation) if err != nil { return nil, err @@ -115,7 +110,6 @@ func New(key *ecdsa.PrivateKey, endpoint string, opts ...Option) (*Client, error client: cli, acc: account, gas: gas, - neo: neo, designate: designate, waitInterval: cfg.waitInterval, }, nil diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index 72b25029e..b55f8cae0 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -1,8 +1,8 @@ package client import ( - "github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" + "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "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/keys" @@ -50,7 +50,6 @@ const ( var ( errNotaryNotEnabled = errors.New("notary support was not enabled on this client") - errInvalidIR = errors.New("invalid inner ring list from netmap contract") errUnexpectedItems = errors.New("invalid number of NEO VM arguments on stack") ) @@ -159,7 +158,7 @@ func (c *Client) UpdateNotaryList(list keys.PublicKeys) error { return c.notaryInvokeAsCommittee(c.designate, setDesignateMethod, - native.RoleP2PNotary, + noderoles.P2PNotary, list, ) } @@ -174,7 +173,7 @@ func (c *Client) UpdateNeoFSAlphabetList(list keys.PublicKeys) error { return c.notaryInvokeAsCommittee(c.designate, setDesignateMethod, - native.RoleNeoFSAlphabet, + noderoles.NeoFSAlphabet, list, ) } @@ -253,7 +252,6 @@ func (c *Client) notaryInvoke(committee bool, contract util.Uint160, method stri }, }, Signers: cosigners, - Network: c.client.GetNetwork(), } // calculate notary fee @@ -366,7 +364,7 @@ func (c *Client) notaryWitnesses(multiaddr *wallet.Account, tx *transaction.Tran w = append(w, transaction.Witness{ InvocationScript: append( []byte{byte(opcode.PUSHDATA1), 64}, - multiaddr.PrivateKey().Sign(tx.GetSignedPart())..., + multiaddr.PrivateKey().SignHashable(uint32(c.client.GetNetwork()), tx)..., ), VerificationScript: multiaddr.GetVerificationScript(), })