2020-02-17 12:17:02 +00:00
|
|
|
package server
|
2019-09-18 15:21:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-10-15 09:52:10 +00:00
|
|
|
"os"
|
2019-09-18 15:21:16 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/config"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2020-01-14 12:32:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/block"
|
2019-09-18 15:21:16 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-09-18 15:21:16 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network"
|
2020-01-14 12:02:38 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/request"
|
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-02-21 12:10:59 +00:00
|
|
|
// InvokeFunctionResult struct for testing.
|
|
|
|
type InvokeFunctionResult struct {
|
|
|
|
Script string `json:"script"`
|
|
|
|
State string `json:"state"`
|
|
|
|
GasConsumed string `json:"gas_consumed"`
|
|
|
|
Stack []request.FuncParam `json:"stack"`
|
|
|
|
TX string `json:"tx,omitempty"`
|
2020-02-15 17:00:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 17:47:48 +00:00
|
|
|
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) {
|
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)
|
2019-09-18 15:21:16 +00:00
|
|
|
handler := http.HandlerFunc(rpcServer.requestHandler)
|
|
|
|
|
|
|
|
return chain, handler
|
|
|
|
}
|