forked from TrueCloudLab/neoneo-go
testchain: move newBlock there from rpc/server
Allow its reuse by other components.
This commit is contained in:
parent
d9b8704b48
commit
7fedb4f4ba
3 changed files with 41 additions and 33 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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++ {
|
||||
|
|
Loading…
Reference in a new issue