2019-12-12 13:01:22 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"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/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-02-29 15:55:16 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-12 13:01:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-12-12 13:01:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Benchmark test to measure number of processed TX.
|
|
|
|
// Same benchmark made on reference C# node https://github.com/neo-project/neo/issues/1321.
|
|
|
|
func BenchmarkTXPerformanceTest(t *testing.B) {
|
2020-06-14 07:34:50 +00:00
|
|
|
net := netmode.UnitTestNet
|
2019-12-12 13:01:22 +00:00
|
|
|
configPath := "../config"
|
|
|
|
cfg, err := config.Load(configPath, net)
|
|
|
|
require.NoError(t, err, "could not load config")
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
logger := zaptest.NewLogger(t)
|
2019-12-12 13:01:22 +00:00
|
|
|
memoryStore := storage.NewMemoryStore()
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, logger)
|
2019-12-12 13:01:22 +00:00
|
|
|
require.NoError(t, err, "could not create chain")
|
|
|
|
|
|
|
|
go chain.Run()
|
|
|
|
|
|
|
|
serverConfig := network.NewServerConfig(cfg)
|
2020-01-22 08:17:51 +00:00
|
|
|
server, err := network.NewServer(serverConfig, chain, logger)
|
|
|
|
require.NoError(t, err, "could not create server")
|
2019-12-12 18:13:54 +00:00
|
|
|
data := prepareData(t)
|
|
|
|
t.ResetTimer()
|
|
|
|
|
2019-12-12 13:01:22 +00:00
|
|
|
for n := 0; n < t.N; n++ {
|
2020-02-29 15:55:16 +00:00
|
|
|
assert.Equal(t, network.RelaySucceed, server.RelayTxn(data[n]))
|
|
|
|
assert.Equal(t, network.RelayAlreadyExists, server.RelayTxn(data[n]))
|
2019-12-12 13:01:22 +00:00
|
|
|
}
|
|
|
|
chain.Close()
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:13:54 +00:00
|
|
|
func prepareData(t *testing.B) []*transaction.Transaction {
|
|
|
|
var data []*transaction.Transaction
|
|
|
|
|
|
|
|
wif := getWif(t)
|
2020-02-28 16:01:07 +00:00
|
|
|
acc, err := wallet.NewAccountFromWIF(wif.S)
|
|
|
|
require.NoError(t, err)
|
2019-12-12 18:13:54 +00:00
|
|
|
|
|
|
|
for n := 0; n < t.N; n++ {
|
|
|
|
tx := getTX(t, wif)
|
2020-02-28 16:01:07 +00:00
|
|
|
require.NoError(t, acc.SignTx(tx))
|
2019-12-12 18:13:54 +00:00
|
|
|
data = append(data, tx)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWif returns Wif.
|
|
|
|
func getWif(t *testing.B) *keys.WIF {
|
|
|
|
var (
|
|
|
|
wifEncoded = "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o"
|
|
|
|
version = byte(0x00)
|
|
|
|
)
|
|
|
|
wif, err := keys.WIFDecode(wifEncoded, version)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return wif
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:01:22 +00:00
|
|
|
// getTX returns Invocation transaction with some random attributes in order to have different hashes.
|
2019-12-12 18:13:54 +00:00
|
|
|
func getTX(t *testing.B, wif *keys.WIF) *transaction.Transaction {
|
|
|
|
fromAddress := wif.PrivateKey.Address()
|
2019-12-25 14:34:18 +00:00
|
|
|
fromAddressHash, err := address.StringToUint160(fromAddress)
|
2019-12-12 18:13:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{0x51}, 1)
|
2020-04-10 10:41:49 +00:00
|
|
|
tx.Version = 0
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: fromAddressHash,
|
|
|
|
Scopes: transaction.FeeOnly,
|
|
|
|
},
|
|
|
|
}
|
2019-12-12 13:01:22 +00:00
|
|
|
tx.Attributes = append(tx.Attributes,
|
|
|
|
transaction.Attribute{
|
2020-07-10 17:40:27 +00:00
|
|
|
Usage: transaction.DescriptionURL,
|
2019-12-12 13:01:22 +00:00
|
|
|
Data: []byte(randString(10)),
|
|
|
|
})
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a random string with the n as its length.
|
|
|
|
func randString(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(Int(65, 90))
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int returns a random integer in [min,max).
|
|
|
|
func Int(min, max int) int {
|
|
|
|
return min + rand.Intn(max-min)
|
|
|
|
}
|