From de83594afbb20af0bd1ff9530a0a616b9eedb86e Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 5 Jun 2024 18:21:27 +0300 Subject: [PATCH] *: get rid of (*Blockchain).GetNotaryContractScriptHash Use `nativehashes.Notary instead of (*Blockchain).GetNotaryContractScriptHash. Signed-off-by: Anna Shaleva --- cli/nep_test/nep17_test.go | 3 ++- internal/fakechain/fakechain.go | 33 +++++++++++------------------- pkg/core/blockchain.go | 11 ++-------- pkg/network/server.go | 4 ++-- pkg/network/server_test.go | 6 +++--- pkg/services/notary/core_test.go | 9 ++++---- pkg/services/notary/notary.go | 6 +++--- pkg/services/notary/notary_test.go | 5 ++--- pkg/services/rpcsrv/server.go | 1 - pkg/services/rpcsrv/server_test.go | 5 +++-- 10 files changed, 34 insertions(+), 49 deletions(-) diff --git a/cli/nep_test/nep17_test.go b/cli/nep_test/nep17_test.go index e7aa29393b..ace23346c0 100644 --- a/cli/nep_test/nep17_test.go +++ b/cli/nep_test/nep17_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/nspcc-dev/neo-go/internal/testcli" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" @@ -236,7 +237,7 @@ func TestNEP17Transfer(t *testing.T) { e.CheckAwaitableTxPersisted(t) }) - cmd = append(cmd, "--to", address.Uint160ToString(e.Chain.GetNotaryContractScriptHash()), + cmd = append(cmd, "--to", address.Uint160ToString(nativehashes.Notary), "[", testcli.ValidatorAddr, strconv.Itoa(int(validTil)), "]") t.Run("with data", func(t *testing.T) { diff --git a/internal/fakechain/fakechain.go b/internal/fakechain/fakechain.go index a2553b61a1..e0d885d68e 100644 --- a/internal/fakechain/fakechain.go +++ b/internal/fakechain/fakechain.go @@ -24,19 +24,18 @@ import ( type FakeChain struct { config.Blockchain *mempool.Pool - blocksCh []chan *block.Block - Blockheight atomic.Uint32 - PoolTxF func(*transaction.Transaction) error - poolTxWithData func(*transaction.Transaction, any, *mempool.Pool) error - blocks map[util.Uint256]*block.Block - hdrHashes map[uint32]util.Uint256 - txs map[util.Uint256]*transaction.Transaction - VerifyWitnessF func() (int64, error) - MaxVerificationGAS int64 - NotaryContractScriptHash util.Uint160 - NotaryDepositExpiration uint32 - PostBlock []func(func(*transaction.Transaction, *mempool.Pool, bool) bool, *mempool.Pool, *block.Block) - UtilityTokenBalance *big.Int + blocksCh []chan *block.Block + Blockheight atomic.Uint32 + PoolTxF func(*transaction.Transaction) error + poolTxWithData func(*transaction.Transaction, any, *mempool.Pool) error + blocks map[util.Uint256]*block.Block + hdrHashes map[uint32]util.Uint256 + txs map[util.Uint256]*transaction.Transaction + VerifyWitnessF func() (int64, error) + MaxVerificationGAS int64 + NotaryDepositExpiration uint32 + PostBlock []func(func(*transaction.Transaction, *mempool.Pool, bool) bool, *mempool.Pool, *block.Block) + UtilityTokenBalance *big.Int } // FakeStateSync implements the StateSync interface. @@ -111,14 +110,6 @@ func (chain *FakeChain) GetNotaryDepositExpiration(acc util.Uint160) uint32 { panic("TODO") } -// GetNotaryContractScriptHash implements the Blockchainer interface. -func (chain *FakeChain) GetNotaryContractScriptHash() util.Uint160 { - if !chain.NotaryContractScriptHash.Equals(util.Uint160{}) { - return chain.NotaryContractScriptHash - } - panic("TODO") -} - // GetNotaryBalance implements the Blockchainer interface. func (chain *FakeChain) GetNotaryBalance(acc util.Uint160) *big.Int { panic("TODO") diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 9c6c876731..da40bc2d54 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -22,6 +22,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mpt" "github.com/nspcc-dev/neo-go/pkg/core/native" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/stateroot" @@ -2114,14 +2115,6 @@ func (bc *Blockchain) GetNotaryServiceFeePerKey() int64 { return bc.contracts.Policy.GetAttributeFeeInternal(bc.dao, transaction.NotaryAssistedT) } -// GetNotaryContractScriptHash returns Notary native contract hash. -func (bc *Blockchain) GetNotaryContractScriptHash() util.Uint160 { - if bc.P2PSigExtensionsEnabled() { - return bc.contracts.Notary.Hash - } - return util.Uint160{} -} - // GetNotaryDepositExpiration returns Notary deposit expiration height for the specified account. func (bc *Blockchain) GetNotaryDepositExpiration(acc util.Uint160) uint32 { return bc.contracts.Notary.ExpirationOf(bc.dao, acc) @@ -2698,7 +2691,7 @@ func (bc *Blockchain) verifyTxAttributes(d *dao.Simple, tx *transaction.Transact if !bc.config.P2PSigExtensions { return fmt.Errorf("%w: NotaryAssisted attribute was found, but P2PSigExtensions are disabled", ErrInvalidAttribute) } - if !tx.HasSigner(bc.contracts.Notary.Hash) { + if !tx.HasSigner(nativehashes.Notary) { return fmt.Errorf("%w: NotaryAssisted attribute was found, but transaction is not signed by the Notary native contract", ErrInvalidAttribute) } default: diff --git a/pkg/network/server.go b/pkg/network/server.go index 34da851b52..de529bcbf6 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -21,6 +21,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" "github.com/nspcc-dev/neo-go/pkg/core/mpt" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" @@ -66,7 +67,6 @@ type ( GetMaxVerificationGAS() int64 GetMemPool() *mempool.Pool GetNotaryBalance(acc util.Uint160) *big.Int - GetNotaryContractScriptHash() util.Uint160 GetNotaryDepositExpiration(acc util.Uint160) uint32 GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error) HasBlock(util.Uint256) bool @@ -1240,7 +1240,7 @@ func (s *Server) verifyNotaryRequest(_ *transaction.Transaction, data any) error if _, err := s.chain.VerifyWitness(payer, r, &r.Witness, s.chain.GetMaxVerificationGAS()); err != nil { return fmt.Errorf("bad P2PNotaryRequest payload witness: %w", err) } - notaryHash := s.chain.GetNotaryContractScriptHash() + notaryHash := nativehashes.Notary if r.FallbackTransaction.Sender() != notaryHash { return fmt.Errorf("P2PNotary contract should be a sender of the fallback transaction, got %s", address.Uint160ToString(r.FallbackTransaction.Sender())) } diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 9d2c6f20bf..b4a9d7ef96 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -19,6 +19,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/mpt" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "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/network/capability" @@ -1048,7 +1049,6 @@ func TestMemPool(t *testing.T) { func TestVerifyNotaryRequest(t *testing.T) { bc := fakechain.NewFakeChain() bc.MaxVerificationGAS = 10 - bc.NotaryContractScriptHash = util.Uint160{1, 2, 3} s, err := newServerFromConstructors(ServerConfig{Addresses: []config.AnnounceableAddress{{Address: ":0"}}}, bc, new(fakechain.FakeStateSync), zaptest.NewLogger(t), newFakeTransp, newTestDiscovery) require.NoError(t, err) newNotaryRequest := func() *payload.P2PNotaryRequest { @@ -1059,7 +1059,7 @@ func TestVerifyNotaryRequest(t *testing.T) { }, FallbackTransaction: &transaction.Transaction{ ValidUntilBlock: 321, - Signers: []transaction.Signer{{Account: bc.NotaryContractScriptHash}, {Account: random.Uint160()}}, + Signers: []transaction.Signer{{Account: nativehashes.Notary}, {Account: random.Uint160()}}, }, Witness: transaction.Witness{}, } @@ -1080,7 +1080,7 @@ func TestVerifyNotaryRequest(t *testing.T) { t.Run("bad main sender", func(t *testing.T) { bc.VerifyWitnessF = func() (int64, error) { return 0, nil } r := newNotaryRequest() - r.MainTransaction.Signers[0] = transaction.Signer{Account: bc.NotaryContractScriptHash} + r.MainTransaction.Signers[0] = transaction.Signer{Account: nativehashes.Notary} require.Error(t, s.verifyNotaryRequest(nil, r)) }) diff --git a/pkg/services/notary/core_test.go b/pkg/services/notary/core_test.go index 57ad225268..314aa07be1 100644 --- a/pkg/services/notary/core_test.go +++ b/pkg/services/notary/core_test.go @@ -14,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/mempool" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "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" @@ -171,7 +172,7 @@ func TestNotary(t *testing.T) { fallback.ValidUntilBlock = bc.BlockHeight() + 2*nvbDiffFallback fallback.Signers = []transaction.Signer{ { - Account: bc.GetNotaryContractScriptHash(), + Account: nativehashes.Notary, Scopes: transaction.None, }, { @@ -240,7 +241,7 @@ func TestNotary(t *testing.T) { verificationScripts = append(verificationScripts, script) } signers[len(signers)-1] = transaction.Signer{ - Account: bc.GetNotaryContractScriptHash(), + Account: nativehashes.Notary, Scopes: transaction.None, } mainTx.Signers = signers @@ -752,9 +753,9 @@ func TestNotary(t *testing.T) { requester1, _ := wallet.NewAccount() requester2, _ := wallet.NewAccount() amount := int64(100_0000_0000) - gasValidatorInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), bc.GetNotaryContractScriptHash(), amount, []any{requester1.ScriptHash(), int64(bc.BlockHeight() + 50)}) + gasValidatorInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), nativehashes.Notary, amount, []any{requester1.ScriptHash(), int64(bc.BlockHeight() + 50)}) e.CheckGASBalance(t, notaryHash, big.NewInt(amount)) - gasValidatorInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), bc.GetNotaryContractScriptHash(), amount, []any{requester2.ScriptHash(), int64(bc.BlockHeight() + 50)}) + gasValidatorInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), nativehashes.Notary, amount, []any{requester2.ScriptHash(), int64(bc.BlockHeight() + 50)}) e.CheckGASBalance(t, notaryHash, big.NewInt(2*amount)) // create request for 2 standard signatures => main tx should be completed after the second request is added to the pool diff --git a/pkg/services/notary/notary.go b/pkg/services/notary/notary.go index 166890675e..44258d7bb7 100644 --- a/pkg/services/notary/notary.go +++ b/pkg/services/notary/notary.go @@ -14,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "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" @@ -31,7 +32,6 @@ type ( Ledger interface { BlockHeight() uint32 GetMaxVerificationGAS() int64 - GetNotaryContractScriptHash() util.Uint160 SubscribeForBlocks(ch chan *block.Block) UnsubscribeFromBlocks(ch chan *block.Block) VerifyWitness(util.Uint160, hash.Hashable, *transaction.Witness, int64) (int64, error) @@ -408,7 +408,7 @@ func (n *Notary) finalize(acc *wallet.Account, tx *transaction.Transaction, h ut VerificationScript: []byte{}, } for i, signer := range tx.Signers { - if signer.Account == n.Config.Chain.GetNotaryContractScriptHash() { + if signer.Account == nativehashes.Notary { tx.Scripts[i] = notaryWitness break } @@ -510,7 +510,7 @@ func (n *Notary) verifyIncompleteWitnesses(tx *transaction.Transaction, nKeysExp if len(tx.Signers) < 2 { return nil, errors.New("transaction should have at least 2 signers") } - if !tx.HasSigner(n.Config.Chain.GetNotaryContractScriptHash()) { + if !tx.HasSigner(nativehashes.Notary) { return nil, fmt.Errorf("P2PNotary contract should be a signer of the transaction") } result := make([]witnessInfo, len(tx.Signers)) diff --git a/pkg/services/notary/notary_test.go b/pkg/services/notary/notary_test.go index 9df86c72fa..afc630f8a7 100644 --- a/pkg/services/notary/notary_test.go +++ b/pkg/services/notary/notary_test.go @@ -7,11 +7,11 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core/mempool" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "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" "github.com/nspcc-dev/neo-go/pkg/smartcontract" - "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" @@ -48,8 +48,7 @@ func TestWallet(t *testing.T) { func TestVerifyIncompleteRequest(t *testing.T) { bc := fakechain.NewFakeChain() - notaryContractHash := util.Uint160{1, 2, 3} - bc.NotaryContractScriptHash = notaryContractHash + notaryContractHash := nativehashes.Notary _, ntr, _ := getTestNotary(t, bc, "./testdata/notary1.json", "one") sig := append([]byte{byte(opcode.PUSHDATA1), keys.SignatureLen}, make([]byte, keys.SignatureLen)...) // we're not interested in signature correctness acc1, _ := keys.NewPrivateKey() diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 755140a365..82c38175f3 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -88,7 +88,6 @@ type ( GetNativeContractScriptHash(string) (util.Uint160, error) GetNatives() []state.Contract GetNextBlockValidators() ([]*keys.PublicKey, error) - GetNotaryContractScriptHash() util.Uint160 GetStateModule() core.StateRoot GetStorageItem(id int32, key []byte) state.StorageItem GetTestHistoricVM(t trigger.Type, tx *transaction.Transaction, nextBlockHeight uint32) (*interop.Context, error) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 1081019982..e29be1e2ef 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -31,6 +31,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/fee" "github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames" + "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" "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/storage/dboper" @@ -2495,7 +2496,7 @@ func createValidNotaryRequest(chain *core.Blockchain, sender *keys.PrivateKey, n {Type: transaction.ConflictsT, Value: &transaction.Conflicts{Hash: mainTx.Hash()}}, {Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 0}}, }, - Signers: []transaction.Signer{{Account: chain.GetNotaryContractScriptHash()}, {Account: sender.GetScriptHash()}}, + Signers: []transaction.Signer{{Account: nativehashes.Notary}, {Account: sender.GetScriptHash()}}, Scripts: []transaction.Witness{ {InvocationScript: append([]byte{byte(opcode.PUSHDATA1), keys.SignatureLen}, make([]byte, keys.SignatureLen)...), VerificationScript: []byte{}}, }, @@ -3924,7 +3925,7 @@ func checkNep17TransfersAux(t *testing.T, e *executor, acc any, sent, rcvd []int { Timestamp: blockDepositGAS.Timestamp, Asset: e.chain.UtilityTokenHash(), - Address: address.Uint160ToString(e.chain.GetNotaryContractScriptHash()), + Address: address.Uint160ToString(nativehashes.Notary), Amount: "1000000000", Index: 8, NotifyIndex: 0,