Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion modules/network/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,18 @@ func (k msgServer) Attest(goCtx context.Context, msg *types.MsgAttest) (*types.M
return nil, sdkerr.Wrapf(sdkerrors.ErrInvalidRequest, "consensus address %s already attested for height %d", msg.ConsensusAddress, msg.Height)
}

// TODO: Verify the vote signature here once we implement vote parsing
// Validate vote payload is non-empty and meets minimum signature length.
// A valid vote must contain at least a cryptographic signature (48 bytes for BLS,
// 64 bytes for Ed25519). We enforce the minimum here; full cryptographic
// verification of the signature against the block data should be added once
// the vote format is finalized.
const minVoteLen = 48 // BLS signature minimum
if len(msg.Vote) == 0 {
return nil, sdkerr.Wrap(sdkerrors.ErrInvalidRequest, "vote payload must not be empty")
}
if len(msg.Vote) < minVoteLen {
return nil, sdkerr.Wrapf(sdkerrors.ErrInvalidRequest, "vote payload too short: got %d bytes, minimum %d", len(msg.Vote), minVoteLen)
}
Comment thread
alpe marked this conversation as resolved.
Outdated
Comment thread
alpe marked this conversation as resolved.
Outdated
Comment thread
alpe marked this conversation as resolved.

// Set the bit
k.bitmapHelper.SetBit(bitmap, int(index))
Expand Down
66 changes: 66 additions & 0 deletions modules/network/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,72 @@
}
}

func TestAttestVotePayloadValidation(t *testing.T) {
myValAddr := sdk.ValAddress("validator1")

specs := map[string]struct {
vote []byte
expErr error
}{
"empty vote rejected": {
vote: []byte{},
expErr: sdkerrors.ErrInvalidRequest,
},
"nil vote rejected": {
vote: nil,
expErr: sdkerrors.ErrInvalidRequest,
},
"short vote rejected": {
vote: make([]byte, 47),
expErr: sdkerrors.ErrInvalidRequest,
},
"min-length vote accepted": {
vote: make([]byte, 48),
},
"valid-length vote accepted": {
vote: make([]byte, 96),
},
}

for name, spec := range specs {
t.Run(name, func(t *testing.T) {
sk := NewMockStakingKeeper()
cdc := moduletestutil.MakeTestEncodingConfig().Codec
keys := storetypes.NewKVStoreKeys(types.StoreKey)
logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)
authority := authtypes.NewModuleAddress("gov")
keeper := NewKeeper(cdc, runtime.NewKVStoreService(keys[types.StoreKey]), sk, nil, nil, authority.String())
server := msgServer{Keeper: keeper}
ctx := sdk.NewContext(cms, cmtproto.Header{
ChainID: "test-chain",
Time: time.Now().UTC(),
Height: 10,
}, false, logger).WithContext(t.Context())

keeper.SetParams(ctx, types.DefaultParams())

Check failure on line 170 in modules/network/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

Error return value of `keeper.SetParams` is not checked (errcheck)
require.NoError(t, keeper.SetAttesterSetMember(ctx, myValAddr.String()))
require.NoError(t, keeper.BuildValidatorIndexMap(ctx))

msg := &types.MsgAttest{
Authority: myValAddr.String(),
ConsensusAddress: myValAddr.String(),
Height: 10,
Vote: spec.vote,
}

rsp, err := server.Attest(ctx, msg)
if spec.expErr != nil {
require.ErrorIs(t, err, spec.expErr)
require.Nil(t, rsp)
return
}
require.NoError(t, err)
require.NotNil(t, rsp)
})
}
}

var _ types.StakingKeeper = &MockStakingKeeper{}

type MockStakingKeeper struct {
Expand Down
Loading