2019-12-02 14:00:17 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-12-02 14:00:17 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
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"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecoveryMessage_Setters(t *testing.T) {
|
2019-12-02 15:32:14 +00:00
|
|
|
const size = 5
|
|
|
|
|
|
|
|
privs := getKeys(t, size)
|
|
|
|
pubs := make([]crypto.PublicKey, 5)
|
|
|
|
for i := range pubs {
|
|
|
|
pubs[i] = &publicKey{privs[i].PublicKey()}
|
|
|
|
}
|
|
|
|
|
2019-12-02 14:00:17 +00:00
|
|
|
r := &recoveryMessage{}
|
|
|
|
p := new(Payload)
|
|
|
|
p.SetType(payload.RecoveryMessageType)
|
|
|
|
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,
|
|
|
|
nonce: 321,
|
|
|
|
transactionHashes: []util.Uint256{{1}},
|
|
|
|
minerTx: *newMinerTx(123),
|
|
|
|
nextConsensus: util.Uint160{1, 2},
|
|
|
|
}
|
|
|
|
p1 := new(Payload)
|
|
|
|
p1.SetType(payload.PrepareRequestType)
|
|
|
|
p1.SetPayload(req)
|
2019-12-02 15:32:14 +00:00
|
|
|
p1.SetValidatorIndex(0)
|
|
|
|
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) {
|
|
|
|
p2 := new(Payload)
|
|
|
|
p2.SetType(payload.PrepareResponseType)
|
|
|
|
p2.SetPayload(&prepareResponse{
|
|
|
|
preparationHash: p1.Hash(),
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
p2.SetValidatorIndex(1)
|
|
|
|
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)
|
|
|
|
require.Equal(t, p2, ps[0])
|
2019-12-02 15:32:14 +00:00
|
|
|
require.True(t, ps[0].(*Payload).Verify())
|
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)
|
|
|
|
require.Equal(t, p1, pr)
|
2019-12-02 15:32:14 +00:00
|
|
|
require.True(t, pr.(*Payload).Verify())
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("change view is added", func(t *testing.T) {
|
|
|
|
p3 := new(Payload)
|
|
|
|
p3.SetType(payload.ChangeViewType)
|
|
|
|
p3.SetPayload(&changeView{
|
|
|
|
newViewNumber: 1,
|
|
|
|
timestamp: 12345,
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
p3.SetValidatorIndex(3)
|
|
|
|
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)
|
|
|
|
require.Equal(t, p3, ps[0])
|
2019-12-02 15:32:14 +00:00
|
|
|
require.True(t, ps[0].(*Payload).Verify())
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("commit is added", func(t *testing.T) {
|
|
|
|
p4 := new(Payload)
|
|
|
|
p4.SetType(payload.CommitType)
|
|
|
|
p4.SetPayload(randomMessage(t, commitType))
|
2019-12-02 15:32:14 +00:00
|
|
|
p4.SetValidatorIndex(4)
|
|
|
|
require.NoError(t, p4.Sign(privs[4]))
|
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)
|
|
|
|
require.Equal(t, p4, ps[0])
|
2019-12-02 15:32:14 +00:00
|
|
|
require.True(t, ps[0].(*Payload).Verify())
|
2019-12-02 14:00:17 +00:00
|
|
|
})
|
|
|
|
}
|
2019-12-02 15:32:14 +00:00
|
|
|
|
|
|
|
func getKeys(t *testing.T, n int) []*privateKey {
|
|
|
|
privs := make([]*privateKey, 0, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, priv)
|
|
|
|
|
|
|
|
privs = append(privs, &privateKey{PrivateKey: priv})
|
|
|
|
}
|
|
|
|
|
|
|
|
return privs
|
|
|
|
}
|