From 2f8896f7a166605e8bcad945ae04c1976703c99f Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 23 Aug 2022 22:00:23 +0300 Subject: [PATCH] rpcclient: add notary subpackage with the notary contract wrapper --- pkg/rpcclient/native.go | 3 + pkg/rpcclient/notary/contract.go | 227 ++++++++++++++++++++++++++ pkg/rpcclient/notary/contract_test.go | 199 ++++++++++++++++++++++ pkg/services/rpcsrv/client_test.go | 91 ++++++++++- 4 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 pkg/rpcclient/notary/contract.go create mode 100644 pkg/rpcclient/notary/contract_test.go diff --git a/pkg/rpcclient/native.go b/pkg/rpcclient/native.go index 0b6526c44..3d41265fd 100644 --- a/pkg/rpcclient/native.go +++ b/pkg/rpcclient/native.go @@ -150,6 +150,9 @@ func (c *Client) NNSUnpackedGetAllRecords(nnsHash util.Uint160, name string) ([] // GetNotaryServiceFeePerKey returns a reward per notary request key for the designated // notary nodes. It doesn't cache the result. +// +// Deprecated: please use the Notary contract wrapper from the notary subpackage. This +// method will be removed in future versions. func (c *Client) GetNotaryServiceFeePerKey() (int64, error) { notaryHash, err := c.GetNativeContractHash(nativenames.Notary) if err != nil { diff --git a/pkg/rpcclient/notary/contract.go b/pkg/rpcclient/notary/contract.go new file mode 100644 index 000000000..aa216cbd4 --- /dev/null +++ b/pkg/rpcclient/notary/contract.go @@ -0,0 +1,227 @@ +/* +Package notary provides an RPC-based wrapper for the Notary subsystem. + +It provides both regular ContractReader/Contract interfaces for the notary +contract and notary-specific functions and interfaces to simplify creation of +notary requests. +*/ +package notary + +import ( + "math" + "math/big" + + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" + "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/neorpc/result" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" + "github.com/nspcc-dev/neo-go/pkg/util" +) + +const ( + setMaxNVBDeltaMethod = "setMaxNotValidBeforeDelta" + setFeePKMethod = "setNotaryServiceFeePerKey" +) + +// ContractInvoker is used by ContractReader to perform read-only calls. +type ContractInvoker interface { + Call(contract util.Uint160, operation string, params ...interface{}) (*result.Invoke, error) +} + +// ContractActor is used by Contract to create and send transactions. +type ContractActor interface { + ContractInvoker + + MakeCall(contract util.Uint160, method string, params ...interface{}) (*transaction.Transaction, error) + MakeRun(script []byte) (*transaction.Transaction, error) + MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...interface{}) (*transaction.Transaction, error) + MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) + SendCall(contract util.Uint160, method string, params ...interface{}) (util.Uint256, uint32, error) + SendRun(script []byte) (util.Uint256, uint32, error) +} + +// ContractReader represents safe (read-only) methods of Notary. It can be +// used to query various data, but `verify` method is not exposed there because +// it can't be successful in standalone invocation (missing transaction with the +// NotaryAssisted attribute and its signature). +type ContractReader struct { + invoker ContractInvoker +} + +// Contract provides full Notary interface, both safe and state-changing methods. +// The only method omitted is onNEP17Payment which can only be called +// successfully from the GASToken native contract. +type Contract struct { + ContractReader + + actor ContractActor +} + +// Hash stores the hash of the native Notary contract. +var Hash = state.CreateNativeContractHash(nativenames.Notary) + +// NewReader creates an instance of ContractReader to get data from the Notary +// contract. +func NewReader(invoker ContractInvoker) *ContractReader { + return &ContractReader{invoker} +} + +// New creates an instance of Contract to perform state-changing actions in the +// Notary contract. +func New(actor ContractActor) *Contract { + return &Contract{*NewReader(actor), actor} +} + +// BalanceOf returns the locked GAS balance for the given account. +func (c *ContractReader) BalanceOf(account util.Uint160) (*big.Int, error) { + return unwrap.BigInt(c.invoker.Call(Hash, "balanceOf", account)) +} + +// ExpirationOf returns the index of the block when the GAS deposit for the given +// account will expire. +func (c *ContractReader) ExpirationOf(account util.Uint160) (uint32, error) { + res, err := c.invoker.Call(Hash, "expirationOf", account) + ret, err := unwrap.LimitedInt64(res, err, 0, math.MaxUint32) + return uint32(ret), err +} + +// GetMaxNotValidBeforeDelta returns the maximum NotValidBefore attribute delta +// that can be used in notary-assisted transactions. +func (c *ContractReader) GetMaxNotValidBeforeDelta() (uint32, error) { + res, err := c.invoker.Call(Hash, "getMaxNotValidBeforeDelta") + ret, err := unwrap.LimitedInt64(res, err, 0, math.MaxUint32) + return uint32(ret), err +} + +// GetNotaryServiceFeePerKey returns the per-key fee amount paid by transactions +// for the NotaryAssisted attribute. +func (c *ContractReader) GetNotaryServiceFeePerKey() (int64, error) { + return unwrap.Int64(c.invoker.Call(Hash, "getNotaryServiceFeePerKey")) +} + +// LockDepositUntil creates and sends a transaction that extends the deposit lock +// time for the given account. The return result from the "lockDepositUntil" +// method is checked to be true, so transaction fails (with FAULT state) if not +// successful. The returned values are transaction hash, its ValidUntilBlock +// value and an error if any. +func (c *Contract) LockDepositUntil(account util.Uint160, index uint32) (util.Uint256, uint32, error) { + return c.actor.SendRun(lockScript(account, index)) +} + +// LockDepositUntilTransaction creates a transaction that extends the deposit lock +// time for the given account. The return result from the "lockDepositUntil" +// method is checked to be true, so transaction fails (with FAULT state) if not +// successful. The returned values are transaction hash, its ValidUntilBlock +// value and an error if any. The transaction is signed, but not sent to the +// network, instead it's returned to the caller. +func (c *Contract) LockDepositUntilTransaction(account util.Uint160, index uint32) (*transaction.Transaction, error) { + return c.actor.MakeRun(lockScript(account, index)) +} + +// LockDepositUntilUnsigned creates a transaction that extends the deposit lock +// time for the given account. The return result from the "lockDepositUntil" +// method is checked to be true, so transaction fails (with FAULT state) if not +// successful. The returned values are transaction hash, its ValidUntilBlock +// value and an error if any. The transaction is not signed and just returned to +// the caller. +func (c *Contract) LockDepositUntilUnsigned(account util.Uint160, index uint32) (*transaction.Transaction, error) { + return c.actor.MakeUnsignedRun(lockScript(account, index), nil) +} + +func lockScript(account util.Uint160, index uint32) []byte { + // We know parameters exactly (unlike with nep17.Transfer), so this can't fail. + script, _ := smartcontract.CreateCallWithAssertScript(Hash, "lockDepositUntil", account.BytesBE(), int64(index)) + return script +} + +// SetMaxNotValidBeforeDelta creates and sends a transaction that sets the new +// maximum NotValidBefore attribute value delta that can be used in +// notary-assisted transactions. The action is successful when transaction +// ends in HALT state. Notice that this setting can be changed only by the +// network's committee, so use an appropriate Actor. The returned values are +// transaction hash, its ValidUntilBlock value and an error if any. +func (c *Contract) SetMaxNotValidBeforeDelta(blocks uint32) (util.Uint256, uint32, error) { + return c.actor.SendCall(Hash, setMaxNVBDeltaMethod, blocks) +} + +// SetMaxNotValidBeforeDeltaTransaction creates a transaction that sets the new +// maximum NotValidBefore attribute value delta that can be used in +// notary-assisted transactions. The action is successful when transaction +// ends in HALT state. Notice that this setting can be changed only by the +// network's committee, so use an appropriate Actor. The transaction is signed, +// but not sent to the network, instead it's returned to the caller. +func (c *Contract) SetMaxNotValidBeforeDeltaTransaction(blocks uint32) (*transaction.Transaction, error) { + return c.actor.MakeCall(Hash, setMaxNVBDeltaMethod, blocks) +} + +// SetMaxNotValidBeforeDeltaUnsigned creates a transaction that sets the new +// maximum NotValidBefore attribute value delta that can be used in +// notary-assisted transactions. The action is successful when transaction +// ends in HALT state. Notice that this setting can be changed only by the +// network's committee, so use an appropriate Actor. The transaction is not +// signed and just returned to the caller. +func (c *Contract) SetMaxNotValidBeforeDeltaUnsigned(blocks uint32) (*transaction.Transaction, error) { + return c.actor.MakeUnsignedCall(Hash, setMaxNVBDeltaMethod, nil, blocks) +} + +// SetNotaryServiceFeePerKey creates and sends a transaction that sets the new +// per-key fee value paid for using the notary service. The action is successful +// when transaction ends in HALT state. Notice that this setting can be changed +// only by the network's committee, so use an appropriate Actor. The returned +// values are transaction hash, its ValidUntilBlock value and an error if any. +func (c *Contract) SetNotaryServiceFeePerKey(fee int64) (util.Uint256, uint32, error) { + return c.actor.SendCall(Hash, setFeePKMethod, fee) +} + +// SetNotaryServiceFeePerKeyTransaction creates a transaction that sets the new +// per-key fee value paid for using the notary service. The action is successful +// when transaction ends in HALT state. Notice that this setting can be changed +// only by the network's committee, so use an appropriate Actor. The transaction +// is signed, but not sent to the network, instead it's returned to the caller. +func (c *Contract) SetNotaryServiceFeePerKeyTransaction(fee int64) (*transaction.Transaction, error) { + return c.actor.MakeCall(Hash, setFeePKMethod, fee) +} + +// SetNotaryServiceFeePerKeyUnsigned creates a transaction that sets the new +// per-key fee value paid for using the notary service. The action is successful +// when transaction ends in HALT state. Notice that this setting can be changed +// only by the network's committee, so use an appropriate Actor. The transaction +// is not signed and just returned to the caller. +func (c *Contract) SetNotaryServiceFeePerKeyUnsigned(fee int64) (*transaction.Transaction, error) { + return c.actor.MakeUnsignedCall(Hash, setFeePKMethod, nil, fee) +} + +// Withdraw creates and sends a transaction that withdraws the deposit belonging +// to "from" account and sends it to "to" account. The return result from the +// "withdraw" method is checked to be true, so transaction fails (with FAULT +// state) if not successful. The returned values are transaction hash, its +// ValidUntilBlock value and an error if any. +func (c *Contract) Withdraw(from util.Uint160, to util.Uint160) (util.Uint256, uint32, error) { + return c.actor.SendRun(withdrawScript(from, to)) +} + +// WithdrawTransaction creates a transaction that withdraws the deposit belonging +// to "from" account and sends it to "to" account. The return result from the +// "withdraw" method is checked to be true, so transaction fails (with FAULT +// state) if not successful. The transaction is signed, but not sent to the +// network, instead it's returned to the caller. +func (c *Contract) WithdrawTransaction(from util.Uint160, to util.Uint160) (*transaction.Transaction, error) { + return c.actor.MakeRun(withdrawScript(from, to)) +} + +// WithdrawUnsigned creates a transaction that withdraws the deposit belonging +// to "from" account and sends it to "to" account. The return result from the +// "withdraw" method is checked to be true, so transaction fails (with FAULT +// state) if not successful. The transaction is not signed and just returned to +// the caller. +func (c *Contract) WithdrawUnsigned(from util.Uint160, to util.Uint160) (*transaction.Transaction, error) { + return c.actor.MakeUnsignedRun(withdrawScript(from, to), nil) +} + +func withdrawScript(from util.Uint160, to util.Uint160) []byte { + // We know parameters exactly (unlike with nep17.Transfer), so this can't fail. + script, _ := smartcontract.CreateCallWithAssertScript(Hash, "withdraw", from.BytesBE(), to.BytesBE()) + return script +} diff --git a/pkg/rpcclient/notary/contract_test.go b/pkg/rpcclient/notary/contract_test.go new file mode 100644 index 000000000..c8498ffbf --- /dev/null +++ b/pkg/rpcclient/notary/contract_test.go @@ -0,0 +1,199 @@ +package notary + +import ( + "errors" + "math/big" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/neorpc/result" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/stretchr/testify/require" +) + +type testAct struct { + err error + res *result.Invoke + tx *transaction.Transaction + txh util.Uint256 + vub uint32 +} + +func (t *testAct) Call(contract util.Uint160, operation string, params ...interface{}) (*result.Invoke, error) { + return t.res, t.err +} +func (t *testAct) MakeRun(script []byte) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) SendRun(script []byte) (util.Uint256, uint32, error) { + return t.txh, t.vub, t.err +} +func (t *testAct) MakeCall(contract util.Uint160, method string, params ...interface{}) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...interface{}) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) SendCall(contract util.Uint160, method string, params ...interface{}) (util.Uint256, uint32, error) { + return t.txh, t.vub, t.err +} + +func TestBalanceOf(t *testing.T) { + ta := &testAct{} + ntr := NewReader(ta) + + ta.err = errors.New("") + _, err := ntr.BalanceOf(util.Uint160{}) + require.Error(t, err) + + ta.err = nil + ta.res = &result.Invoke{ + State: "HALT", + Stack: []stackitem.Item{ + stackitem.Make(42), + }, + } + res, err := ntr.BalanceOf(util.Uint160{}) + require.NoError(t, err) + require.Equal(t, big.NewInt(42), res) +} + +func TestUint32Getters(t *testing.T) { + ta := &testAct{} + ntr := NewReader(ta) + + for name, fun := range map[string]func() (uint32, error){ + "ExpirationOf": func() (uint32, error) { + return ntr.ExpirationOf(util.Uint160{1, 2, 3}) + }, + "GetMaxNotValidBeforeDelta": ntr.GetMaxNotValidBeforeDelta, + } { + t.Run(name, func(t *testing.T) { + ta.err = errors.New("") + _, err := fun() + require.Error(t, err) + + ta.err = nil + ta.res = &result.Invoke{ + State: "HALT", + Stack: []stackitem.Item{ + stackitem.Make(42), + }, + } + res, err := fun() + require.NoError(t, err) + require.Equal(t, uint32(42), res) + + ta.res = &result.Invoke{ + State: "HALT", + Stack: []stackitem.Item{ + stackitem.Make(-1), + }, + } + _, err = fun() + require.Error(t, err) + }) + } +} + +func TestGetNotaryServiceFeePerKey(t *testing.T) { + ta := &testAct{} + ntr := NewReader(ta) + + ta.err = errors.New("") + _, err := ntr.GetNotaryServiceFeePerKey() + require.Error(t, err) + + ta.err = nil + ta.res = &result.Invoke{ + State: "HALT", + Stack: []stackitem.Item{ + stackitem.Make(42), + }, + } + res, err := ntr.GetNotaryServiceFeePerKey() + require.NoError(t, err) + require.Equal(t, int64(42), res) +} + +func TestTxSenders(t *testing.T) { + ta := new(testAct) + ntr := New(ta) + + for name, fun := range map[string]func() (util.Uint256, uint32, error){ + "LockDepositUntil": func() (util.Uint256, uint32, error) { + return ntr.LockDepositUntil(util.Uint160{1, 2, 3}, 100500) + }, + "SetMaxNotValidBeforeDelta": func() (util.Uint256, uint32, error) { + return ntr.SetMaxNotValidBeforeDelta(42) + }, + "SetNotaryServiceFeePerKey": func() (util.Uint256, uint32, error) { + return ntr.SetNotaryServiceFeePerKey(100500) + }, + "Withdraw": func() (util.Uint256, uint32, error) { + return ntr.Withdraw(util.Uint160{1, 2, 3}, util.Uint160{3, 2, 1}) + }, + } { + t.Run(name, func(t *testing.T) { + ta.err = errors.New("") + _, _, err := fun() + require.Error(t, err) + + ta.err = nil + ta.txh = util.Uint256{1, 2, 3} + ta.vub = 42 + h, vub, err := fun() + require.NoError(t, err) + require.Equal(t, ta.txh, h) + require.Equal(t, ta.vub, vub) + }) + } +} + +func TestTxMakers(t *testing.T) { + ta := new(testAct) + ntr := New(ta) + + for name, fun := range map[string]func() (*transaction.Transaction, error){ + "LockDepositUntilTransaction": func() (*transaction.Transaction, error) { + return ntr.LockDepositUntilTransaction(util.Uint160{1, 2, 3}, 100500) + }, + "LockDepositUntilUnsigned": func() (*transaction.Transaction, error) { + return ntr.LockDepositUntilUnsigned(util.Uint160{1, 2, 3}, 100500) + }, + "SetMaxNotValidBeforeDeltaTransaction": func() (*transaction.Transaction, error) { + return ntr.SetMaxNotValidBeforeDeltaTransaction(42) + }, + "SetMaxNotValidBeforeDeltaUnsigned": func() (*transaction.Transaction, error) { + return ntr.SetMaxNotValidBeforeDeltaUnsigned(42) + }, + "SetNotaryServiceFeePerKeyTransaction": func() (*transaction.Transaction, error) { + return ntr.SetNotaryServiceFeePerKeyTransaction(100500) + }, + "SetNotaryServiceFeePerKeyUnsigned": func() (*transaction.Transaction, error) { + return ntr.SetNotaryServiceFeePerKeyUnsigned(100500) + }, + "WithdrawTransaction": func() (*transaction.Transaction, error) { + return ntr.WithdrawTransaction(util.Uint160{1, 2, 3}, util.Uint160{3, 2, 1}) + }, + "WithdrawUnsigned": func() (*transaction.Transaction, error) { + return ntr.WithdrawUnsigned(util.Uint160{1, 2, 3}, util.Uint160{3, 2, 1}) + }, + } { + t.Run(name, func(t *testing.T) { + ta.err = errors.New("") + _, err := fun() + require.Error(t, err) + + ta.err = nil + ta.tx = &transaction.Transaction{Nonce: 100500, ValidUntilBlock: 42} + tx, err := fun() + require.NoError(t, err) + require.Equal(t, ta.tx, tx) + }) + } +} diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index c977681e7..ec3a6b70d 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -37,6 +37,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nns" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/notary" "github.com/nspcc-dev/neo-go/pkg/rpcclient/oracle" "github.com/nspcc-dev/neo-go/pkg/rpcclient/policy" "github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt" @@ -422,6 +423,94 @@ func TestClientNEOContract(t *testing.T) { require.NoError(t, err) } +func TestClientNotary(t *testing.T) { + chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) + defer chain.Close() + defer rpcSrv.Shutdown() + + c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) + require.NoError(t, err) + require.NoError(t, c.Init()) + + notaReader := notary.NewReader(invoker.New(c, nil)) + + priv0 := testchain.PrivateKeyByID(0) + priv0Hash := priv0.PublicKey().GetScriptHash() + bal, err := notaReader.BalanceOf(priv0Hash) + require.NoError(t, err) + require.Equal(t, big.NewInt(10_0000_0000), bal) + + expir, err := notaReader.ExpirationOf(priv0Hash) + require.NoError(t, err) + require.Equal(t, uint32(1007), expir) + + maxNVBd, err := notaReader.GetMaxNotValidBeforeDelta() + require.NoError(t, err) + require.Equal(t, uint32(140), maxNVBd) + + feePerKey, err := notaReader.GetNotaryServiceFeePerKey() + require.NoError(t, err) + require.Equal(t, int64(1000_0000), feePerKey) + + commAct, err := actor.New(c, []actor.SignerAccount{{ + Signer: transaction.Signer{ + Account: testchain.CommitteeScriptHash(), + Scopes: transaction.CalledByEntry, + }, + Account: &wallet.Account{ + Address: testchain.CommitteeAddress(), + Contract: &wallet.Contract{ + Script: testchain.CommitteeVerificationScript(), + }, + }, + }}) + require.NoError(t, err) + notaComm := notary.New(commAct) + + txNVB, err := notaComm.SetMaxNotValidBeforeDeltaUnsigned(210) + require.NoError(t, err) + txFee, err := notaComm.SetNotaryServiceFeePerKeyUnsigned(500_0000) + require.NoError(t, err) + + txNVB.Scripts[0].InvocationScript = testchain.SignCommittee(txNVB) + txFee.Scripts[0].InvocationScript = testchain.SignCommittee(txFee) + bl := testchain.NewBlock(t, chain, 1, 0, txNVB, txFee) + _, err = c.SubmitBlock(*bl) + require.NoError(t, err) + + maxNVBd, err = notaReader.GetMaxNotValidBeforeDelta() + require.NoError(t, err) + require.Equal(t, uint32(210), maxNVBd) + + feePerKey, err = notaReader.GetNotaryServiceFeePerKey() + require.NoError(t, err) + require.Equal(t, int64(500_0000), feePerKey) + + privAct, err := actor.New(c, []actor.SignerAccount{{ + Signer: transaction.Signer{ + Account: priv0Hash, + Scopes: transaction.CalledByEntry, + }, + Account: wallet.NewAccountFromPrivateKey(priv0), + }}) + require.NoError(t, err) + notaPriv := notary.New(privAct) + + txLock, err := notaPriv.LockDepositUntilTransaction(priv0Hash, 1111) + require.NoError(t, err) + + bl = testchain.NewBlock(t, chain, 1, 0, txLock) + _, err = c.SubmitBlock(*bl) + require.NoError(t, err) + + expir, err = notaReader.ExpirationOf(priv0Hash) + require.NoError(t, err) + require.Equal(t, uint32(1111), expir) + + _, err = notaPriv.WithdrawTransaction(priv0Hash, priv0Hash) + require.Error(t, err) // Can't be withdrawn until 1111. +} + func TestAddNetworkFeeCalculateNetworkFee(t *testing.T) { chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) defer chain.Close() @@ -1618,7 +1707,7 @@ func TestClient_GetNotaryServiceFeePerKey(t *testing.T) { require.NoError(t, c.Init()) var defaultNotaryServiceFeePerKey int64 = 1000_0000 - actual, err := c.GetNotaryServiceFeePerKey() + actual, err := c.GetNotaryServiceFeePerKey() //nolint:staticcheck // SA1019: c.GetNotaryServiceFeePerKey is deprecated require.NoError(t, err) require.Equal(t, defaultNotaryServiceFeePerKey, actual) }