From 7fedb4f4ba5d06d61facbf152efc683fdcb276fe Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 Aug 2020 18:47:14 +0300 Subject: [PATCH] testchain: move newBlock there from rpc/server Allow its reuse by other components. --- pkg/internal/testchain/address.go | 36 +++++++++++++++++++++++++++++ pkg/rpc/server/server_test.go | 34 +++------------------------ pkg/rpc/server/subscription_test.go | 4 ++-- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/pkg/internal/testchain/address.go b/pkg/internal/testchain/address.go index 27cc58d6a..e3cd1ca0f 100644 --- a/pkg/internal/testchain/address.go +++ b/pkg/internal/testchain/address.go @@ -1,6 +1,12 @@ package testchain import ( + "testing" + "time" + + "github.com/nspcc-dev/neo-go/pkg/core/block" + "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" + "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/address" @@ -8,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/emit" + "github.com/stretchr/testify/require" ) // privNetKeys is a list of unencrypted WIFs sorted by public key. @@ -94,3 +101,32 @@ func Sign(data []byte) []byte { } return buf.Bytes() } + +// NewBlock creates new block for the given blockchain with the given offset +// (usually, 1), primary node index and transactions. +func NewBlock(t *testing.T, bc blockchainer.Blockchainer, offset uint32, primary uint32, txs ...*transaction.Transaction) *block.Block { + witness := transaction.Witness{VerificationScript: MultisigVerificationScript()} + height := bc.BlockHeight() + h := bc.GetHeaderHash(int(height)) + hdr, err := bc.GetHeader(h) + require.NoError(t, err) + b := &block.Block{ + Base: block.Base{ + PrevHash: hdr.Hash(), + Timestamp: (uint64(time.Now().UTC().Unix()) + uint64(hdr.Index)) * 1000, + Index: hdr.Index + offset, + NextConsensus: witness.ScriptHash(), + Script: witness, + Network: bc.GetConfig().Magic, + }, + ConsensusData: block.ConsensusData{ + PrimaryIndex: primary, + Nonce: 1111, + }, + Transactions: txs, + } + _ = b.RebuildMerkleRoot() + + b.Script.InvocationScript = Sign(b.GetSignedPart()) + return b +} diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index d0a759625..eb7cdd273 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -18,7 +18,6 @@ import ( "github.com/gorilla/websocket" "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/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/encoding/address" @@ -729,7 +728,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] t.Run("submit", func(t *testing.T) { rpc := `{"jsonrpc": "2.0", "id": 1, "method": "submitblock", "params": ["%s"]}` t.Run("invalid signature", func(t *testing.T) { - s := newBlock(t, chain, 1, 0) + s := testchain.NewBlock(t, chain, 1, 0) s.Script.VerificationScript[8] ^= 0xff body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, s)), httpSrv.URL, t) checkErrGetResult(t, body, true) @@ -759,13 +758,13 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] } t.Run("invalid height", func(t *testing.T) { - b := newBlock(t, chain, 2, 0, newTx()) + b := testchain.NewBlock(t, chain, 2, 0, newTx()) body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, b)), httpSrv.URL, t) checkErrGetResult(t, body, true) }) t.Run("positive", func(t *testing.T) { - b := newBlock(t, chain, 1, 0, newTx()) + b := testchain.NewBlock(t, chain, 1, 0, newTx()) body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, b)), httpSrv.URL, t) data := checkErrGetResult(t, body, false) var res = new(result.RelayResult) @@ -934,33 +933,6 @@ func encodeBlock(t *testing.T, b *block.Block) string { return hex.EncodeToString(w.Bytes()) } -func newBlock(t *testing.T, bc blockchainer.Blockchainer, index uint32, primary uint32, txs ...*transaction.Transaction) *block.Block { - witness := transaction.Witness{VerificationScript: testchain.MultisigVerificationScript()} - height := bc.BlockHeight() - h := bc.GetHeaderHash(int(height)) - hdr, err := bc.GetHeader(h) - require.NoError(t, err) - b := &block.Block{ - Base: block.Base{ - PrevHash: hdr.Hash(), - Timestamp: (uint64(time.Now().UTC().Unix()) + uint64(hdr.Index)) * 1000, - Index: hdr.Index + index, - NextConsensus: witness.ScriptHash(), - Script: witness, - Network: bc.GetConfig().Magic, - }, - ConsensusData: block.ConsensusData{ - PrimaryIndex: primary, - Nonce: 1111, - }, - Transactions: txs, - } - _ = b.RebuildMerkleRoot() - - b.Script.InvocationScript = testchain.Sign(b.GetSignedPart()) - return b -} - func (tc rpcTestCase) getResultPair(e *executor) (expected interface{}, res interface{}) { expected = tc.result(e) resVal := reflect.New(reflect.TypeOf(expected).Elem()) diff --git a/pkg/rpc/server/subscription_test.go b/pkg/rpc/server/subscription_test.go index 69377b6ac..0cb1c8197 100644 --- a/pkg/rpc/server/subscription_test.go +++ b/pkg/rpc/server/subscription_test.go @@ -284,7 +284,7 @@ func TestFilteredBlockSubscriptions(t *testing.T) { if primary == 3 { expectedCnt++ } - b := newBlock(t, chain, 1, primary) + b := testchain.NewBlock(t, chain, 1, primary) require.NoError(t, chain.AddBlock(b)) } @@ -437,7 +437,7 @@ func testSubscriptionOverflow(t *testing.T) { // Push a lot of new blocks, but don't read events for them. for i := 0; i < blockCnt; i++ { - b := newBlock(t, chain, 1, 0) + b := testchain.NewBlock(t, chain, 1, 0) require.NoError(t, chain.AddBlock(b)) } for i := 0; i < blockCnt; i++ {