2020-02-17 12:17:02 +00:00
|
|
|
package server
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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-03-03 14:21:42 +00:00
|
|
|
"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/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-09-18 15:21:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-09-18 15:21:16 +00:00
|
|
|
)
|
|
|
|
|
2020-05-09 20:59:21 +00:00
|
|
|
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *Server, *httptest.Server) {
|
2019-10-15 09:52:10 +00:00
|
|
|
var nBlocks uint32
|
|
|
|
|
2019-09-18 15:21:16 +00:00
|
|
|
net := config.ModeUnitTestNet
|
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)
|
|
|
|
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, logger)
|
2019-09-18 15:21:16 +00:00
|
|
|
require.NoError(t, err, "could not create chain")
|
|
|
|
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-15 09:52:10 +00:00
|
|
|
|
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)
|
2019-12-12 15:52:23 +00:00
|
|
|
nBlocks = br.ReadU32LE()
|
2019-10-15 09:52:10 +00:00
|
|
|
require.Nil(t, br.Err)
|
|
|
|
for i := 0; i < int(nBlocks); i++ {
|
2020-02-26 14:51:30 +00:00
|
|
|
_ = br.ReadU32LE()
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{}
|
|
|
|
b.DecodeBinary(br)
|
2019-10-15 09:52:10 +00:00
|
|
|
require.Nil(t, br.Err)
|
2020-01-14 12:32:07 +00:00
|
|
|
require.NoError(t, chain.AddBlock(b))
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
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-02-17 12:17:02 +00:00
|
|
|
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
2020-05-09 20:59:21 +00:00
|
|
|
errCh := make(chan error, 2)
|
|
|
|
go 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
|
|
|
|
|
|
|
type FeerStub struct{}
|
|
|
|
|
|
|
|
func (fs *FeerStub) NetworkFee(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) IsLowPriority(util.Fixed8) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) FeePerByte(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) SystemFee(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return 0
|
|
|
|
}
|