2019-11-08 15:40:21 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
"github.com/nspcc-dev/dbft/payload"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-03-27 07:14:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2019-11-15 10:32:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-08 15:40:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var messageTypes = []messageType{
|
|
|
|
changeViewType,
|
|
|
|
prepareRequestType,
|
|
|
|
prepareResponseType,
|
|
|
|
commitType,
|
|
|
|
recoveryRequestType,
|
|
|
|
recoveryMessageType,
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
func TestConsensusPayload_Setters(t *testing.T) {
|
|
|
|
var p Payload
|
2020-04-15 15:56:45 +00:00
|
|
|
p.message = &message{}
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
p.SetVersion(1)
|
|
|
|
assert.EqualValues(t, 1, p.Version())
|
|
|
|
|
|
|
|
p.SetPrevHash(util.Uint256{1, 2, 3})
|
|
|
|
assert.Equal(t, util.Uint256{1, 2, 3}, p.PrevHash())
|
|
|
|
|
|
|
|
p.SetValidatorIndex(4)
|
|
|
|
assert.EqualValues(t, 4, p.ValidatorIndex())
|
|
|
|
|
|
|
|
p.SetHeight(11)
|
|
|
|
assert.EqualValues(t, 11, p.Height())
|
|
|
|
|
|
|
|
p.SetViewNumber(2)
|
|
|
|
assert.EqualValues(t, 2, p.ViewNumber())
|
|
|
|
|
|
|
|
p.SetType(payload.PrepareRequestType)
|
|
|
|
assert.Equal(t, payload.PrepareRequestType, p.Type())
|
|
|
|
|
|
|
|
pl := randomMessage(t, prepareRequestType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.Payload())
|
|
|
|
require.Equal(t, pl, p.GetPrepareRequest())
|
|
|
|
|
|
|
|
pl = randomMessage(t, prepareResponseType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.GetPrepareResponse())
|
|
|
|
|
|
|
|
pl = randomMessage(t, commitType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.GetCommit())
|
|
|
|
|
|
|
|
pl = randomMessage(t, changeViewType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.GetChangeView())
|
|
|
|
|
|
|
|
pl = randomMessage(t, recoveryRequestType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.GetRecoveryRequest())
|
|
|
|
|
|
|
|
pl = randomMessage(t, recoveryMessageType)
|
|
|
|
p.SetPayload(pl)
|
|
|
|
require.Equal(t, pl, p.GetRecoveryMessage())
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
func TestConsensusPayload_Hash(t *testing.T) {
|
|
|
|
dataHex := "00000000d8fb8d3b143b5f98468ef701909c976505a110a01e26c5e99be9a90cff979199b6fc33000400000000008d2000184dc95de24018f9ad71f4448a2b438eaca8b4b2ab6b4524b5a69a45d920c35103f3901444320656c390ff39c0062f5e8e138ce446a40c7e4ba1af1f8247ebbdf49295933715d3a67949714ff924f8a28cec5b954c71eca3bfaf0e9d4b1f87b4e21e9ba4ae18f97de71501b5c5d07edc200bd66a46b9b28b1a371f2195c10b0af90000e24018f900000000014140c9faaee59942f58da0e5268bc199632f2a3ad0fcbee68681a4437f140b49512e8d9efc6880eb44d3490782895a5794f35eeccee2923ce0c76fa7a1890f934eac232103c089d7122b840a4935234e82e26ae5efd0c2acb627239dc9f207311337b6f2c1ac"
|
|
|
|
data, err := hex.DecodeString(dataHex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var p Payload
|
2020-03-26 14:43:24 +00:00
|
|
|
require.NoError(t, testserdes.DecodeBinary(data, &p))
|
2019-11-08 15:40:21 +00:00
|
|
|
require.Equal(t, p.Hash().String(), "45859759c8491597804f1922773947e0d37bf54484af82f80cd642f7b063aa56")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensusPayload_Serializable(t *testing.T) {
|
|
|
|
for _, mt := range messageTypes {
|
|
|
|
p := randomPayload(t, mt)
|
2020-04-15 15:56:45 +00:00
|
|
|
actual := new(Payload)
|
|
|
|
data, err := testserdes.EncodeBinary(p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, testserdes.DecodeBinary(data, actual))
|
|
|
|
// message is nil after decoding as we didn't yet call decodeData
|
|
|
|
require.Nil(t, actual.message)
|
|
|
|
// message should now be decoded from actual.data byte array
|
2020-06-23 06:41:53 +00:00
|
|
|
assert.NoError(t, actual.decodeData(false))
|
2020-04-15 15:56:45 +00:00
|
|
|
require.Equal(t, p, actual)
|
|
|
|
|
|
|
|
data = p.MarshalUnsigned()
|
2019-11-15 10:32:40 +00:00
|
|
|
pu := new(Payload)
|
|
|
|
require.NoError(t, pu.UnmarshalUnsigned(data))
|
2020-06-23 06:41:53 +00:00
|
|
|
assert.NoError(t, pu.decodeData(false))
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
p.Witness = transaction.Witness{}
|
|
|
|
require.Equal(t, p, pu)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
|
|
|
|
// PrepareResponse ConsensusPayload consists of:
|
|
|
|
// 46-byte common prefix
|
|
|
|
// 1-byte varint length of the payload (34),
|
|
|
|
// - 1-byte view number
|
|
|
|
// - 1-byte message type (PrepareResponse)
|
|
|
|
// - 32-byte preparation hash
|
|
|
|
// 1-byte delimiter (1)
|
|
|
|
// 2-byte for empty invocation and verification scripts
|
|
|
|
const (
|
|
|
|
lenIndex = 46
|
|
|
|
typeIndex = 47
|
|
|
|
delimeterIndex = 81
|
|
|
|
)
|
|
|
|
|
|
|
|
buf := make([]byte, 46+1+34+1+2)
|
|
|
|
|
|
|
|
expected := &Payload{
|
2020-04-15 15:56:45 +00:00
|
|
|
message: &message{
|
2019-11-15 10:32:40 +00:00
|
|
|
Type: prepareResponseType,
|
|
|
|
payload: &prepareResponse{},
|
|
|
|
},
|
|
|
|
Witness: transaction.Witness{
|
|
|
|
InvocationScript: []byte{},
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
},
|
|
|
|
}
|
2020-04-15 15:56:45 +00:00
|
|
|
// fill `data` for next check
|
|
|
|
_ = expected.Hash()
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
// valid payload
|
|
|
|
buf[delimeterIndex] = 1
|
|
|
|
buf[lenIndex] = 34
|
|
|
|
buf[typeIndex] = byte(prepareResponseType)
|
|
|
|
p := new(Payload)
|
2020-03-26 14:43:24 +00:00
|
|
|
require.NoError(t, testserdes.DecodeBinary(buf, p))
|
2020-04-15 15:56:45 +00:00
|
|
|
// decode `data` into `message`
|
2020-06-23 06:41:53 +00:00
|
|
|
assert.NoError(t, p.decodeData(false))
|
2019-11-15 10:32:40 +00:00
|
|
|
require.Equal(t, expected, p)
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
buf[typeIndex] = 0xFF
|
2020-04-15 15:56:45 +00:00
|
|
|
actual := new(Payload)
|
|
|
|
require.NoError(t, testserdes.DecodeBinary(buf, actual))
|
2020-06-23 06:41:53 +00:00
|
|
|
require.Error(t, actual.decodeData(false))
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
// invalid format
|
|
|
|
buf[delimeterIndex] = 0
|
|
|
|
buf[typeIndex] = byte(prepareResponseType)
|
2020-03-26 14:43:24 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
// invalid message length
|
|
|
|
buf[delimeterIndex] = 1
|
|
|
|
buf[lenIndex] = 0xFF
|
|
|
|
buf[typeIndex] = byte(prepareResponseType)
|
2020-03-26 14:43:24 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
func testEncodeDecode(srEnabled bool, mt messageType, actual io.Serializable) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
expected := randomMessage(t, mt, srEnabled)
|
|
|
|
testserdes.EncodeDecodeBinary(t, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
func TestCommit_Serializable(t *testing.T) {
|
2020-06-23 06:41:53 +00:00
|
|
|
t.Run("WithStateRoot", testEncodeDecode(true, commitType, &commit{stateRootEnabled: true}))
|
|
|
|
t.Run("NoStateRoot", testEncodeDecode(false, commitType, &commit{stateRootEnabled: false}))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareResponse_Serializable(t *testing.T) {
|
|
|
|
resp := randomMessage(t, prepareResponseType)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, resp, new(prepareResponse))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareRequest_Serializable(t *testing.T) {
|
2020-06-23 06:41:53 +00:00
|
|
|
t.Run("WithStateRoot", testEncodeDecode(true, prepareRequestType, &prepareRequest{stateRootEnabled: true}))
|
|
|
|
t.Run("NoStateRoot", testEncodeDecode(false, prepareRequestType, &prepareRequest{stateRootEnabled: false}))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoveryRequest_Serializable(t *testing.T) {
|
|
|
|
req := randomMessage(t, recoveryRequestType)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, req, new(recoveryRequest))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoveryMessage_Serializable(t *testing.T) {
|
2020-06-23 06:41:53 +00:00
|
|
|
t.Run("WithStateRoot", testEncodeDecode(true, recoveryMessageType, &recoveryMessage{stateRootEnabled: true}))
|
|
|
|
t.Run("NoStateRoot", testEncodeDecode(false, recoveryMessageType, &recoveryMessage{stateRootEnabled: false}))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randomPayload(t *testing.T, mt messageType) *Payload {
|
|
|
|
p := &Payload{
|
2020-04-15 15:56:45 +00:00
|
|
|
message: &message{
|
2019-11-08 15:40:21 +00:00
|
|
|
Type: mt,
|
|
|
|
ViewNumber: byte(rand.Uint32()),
|
|
|
|
payload: randomMessage(t, mt),
|
|
|
|
},
|
2019-11-15 10:32:40 +00:00
|
|
|
version: 1,
|
|
|
|
validatorIndex: 13,
|
|
|
|
height: rand.Uint32(),
|
2020-03-27 07:14:40 +00:00
|
|
|
prevHash: random.Uint256(),
|
2019-11-15 10:32:40 +00:00
|
|
|
timestamp: rand.Uint32(),
|
2019-11-08 15:40:21 +00:00
|
|
|
Witness: transaction.Witness{
|
2020-03-27 07:14:40 +00:00
|
|
|
InvocationScript: random.Bytes(3),
|
2019-12-12 14:38:03 +00:00
|
|
|
VerificationScript: []byte{byte(opcode.PUSH0)},
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if mt == changeViewType {
|
2019-11-15 10:32:40 +00:00
|
|
|
p.payload.(*changeView).newViewNumber = p.ViewNumber() + 1
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
func randomMessage(t *testing.T, mt messageType, srEnabled ...bool) io.Serializable {
|
2019-11-08 15:40:21 +00:00
|
|
|
switch mt {
|
|
|
|
case changeViewType:
|
|
|
|
return &changeView{
|
2019-11-15 10:32:40 +00:00
|
|
|
timestamp: rand.Uint32(),
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
case prepareRequestType:
|
2020-06-23 06:41:53 +00:00
|
|
|
return randomPrepareRequest(t, srEnabled...)
|
2019-11-08 15:40:21 +00:00
|
|
|
case prepareResponseType:
|
2020-03-27 07:14:40 +00:00
|
|
|
return &prepareResponse{preparationHash: random.Uint256()}
|
2019-11-08 15:40:21 +00:00
|
|
|
case commitType:
|
|
|
|
var c commit
|
2020-03-27 07:14:40 +00:00
|
|
|
random.Fill(c.signature[:])
|
2020-06-23 06:41:53 +00:00
|
|
|
if len(srEnabled) > 0 && srEnabled[0] {
|
|
|
|
c.stateRootEnabled = true
|
|
|
|
random.Fill(c.stateSig[:])
|
|
|
|
}
|
2019-11-08 15:40:21 +00:00
|
|
|
return &c
|
|
|
|
case recoveryRequestType:
|
2019-11-15 10:32:40 +00:00
|
|
|
return &recoveryRequest{timestamp: rand.Uint32()}
|
2019-11-08 15:40:21 +00:00
|
|
|
case recoveryMessageType:
|
2020-06-23 06:41:53 +00:00
|
|
|
return randomRecoveryMessage(t, srEnabled...)
|
2019-11-08 15:40:21 +00:00
|
|
|
default:
|
|
|
|
require.Fail(t, "invalid type")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
func randomPrepareRequest(t *testing.T, srEnabled ...bool) *prepareRequest {
|
2019-11-08 15:40:21 +00:00
|
|
|
const txCount = 3
|
|
|
|
|
|
|
|
req := &prepareRequest{
|
2019-11-15 10:32:40 +00:00
|
|
|
timestamp: rand.Uint32(),
|
|
|
|
nonce: rand.Uint64(),
|
|
|
|
transactionHashes: make([]util.Uint256, txCount),
|
|
|
|
minerTx: *newMinerTx(rand.Uint32()),
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
req.transactionHashes[0] = req.minerTx.Hash()
|
2019-11-08 15:40:21 +00:00
|
|
|
for i := 1; i < txCount; i++ {
|
2020-03-27 07:14:40 +00:00
|
|
|
req.transactionHashes[i] = random.Uint256()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
2020-03-27 07:14:40 +00:00
|
|
|
req.nextConsensus = random.Uint160()
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
if len(srEnabled) > 0 && srEnabled[0] {
|
|
|
|
req.stateRootEnabled = true
|
|
|
|
req.proposalStateRoot.Index = rand.Uint32()
|
|
|
|
req.proposalStateRoot.PrevHash = random.Uint256()
|
|
|
|
req.proposalStateRoot.Root = random.Uint256()
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
func randomRecoveryMessage(t *testing.T, srEnabled ...bool) *recoveryMessage {
|
|
|
|
result := randomMessage(t, prepareRequestType, srEnabled...)
|
2019-11-08 15:40:21 +00:00
|
|
|
require.IsType(t, (*prepareRequest)(nil), result)
|
|
|
|
prepReq := result.(*prepareRequest)
|
|
|
|
|
2020-06-23 06:41:53 +00:00
|
|
|
rec := &recoveryMessage{
|
2019-11-15 10:32:40 +00:00
|
|
|
preparationPayloads: []*preparationCompact{
|
2019-11-08 15:40:21 +00:00
|
|
|
{
|
|
|
|
ValidatorIndex: 1,
|
2020-03-27 07:14:40 +00:00
|
|
|
InvocationScript: random.Bytes(10),
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-15 10:32:40 +00:00
|
|
|
commitPayloads: []*commitCompact{
|
2019-11-08 15:40:21 +00:00
|
|
|
{
|
|
|
|
ViewNumber: 0,
|
|
|
|
ValidatorIndex: 1,
|
2019-11-15 10:32:40 +00:00
|
|
|
Signature: [64]byte{1, 2, 3},
|
2020-03-27 07:14:40 +00:00
|
|
|
InvocationScript: random.Bytes(20),
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ViewNumber: 0,
|
|
|
|
ValidatorIndex: 2,
|
2019-11-15 10:32:40 +00:00
|
|
|
Signature: [64]byte{11, 3, 4, 98},
|
2020-03-27 07:14:40 +00:00
|
|
|
InvocationScript: random.Bytes(10),
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-15 10:32:40 +00:00
|
|
|
changeViewPayloads: []*changeViewCompact{
|
2019-11-08 15:40:21 +00:00
|
|
|
{
|
|
|
|
Timestamp: rand.Uint32(),
|
|
|
|
ValidatorIndex: 3,
|
|
|
|
OriginalViewNumber: 3,
|
2020-03-27 07:14:40 +00:00
|
|
|
InvocationScript: random.Bytes(4),
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-18 12:12:20 +00:00
|
|
|
prepareRequest: &message{
|
|
|
|
Type: prepareRequestType,
|
|
|
|
payload: prepReq,
|
|
|
|
},
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
2020-06-23 06:41:53 +00:00
|
|
|
if len(srEnabled) > 0 && srEnabled[0] {
|
|
|
|
rec.stateRootEnabled = true
|
|
|
|
rec.prepareRequest.stateRootEnabled = true
|
|
|
|
for _, c := range rec.commitPayloads {
|
|
|
|
c.stateRootEnabled = true
|
|
|
|
random.Fill(c.StateSignature[:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rec
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
func TestPayload_Sign(t *testing.T) {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
priv := &privateKey{key}
|
|
|
|
p := randomPayload(t, prepareRequestType)
|
2019-12-16 08:57:49 +00:00
|
|
|
require.False(t, p.Verify(util.Uint160{}))
|
2019-11-15 10:32:40 +00:00
|
|
|
require.NoError(t, p.Sign(priv))
|
2019-12-16 08:57:49 +00:00
|
|
|
require.True(t, p.Verify(p.Witness.ScriptHash()))
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
func TestMessageType_String(t *testing.T) {
|
|
|
|
require.Equal(t, "ChangeView", changeViewType.String())
|
|
|
|
require.Equal(t, "PrepareRequest", prepareRequestType.String())
|
|
|
|
require.Equal(t, "PrepareResponse", prepareResponseType.String())
|
|
|
|
require.Equal(t, "Commit", commitType.String())
|
|
|
|
require.Equal(t, "RecoveryMessage", recoveryMessageType.String())
|
|
|
|
require.Equal(t, "RecoveryRequest", recoveryRequestType.String())
|
|
|
|
require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMinerTx(nonce uint32) *transaction.Transaction {
|
|
|
|
return &transaction.Transaction{
|
|
|
|
Type: transaction.MinerType,
|
|
|
|
Version: 0,
|
|
|
|
Data: &transaction.MinerTX{
|
|
|
|
Nonce: rand.Uint32(),
|
|
|
|
},
|
2019-12-09 14:14:10 +00:00
|
|
|
Attributes: []transaction.Attribute{},
|
|
|
|
Inputs: []transaction.Input{},
|
|
|
|
Outputs: []transaction.Output{},
|
|
|
|
Scripts: []transaction.Witness{},
|
2019-11-08 15:40:21 +00:00
|
|
|
Trimmed: false,
|
|
|
|
}
|
|
|
|
}
|