2022-07-21 13:21:44 +00:00
|
|
|
package rpcsrv
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-04-26 09:32:06 +00:00
|
|
|
"fmt"
|
2020-07-09 09:57:24 +00:00
|
|
|
"math/big"
|
2019-09-18 15:21:16 +00:00
|
|
|
"net/http"
|
2020-04-29 12:14:56 +00:00
|
|
|
"net/http/httptest"
|
2019-10-15 09:52:10 +00:00
|
|
|
"os"
|
2019-09-18 15:21:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-12-11 12:22:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-06-23 14:15:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2020-03-03 14:21:42 +00: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 11:58:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/oracle"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-09-18 15:21:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-10 22:00:19 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-09-18 15:21:16 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 09:05:45 +00:00
|
|
|
const (
|
2022-07-21 13:21:44 +00:00
|
|
|
notaryPath = "../notary/testdata/notary1.json"
|
2021-02-09 09:05:45 +00:00
|
|
|
notaryPass = "one"
|
|
|
|
)
|
|
|
|
|
2022-06-15 18:23:29 +00: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 13:21:44 +00:00
|
|
|
Path: "../oracle/testdata/oracle1.json",
|
2022-06-15 18:23:29 +00: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 07:34:50 +00:00
|
|
|
net := netmode.UnitTestNet
|
2020-02-17 12:17:02 +00:00
|
|
|
configPath := "../../../config"
|
2019-09-18 15:21:16 +00:00
|
|
|
cfg, err := config.Load(configPath, net)
|
|
|
|
require.NoError(t, err, "could not load config")
|
2022-06-15 18:23:29 +00:00
|
|
|
if customCfg != nil {
|
|
|
|
customCfg(&cfg)
|
|
|
|
}
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
memoryStore := storage.NewMemoryStore()
|
2019-12-30 08:44:52 +00:00
|
|
|
logger := zaptest.NewLogger(t)
|
2022-12-06 13:34:38 +00:00
|
|
|
chain, err := core.NewBlockchain(memoryStore, cfg.Blockchain(), logger)
|
2019-09-18 15:21:16 +00:00
|
|
|
require.NoError(t, err, "could not create chain")
|
|
|
|
|
2020-09-28 11:58:04 +00: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 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-15 09:52:10 +00:00
|
|
|
|
2020-09-28 11:58:04 +00:00
|
|
|
return chain, orc, cfg, logger
|
2020-05-10 22:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTestBlocks(t *testing.T) []*block.Block {
|
2022-02-04 16:21:12 +00:00
|
|
|
// File "./testdata/testblocks.acc" was generated by function core.TestCreateBasicChain
|
2020-02-17 12:04:25 +00: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 17:00:38 +00:00
|
|
|
f, err := os.Open("testdata/testblocks.acc")
|
2019-10-15 09:52:10 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
br := io.NewBinReaderFromIO(f)
|
2020-05-10 22:00:19 +00:00
|
|
|
nBlocks := br.ReadU32LE()
|
2019-10-15 09:52:10 +00:00
|
|
|
require.Nil(t, br.Err)
|
2020-05-10 22:00:19 +00:00
|
|
|
blocks := make([]*block.Block, 0, int(nBlocks))
|
2019-10-15 09:52:10 +00:00
|
|
|
for i := 0; i < int(nBlocks); i++ {
|
2020-02-26 14:51:30 +00:00
|
|
|
_ = br.ReadU32LE()
|
2021-03-25 18:46:52 +00:00
|
|
|
b := block.New(false)
|
2020-01-14 12:32:07 +00:00
|
|
|
b.DecodeBinary(br)
|
2019-10-15 09:52:10 +00:00
|
|
|
require.Nil(t, br.Err)
|
2020-05-10 22:00:19 +00:00
|
|
|
blocks = append(blocks, b)
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
2020-05-10 22:00:19 +00:00
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:23:29 +00: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 09:19:49 +00:00
|
|
|
return wrapUnitTestChain(t, chain, orc, cfg, logger)
|
|
|
|
}
|
2019-09-18 15:21:16 +00:00
|
|
|
|
2022-11-23 09:19:49 +00: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 14:43:08 +00:00
|
|
|
serverConfig, err := network.NewServerConfig(cfg)
|
|
|
|
require.NoError(t, err)
|
2022-05-16 03:59:23 +00:00
|
|
|
serverConfig.UserAgent = fmt.Sprintf(config.UserAgentFormat, "0.98.6-test")
|
2022-11-29 14:43:08 +00:00
|
|
|
serverConfig.Addresses = []config.AnnounceableAddress{{Address: ":0"}}
|
2022-01-12 21:20:03 +00:00
|
|
|
server, err := network.NewServer(serverConfig, chain, chain.GetStateSyncModule(), logger)
|
2020-01-22 08:17:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-09 20:59:21 +00:00
|
|
|
errCh := make(chan error, 2)
|
2022-04-22 07:49:06 +00:00
|
|
|
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, orc, logger, errCh)
|
|
|
|
rpcServer.Start()
|
2020-04-29 12:14:56 +00:00
|
|
|
|
2020-04-28 13:56:33 +00:00
|
|
|
handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
|
2020-04-29 12:14:56 +00:00
|
|
|
srv := httptest.NewServer(handler)
|
2019-09-18 15:21:16 +00:00
|
|
|
|
2020-05-09 20:59:21 +00:00
|
|
|
return chain, &rpcServer, srv
|
2019-09-18 15:21:16 +00:00
|
|
|
}
|
2020-03-02 16:13:44 +00:00
|
|
|
|
2022-11-23 09:19:49 +00: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 11:32:01 +00:00
|
|
|
func initClearServerWithInMemoryChain(t testing.TB) (*core.Blockchain, *Server, *httptest.Server) {
|
2022-06-15 18:23:29 +00:00
|
|
|
return initClearServerWithServices(t, false, false, false)
|
2020-09-28 11:58:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:00:19 +00: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 18:23:29 +00: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 09:05:45 +00:00
|
|
|
|
|
|
|
for _, b := range getTestBlocks(t) {
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
}
|
|
|
|
return chain, rpcServer, srv
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:13:44 +00:00
|
|
|
type FeerStub struct{}
|
|
|
|
|
2020-06-23 14:15:35 +00:00
|
|
|
func (fs *FeerStub) FeePerByte() int64 {
|
2020-03-02 16:13:44 +00:00
|
|
|
return 0
|
|
|
|
}
|
2020-05-18 08:20:41 +00:00
|
|
|
|
2020-09-09 12:32:31 +00:00
|
|
|
func (fs *FeerStub) BlockHeight() uint32 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:57:24 +00:00
|
|
|
func (fs *FeerStub) GetUtilityTokenBalance(acc util.Uint160) *big.Int {
|
|
|
|
return big.NewInt(1000000 * native.GASFactor)
|
2020-05-18 08:20:41 +00:00
|
|
|
}
|
2020-10-15 11:45:29 +00:00
|
|
|
|
|
|
|
func (fs FeerStub) P2PSigExtensionsEnabled() bool {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-11 12:22:49 +00:00
|
|
|
|
|
|
|
func (fs FeerStub) GetBaseExecFee() int64 {
|
|
|
|
return interop.DefaultBaseExecFee
|
|
|
|
}
|