2019-11-08 15:40:21 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2021-02-11 15:25:44 +00:00
|
|
|
"encoding/hex"
|
|
|
|
gio "io"
|
2019-11-08 15:40:21 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2024-03-21 19:49:39 +00:00
|
|
|
"github.com/nspcc-dev/dbft"
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2021-02-11 15:25:44 +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/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-01-14 11:17:00 +00:00
|
|
|
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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
|
|
|
|
|
2021-01-14 11:17:00 +00:00
|
|
|
//p.SetVersion(1)
|
|
|
|
//assert.EqualValues(t, 1, p.Version())
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2021-01-14 11:17:00 +00:00
|
|
|
//p.SetPrevHash(util.Uint256{1, 2, 3})
|
|
|
|
//assert.Equal(t, util.Uint256{1, 2, 3}, p.PrevHash())
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
|
2024-03-21 19:49:39 +00:00
|
|
|
p.SetType(dbft.PrepareRequestType)
|
|
|
|
assert.Equal(t, dbft.PrepareRequestType, p.Type())
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
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_Serializable(t *testing.T) {
|
|
|
|
for _, mt := range messageTypes {
|
|
|
|
p := randomPayload(t, mt)
|
2021-03-25 18:59:54 +00:00
|
|
|
actual := &Payload{Extensible: npayload.Extensible{}, network: netmode.UnitTestNet}
|
2020-04-15 15:56:45 +00:00
|
|
|
data, err := testserdes.EncodeBinary(p)
|
|
|
|
require.NoError(t, err)
|
2021-01-14 11:17:00 +00:00
|
|
|
require.NoError(t, testserdes.DecodeBinary(data, &actual.Extensible))
|
2020-04-15 15:56:45 +00:00
|
|
|
assert.NoError(t, actual.decodeData())
|
|
|
|
require.Equal(t, p, actual)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:25:44 +00:00
|
|
|
func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
|
|
|
|
// PrepareResponse payload consists of:
|
|
|
|
// - 1-byte message type (PrepareResponse)
|
|
|
|
// - 4-byte block index
|
|
|
|
// - 1-byte validator index
|
|
|
|
// - 1-byte view number
|
|
|
|
// - 32-byte preparation hash
|
|
|
|
const (
|
|
|
|
typeIndex = 0
|
|
|
|
size = 39
|
|
|
|
)
|
|
|
|
|
|
|
|
buf := make([]byte, size)
|
|
|
|
expected := message{
|
|
|
|
Type: prepareResponseType,
|
|
|
|
payload: &prepareResponse{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid payload
|
|
|
|
buf[typeIndex] = byte(prepareResponseType)
|
|
|
|
p := &Payload{Extensible: npayload.Extensible{Data: buf}}
|
|
|
|
require.NoError(t, p.decodeData())
|
|
|
|
require.Equal(t, expected, p.message)
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
buf[typeIndex] = 0xFF
|
|
|
|
p = &Payload{Extensible: npayload.Extensible{Data: buf}}
|
|
|
|
require.Error(t, p.decodeData())
|
|
|
|
|
|
|
|
// invalid length
|
|
|
|
buf[typeIndex] = byte(prepareResponseType)
|
|
|
|
p = &Payload{Extensible: npayload.Extensible{Data: buf[:len(buf)-1]}}
|
|
|
|
require.Error(t, p.decodeData())
|
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
func TestCommit_Serializable(t *testing.T) {
|
|
|
|
c := randomMessage(t, commitType)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, c, new(commit))
|
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) {
|
|
|
|
req := randomMessage(t, prepareRequestType)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, req, new(prepareRequest))
|
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) {
|
|
|
|
msg := randomMessage(t, recoveryMessageType)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, msg, new(recoveryMessage))
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randomPayload(t *testing.T, mt messageType) *Payload {
|
|
|
|
p := &Payload{
|
2021-01-14 11:17:00 +00:00
|
|
|
message: message{
|
|
|
|
Type: mt,
|
|
|
|
ValidatorIndex: byte(rand.Uint32()),
|
|
|
|
BlockIndex: rand.Uint32(),
|
|
|
|
ViewNumber: byte(rand.Uint32()),
|
|
|
|
payload: randomMessage(t, mt),
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
2021-01-14 11:17:00 +00:00
|
|
|
Extensible: npayload.Extensible{
|
|
|
|
Witness: transaction.Witness{
|
|
|
|
InvocationScript: random.Bytes(3),
|
|
|
|
VerificationScript: []byte{byte(opcode.PUSH0)},
|
|
|
|
},
|
2019-11-08 15:40:21 +00:00
|
|
|
},
|
2021-03-25 18:59:54 +00:00
|
|
|
network: netmode.UnitTestNet,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomMessage(t *testing.T, mt messageType) io.Serializable {
|
|
|
|
switch mt {
|
|
|
|
case changeViewType:
|
|
|
|
return &changeView{
|
2020-07-11 07:48:25 +00:00
|
|
|
timestamp: rand.Uint64(),
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
case prepareRequestType:
|
|
|
|
return randomPrepareRequest(t)
|
|
|
|
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[:])
|
2019-11-08 15:40:21 +00:00
|
|
|
return &c
|
|
|
|
case recoveryRequestType:
|
2020-07-11 07:48:25 +00:00
|
|
|
return &recoveryRequest{timestamp: rand.Uint64()}
|
2019-11-08 15:40:21 +00:00
|
|
|
case recoveryMessageType:
|
|
|
|
return randomRecoveryMessage(t)
|
|
|
|
default:
|
|
|
|
require.Fail(t, "invalid type")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomPrepareRequest(t *testing.T) *prepareRequest {
|
|
|
|
const txCount = 3
|
|
|
|
|
|
|
|
req := &prepareRequest{
|
2020-04-21 11:26:57 +00:00
|
|
|
timestamp: rand.Uint64(),
|
2019-11-15 10:32:40 +00:00
|
|
|
transactionHashes: make([]util.Uint256, txCount),
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:42:38 +00:00
|
|
|
for i := 0; i < txCount; i++ {
|
2020-03-27 07:14:40 +00:00
|
|
|
req.transactionHashes[i] = random.Uint256()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomRecoveryMessage(t *testing.T) *recoveryMessage {
|
|
|
|
result := randomMessage(t, prepareRequestType)
|
|
|
|
require.IsType(t, (*prepareRequest)(nil), result)
|
|
|
|
prepReq := result.(*prepareRequest)
|
|
|
|
|
|
|
|
return &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,
|
2022-10-05 07:45:52 +00:00
|
|
|
Signature: [keys.SignatureLen]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,
|
2022-10-05 07:45:52 +00:00
|
|
|
Signature: [keys.SignatureLen]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
|
|
|
{
|
2020-07-11 07:48:25 +00:00
|
|
|
Timestamp: rand.Uint64(),
|
2019-11-08 15:40:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
2020-08-20 08:02:11 +00:00
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
p := randomPayload(t, prepareRequestType)
|
2020-08-20 08:02:11 +00:00
|
|
|
h := priv.PublicKey().GetScriptHash()
|
2020-11-17 12:57:50 +00:00
|
|
|
bc := newTestChain(t, false)
|
2021-10-25 14:42:20 +00:00
|
|
|
_, err = bc.VerifyWitness(h, p, &p.Witness, payloadGasLimit)
|
|
|
|
require.Error(t, err)
|
2019-11-15 10:32:40 +00:00
|
|
|
require.NoError(t, p.Sign(priv))
|
2021-10-25 14:42:20 +00:00
|
|
|
_, err = bc.VerifyWitness(h, p, &p.Witness, payloadGasLimit)
|
|
|
|
require.NoError(t, err)
|
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())
|
|
|
|
}
|
2020-08-18 12:19:24 +00:00
|
|
|
|
2021-02-11 15:25:44 +00:00
|
|
|
func TestPayload_DecodeFromPrivnet(t *testing.T) {
|
|
|
|
hexDump := "0464424654000000000200000018ca088345a4926bcc9a1daccfba0ac4436082a847300200000003003c57d952539c5e0c39a83c0de5744a772c0dcb0e8ccd7c5bba27ef" +
|
|
|
|
"498506cd860cdfd01ad215b251ab64dc64cd544a6f453f3b0128ddc98d95ac15915dbe6f6301420c40b39c9136af3b8186409dec2dbd31d0fd4f3e637b3eeb96d8556b41f8512dd25d91134f62a6c293db089b7e82b7a0fd23bf9a1" +
|
|
|
|
"5ee26c42a5738b913beef74176d290c2103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee6990b4195440d78"
|
|
|
|
data, err := hex.DecodeString(hexDump)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buf := io.NewBinReaderFromBuf(data)
|
|
|
|
p := NewPayload(netmode.PrivNet, false)
|
|
|
|
p.DecodeBinary(buf)
|
|
|
|
require.NoError(t, buf.Err)
|
2024-03-21 19:49:39 +00:00
|
|
|
require.Equal(t, dbft.CommitType, p.Type())
|
2021-02-11 15:25:44 +00:00
|
|
|
require.Equal(t, uint32(2), p.Height())
|
|
|
|
require.Equal(t, uint16(3), p.ValidatorIndex())
|
|
|
|
require.Equal(t, byte(0), p.ViewNumber())
|
|
|
|
require.NotNil(t, p.message.payload)
|
|
|
|
|
|
|
|
buf.ReadB()
|
|
|
|
require.Equal(t, gio.EOF, buf.Err)
|
|
|
|
}
|