2019-12-02 14:00:17 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
"github.com/nspcc-dev/dbft/crypto"
|
2019-12-02 14:00:17 +00:00
|
|
|
"github.com/nspcc-dev/dbft/payload"
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testchain"
|
2020-08-18 12:19:24 +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/util"
|
2019-12-02 14:00:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
func TestRecoveryMessageSetters(t *testing.T) {
|
|
|
|
t.Run("NoStateRoot", func(t *testing.T) {
|
|
|
|
testRecoveryMessageSetters(t, false)
|
|
|
|
})
|
|
|
|
t.Run("WithStateRoot", func(t *testing.T) {
|
|
|
|
testRecoveryMessageSetters(t, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
|
|
|
|
srv := newTestServiceWithState(t, enableStateRoot)
|
2020-08-20 08:02:11 +00:00
|
|
|
privs := make([]*privateKey, testchain.Size())
|
|
|
|
pubs := make([]crypto.PublicKey, testchain.Size())
|
|
|
|
for i := 0; i < testchain.Size(); i++ {
|
|
|
|
privs[i], pubs[i] = getTestValidator(i)
|
2019-12-02 15:32:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 11:17:00 +00:00
|
|
|
const msgHeight = 10
|
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
r := &recoveryMessage{stateRootEnabled: enableStateRoot}
|
|
|
|
p := NewPayload(netmode.UnitTestNet, enableStateRoot)
|
2019-12-02 14:00:17 +00:00
|
|
|
p.SetType(payload.RecoveryMessageType)
|
2021-01-14 11:17:00 +00:00
|
|
|
p.SetHeight(msgHeight)
|
2019-12-02 14:00:17 +00:00
|
|
|
p.SetPayload(r)
|
2019-12-02 15:32:14 +00:00
|
|
|
// sign payload to have verification script
|
|
|
|
require.NoError(t, p.Sign(privs[0]))
|
2019-12-02 14:00:17 +00:00
|
|
|
|
|
|
|
req := &prepareRequest{
|
|
|
|
timestamp: 87,
|
|
|
|
transactionHashes: []util.Uint256{{1}},
|
2020-11-17 12:57:50 +00:00
|
|
|
stateRootEnabled: enableStateRoot,
|
2019-12-02 14:00:17 +00:00
|
|
|
}
|
2020-11-17 12:57:50 +00:00
|
|
|
p1 := NewPayload(netmode.UnitTestNet, enableStateRoot)
|
2019-12-02 14:00:17 +00:00
|
|
|
p1.SetType(payload.PrepareRequestType)
|
2021-01-14 11:17:00 +00:00
|
|
|
p1.SetHeight(msgHeight)
|
2019-12-02 14:00:17 +00:00
|
|
|
p1.SetPayload(req)
|
2019-12-02 15:32:14 +00:00
|
|
|
p1.SetValidatorIndex(0)
|
2021-01-14 11:17:00 +00:00
|
|
|
p1.Sender = privs[0].GetScriptHash()
|
2019-12-02 15:32:14 +00:00
|
|
|
require.NoError(t, p1.Sign(privs[0]))
|
2019-12-02 14:00:17 +00:00
|
|
|
|
|
|
|
t.Run("prepare response is added", func(t *testing.T) {
|
2020-11-17 12:57:50 +00:00
|
|
|
p2 := NewPayload(netmode.UnitTestNet, enableStateRoot)
|
2019-12-02 14:00:17 +00:00
|
|
|
p2.SetType(payload.PrepareResponseType)
|
2021-01-14 11:17:00 +00:00
|
|
|
p2.SetHeight(msgHeight)
|
2019-12-02 14:00:17 +00:00
|
|
|
p2.SetPayload(&prepareResponse{
|
|
|
|
preparationHash: p1.Hash(),
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
p2.SetValidatorIndex(1)
|
2021-01-14 11:17:00 +00:00
|
|
|
p2.Sender = privs[1].GetScriptHash()
|
2019-12-02 15:32:14 +00:00
|
|
|
require.NoError(t, p2.Sign(privs[1]))
|
2019-12-02 14:00:17 +00:00
|
|
|
|
|
|
|
r.AddPayload(p2)
|
|
|
|
require.NotNil(t, r.PreparationHash())
|
|
|
|
require.Equal(t, p1.Hash(), *r.PreparationHash())
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
ps := r.GetPrepareResponses(p, pubs)
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Len(t, ps, 1)
|
2020-08-29 15:44:45 +00:00
|
|
|
// Update hashes and serialized data.
|
|
|
|
_ = ps[0].Hash()
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Equal(t, p2, ps[0])
|
2019-12-16 08:57:49 +00:00
|
|
|
ps0 := ps[0].(*Payload)
|
2020-08-20 08:02:11 +00:00
|
|
|
require.True(t, srv.validatePayload(ps0))
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("prepare request is added", func(t *testing.T) {
|
2019-12-02 15:32:14 +00:00
|
|
|
pr := r.GetPrepareRequest(p, pubs, p1.ValidatorIndex())
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Nil(t, pr)
|
|
|
|
|
|
|
|
r.AddPayload(p1)
|
2019-12-02 15:32:14 +00:00
|
|
|
pr = r.GetPrepareRequest(p, pubs, p1.ValidatorIndex())
|
2019-12-02 14:00:17 +00:00
|
|
|
require.NotNil(t, pr)
|
2020-04-15 15:56:45 +00:00
|
|
|
require.Equal(t, p1.Hash(), pr.Hash())
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Equal(t, p1, pr)
|
2019-12-16 08:57:49 +00:00
|
|
|
|
|
|
|
pl := pr.(*Payload)
|
2020-08-20 08:02:11 +00:00
|
|
|
require.True(t, srv.validatePayload(pl))
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("change view is added", func(t *testing.T) {
|
2020-11-17 12:57:50 +00:00
|
|
|
p3 := NewPayload(netmode.UnitTestNet, enableStateRoot)
|
2019-12-02 14:00:17 +00:00
|
|
|
p3.SetType(payload.ChangeViewType)
|
2021-01-14 11:17:00 +00:00
|
|
|
p3.SetHeight(msgHeight)
|
2019-12-02 14:00:17 +00:00
|
|
|
p3.SetPayload(&changeView{
|
|
|
|
newViewNumber: 1,
|
|
|
|
timestamp: 12345,
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
p3.SetValidatorIndex(3)
|
2021-01-14 11:17:00 +00:00
|
|
|
p3.Sender = privs[3].GetScriptHash()
|
2019-12-02 15:32:14 +00:00
|
|
|
require.NoError(t, p3.Sign(privs[3]))
|
2019-12-02 14:00:17 +00:00
|
|
|
|
|
|
|
r.AddPayload(p3)
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
ps := r.GetChangeViews(p, pubs)
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Len(t, ps, 1)
|
2020-08-29 15:44:45 +00:00
|
|
|
// update hashes and serialized data.
|
|
|
|
_ = ps[0].Hash()
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Equal(t, p3, ps[0])
|
2019-12-16 08:57:49 +00:00
|
|
|
|
|
|
|
ps0 := ps[0].(*Payload)
|
2020-08-20 08:02:11 +00:00
|
|
|
require.True(t, srv.validatePayload(ps0))
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("commit is added", func(t *testing.T) {
|
2020-11-17 12:57:50 +00:00
|
|
|
p4 := NewPayload(netmode.UnitTestNet, enableStateRoot)
|
2019-12-02 14:00:17 +00:00
|
|
|
p4.SetType(payload.CommitType)
|
2021-01-14 11:17:00 +00:00
|
|
|
p4.SetHeight(msgHeight)
|
2019-12-02 14:00:17 +00:00
|
|
|
p4.SetPayload(randomMessage(t, commitType))
|
2020-08-20 08:02:11 +00:00
|
|
|
p4.SetValidatorIndex(3)
|
2021-01-14 11:17:00 +00:00
|
|
|
p4.Sender = privs[3].GetScriptHash()
|
2020-08-20 08:02:11 +00:00
|
|
|
require.NoError(t, p4.Sign(privs[3]))
|
2019-12-02 14:00:17 +00:00
|
|
|
|
|
|
|
r.AddPayload(p4)
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
ps := r.GetCommits(p, pubs)
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Len(t, ps, 1)
|
2020-08-29 15:44:45 +00:00
|
|
|
// update hashes and serialized data.
|
|
|
|
_ = ps[0].Hash()
|
2019-12-02 14:00:17 +00:00
|
|
|
require.Equal(t, p4, ps[0])
|
2019-12-16 08:57:49 +00:00
|
|
|
|
|
|
|
ps0 := ps[0].(*Payload)
|
2020-08-20 08:02:11 +00:00
|
|
|
require.True(t, srv.validatePayload(ps0))
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
}
|
2019-12-02 15:32:14 +00:00
|
|
|
|
2020-08-23 13:44:56 +00:00
|
|
|
/*
|
2020-08-18 12:19:24 +00:00
|
|
|
func TestRecoveryMessage_Decode(t *testing.T) {
|
|
|
|
hexDump := "000000007f5b6094e1281e6bac667f1f871aee755dbe62c012868c718d7709de62135d250d1800000100fd0f024100000120003db64b5e000000008e4ab7138abe65a30133175ebcf3c66ad59ed2c532ca19bbb84cb3802f7dc9b6decde10e117ff6fc3303000041e52280e60c46778876e4c7fdcd262170d906090256ff2ac11d14d45516dd465b5b8f241ff78096ee7280f226df677681bff091884dcd7c4f25cd9a61856ce0bc6a01004136b0b971ef320135f61c11475ff07c5cad04635fc1dad41d346d085646e29e6ff1c5181421a203e5d4b627c6bacdd78a78c9f4cb0a749877ea5a9ed2b02196f17f020041ac5e279927ded591c234391078db55cad2ada58bded974fa2d2751470d0b2f94dddc84ed312f31ee960c884066f778e000f4f05883c74defa75d2a2eb524359c7d020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041546d2e34cbbfd0d09b7ce937eec07b402bd597f7bef24938f4a01041f443fb4dd31bebcabdaae3942bb9d549724a152e851bee43ebc5f482ddd9316f2690b48e7d00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000415281a6579b875c480d9b3cc9144485d1f898e13405eaf1e4117d83844f1265f81a71998d53fa32d6c3b5249446ac036ecda73b1fe8c1341475fcc4b8d6ba8ec6e20141d775fd1a605173a8ed02084fef903ee043239ca4c76cb658809c6216031437e8f4d5a265550d5934fe386732364d9b49a14baef5a1236d02c557cb394a3a0873c82364f65259a991768a35ba18777f76901e1022f87d71910f4e3e46f161299401f2074d0c"
|
2020-02-18 12:12:20 +00:00
|
|
|
data, err := hex.DecodeString(hexDump)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buf := io.NewBinReaderFromBuf(data)
|
2020-08-18 12:19:24 +00:00
|
|
|
p := NewPayload(netmode.TestNet)
|
2020-02-18 12:12:20 +00:00
|
|
|
p.DecodeBinary(buf)
|
|
|
|
require.NoError(t, buf.Err)
|
2020-08-18 12:19:24 +00:00
|
|
|
require.NoError(t, p.decodeData())
|
|
|
|
require.Equal(t, payload.RecoveryMessageType, p.Type())
|
|
|
|
require.NotNil(t, p.message.payload)
|
|
|
|
req := p.message.payload.(*recoveryMessage).prepareRequest
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.Equal(t, prepareRequestType, p.message.payload.(*recoveryMessage).prepareRequest.Type)
|
2020-02-18 12:12:20 +00:00
|
|
|
|
|
|
|
buf.ReadB()
|
|
|
|
require.Equal(t, gio.EOF, buf.Err)
|
|
|
|
}
|
2020-08-23 13:44:56 +00:00
|
|
|
*/
|