github.com/koko1123/flow-go-1@v0.29.6/consensus/hotstuff/verification/staking_signer_test.go (about) 1 package verification 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/mock" 8 "github.com/stretchr/testify/require" 9 10 "github.com/koko1123/flow-go-1/consensus/hotstuff/helper" 11 "github.com/koko1123/flow-go-1/consensus/hotstuff/model" 12 "github.com/koko1123/flow-go-1/model/flow" 13 "github.com/koko1123/flow-go-1/module/local" 14 modulemock "github.com/koko1123/flow-go-1/module/mock" 15 "github.com/koko1123/flow-go-1/utils/unittest" 16 ) 17 18 // TestStakingSigner_CreateProposal verifies that StakingSigner can produce correctly signed proposal 19 // that can be verified later using StakingVerifier. 20 // Additionally, we check cases where errors during signing are happening. 21 func TestStakingSigner_CreateProposal(t *testing.T) { 22 stakingPriv := unittest.StakingPrivKeyFixture() 23 signer := unittest.IdentityFixture() 24 signerID := signer.NodeID 25 signer.StakingPubKey = stakingPriv.PublicKey() 26 27 t.Run("invalid-signer-id", func(t *testing.T) { 28 me := &modulemock.Local{} 29 me.On("NodeID").Return(signerID) 30 signer := NewStakingSigner(me) 31 32 block := helper.MakeBlock() 33 proposal, err := signer.CreateProposal(block) 34 require.Error(t, err) 35 require.Nil(t, proposal) 36 }) 37 t.Run("could-not-sign", func(t *testing.T) { 38 signException := errors.New("sign-exception") 39 me := &modulemock.Local{} 40 me.On("NodeID").Return(signerID) 41 me.On("Sign", mock.Anything, mock.Anything).Return(nil, signException).Once() 42 signer := NewStakingSigner(me) 43 44 block := helper.MakeBlock() 45 proposal, err := signer.CreateProposal(block) 46 require.ErrorAs(t, err, &signException) 47 require.Nil(t, proposal) 48 }) 49 t.Run("created-proposal", func(t *testing.T) { 50 me, err := local.New(signer, stakingPriv) 51 require.NoError(t, err) 52 53 signerIdentity := unittest.IdentityFixture(unittest.WithNodeID(signerID), 54 unittest.WithStakingPubKey(stakingPriv.PublicKey())) 55 56 signer := NewStakingSigner(me) 57 58 block := helper.MakeBlock(helper.WithBlockProposer(signerID)) 59 proposal, err := signer.CreateProposal(block) 60 require.NoError(t, err) 61 require.NotNil(t, proposal) 62 63 verifier := NewStakingVerifier() 64 err = verifier.VerifyVote(signerIdentity, proposal.SigData, proposal.Block) 65 require.NoError(t, err) 66 }) 67 } 68 69 // TestStakingSigner_CreateVote verifies that StakingSigner can produce correctly signed vote 70 // that can be verified later using StakingVerifier. 71 // Additionally, we check cases where errors during signing are happening. 72 func TestStakingSigner_CreateVote(t *testing.T) { 73 stakingPriv := unittest.StakingPrivKeyFixture() 74 signer := unittest.IdentityFixture() 75 signer.StakingPubKey = stakingPriv.PublicKey() 76 signerID := signer.NodeID 77 78 t.Run("could-not-sign", func(t *testing.T) { 79 signException := errors.New("sign-exception") 80 me := &modulemock.Local{} 81 me.On("NodeID").Return(signerID) 82 me.On("Sign", mock.Anything, mock.Anything).Return(nil, signException).Once() 83 signer := NewStakingSigner(me) 84 85 block := helper.MakeBlock() 86 proposal, err := signer.CreateProposal(block) 87 require.ErrorAs(t, err, &signException) 88 require.Nil(t, proposal) 89 }) 90 t.Run("created-vote", func(t *testing.T) { 91 me, err := local.New(signer, stakingPriv) 92 require.NoError(t, err) 93 94 signerIdentity := unittest.IdentityFixture(unittest.WithNodeID(signerID), 95 unittest.WithStakingPubKey(stakingPriv.PublicKey())) 96 97 signer := NewStakingSigner(me) 98 99 block := helper.MakeBlock(helper.WithBlockProposer(signerID)) 100 vote, err := signer.CreateVote(block) 101 require.NoError(t, err) 102 require.NotNil(t, vote) 103 104 verifier := NewStakingVerifier() 105 err = verifier.VerifyVote(signerIdentity, vote.SigData, block) 106 require.NoError(t, err) 107 }) 108 } 109 110 // TestStakingSigner_VerifyQC checks that a QC without any signers is rejected right away without calling into any sub-components 111 func TestStakingSigner_VerifyQC(t *testing.T) { 112 header := unittest.BlockHeaderFixture() 113 block := model.BlockFromFlow(header, header.View-1) 114 sigData := unittest.RandomBytes(127) 115 116 verifier := NewStakingVerifier() 117 err := verifier.VerifyQC([]*flow.Identity{}, sigData, block) 118 require.True(t, model.IsInvalidFormatError(err)) 119 120 err = verifier.VerifyQC(nil, sigData, block) 121 require.True(t, model.IsInvalidFormatError(err)) 122 }