2020-02-17 12:17:02 +00:00
|
|
|
package server
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
import (
|
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 (
|
|
|
|
notaryPath = "../../services/notary/testdata/notary1.json"
|
|
|
|
notaryPass = "one"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getUnitTestChain(t *testing.T, enableOracle bool, enableNotary bool) (*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")
|
|
|
|
|
|
|
|
memoryStore := storage.NewMemoryStore()
|
2019-12-30 08:44:52 +00:00
|
|
|
logger := zaptest.NewLogger(t)
|
2021-02-09 09:05:45 +00:00
|
|
|
if enableNotary {
|
|
|
|
cfg.ProtocolConfiguration.P2PSigExtensions = true
|
|
|
|
cfg.ProtocolConfiguration.P2PNotaryRequestPayloadPoolSize = 1000
|
2021-02-16 10:49:56 +00:00
|
|
|
cfg.ApplicationConfiguration.P2PNotary = config.P2PNotary{
|
2021-02-09 09:05:45 +00:00
|
|
|
Enabled: true,
|
|
|
|
UnlockWallet: config.Wallet{
|
|
|
|
Path: notaryPath,
|
|
|
|
Password: notaryPass,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 10:49:56 +00:00
|
|
|
cfg.ApplicationConfiguration.P2PNotary.Enabled = false
|
2021-02-09 09:05:45 +00:00
|
|
|
}
|
2019-12-30 08:44:52 +00:00
|
|
|
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, 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 {
|
|
|
|
cfg.ApplicationConfiguration.Oracle.Enabled = true
|
|
|
|
cfg.ApplicationConfiguration.Oracle.UnlockWallet = config.Wallet{
|
|
|
|
Path: "../../services/oracle/testdata/oracle1.json",
|
|
|
|
Password: "one",
|
|
|
|
}
|
|
|
|
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 {
|
2020-02-17 12:04:25 +00:00
|
|
|
// File "./testdata/testblocks.acc" was generated by function core._
|
|
|
|
// ("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()
|
2020-11-17 12:57:50 +00:00
|
|
|
b := block.New(netmode.UnitTestNet, 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
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:05:45 +00:00
|
|
|
func initClearServerWithServices(t *testing.T, needOracle bool, needNotary bool) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, orc, cfg, logger := getUnitTestChain(t, needOracle, needNotary)
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
serverConfig := network.NewServerConfig(cfg)
|
2020-01-22 08:17:51 +00:00
|
|
|
server, err := network.NewServer(serverConfig, chain, logger)
|
|
|
|
require.NoError(t, err)
|
2020-09-28 11:58:04 +00:00
|
|
|
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, orc, logger)
|
2020-05-09 20:59:21 +00:00
|
|
|
errCh := make(chan error, 2)
|
2020-08-31 12:35:55 +00:00
|
|
|
rpcServer.Start(errCh)
|
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
|
|
|
|
2020-09-28 11:58:04 +00:00
|
|
|
func initClearServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *Server, *httptest.Server) {
|
2021-02-09 09:05:45 +00:00
|
|
|
return initClearServerWithServices(t, 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
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:05:45 +00:00
|
|
|
func initServerWithInMemoryChainAndServices(t *testing.T, needOracle bool, needNotary bool) (*core.Blockchain, *Server, *httptest.Server) {
|
|
|
|
chain, rpcServer, srv := initClearServerWithServices(t, needOracle, needNotary)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|