github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/services/rpcsrv/local_test.go (about)

     1  package rpcsrv
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/nspcc-dev/neo-go/internal/testchain"
     9  	"github.com/nspcc-dev/neo-go/pkg/config"
    10  	"github.com/nspcc-dev/neo-go/pkg/rpcclient"
    11  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
    12  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
    13  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
    14  	"github.com/nspcc-dev/neo-go/pkg/util"
    15  	"github.com/nspcc-dev/neo-go/pkg/wallet"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestLocalClient(t *testing.T) {
    20  	_, rpcSrv, _ := initClearServerWithCustomConfig(t, func(cfg *config.Config) {
    21  		// No addresses configured -> RPC server listens nothing (but it
    22  		// has MaxGasInvoke, sessions and other stuff).
    23  		cfg.ApplicationConfiguration.RPC.BasicService.Enabled = true
    24  		cfg.ApplicationConfiguration.RPC.BasicService.Addresses = nil
    25  		cfg.ApplicationConfiguration.RPC.TLSConfig.Addresses = nil
    26  	})
    27  	// RPC server listens nothing (not exposed in any way), but it works for internal clients.
    28  	c, err := rpcclient.NewInternal(context.TODO(), rpcSrv.RegisterLocal)
    29  	require.NoError(t, err)
    30  	require.NoError(t, c.Init())
    31  
    32  	// Invokers can use this client.
    33  	gasReader := gas.NewReader(invoker.New(c, nil))
    34  	d, err := gasReader.Decimals()
    35  	require.NoError(t, err)
    36  	require.EqualValues(t, 8, d)
    37  
    38  	// Actors can use it as well
    39  	priv := testchain.PrivateKeyByID(0)
    40  	acc := wallet.NewAccountFromPrivateKey(priv)
    41  	addr := priv.PublicKey().GetScriptHash()
    42  
    43  	act, err := actor.NewSimple(c, acc)
    44  	require.NoError(t, err)
    45  	gasprom := gas.New(act)
    46  	txHash, _, err := gasprom.Transfer(addr, util.Uint160{}, big.NewInt(1000), nil)
    47  	require.NoError(t, err)
    48  	// No new blocks are produced here, but the tx is OK and is in the mempool.
    49  	txes, err := c.GetRawMemPool()
    50  	require.NoError(t, err)
    51  	require.Equal(t, []util.Uint256{txHash}, txes)
    52  	// Subscriptions are checked by other tests.
    53  }