2022-07-21 16:21:44 +03:00
|
|
|
package rpcsrv
|
2019-09-18 18:21:16 +03:00
|
|
|
|
|
|
|
import (
|
2022-04-26 12:32:06 +03:00
|
|
|
"fmt"
|
2020-07-09 12:57:24 +03:00
|
|
|
"math/big"
|
2019-09-18 18:21:16 +03:00
|
|
|
"net/http"
|
2020-04-29 15:14:56 +03:00
|
|
|
"net/http/httptest"
|
2019-10-15 12:52:10 +03:00
|
|
|
"os"
|
2019-09-18 18:21:16 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-03-25 18:30:21 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-06-14 10:34:50 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-12-11 15:22:49 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-06-23 17:15:35 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network"
|
2020-09-28 14:58:04 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/oracle"
|
2020-03-03 17:21:42 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-09-18 18:21:16 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-11 01:00:19 +03:00
|
|
|
"go.uber.org/zap"
|
2019-12-30 10:43:05 +03:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-09-18 18:21:16 +03:00
|
|
|
)
|
|
|
|
|
2021-02-09 12:05:45 +03:00
|
|
|
const (
|
2022-07-21 16:21:44 +03:00
|
|
|
notaryPath = "../notary/testdata/notary1.json"
|
2021-02-09 12:05:45 +03:00
|
|
|
notaryPass = "one"
|
|
|
|
)
|
|
|
|
|
2022-06-15 21:23:29 +03:00
|
|
|
func getUnitTestChain(t testing.TB, enableOracle bool, enableNotary bool, disableIteratorSessions bool) (*core.Blockchain, *oracle.Oracle, config.Config, *zap.Logger) {
|
|
|
|
return getUnitTestChainWithCustomConfig(t, enableOracle, enableNotary, func(cfg *config.Config) {
|
|
|
|
if disableIteratorSessions {
|
|
|
|
cfg.ApplicationConfiguration.RPC.SessionEnabled = false
|
|
|
|
}
|
|
|
|
if enableNotary {
|
|
|
|
cfg.ProtocolConfiguration.P2PSigExtensions = true
|
|
|
|
cfg.ProtocolConfiguration.P2PNotaryRequestPayloadPoolSize = 1000
|
|
|
|
cfg.ApplicationConfiguration.P2PNotary = config.P2PNotary{
|
|
|
|
Enabled: true,
|
|
|
|
UnlockWallet: config.Wallet{
|
|
|
|
Path: notaryPath,
|
|
|
|
Password: notaryPass,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfg.ApplicationConfiguration.P2PNotary.Enabled = false
|
|
|
|
}
|
|
|
|
if enableOracle {
|
|
|
|
cfg.ApplicationConfiguration.Oracle.Enabled = true
|
|
|
|
cfg.ApplicationConfiguration.Oracle.UnlockWallet = config.Wallet{
|
2022-07-21 16:21:44 +03:00
|
|
|
Path: "../oracle/testdata/oracle1.json",
|
2022-06-15 21:23:29 +03:00
|
|
|
Password: "one",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func getUnitTestChainWithCustomConfig(t testing.TB, enableOracle bool, enableNotary bool, customCfg func(configuration *config.Config)) (*core.Blockchain, *oracle.Oracle, config.Config, *zap.Logger) {
|
2020-06-14 10:34:50 +03:00
|
|
|
net := netmode.UnitTestNet
|
2020-02-17 15:17:02 +03:00
|
|
|
configPath := "../../../config"
|
2019-09-18 18:21:16 +03:00
|
|
|
cfg, err := config.Load(configPath, net)
|
|
|
|
require.NoError(t, err, "could not load config")
|
2022-06-15 21:23:29 +03:00
|
|
|
if customCfg != nil {
|
|
|
|
customCfg(&cfg)
|
|
|
|
}
|
2019-09-18 18:21:16 +03:00
|
|
|
|
|
|
|
memoryStore := storage.NewMemoryStore()
|
2019-12-30 11:44:52 +03:00
|
|
|
logger := zaptest.NewLogger(t)
|
2022-12-06 16:34:38 +03:00
|
|
|
chain, err := core.NewBlockchain(memoryStore, cfg.Blockchain(), logger)
|
2019-09-18 18:21:16 +03:00
|
|
|
require.NoError(t, err, "could not create chain")
|
|
|
|
|
2020-09-28 14:58:04 +03:00
|
|
|
var orc *oracle.Oracle
|
|
|
|
if enableOracle {
|
|
|
|
orc, err = oracle.NewOracle(oracle.Config{
|
|
|
|
Log: logger,
|
|
|
|
Network: netmode.UnitTestNet,
|
|
|
|
MainCfg: cfg.ApplicationConfiguration.Oracle,
|
|
|
|
Chain: chain,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
chain.SetOracle(orc)
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:47:48 +03:00
|
|
|
go chain.Run()
|
2019-10-15 12:52:10 +03:00
|
|
|
|
2020-09-28 14:58:04 +03:00
|
|
|
return chain, orc, cfg, logger
|
2020-05-11 01:00:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTestBlocks(t *testing.T) []*block.Block {
|
2022-02-04 19:21:12 +03:00
|
|
|
// File "./testdata/testblocks.acc" was generated by function core.TestCreateBasicChain
|
2020-02-17 15:04:25 +03:00
|
|
|
// ("neo-go/pkg/core/helper_test.go").
|
|
|
|
// To generate new "./testdata/testblocks.acc", follow the steps:
|
|
|
|
// 1. Rename the function
|
|
|
|
// 2. Add specific test-case into "neo-go/pkg/core/blockchain_test.go"
|
|
|
|
// 3. Run tests with `$ make test`
|
2020-02-15 20:00:38 +03:00
|
|
|
f, err := os.Open("testdata/testblocks.acc")
|
2019-10-15 12:52:10 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
br := io.NewBinReaderFromIO(f)
|
2020-05-11 01:00:19 +03:00
|
|
|
nBlocks := br.ReadU32LE()
|
2019-10-15 12:52:10 +03:00
|
|
|
require.Nil(t, br.Err)
|
2020-05-11 01:00:19 +03:00
|
|
|
blocks := make([]*block.Block, 0, int(nBlocks))
|
2019-10-15 12:52:10 +03:00
|
|
|
for i := 0; i < int(nBlocks); i++ {
|
2020-02-26 17:51:30 +03:00
|
|
|
_ = br.ReadU32LE()
|
2021-03-25 21:46:52 +03:00
|
|
|
b := block.New(false)
|
2020-01-14 15:32:07 +03:00
|
|
|
b.DecodeBinary(br)
|
2019-10-15 12:52:10 +03:00
|
|
|
require.Nil(t, br.Err)
|
2020-05-11 01:00:19 +03:00
|
|
|
blocks = append(blocks, b)
|
2019-10-15 12:52:10 +03:00
|
|
|
}
|
2020-05-11 01:00:19 +03:00
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:23:29 +03:00
|
|
|
func initClearServerWithServices(t testing.TB, needOracle bool, needNotary bool, disableIteratorsSessions bool) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, orc, cfg, logger := getUnitTestChain(t, needOracle, needNotary, disableIteratorsSessions)
|
2022-11-23 12:19:49 +03:00
|
|
|
return wrapUnitTestChain(t, chain, orc, cfg, logger)
|
|
|
|
}
|
2019-09-18 18:21:16 +03:00
|
|
|
|
2022-11-23 12:19:49 +03:00
|
|
|
func wrapUnitTestChain(t testing.TB, chain *core.Blockchain, orc *oracle.Oracle, cfg config.Config, logger *zap.Logger) (*core.Blockchain, *Server, *httptest.Server) {
|
2022-11-29 17:43:08 +03:00
|
|
|
serverConfig, err := network.NewServerConfig(cfg)
|
|
|
|
require.NoError(t, err)
|
2022-05-16 06:59:23 +03:00
|
|
|
serverConfig.UserAgent = fmt.Sprintf(config.UserAgentFormat, "0.98.6-test")
|
2022-11-29 17:43:08 +03:00
|
|
|
serverConfig.Addresses = []config.AnnounceableAddress{{Address: ":0"}}
|
2022-01-13 00:20:03 +03:00
|
|
|
server, err := network.NewServer(serverConfig, chain, chain.GetStateSyncModule(), logger)
|
2020-01-22 11:17:51 +03:00
|
|
|
require.NoError(t, err)
|
2020-05-09 23:59:21 +03:00
|
|
|
errCh := make(chan error, 2)
|
2022-04-22 10:49:06 +03:00
|
|
|
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, orc, logger, errCh)
|
|
|
|
rpcServer.Start()
|
2020-04-29 15:14:56 +03:00
|
|
|
|
2020-04-28 16:56:33 +03:00
|
|
|
handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
|
2020-04-29 15:14:56 +03:00
|
|
|
srv := httptest.NewServer(handler)
|
2019-09-18 18:21:16 +03:00
|
|
|
|
2020-05-09 23:59:21 +03:00
|
|
|
return chain, &rpcServer, srv
|
2019-09-18 18:21:16 +03:00
|
|
|
}
|
2020-03-02 19:13:44 +03:00
|
|
|
|
2022-11-23 12:19:49 +03:00
|
|
|
func initClearServerWithCustomConfig(t testing.TB, ccfg func(configuration *config.Config)) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, orc, cfg, logger := getUnitTestChainWithCustomConfig(t, false, false, ccfg)
|
|
|
|
return wrapUnitTestChain(t, chain, orc, cfg, logger)
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:32:01 +03:00
|
|
|
func initClearServerWithInMemoryChain(t testing.TB) (*core.Blockchain, *Server, *httptest.Server) {
|
2022-06-15 21:23:29 +03:00
|
|
|
return initClearServerWithServices(t, false, false, false)
|
2020-09-28 14:58:04 +03:00
|
|
|
}
|
|
|
|
|
2020-05-11 01:00:19 +03:00
|
|
|
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, rpcServer, srv := initClearServerWithInMemoryChain(t)
|
|
|
|
|
|
|
|
for _, b := range getTestBlocks(t) {
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
}
|
|
|
|
return chain, rpcServer, srv
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:23:29 +03:00
|
|
|
func initServerWithInMemoryChainAndServices(t *testing.T, needOracle bool, needNotary bool, disableIteratorSessions bool) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, rpcServer, srv := initClearServerWithServices(t, needOracle, needNotary, disableIteratorSessions)
|
2021-02-09 12:05:45 +03:00
|
|
|
|
|
|
|
for _, b := range getTestBlocks(t) {
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
}
|
|
|
|
return chain, rpcServer, srv
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:13:44 +03:00
|
|
|
type FeerStub struct{}
|
|
|
|
|
2020-06-23 17:15:35 +03:00
|
|
|
func (fs *FeerStub) FeePerByte() int64 {
|
2020-03-02 19:13:44 +03:00
|
|
|
return 0
|
|
|
|
}
|
2020-05-18 11:20:41 +03:00
|
|
|
|
2020-09-09 15:32:31 +03:00
|
|
|
func (fs *FeerStub) BlockHeight() uint32 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-09 12:57:24 +03:00
|
|
|
func (fs *FeerStub) GetUtilityTokenBalance(acc util.Uint160) *big.Int {
|
|
|
|
return big.NewInt(1000000 * native.GASFactor)
|
2020-05-18 11:20:41 +03:00
|
|
|
}
|
2020-10-15 14:45:29 +03:00
|
|
|
|
|
|
|
func (fs FeerStub) P2PSigExtensionsEnabled() bool {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-11 15:22:49 +03:00
|
|
|
|
|
|
|
func (fs FeerStub) GetBaseExecFee() int64 {
|
|
|
|
return interop.DefaultBaseExecFee
|
|
|
|
}
|