From dbf7d953f2354f6d87103f566de06eacb1a30d35 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 11 Feb 2025 17:46:09 +0100 Subject: [PATCH 1/4] chore: add validator params to validatorSet --- internal/consensus/replay_test.go | 1 + internal/consensus/replayer.go | 14 +- .../selectproposer/height_proposer_test.go | 2 +- internal/evidence/pool_test.go | 4 +- internal/evidence/reactor_test.go | 5 +- internal/evidence/verify_test.go | 2 +- internal/state/current_round_state.go | 4 +- internal/state/state.go | 9 +- internal/state/state_test.go | 6 +- internal/test/factory/validator_set.go | 2 +- light/helpers_test.go | 1 + light/provider/http/http.go | 2 +- proto/tendermint/types/params.pb.go | 228 ++++++++++++++---- proto/tendermint/types/params.proto | 12 + rpc/client/evidence_test.go | 2 +- test/e2e/runner/evidence.go | 2 +- test/e2e/tests/validator_test.go | 4 +- types/evidence.go | 6 +- types/evidence_test.go | 3 +- types/generator.go | 2 +- types/params.go | 43 +++- types/params_test.go | 3 +- types/protobuf.go | 9 +- types/protobuf_test.go | 2 +- types/validation_test.go | 2 +- types/validator_set.go | 34 ++- types/validator_set_test.go | 18 +- types/vote_set_test.go | 2 +- 28 files changed, 331 insertions(+), 93 deletions(-) diff --git a/internal/consensus/replay_test.go b/internal/consensus/replay_test.go index 4f12f9d8c..eba868d17 100644 --- a/internal/consensus/replay_test.go +++ b/internal/consensus/replay_test.go @@ -1282,6 +1282,7 @@ func TestHandshakeUpdatesValidators(t *testing.T) { btcjson.LLMQType_5_60, randQuorumHash, true, + nil, ) abciValidatorSetUpdates := types.TM2PB.ValidatorUpdates(vals) app := &initChainApp{vals: &abciValidatorSetUpdates} diff --git a/internal/consensus/replayer.go b/internal/consensus/replayer.go index d950de4b2..4fd5e8645 100644 --- a/internal/consensus/replayer.go +++ b/internal/consensus/replayer.go @@ -344,7 +344,12 @@ func (r *BlockReplayer) execInitChain(ctx context.Context, rs *replayState, stat if len(res.ValidatorSetUpdate.ValidatorUpdates) != 0 { // we replace existing validator with the one from InitChain instead of applying it as a diff - state.Validators = types.NewValidatorSet(nil, nil, quorumType, nil, false) + var valParams *types.ValidatorParams + if res.ConsensusParams != nil && res.ConsensusParams.Validator != nil { + params := types.ValidatorParamsFromProto(res.ConsensusParams.Validator) + valParams = ¶ms + } + state.Validators = types.NewValidatorSet(nil, nil, quorumType, nil, false, valParams) } // we only update state when we are in initial state @@ -401,11 +406,18 @@ func validatorSetUpdateFromGenesis(genDoc *types.GenesisDoc) (*abci.ValidatorSet return nil, fmt.Errorf("blockReplayer blocks error when validating validator: %s", err) } } + + var validatorParams types.ValidatorParams + if genDoc.ConsensusParams != nil { + validatorParams = genDoc.ConsensusParams.Validator + } + validatorSet := types.NewValidatorSetCheckPublicKeys( validators, genDoc.ThresholdPublicKey, genDoc.QuorumType, genDoc.QuorumHash, + &validatorParams, ) err := validatorSet.ValidateBasic() if err != nil { diff --git a/internal/consensus/versioned/selectproposer/height_proposer_test.go b/internal/consensus/versioned/selectproposer/height_proposer_test.go index 9e370078e..248b651da 100644 --- a/internal/consensus/versioned/selectproposer/height_proposer_test.go +++ b/internal/consensus/versioned/selectproposer/height_proposer_test.go @@ -28,7 +28,7 @@ func TestProposerSelection1(t *testing.T) { types.NewTestValidatorGeneratedFromProTxHash(fooProTxHash), types.NewTestValidatorGeneratedFromProTxHash(barProTxHash), types.NewTestValidatorGeneratedFromProTxHash(bazProTxHash), - }, bls12381.GenPrivKey().PubKey(), btcjson.LLMQType_5_60, crypto.RandQuorumHash(), true) + }, bls12381.GenPrivKey().PubKey(), btcjson.LLMQType_5_60, crypto.RandQuorumHash(), true, nil) var proposers []string vs, err := selectproposer.NewProposerSelector(types.ConsensusParams{}, vset, 0, 0, nil, log.NewTestingLogger(t)) diff --git a/internal/evidence/pool_test.go b/internal/evidence/pool_test.go index 34b7aed6d..92b08ade4 100644 --- a/internal/evidence/pool_test.go +++ b/internal/evidence/pool_test.go @@ -130,7 +130,7 @@ func TestAddExpiredEvidence(t *testing.T) { quorumHash = crypto.RandQuorumHash() privval = types.NewMockPVForQuorum(quorumHash) val = privval.ExtractIntoValidator(ctx, quorumHash) - valSet = types.NewValidatorSet([]*types.Validator{val}, val.PubKey, btcjson.LLMQType_5_60, quorumHash, true) + valSet = types.NewValidatorSet([]*types.Validator{val}, val.PubKey, btcjson.LLMQType_5_60, quorumHash, true, nil) height = int64(30) stateStore = initializeValidatorState(ctx, t, privval, height, btcjson.LLMQType_5_60, quorumHash) evidenceDB = dbm.NewMemDB() @@ -221,7 +221,7 @@ func TestReportConflictingVotes(t *testing.T) { state.LastBlockHeight++ state.LastBlockTime = ev.Time() state.LastValidators = types.NewValidatorSet([]*types.Validator{val}, val.PubKey, btcjson.LLMQType_5_60, - quorumHash, true) + quorumHash, true, nil) pool.Update(ctx, state, []types.Evidence{}) // should be able to retrieve evidence from pool diff --git a/internal/evidence/reactor_test.go b/internal/evidence/reactor_test.go index cec9c185a..088232cec 100644 --- a/internal/evidence/reactor_test.go +++ b/internal/evidence/reactor_test.go @@ -99,14 +99,14 @@ func setup(ctx context.Context, t *testing.T, stateStores []sm.Store) *reactorTe rts.network.Nodes[nodeID].PeerManager.Register(ctx, pu) rts.nodes = append(rts.nodes, rts.network.Nodes[nodeID]) - chCreator := func(ctx context.Context, chdesc *p2p.ChannelDescriptor) (p2p.Channel, error) { + chCreator := func(_ctx context.Context, chdesc *p2p.ChannelDescriptor) (p2p.Channel, error) { return rts.evidenceChannels[nodeID], nil } rts.reactors[nodeID] = evidence.NewReactor( logger, chCreator, - func(ctx context.Context, _ string) *p2p.PeerUpdates { return pu }, + func(_ctx context.Context, _ string) *p2p.PeerUpdates { return pu }, rts.pools[nodeID]) require.NoError(t, rts.reactors[nodeID].Start(ctx)) @@ -550,6 +550,7 @@ func TestEvidenceListSerialization(t *testing.T) { btcjson.LLMQType_5_60, crypto.RandQuorumHash(), true, + nil, ) dupl, err := types.NewDuplicateVoteEvidence( diff --git a/internal/evidence/verify_test.go b/internal/evidence/verify_test.go index fde060ad3..df06a8f95 100644 --- a/internal/evidence/verify_test.go +++ b/internal/evidence/verify_test.go @@ -39,7 +39,7 @@ func TestVerifyDuplicateVoteEvidence(t *testing.T) { val := types.NewMockPVForQuorum(quorumHash) val2 := types.NewMockPVForQuorum(quorumHash) validator1 := val.ExtractIntoValidator(context.Background(), quorumHash) - valSet := types.NewValidatorSet([]*types.Validator{validator1}, validator1.PubKey, quorumType, quorumHash, true) + valSet := types.NewValidatorSet([]*types.Validator{validator1}, validator1.PubKey, quorumType, quorumHash, true, nil) stateID := types.RandStateID() blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"), stateID) blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"), stateID) diff --git a/internal/state/current_round_state.go b/internal/state/current_round_state.go index a6848ccea..3842de27a 100644 --- a/internal/state/current_round_state.go +++ b/internal/state/current_round_state.go @@ -329,7 +329,7 @@ func valsetUpdate( } else { // if we don't have proTxHash, NewValidatorSetWithLocalNodeProTxHash behaves like NewValidatorSet nValSet = types.NewValidatorSetCheckPublicKeys(validatorUpdates, thresholdPubKey, - currentVals.QuorumType, quorumHash) + currentVals.QuorumType, quorumHash, ¶ms) } } else { // validators not changed, but we might have a new quorum hash or threshold public key @@ -342,5 +342,5 @@ func valsetUpdate( } } - return nValSet, nil + return nValSet, nValSet.ValidateBasic() } diff --git a/internal/state/state.go b/internal/state/state.go index cbffe452e..d18b02555 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -382,8 +382,13 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { } var validatorSet *types.ValidatorSet + var valParams *types.ValidatorParams + if genDoc.ConsensusParams != nil { + valParams = &genDoc.ConsensusParams.Validator + } + if len(genDoc.Validators) == 0 { - validatorSet = types.NewValidatorSet(nil, nil, genDoc.QuorumType, nil, false) + validatorSet = types.NewValidatorSet(nil, nil, genDoc.QuorumType, nil, false, valParams) } else { validators := make([]*types.Validator, len(genDoc.Validators)) hasAllPublicKeys := true @@ -394,7 +399,7 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { } } validatorSet = types.NewValidatorSet( - validators, genDoc.ThresholdPublicKey, genDoc.QuorumType, genDoc.QuorumHash, hasAllPublicKeys, + validators, genDoc.ThresholdPublicKey, genDoc.QuorumType, genDoc.QuorumHash, hasAllPublicKeys, valParams, ) } diff --git a/internal/state/state_test.go b/internal/state/state_test.go index 6d297bec0..31d95ca93 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -354,12 +354,14 @@ func TestEmptyValidatorUpdates(t *testing.T) { tearDown, _, state := setupTestCase(t) defer tearDown(t) + originalValidatorSet, _ := types.RandValidatorSet(2) + state.Validators = originalValidatorSet + firstNode := state.Validators.GetByIndex(0) require.NotZero(t, firstNode.ProTxHash) ctx := dash.ContextWithProTxHash(context.Background(), firstNode.ProTxHash) - newPrivKey := bls12381.GenPrivKeyFromSecret([]byte("test")) - newPubKey := newPrivKey.PubKey() + newPubKey := originalValidatorSet.ThresholdPublicKey newQuorumHash := crypto.RandQuorumHash() expectValidators := types.ValidatorListString(state.Validators.Validators) diff --git a/internal/test/factory/validator_set.go b/internal/test/factory/validator_set.go index 45ccddcd6..624c2f5fc 100644 --- a/internal/test/factory/validator_set.go +++ b/internal/test/factory/validator_set.go @@ -44,5 +44,5 @@ func MockValidatorSet() (*types.ValidatorSet, []types.PrivValidator) { false, ) } - return types.NewValidatorSet(valz, thPubKey, btcjson.LLMQType_5_60, quorumHash, true), privVals + return types.NewValidatorSet(valz, thPubKey, btcjson.LLMQType_5_60, quorumHash, true, nil), privVals } diff --git a/light/helpers_test.go b/light/helpers_test.go index 9ff02e0ca..d9c0766fb 100644 --- a/light/helpers_test.go +++ b/light/helpers_test.go @@ -51,6 +51,7 @@ func (pkz privKeys) ToValidators(thresholdPublicKey crypto.PubKey) *types.Valida btcjson.LLMQType_5_60, crypto.Checksum(thresholdPublicKey.Bytes()), true, + nil, ) } diff --git a/light/provider/http/http.go b/light/provider/http/http.go index 73451e6b4..2fdc7550b 100644 --- a/light/provider/http/http.go +++ b/light/provider/http/http.go @@ -238,7 +238,7 @@ func (p *http) validatorSet(ctx context.Context, height *int64, proposer types.P break } } - valSet := types.NewValidatorSet(vals, thresholdPubKey, quorumType, quorumHash, false) + valSet := types.NewValidatorSet(vals, thresholdPubKey, quorumType, quorumHash, false, nil) if valSet == nil || valSet.IsNilOrEmpty() { return nil, provider.ErrBadLightBlock{Reason: fmt.Errorf("retrieved nil or empty validator set")} diff --git a/proto/tendermint/types/params.pb.go b/proto/tendermint/types/params.pb.go index 9f0460c1a..65f620145 100644 --- a/proto/tendermint/types/params.pb.go +++ b/proto/tendermint/types/params.pb.go @@ -284,6 +284,9 @@ func (m *EvidenceParams) GetMaxBytes() int64 { // NOTE: uses ABCI pubkey naming, not Amino names. type ValidatorParams struct { PubKeyTypes []string `protobuf:"bytes,1,rep,name=pub_key_types,json=pubKeyTypes,proto3" json:"pub_key_types,omitempty"` + // Types that are valid to be assigned to XVotingPowerThreshold: + // *ValidatorParams_VotingPowerThreshold + XVotingPowerThreshold isValidatorParams_XVotingPowerThreshold `protobuf_oneof:"_voting_power_threshold"` } func (m *ValidatorParams) Reset() { *m = ValidatorParams{} } @@ -319,6 +322,26 @@ func (m *ValidatorParams) XXX_DiscardUnknown() { var xxx_messageInfo_ValidatorParams proto.InternalMessageInfo +type isValidatorParams_XVotingPowerThreshold interface { + isValidatorParams_XVotingPowerThreshold() + Equal(interface{}) bool + MarshalTo([]byte) (int, error) + Size() int +} + +type ValidatorParams_VotingPowerThreshold struct { + VotingPowerThreshold uint64 `protobuf:"varint,2,opt,name=voting_power_threshold,json=votingPowerThreshold,proto3,oneof" json:"voting_power_threshold,omitempty"` +} + +func (*ValidatorParams_VotingPowerThreshold) isValidatorParams_XVotingPowerThreshold() {} + +func (m *ValidatorParams) GetXVotingPowerThreshold() isValidatorParams_XVotingPowerThreshold { + if m != nil { + return m.XVotingPowerThreshold + } + return nil +} + func (m *ValidatorParams) GetPubKeyTypes() []string { if m != nil { return m.PubKeyTypes @@ -326,6 +349,20 @@ func (m *ValidatorParams) GetPubKeyTypes() []string { return nil } +func (m *ValidatorParams) GetVotingPowerThreshold() uint64 { + if x, ok := m.GetXVotingPowerThreshold().(*ValidatorParams_VotingPowerThreshold); ok { + return x.VotingPowerThreshold + } + return 0 +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*ValidatorParams) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*ValidatorParams_VotingPowerThreshold)(nil), + } +} + // VersionParams contains the ABCI application version. type VersionParams struct { AppVersion uint64 `protobuf:"varint,1,opt,name=app_version,json=appVersion,proto3" json:"app_version,omitempty"` @@ -659,57 +696,60 @@ func init() { func init() { proto.RegisterFile("tendermint/types/params.proto", fileDescriptor_e12598271a686f57) } var fileDescriptor_e12598271a686f57 = []byte{ - // 801 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x55, 0x4f, 0x8f, 0xdb, 0x44, - 0x14, 0x8f, 0x1b, 0x6f, 0x36, 0x79, 0xd9, 0x6c, 0xcc, 0x00, 0xaa, 0x29, 0xac, 0xb3, 0xf8, 0x80, - 0x2a, 0x55, 0xb2, 0x97, 0x2e, 0x1c, 0x90, 0xf8, 0xa3, 0x26, 0x59, 0x95, 0x16, 0x35, 0x45, 0xce, - 0xd2, 0x03, 0x12, 0xb2, 0xc6, 0xce, 0xe0, 0x58, 0x1b, 0x7b, 0x2c, 0x8f, 0x1d, 0xc5, 0x9f, 0x80, - 0x2b, 0x27, 0xc4, 0x47, 0x80, 0x0b, 0xe2, 0x63, 0xf4, 0xd8, 0x0b, 0x12, 0x27, 0x40, 0xd9, 0x2f, - 0x82, 0x66, 0x3c, 0x4e, 0x48, 0x36, 0x65, 0x73, 0xb2, 0xe7, 0xbd, 0xdf, 0x6f, 0x7e, 0x7e, 0xf3, - 0x7e, 0x6f, 0x0c, 0x27, 0x19,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0xcf, 0x91, 0x64, 0x0d, 0x39, 0x09, 0x9d, 0x83, 0x3a, - 0xa7, 0x19, 0x91, 0x57, 0xdb, 0xad, 0x64, 0x01, 0x46, 0x9f, 0x03, 0xf0, 0xa7, 0xd4, 0x55, 0xf7, - 0x3c, 0x07, 0x4e, 0x11, 0xa2, 0x4f, 0xd5, 0xe6, 0x81, 0xd6, 0x78, 0xaa, 0x36, 0x1b, 0xda, 0xa1, - 0xd3, 0xf0, 0x69, 0x14, 0x85, 0x99, 0xf3, 0xb6, 0x57, 0x24, 0x98, 0x31, 0xb7, 0x5c, 0xba, 0xf2, - 0x1e, 0x32, 0x1f, 0x00, 0xac, 0x6f, 0x1a, 0x74, 0x02, 0x90, 0x12, 0x7f, 0x4a, 0xfc, 0x2b, 0x37, - 0x5b, 0x88, 0x13, 0x6b, 0x3a, 0x2d, 0x19, 0xb9, 0x5c, 0xf4, 0x9d, 0x5f, 0x96, 0x86, 0xf2, 0x72, - 0x69, 0x28, 0xaf, 0x96, 0x86, 0xf2, 0xcf, 0xd2, 0x50, 0x7e, 0xbc, 0x36, 0x6a, 0xaf, 0xae, 0x8d, - 0xda, 0x9f, 0xd7, 0x46, 0xed, 0xdb, 0x8f, 0x82, 0x30, 0x9b, 0xe6, 0x9e, 0xe5, 0xd3, 0xc8, 0x9e, - 0x60, 0x36, 0x4d, 0x70, 0x61, 0x97, 0xc3, 0xc3, 0x57, 0xe5, 0x8f, 0xc8, 0xde, 0xfe, 0xb9, 0x79, - 0x0d, 0x11, 0x3f, 0xff, 0x37, 0x00, 0x00, 0xff, 0xff, 0xe5, 0x7d, 0x36, 0x05, 0xf7, 0x06, 0x00, + // 849 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x95, 0x4d, 0x6f, 0x1b, 0x45, + 0x18, 0xc7, 0x3d, 0xf5, 0xc6, 0xb1, 0x1f, 0xc7, 0xb1, 0x19, 0x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x66, + 0xdf, 0x05, 0x58, 0x3c, 0x60, 0x78, 0x17, 0x20, 0xa3, 0xc1, 0x84, 0x06, 0x67, 0x9e, 0x98, 0xab, + 0x1b, 0xab, 0xbb, 0x0d, 0x3d, 0x73, 0x3a, 0x1f, 0xb8, 0xbf, 0x9c, 0xf7, 0xd0, 0xcb, 0xf3, 0x1e, + 0x7a, 0x75, 0xde, 0x43, 0xff, 0x9c, 0xf7, 0xd0, 0x8f, 0x17, 0xbd, 0xca, 0xab, 0x8b, 0x5e, 0xe5, + 0xcf, 0x8b, 0x5e, 0xe5, 0xab, 0x8f, 0xc3, 0x48, 0x4c, 0x72, 0xdf, 0x09, 0x58, 0xdc, 0x1f, 0x13, + 0x3e, 0x49, 0x49, 0xd1, 0x2f, 0x9b, 0x47, 0x8e, 0xca, 0xef, 0x5b, 0x7f, 0xf5, 0x9b, 0xe9, 0xd7, + 0xd4, 0xfc, 0xe1, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb2, 0xb1, 0x36, 0x07, 0x4e, 0x07, 0x00, 0x00, } @@ -839,6 +879,39 @@ func (this *ValidatorParams) Equal(that interface{}) bool { return false } } + if that1.XVotingPowerThreshold == nil { + if this.XVotingPowerThreshold != nil { + return false + } + } else if this.XVotingPowerThreshold == nil { + return false + } else if !this.XVotingPowerThreshold.Equal(that1.XVotingPowerThreshold) { + return false + } + return true +} +func (this *ValidatorParams_VotingPowerThreshold) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*ValidatorParams_VotingPowerThreshold) + if !ok { + that2, ok := that.(ValidatorParams_VotingPowerThreshold) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.VotingPowerThreshold != that1.VotingPowerThreshold { + return false + } return true } func (this *VersionParams) Equal(that interface{}) bool { @@ -1219,6 +1292,15 @@ func (m *ValidatorParams) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.XVotingPowerThreshold != nil { + { + size := m.XVotingPowerThreshold.Size() + i -= size + if _, err := m.XVotingPowerThreshold.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + } + } if len(m.PubKeyTypes) > 0 { for iNdEx := len(m.PubKeyTypes) - 1; iNdEx >= 0; iNdEx-- { i -= len(m.PubKeyTypes[iNdEx]) @@ -1231,6 +1313,18 @@ func (m *ValidatorParams) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *ValidatorParams_VotingPowerThreshold) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ValidatorParams_VotingPowerThreshold) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + i = encodeVarintParams(dAtA, i, uint64(m.VotingPowerThreshold)) + i-- + dAtA[i] = 0x10 + return len(dAtA) - i, nil +} func (m *VersionParams) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -1533,9 +1627,21 @@ func (m *ValidatorParams) Size() (n int) { n += 1 + l + sovParams(uint64(l)) } } + if m.XVotingPowerThreshold != nil { + n += m.XVotingPowerThreshold.Size() + } return n } +func (m *ValidatorParams_VotingPowerThreshold) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + n += 1 + sovParams(uint64(m.VotingPowerThreshold)) + return n +} func (m *VersionParams) Size() (n int) { if m == nil { return 0 @@ -2201,6 +2307,26 @@ func (m *ValidatorParams) Unmarshal(dAtA []byte) error { } m.PubKeyTypes = append(m.PubKeyTypes, string(dAtA[iNdEx:postIndex])) iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field VotingPowerThreshold", wireType) + } + var v uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.XVotingPowerThreshold = &ValidatorParams_VotingPowerThreshold{v} default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) diff --git a/proto/tendermint/types/params.proto b/proto/tendermint/types/params.proto index 3c4a7feec..5cc6ab2e6 100644 --- a/proto/tendermint/types/params.proto +++ b/proto/tendermint/types/params.proto @@ -58,6 +58,18 @@ message EvidenceParams { // NOTE: uses ABCI pubkey naming, not Amino names. message ValidatorParams { repeated string pub_key_types = 1; + // Threshold defines the minimum voting power of validators that must vote for a proposal + // to be considered signed and added to the block. + // + // For Dash Platform, the voting power can be calculated by multiply of the number of validators + // and const `DefaultDashVotingPower == 100`. + // + // For validator sets containing more than 3 validators, the threshold MUST be + // greater than 2/3 of the total voting power of all validators. + // + // If set to 0 or absent, default value is determined based on LLMQ quorum type defined for the network, + // with a fall back to 2/3 + 1 of the total voting power. + optional uint64 voting_power_threshold = 2; } // VersionParams contains the ABCI application version. diff --git a/rpc/client/evidence_test.go b/rpc/client/evidence_test.go index 5789e6a64..f15430df6 100644 --- a/rpc/client/evidence_test.go +++ b/rpc/client/evidence_test.go @@ -39,7 +39,7 @@ func newEvidence(t *testing.T, val *privval.FilePV, require.NoError(t, err) validator := types.NewValidator(privKey.PubKey(), types.DefaultDashVotingPower, val.Key.ProTxHash, "") - valSet := types.NewValidatorSet([]*types.Validator{validator}, validator.PubKey, quorumType, quorumHash, true) + valSet := types.NewValidatorSet([]*types.Validator{validator}, validator.PubKey, quorumType, quorumHash, true, nil) ev, err := types.NewDuplicateVoteEvidence(vote, vote2, timestamp, valSet) require.NoError(t, err) diff --git a/test/e2e/runner/evidence.go b/test/e2e/runner/evidence.go index 090199019..80d98ba35 100644 --- a/test/e2e/runner/evidence.go +++ b/test/e2e/runner/evidence.go @@ -71,7 +71,7 @@ func InjectEvidence(ctx context.Context, logger log.Logger, r *rand.Rand, testne valSet := types.NewValidatorSet(valRes.Validators, *valRes.ThresholdPublicKey, valRes.QuorumType, *valRes.QuorumHash, - false) + false, nil) if valSet == nil { return fmt.Errorf("could not create validator set from response") } diff --git a/test/e2e/tests/validator_test.go b/test/e2e/tests/validator_test.go index 837980060..db46577af 100644 --- a/test/e2e/tests/validator_test.go +++ b/test/e2e/tests/validator_test.go @@ -186,7 +186,7 @@ func newValidatorSchedule(testnet e2e.Testnet) *validatorSchedule { } } - vs := types.NewValidatorSet(makeVals(valMap), thresholdPublicKey, quorumType, quorumHash, true) + vs := types.NewValidatorSet(makeVals(valMap), thresholdPublicKey, quorumType, quorumHash, true, nil) ps, err := selectproposer.NewProposerSelector(*types.DefaultConsensusParams(), vs, testnet.InitialHeight, 0, nil, nil) if err != nil { panic(err) @@ -257,7 +257,7 @@ func (s *validatorSchedule) Increment(heights int64) error { } } else { vs := types.NewValidatorSet(makeVals(update), thresholdPublicKeyUpdate, btcjson.LLMQType_5_60, - quorumHashUpdate, true) + quorumHashUpdate, true, &cp.Validator) ps, err := selectproposer.NewProposerSelector(cp, vs, s.height, 0, nil, nil) if err != nil { diff --git a/types/evidence.go b/types/evidence.go index 86e3f0863..42d058de2 100644 --- a/types/evidence.go +++ b/types/evidence.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "strings" + "testing" "time" "github.com/dashpay/dashd-go/btcjson" @@ -490,6 +491,9 @@ func NewMockDuplicateVoteEvidenceWithValidator( quorumType btcjson.LLMQType, quorumHash crypto.QuorumHash, ) (*DuplicateVoteEvidence, error) { + if !testing.Testing() { + panic("NewMockDuplicateVoteEvidenceWithValidator should only be used in tests") + } pubKey, err := pv.GetPubKey(ctx, quorumHash) if err != nil { panic(err) @@ -510,7 +514,7 @@ func NewMockDuplicateVoteEvidenceWithValidator( voteA, voteB, time, - NewValidatorSet([]*Validator{val}, val.PubKey, quorumType, quorumHash, true), + NewValidatorSet([]*Validator{val}, val.PubKey, quorumType, quorumHash, true, nil), ) } diff --git a/types/evidence_test.go b/types/evidence_test.go index 19df99c4f..e7ed069c9 100644 --- a/types/evidence_test.go +++ b/types/evidence_test.go @@ -165,7 +165,8 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) { thresholdPublicKey, err := val.GetThresholdPublicKey(context.Background(), quorumHash) assert.NoError(t, err) valSet := NewValidatorSet( - []*Validator{val.ExtractIntoValidator(context.Background(), quorumHash)}, thresholdPublicKey, quorumType, quorumHash, true) + []*Validator{val.ExtractIntoValidator(context.Background(), quorumHash)}, + thresholdPublicKey, quorumType, quorumHash, true, nil) ev, err := NewDuplicateVoteEvidence(vote1, vote2, defaultVoteTime, valSet) require.NoError(t, err) tc.malleateEvidence(ev) diff --git a/types/generator.go b/types/generator.go index 75c8f034f..115d1cac0 100644 --- a/types/generator.go +++ b/types/generator.go @@ -83,7 +83,7 @@ func GenerateValidatorSet(valParams []ValSetParam, opts ...ValSetOptionFunc) (*V valz = append(valz, NewValidator(qks.PubKey, opt.VotingPower, proTxHash, "")) } sort.Sort(PrivValidatorsByProTxHash(privValidators)) - return NewValidatorSet(valz, ld.ThresholdPubKey, crypto.SmallQuorumType(), quorumHash, true), privValidators + return NewValidatorSet(valz, ld.ThresholdPubKey, crypto.SmallQuorumType(), quorumHash, true, nil), privValidators } // MakeGenesisValsFromValidatorSet converts ValidatorSet data into a list of GenesisValidator diff --git a/types/params.go b/types/params.go index fa7243825..ce2065eab 100644 --- a/types/params.go +++ b/types/params.go @@ -69,7 +69,8 @@ type EvidenceParams struct { // ValidatorParams restrict the public key types validators can use. // NOTE: uses ABCI pubkey naming, not Amino names. type ValidatorParams struct { - PubKeyTypes []string `json:"pub_key_types"` + PubKeyTypes []string `json:"pub_key_types"` + VotingPowerThreshold uint64 `json:"threshold"` } type VersionParams struct { @@ -141,7 +142,8 @@ func DefaultEvidenceParams() EvidenceParams { // only bls12381 pubkeys. func DefaultValidatorParams() ValidatorParams { return ValidatorParams{ - PubKeyTypes: []string{ABCIPubKeyTypeBLS12381}, + PubKeyTypes: []string{ABCIPubKeyTypeBLS12381}, + VotingPowerThreshold: 0, } } @@ -239,6 +241,33 @@ func (val *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool { return false } +func ValidatorParamsFromProto(pbValParams *tmproto.ValidatorParams) ValidatorParams { + if pbValParams != nil { + return ValidatorParams{ + // Copy Validator.PubkeyTypes, and set result's value to the copy. + // This avoids having to initialize the slice to 0 values, and then write to it again. + PubKeyTypes: append([]string{}, pbValParams.PubKeyTypes...), + VotingPowerThreshold: pbValParams.GetVotingPowerThreshold(), + } + } + + return ValidatorParams{ + PubKeyTypes: []string{}, + VotingPowerThreshold: 0, + } +} + +// GetVotingPowerThreshold returns the voting power threshold for the validator set. +// For nil ValidatorParams, it returns 0. +// +// Value of 0 means that the threshold is not set. +func (val *ValidatorParams) GetVotingPowerThreshold() uint64 { + if val != nil { + return val.VotingPowerThreshold + } + return 0 +} + func (params *ConsensusParams) Complete() { if params.Synchrony == (SynchronyParams{}) { params.Synchrony = DefaultSynchronyParams() @@ -392,9 +421,7 @@ func (params ConsensusParams) UpdateConsensusParams(params2 *tmproto.ConsensusPa res.Evidence.MaxBytes = params2.Evidence.MaxBytes } if params2.Validator != nil { - // Copy params2.Validator.PubkeyTypes, and set result's value to the copy. - // This avoids having to initialize the slice to 0 values, and then write to it again. - res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...) + res.Validator = ValidatorParamsFromProto(params2.Validator) } if params2.Version != nil { res.Version.AppVersion = params2.Version.AppVersion @@ -441,6 +468,9 @@ func (params *ConsensusParams) ToProto() tmproto.ConsensusParams { }, Validator: &tmproto.ValidatorParams{ PubKeyTypes: params.Validator.PubKeyTypes, + XVotingPowerThreshold: &tmproto.ValidatorParams_VotingPowerThreshold{ + VotingPowerThreshold: params.Validator.VotingPowerThreshold, + }, }, Version: &tmproto.VersionParams{ AppVersion: params.Version.AppVersion, @@ -483,7 +513,8 @@ func ConsensusParamsFromProto(pbParams tmproto.ConsensusParams) ConsensusParams if pbParams.Validator != nil { c.Validator = ValidatorParams{ - PubKeyTypes: pbParams.Validator.PubKeyTypes, + PubKeyTypes: pbParams.Validator.PubKeyTypes, + VotingPowerThreshold: pbParams.Validator.GetVotingPowerThreshold(), } } diff --git a/types/params_test.go b/types/params_test.go index ccd18e8e7..241181233 100644 --- a/types/params_test.go +++ b/types/params_test.go @@ -218,7 +218,8 @@ func makeParams(args makeParamsArgs) ConsensusParams { MaxBytes: args.maxEvidenceBytes, }, Validator: ValidatorParams{ - PubKeyTypes: args.pubkeyTypes, + PubKeyTypes: args.pubkeyTypes, + VotingPowerThreshold: 0, }, Synchrony: SynchronyParams{ Precision: args.precision, diff --git a/types/protobuf.go b/types/protobuf.go index 671e21321..979ac2fe4 100644 --- a/types/protobuf.go +++ b/types/protobuf.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "testing" "github.com/dashpay/dashd-go/btcjson" @@ -159,10 +160,16 @@ func (pb2tm) ValidatorUpdatesFromValidatorSet(valSetUpdate *abci.ValidatorSetUpd return tmVals, pub, valSetUpdate.QuorumHash, nil } +// ValidatorSetFromProtoUpdate creates validator set from validator update. +// +// Only use in tests. func (pb2tm) ValidatorSetFromProtoUpdate( quorumType btcjson.LLMQType, valSetUpdate *abci.ValidatorSetUpdate, ) (*ValidatorSet, error) { + if !testing.Testing() { + panic("ValidatorSetFromProtoUpdate should only be used in tests") + } hasPublicKeys := true for i, v := range valSetUpdate.ValidatorUpdates { if v.PubKey == nil { @@ -183,7 +190,7 @@ func (pb2tm) ValidatorSetFromProtoUpdate( if err != nil { return nil, err } - return NewValidatorSet(tmVals, pub, quorumType, quorumHash, hasPublicKeys), nil + return NewValidatorSet(tmVals, pub, quorumType, quorumHash, hasPublicKeys, nil), nil } func (pb2tm) ThresholdPublicKeyUpdate(thresholdPublicKey crypto2.PublicKey) (crypto.PubKey, error) { diff --git a/types/protobuf_test.go b/types/protobuf_test.go index d1530e134..4774d40eb 100644 --- a/types/protobuf_test.go +++ b/types/protobuf_test.go @@ -43,7 +43,7 @@ func TestABCIValidators(t *testing.T) { assert.NoError(t, err) assert.Equal(t, tmValExpected, tmVals[0]) - abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals, tmVal.PubKey, btcjson.LLMQType_5_60, quorumHash, true)) + abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals, tmVal.PubKey, btcjson.LLMQType_5_60, quorumHash, true, nil)) assert.Equal(t, abci.ValidatorSetUpdate{ ValidatorUpdates: []abci.ValidatorUpdate{abciVal}, ThresholdPublicKey: *abciVal.PubKey, diff --git a/types/validation_test.go b/types/validation_test.go index f518d1f7e..59568dafb 100644 --- a/types/validation_test.go +++ b/types/validation_test.go @@ -23,7 +23,7 @@ func TestValidatorSet_VerifyCommit_All(t *testing.T) { pubKey = privKey.PubKey() v1 = NewValidatorDefaultVotingPower(pubKey, proTxHash) quorumHash = crypto.RandQuorumHash() - vset = NewValidatorSet([]*Validator{v1}, v1.PubKey, btcjson.LLMQType_5_60, quorumHash, true) + vset = NewValidatorSet([]*Validator{v1}, v1.PubKey, btcjson.LLMQType_5_60, quorumHash, true, nil) chainID = "Lalande21185" ) diff --git a/types/validator_set.go b/types/validator_set.go index 37aff76e3..2cdc0052c 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -64,6 +64,7 @@ type ValidatorSet struct { ThresholdPublicKey crypto.PubKey `json:"threshold_public_key"` QuorumHash crypto.QuorumHash `json:"quorum_hash"` QuorumType btcjson.LLMQType `json:"quorum_type"` + Params ValidatorParams `json:"validator_consensus_params"` HasPublicKeys bool `json:"has_public_keys"` } @@ -78,12 +79,15 @@ type ValidatorSet struct { // MaxVotesCount - commits by a validator set larger than this will fail // validation. func NewValidatorSet(valz []*Validator, newThresholdPublicKey crypto.PubKey, quorumType btcjson.LLMQType, - quorumHash crypto.QuorumHash, hasPublicKeys bool) *ValidatorSet { + quorumHash crypto.QuorumHash, hasPublicKeys bool, validatorParams *ValidatorParams) *ValidatorSet { vals := &ValidatorSet{ QuorumHash: quorumHash, QuorumType: quorumType, HasPublicKeys: hasPublicKeys, } + if validatorParams != nil { + vals.Params = *validatorParams + } err := vals.updateWithChangeSet(valz, false, newThresholdPublicKey, quorumHash) if err != nil { panic(fmt.Sprintf("Cannot create validator set: %v", err)) @@ -99,6 +103,7 @@ func NewValidatorSetCheckPublicKeys( newThresholdPublicKey crypto.PubKey, quorumType btcjson.LLMQType, quorumHash crypto.QuorumHash, + params *ValidatorParams, ) *ValidatorSet { hasPublicKeys := true for _, val := range valz { @@ -106,12 +111,12 @@ func NewValidatorSetCheckPublicKeys( hasPublicKeys = false } } - return NewValidatorSet(valz, newThresholdPublicKey, quorumType, quorumHash, hasPublicKeys) + return NewValidatorSet(valz, newThresholdPublicKey, quorumType, quorumHash, hasPublicKeys, params) } // NewEmptyValidatorSet initializes a ValidatorSet with no validators func NewEmptyValidatorSet() *ValidatorSet { - return NewValidatorSet(nil, nil, 0, nil, false) + return NewValidatorSet(nil, nil, 0, nil, false, nil) } func (vals *ValidatorSet) ValidateBasic() error { @@ -152,6 +157,29 @@ func (vals *ValidatorSet) ValidateBasic() error { return fmt.Errorf("proposer failed validate basic, error: %w", err) } + if len(vals.Params.PubKeyTypes) != 0 { + for _, validator := range vals.Validators { + if !vals.Params.IsValidPubkeyType(validator.PubKey.Type()) { + return fmt.Errorf( + "validator %s is using pubkey %s, which is unsupported for consensus - expected %v", + validator.String(), + validator.PubKey.Type(), + vals.Params.PubKeyTypes, + ) + } + } + } + + if vals.Params.VotingPowerThreshold > 0 && vals.QuorumTypeMemberCount() > 3 { + power := vals.QuorumVotingPower() + if power < 0 { + return fmt.Errorf("quorum voting power %d is negative", power) + } + if uint64(power) < vals.Params.VotingPowerThreshold*2/3+1 { + return fmt.Errorf("quorum voting power %d is less than threshold %d", power, vals.Params.VotingPowerThreshold) + } + } + return nil } diff --git a/types/validator_set_test.go b/types/validator_set_test.go index c423ff771..ee0b38fc2 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -24,7 +24,7 @@ import ( func TestValidatorSetBasic(t *testing.T) { // empty or nil validator lists are allowed, - vset := NewValidatorSet([]*Validator{}, nil, btcjson.LLMQType_5_60, nil, true) + vset := NewValidatorSet([]*Validator{}, nil, btcjson.LLMQType_5_60, nil, true, nil) assert.EqualValues(t, vset, vset.Copy()) assert.False(t, vset.HasProTxHash([]byte("some val"))) @@ -233,7 +233,7 @@ func TestCopy(t *testing.T) { func BenchmarkValidatorSetCopy(b *testing.B) { b.StopTimer() - vset := NewValidatorSet([]*Validator{}, nil, btcjson.LLMQType_5_60, nil, true) + vset := NewValidatorSet([]*Validator{}, nil, btcjson.LLMQType_5_60, nil, true, nil) for i := 0; i < 1000; i++ { privKey := bls12381.GenPrivKey() pubKey := privKey.PubKey() @@ -279,7 +279,7 @@ func randValidatorInQuorum(ctx context.Context, t *testing.T, quorumHash crypto. func TestEmptySet(t *testing.T) { var valList []*Validator - valSet := NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true) + valSet := NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true, nil) // Add to empty set proTxHashes := []crypto.ProTxHash{crypto.Checksum([]byte("v1")), crypto.Checksum([]byte("v2"))} @@ -310,14 +310,18 @@ func TestUpdatesForNewValidatorSet(t *testing.T) { v112 := NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v1"))) v113 := NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v1"))) valList := []*Validator{v111, v112, v113} - assert.Panics(t, func() { NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true) }) + assert.Panics(t, func() { + NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true, nil) + }) // Verify set including validator with voting power 0 cannot be created v1 := NewTestRemoveValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v1"))) v2 := NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v2"))) v3 := NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v3"))) valList = []*Validator{v1, v2, v3} - assert.Panics(t, func() { NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true) }) + assert.Panics(t, func() { + NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true, nil) + }) // Verify set including validator with negative voting power cannot be created v1 = NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v1"))) @@ -327,7 +331,9 @@ func TestUpdatesForNewValidatorSet(t *testing.T) { } v3 = NewTestValidatorGeneratedFromProTxHash(crypto.Checksum([]byte("v3"))) valList = []*Validator{v1, v2, v3} - assert.Panics(t, func() { NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true) }) + assert.Panics(t, func() { + NewValidatorSet(valList, bls12381.PubKey{}, btcjson.LLMQType_5_60, crypto.QuorumHash{}, true, nil) + }) } diff --git a/types/vote_set_test.go b/types/vote_set_test.go index f3a47aaa5..2ce386e54 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -640,7 +640,7 @@ func randVoteSetWithLLMQType( sort.Sort(PrivValidatorsByProTxHash(privValidators)) - valSet := NewValidatorSet(valz, ld.ThresholdPubKey, llmqType, quorumHash, true) + valSet := NewValidatorSet(valz, ld.ThresholdPubKey, llmqType, quorumHash, true, nil) voteSet := NewVoteSet("test_chain_id", height, round, signedMsgType, valSet) return voteSet, valSet, privValidators From 4ec14c413bd5ed52d0942b0105e76ac7868acdfe Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 12 Feb 2025 14:02:24 +0100 Subject: [PATCH 2/4] feat: use threshold from ValidatorParam to construct bls sigs --- types/params.go | 10 ++++- types/validator_set.go | 4 ++ types/vote_set_test.go | 89 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/types/params.go b/types/params.go index ce2065eab..0528b33fe 100644 --- a/types/params.go +++ b/types/params.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "errors" "fmt" + "math" "os" "time" @@ -261,9 +262,14 @@ func ValidatorParamsFromProto(pbValParams *tmproto.ValidatorParams) ValidatorPar // For nil ValidatorParams, it returns 0. // // Value of 0 means that the threshold is not set. -func (val *ValidatorParams) GetVotingPowerThreshold() uint64 { +func (val *ValidatorParams) GetVotingPowerThreshold() int64 { if val != nil { - return val.VotingPowerThreshold + //nolint:goosec + if val.VotingPowerThreshold > math.MaxInt64 || int64(val.VotingPowerThreshold) > MaxTotalVotingPower { + // this should never happen + panic(fmt.Sprintf("VotingPowerThreshold %d is too big", val.VotingPowerThreshold)) + } + return int64(val.VotingPowerThreshold) } return 0 } diff --git a/types/validator_set.go b/types/validator_set.go index 2cdc0052c..c689f83da 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -425,6 +425,10 @@ func (vals *ValidatorSet) QuorumVotingPower() int64 { // QuorumVotingThresholdPower returns the threshold power of the voting power of the quorum if all the members existed. // Voting is considered successful when voting power is at or above this threshold. func (vals *ValidatorSet) QuorumVotingThresholdPower() int64 { + if thresholdPower := vals.Params.GetVotingPowerThreshold(); thresholdPower > 0 { + return thresholdPower + } + return int64(vals.QuorumTypeThresholdCount()) * DefaultDashVotingPower } diff --git a/types/vote_set_test.go b/types/vote_set_test.go index 2ce386e54..9890b7464 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -513,6 +513,7 @@ func TestVoteSet_LLMQType_50_60(t *testing.T) { tt.numValidators, tt.llmqType, tt.threshold, + nil, ) assert.EqualValues(t, tt.threshold, valSet.QuorumTypeThresholdCount()) assert.GreaterOrEqual(t, len(privValidators), tt.threshold+3, @@ -548,6 +549,91 @@ func TestVoteSet_LLMQType_50_60(t *testing.T) { } } +// Given a set of validators and a threshold defined in ValidatorParams, +// when votes are cast, +// then the threshold from ValidatorParams is respected. +// FIXME: threshold 1 causes "panic: At least 2 coefficients required" +// in bls-signatures C++ library. +func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { + const ( + height = int64(1) + round = int32(0) + ) + testCases := []struct { + llmqType btcjson.LLMQType + numValidators int + threshold int + }{ + { // threshold allowing 1 vote + llmqType: btcjson.LLMQType_100_67, + numValidators: 100, + threshold: 1, + }, + { // single node network + llmqType: btcjson.LLMQType_100_67, + numValidators: 2, + threshold: 1, + }, + { // normal network + llmqType: btcjson.LLMQType_100_67, + numValidators: 100, + threshold: 67, + }, + { // network below threshold + llmqType: btcjson.LLMQType_100_67, + numValidators: 66, + threshold: 67, + }, + } + + for ti, tt := range testCases { + name := strconv.Itoa(ti) + t.Run(name, func(t *testing.T) { + params := ValidatorParams{ + VotingPowerThreshold: uint64(int64(tt.threshold) * DefaultDashVotingPower), + } + voteSet, _, privValidators := randVoteSetWithLLMQType( + height, + round, + tmproto.PrevoteType, + tt.numValidators, + tt.llmqType, + tt.threshold, + ¶ms, + ) + assert.GreaterOrEqual(t, len(privValidators), tt.threshold+3, + "need at least %d validators", tt.threshold+3) + + blockHash := crypto.CRandBytes(32) + stateID := RandStateID() + blockPartSetHeader := PartSetHeader{uint32(123), crypto.CRandBytes(32)} + votedBlock := BlockID{blockHash, blockPartSetHeader, stateID.Hash()} + + // below threshold + for i := 0; i < tt.threshold-1; i++ { + blockMaj, anyMaj := castVote(t, votedBlock, height, round, privValidators, int32(i), voteSet) + assert.False(t, blockMaj, "no block majority expected here: i=%d, threshold=%d", i, tt.threshold) + assert.False(t, anyMaj, "no 'any' majority expected here: i=%d, threshold=%d", i, tt.threshold) + } + + // we add null vote + blockMaj, anyMaj := castVote(t, BlockID{}, height, round, privValidators, int32(tt.threshold), voteSet) + assert.False(t, blockMaj, "no block majority expected after nil vote") + assert.True(t, anyMaj, "'any' majority expected after nil vote at threshold") + + // at threshold + blockMaj, anyMaj = castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+1), voteSet) + assert.True(t, blockMaj, "block majority expected") + assert.True(t, anyMaj, "'any' majority expected") + + // above threshold + blockMaj, anyMaj = castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+2), voteSet) + assert.True(t, blockMaj, "block majority expected") + assert.True(t, anyMaj, "'any' majority expected") + }) + } +} + func TestVoteSet_json_marshal_nil(t *testing.T) { var vset *VoteSet jsonBytes, err := json.Marshal(vset) @@ -619,6 +705,7 @@ func randVoteSetWithLLMQType( numValidators int, llmqType btcjson.LLMQType, threshold int, + params *ValidatorParams, ) (*VoteSet, *ValidatorSet, []PrivValidator) { valz := make([]*Validator, 0, numValidators) privValidators := make([]PrivValidator, 0, numValidators) @@ -640,7 +727,7 @@ func randVoteSetWithLLMQType( sort.Sort(PrivValidatorsByProTxHash(privValidators)) - valSet := NewValidatorSet(valz, ld.ThresholdPubKey, llmqType, quorumHash, true, nil) + valSet := NewValidatorSet(valz, ld.ThresholdPubKey, llmqType, quorumHash, true, params) voteSet := NewVoteSet("test_chain_id", height, round, signedMsgType, valSet) return voteSet, valSet, privValidators From 22e2da0e0d58542e5247f3b86228aaed52232ee4 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:36:42 +0100 Subject: [PATCH 3/4] chore: add threshold to validator set --- proto/tendermint/types/params.proto | 9 +- proto/tendermint/types/validator.pb.go | 112 ++++++++++++++++--------- proto/tendermint/types/validator.proto | 15 ++-- types/params.go | 24 ++++-- types/validator_set.go | 50 ++++++----- types/vote_set_test.go | 41 ++++----- 6 files changed, 155 insertions(+), 96 deletions(-) diff --git a/proto/tendermint/types/params.proto b/proto/tendermint/types/params.proto index 5cc6ab2e6..c549b2fcc 100644 --- a/proto/tendermint/types/params.proto +++ b/proto/tendermint/types/params.proto @@ -64,8 +64,13 @@ message ValidatorParams { // For Dash Platform, the voting power can be calculated by multiply of the number of validators // and const `DefaultDashVotingPower == 100`. // - // For validator sets containing more than 3 validators, the threshold MUST be - // greater than 2/3 of the total voting power of all validators. + // The following validation rules MUST be enforced: + // + // 1. For validator sets containing 1 or 2 validators, the threshold MUST be equal to the total voting power + // of these validators. + // 2. For validator set with 3 validators, the threshold MUST be equal or greater than 2/3 of the total voting power. + // 3. For validator sets containing more than 3 validators, the threshold MUST be greater than 2/3 of the + // total voting power. // // If set to 0 or absent, default value is determined based on LLMQ quorum type defined for the network, // with a fall back to 2/3 + 1 of the total voting power. diff --git a/proto/tendermint/types/validator.pb.go b/proto/tendermint/types/validator.pb.go index a37aacd83..b3986ca88 100644 --- a/proto/tendermint/types/validator.pb.go +++ b/proto/tendermint/types/validator.pb.go @@ -25,13 +25,14 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type ValidatorSet struct { - Validators []*Validator `protobuf:"bytes,1,rep,name=validators,proto3" json:"validators,omitempty"` - Proposer *Validator `protobuf:"bytes,2,opt,name=proposer,proto3" json:"proposer,omitempty"` - TotalVotingPower int64 `protobuf:"varint,3,opt,name=total_voting_power,json=totalVotingPower,proto3" json:"total_voting_power,omitempty"` - ThresholdPublicKey crypto.PublicKey `protobuf:"bytes,4,opt,name=threshold_public_key,json=thresholdPublicKey,proto3" json:"threshold_public_key"` - QuorumType int32 `protobuf:"varint,5,opt,name=quorum_type,json=quorumType,proto3" json:"quorum_type,omitempty"` - QuorumHash []byte `protobuf:"bytes,6,opt,name=quorum_hash,json=quorumHash,proto3" json:"quorum_hash,omitempty"` - HasPublicKeys bool `protobuf:"varint,7,opt,name=has_public_keys,json=hasPublicKeys,proto3" json:"has_public_keys,omitempty"` + Validators []*Validator `protobuf:"bytes,1,rep,name=validators,proto3" json:"validators,omitempty"` + Proposer *Validator `protobuf:"bytes,2,opt,name=proposer,proto3" json:"proposer,omitempty"` + TotalVotingPower int64 `protobuf:"varint,3,opt,name=total_voting_power,json=totalVotingPower,proto3" json:"total_voting_power,omitempty"` + ThresholdPublicKey crypto.PublicKey `protobuf:"bytes,4,opt,name=threshold_public_key,json=thresholdPublicKey,proto3" json:"threshold_public_key"` + QuorumType int32 `protobuf:"varint,5,opt,name=quorum_type,json=quorumType,proto3" json:"quorum_type,omitempty"` + QuorumHash []byte `protobuf:"bytes,6,opt,name=quorum_hash,json=quorumHash,proto3" json:"quorum_hash,omitempty"` + HasPublicKeys bool `protobuf:"varint,7,opt,name=has_public_keys,json=hasPublicKeys,proto3" json:"has_public_keys,omitempty"` + VotingPowerThreshold uint64 `protobuf:"varint,8,opt,name=voting_power_threshold,json=votingPowerThreshold,proto3" json:"voting_power_threshold,omitempty"` } func (m *ValidatorSet) Reset() { *m = ValidatorSet{} } @@ -116,6 +117,13 @@ func (m *ValidatorSet) GetHasPublicKeys() bool { return false } +func (m *ValidatorSet) GetVotingPowerThreshold() uint64 { + if m != nil { + return m.VotingPowerThreshold + } + return 0 +} + type Validator struct { PubKey *crypto.PublicKey `protobuf:"bytes,1,opt,name=pub_key,json=pubKey,proto3" json:"pub_key,omitempty"` VotingPower int64 `protobuf:"varint,2,opt,name=voting_power,json=votingPower,proto3" json:"voting_power,omitempty"` @@ -253,37 +261,38 @@ func init() { func init() { proto.RegisterFile("tendermint/types/validator.proto", fileDescriptor_4e92274df03d3088) } var fileDescriptor_4e92274df03d3088 = []byte{ - // 475 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0xcd, 0x6a, 0xdb, 0x40, - 0x18, 0xf4, 0xda, 0x8e, 0x13, 0xaf, 0x5c, 0x92, 0x2e, 0x39, 0x88, 0x34, 0x28, 0x8a, 0x0f, 0x45, - 0xd0, 0x22, 0x41, 0x7f, 0xe8, 0x21, 0xa7, 0xe6, 0x54, 0x28, 0x14, 0xa3, 0x98, 0x1c, 0x7a, 0x11, - 0x2b, 0x6b, 0xf1, 0x0a, 0xcb, 0xfe, 0xb6, 0xbb, 0x2b, 0x37, 0x7a, 0x8b, 0x3e, 0x56, 0x8e, 0x39, - 0x16, 0x0a, 0xa5, 0xd8, 0x6f, 0xd0, 0x27, 0x28, 0x5a, 0x55, 0x8a, 0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x96, 0x41, 0x42, 0x15, 0x17, 0xb4, 0x08, - 0x2a, 0xd3, 0xf2, 0x2f, 0xa8, 0xee, 0xe4, 0xe1, 0x95, 0xc5, 0x03, 0x83, 0xbf, 0xfe, 0x13, 0x00, - 0x00, 0xff, 0xff, 0x84, 0xb2, 0x3f, 0xb9, 0x80, 0x03, 0x00, 0x00, + // 495 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0x41, 0x6b, 0xdb, 0x30, + 0x18, 0x8d, 0x9a, 0x34, 0x4d, 0x94, 0x8c, 0x76, 0x22, 0x0c, 0xd3, 0x15, 0xd7, 0xcd, 0x61, 0x18, + 0x36, 0x6c, 0xd8, 0x3a, 0x76, 0xe8, 0x69, 0x3d, 0x0d, 0x06, 0x23, 0xb8, 0xa1, 0x87, 0x5d, 0x8c, + 0x1c, 0x8b, 0xc8, 0xc4, 0xc9, 0xa7, 0x49, 0x72, 0x56, 0xff, 0x8b, 0xfd, 0x8c, 0xfd, 0x94, 0x1e, + 0x7b, 0x1c, 0x0c, 0xc6, 0x48, 0xfe, 0xc8, 0xb0, 0x3c, 0x3b, 0x5e, 0x2f, 0x6b, 0x6f, 0xf6, 0x7b, + 0xef, 0xd3, 0xfb, 0xde, 0x13, 0xc2, 0x8e, 0x66, 0xab, 0x98, 0xc9, 0x65, 0xb2, 0xd2, 0xbe, 0xce, + 0x05, 0x53, 0xfe, 0x9a, 0xa6, 0x49, 0x4c, 0x35, 0x48, 0x4f, 0x48, 0xd0, 0x40, 0x8e, 0x76, 0x0a, + 0xcf, 0x28, 0x8e, 0x47, 0x73, 0x98, 0x83, 0x21, 0xfd, 0xe2, 0xab, 0xd4, 0x1d, 0x9f, 0x34, 0x4e, + 0x9a, 0xc9, 0x5c, 0x68, 0xf0, 0x17, 0x2c, 0x57, 0x25, 0x3b, 0xfe, 0xde, 0xc6, 0xc3, 0xeb, 0xea, + 0xe4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e1, 0x7e, 0xdd, 0x1d, 0xb9, 0xc0, 0x07, 0x22, 0x8b, + 0x4c, 0x6c, 0xf4, 0xc0, 0xd8, 0x28, 0xe8, 0x8a, 0x2c, 0x2a, 0xa2, 0x9e, 0xe1, 0xe1, 0x3f, 0x45, + 0xef, 0x99, 0xa2, 0x07, 0x0d, 0x5b, 0xf2, 0x12, 0x3f, 0xad, 0x6e, 0x27, 0x14, 0x32, 0x01, 0x99, + 0xe8, 0xbc, 0xba, 0x90, 0x8a, 0x98, 0xfc, 0xc5, 0x89, 0x8d, 0x07, 0x42, 0x42, 0xa8, 0x6f, 0xca, + 0x66, 0x3a, 0xa6, 0x99, 0xbe, 0x90, 0x30, 0xbd, 0x31, 0xc5, 0x9c, 0xe1, 0xe1, 0x0a, 0x62, 0x16, + 0xd2, 0x38, 0x96, 0x4c, 0x29, 0xd3, 0x6d, 0x3f, 0x18, 0x14, 0xd8, 0xfb, 0x12, 0x1a, 0x2f, 0xf0, + 0xe1, 0x55, 0xb2, 0x14, 0x29, 0xdb, 0x45, 0x7c, 0xfb, 0xa8, 0x88, 0x8f, 0x08, 0x77, 0xf9, 0xe9, + 0x76, 0x63, 0xa3, 0xbb, 0x8d, 0x8d, 0x7e, 0x6f, 0x6c, 0xf4, 0x6d, 0x6b, 0xb7, 0xee, 0xb6, 0x76, + 0xeb, 0xc7, 0xd6, 0x6e, 0x7d, 0x3e, 0x9f, 0x27, 0x9a, 0x67, 0x91, 0x37, 0x83, 0xa5, 0x1f, 0x53, + 0xc5, 0x05, 0xcd, 0xfd, 0xd2, 0xb4, 0xf8, 0xf3, 0xcb, 0xd7, 0x75, 0xff, 0x6d, 0x46, 0x5d, 0x83, + 0xbf, 0xf9, 0x13, 0x00, 0x00, 0xff, 0xff, 0x0f, 0xc1, 0xfd, 0x1a, 0xb6, 0x03, 0x00, 0x00, } func (m *ValidatorSet) Marshal() (dAtA []byte, err error) { @@ -306,6 +315,11 @@ func (m *ValidatorSet) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.VotingPowerThreshold != 0 { + i = encodeVarintValidator(dAtA, i, uint64(m.VotingPowerThreshold)) + i-- + dAtA[i] = 0x40 + } if m.HasPublicKeys { i-- if m.HasPublicKeys { @@ -513,6 +527,9 @@ func (m *ValidatorSet) Size() (n int) { if m.HasPublicKeys { n += 2 } + if m.VotingPowerThreshold != 0 { + n += 1 + sovValidator(uint64(m.VotingPowerThreshold)) + } return n } @@ -789,6 +806,25 @@ func (m *ValidatorSet) Unmarshal(dAtA []byte) error { } } m.HasPublicKeys = bool(v != 0) + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field VotingPowerThreshold", wireType) + } + m.VotingPowerThreshold = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowValidator + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.VotingPowerThreshold |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipValidator(dAtA[iNdEx:]) diff --git a/proto/tendermint/types/validator.proto b/proto/tendermint/types/validator.proto index 9c0fe6ab1..23fb02d65 100644 --- a/proto/tendermint/types/validator.proto +++ b/proto/tendermint/types/validator.proto @@ -7,13 +7,14 @@ import "gogoproto/gogo.proto"; import "tendermint/crypto/keys.proto"; message ValidatorSet { - repeated Validator validators = 1; - Validator proposer = 2; - int64 total_voting_power = 3; - tendermint.crypto.PublicKey threshold_public_key = 4 [(gogoproto.nullable) = false]; - int32 quorum_type = 5; - bytes quorum_hash = 6; - bool has_public_keys = 7; + repeated Validator validators = 1; + Validator proposer = 2; + int64 total_voting_power = 3; + tendermint.crypto.PublicKey threshold_public_key = 4 [(gogoproto.nullable) = false]; + int32 quorum_type = 5; + bytes quorum_hash = 6; + bool has_public_keys = 7; + uint64 voting_power_threshold = 8; // must equal ValidatorParams.voting_power_threshold } message Validator { diff --git a/types/params.go b/types/params.go index 0528b33fe..b6239f95c 100644 --- a/types/params.go +++ b/types/params.go @@ -242,6 +242,21 @@ func (val *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool { return false } +func (val *ValidatorParams) ToProto() *tmproto.ValidatorParams { + if val == nil { + return nil + } + + return &tmproto.ValidatorParams{ + PubKeyTypes: val.PubKeyTypes, + XVotingPowerThreshold: &tmproto.ValidatorParams_VotingPowerThreshold{ + VotingPowerThreshold: val.VotingPowerThreshold, + }, + } +} + +// ValidatorParamsFromProto returns a ValidatorParams from a protobuf representation. +// If pbValParams is nil, it returns a ValidatorParams with empty PubKeyTypes and 0 VotingPowerThreshold. func ValidatorParamsFromProto(pbValParams *tmproto.ValidatorParams) ValidatorParams { if pbValParams != nil { return ValidatorParams{ @@ -472,12 +487,7 @@ func (params *ConsensusParams) ToProto() tmproto.ConsensusParams { MaxAgeDuration: params.Evidence.MaxAgeDuration, MaxBytes: params.Evidence.MaxBytes, }, - Validator: &tmproto.ValidatorParams{ - PubKeyTypes: params.Validator.PubKeyTypes, - XVotingPowerThreshold: &tmproto.ValidatorParams_VotingPowerThreshold{ - VotingPowerThreshold: params.Validator.VotingPowerThreshold, - }, - }, + Validator: params.Validator.ToProto(), Version: &tmproto.VersionParams{ AppVersion: params.Version.AppVersion, ConsensusVersion: tmproto.VersionParams_ConsensusVersion(params.Version.ConsensusVersion), @@ -519,7 +529,7 @@ func ConsensusParamsFromProto(pbParams tmproto.ConsensusParams) ConsensusParams if pbParams.Validator != nil { c.Validator = ValidatorParams{ - PubKeyTypes: pbParams.Validator.PubKeyTypes, + PubKeyTypes: append([]string{}, pbParams.Validator.PubKeyTypes...), VotingPowerThreshold: pbParams.Validator.GetVotingPowerThreshold(), } } diff --git a/types/validator_set.go b/types/validator_set.go index c689f83da..b81ededb6 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -64,8 +64,9 @@ type ValidatorSet struct { ThresholdPublicKey crypto.PubKey `json:"threshold_public_key"` QuorumHash crypto.QuorumHash `json:"quorum_hash"` QuorumType btcjson.LLMQType `json:"quorum_type"` - Params ValidatorParams `json:"validator_consensus_params"` HasPublicKeys bool `json:"has_public_keys"` + // Threshold power, must equal to ValidatorParams.voting_power_threshold + VotingPowerThreshold uint64 `json:"threshold_power"` } // NewValidatorSet initializes a ValidatorSet by copying over the values from @@ -86,7 +87,7 @@ func NewValidatorSet(valz []*Validator, newThresholdPublicKey crypto.PubKey, quo HasPublicKeys: hasPublicKeys, } if validatorParams != nil { - vals.Params = *validatorParams + vals.VotingPowerThreshold = validatorParams.VotingPowerThreshold } err := vals.updateWithChangeSet(valz, false, newThresholdPublicKey, quorumHash) if err != nil { @@ -156,27 +157,26 @@ func (vals *ValidatorSet) ValidateBasic() error { if err := vals.Proposer().ValidateBasic(); err != nil { return fmt.Errorf("proposer failed validate basic, error: %w", err) } - - if len(vals.Params.PubKeyTypes) != 0 { - for _, validator := range vals.Validators { - if !vals.Params.IsValidPubkeyType(validator.PubKey.Type()) { - return fmt.Errorf( - "validator %s is using pubkey %s, which is unsupported for consensus - expected %v", - validator.String(), - validator.PubKey.Type(), - vals.Params.PubKeyTypes, - ) - } + // TODO: Validate that vals.VotingPowerThreshold == ValidatorParams.voting_power_threshold + threshold := vals.QuorumVotingThresholdPower() + totalPower := vals.TotalVotingPower() + switch len(vals.Validators) { + case 1, 2: + // For validator sets containing 1 or 2 validators, the threshold MUST be equal to the total voting power. + if totalPower != threshold { + return fmt.Errorf("with 1 validator, quorum voting power %d must be equal to threshold %d", totalPower, threshold) } - } - - if vals.Params.VotingPowerThreshold > 0 && vals.QuorumTypeMemberCount() > 3 { - power := vals.QuorumVotingPower() - if power < 0 { - return fmt.Errorf("quorum voting power %d is negative", power) + case 3: + // For validator set with 3 validators, the threshold MUST be equal or greater than 2/3 of the total voting power. + if threshold < totalPower*2/3 { + return fmt.Errorf("%d-members quorum voting power %d is less than threshold %d", + len(vals.Validators), totalPower, vals.VotingPowerThreshold) } - if uint64(power) < vals.Params.VotingPowerThreshold*2/3+1 { - return fmt.Errorf("quorum voting power %d is less than threshold %d", power, vals.Params.VotingPowerThreshold) + default: + // For validator sets containing more than 3 validators, the threshold MUST be at least 2/3 + 1 of the total voting power. + if threshold < (totalPower*2/3)+1 { + return fmt.Errorf("quorum voting power %d is less than threshold %d", totalPower, threshold) + } } @@ -425,8 +425,8 @@ func (vals *ValidatorSet) QuorumVotingPower() int64 { // QuorumVotingThresholdPower returns the threshold power of the voting power of the quorum if all the members existed. // Voting is considered successful when voting power is at or above this threshold. func (vals *ValidatorSet) QuorumVotingThresholdPower() int64 { - if thresholdPower := vals.Params.GetVotingPowerThreshold(); thresholdPower > 0 { - return thresholdPower + if thresholdPower := vals.VotingPowerThreshold; thresholdPower > 0 { + return int64(thresholdPower) } return int64(vals.QuorumTypeThresholdCount()) * DefaultDashVotingPower @@ -934,6 +934,8 @@ func (vals *ValidatorSet) ToProto() (*tmproto.ValidatorSet, error) { // be consistent with cached data vp.TotalVotingPower = 0 + vp.VotingPowerThreshold = vals.VotingPowerThreshold + if vals.ThresholdPublicKey == nil { return nil, fmt.Errorf("thresholdPublicKey is not set") } @@ -988,6 +990,8 @@ func ValidatorSetFromProto(vp *tmproto.ValidatorSet) (*ValidatorSet, error) { } } + vals.VotingPowerThreshold = vp.GetVotingPowerThreshold() + // NOTE: We can't trust the total voting power given to us by other peers. If someone were to // inject a non-zeo value that wasn't the correct voting power we could assume a wrong total // power hence we need to recompute it. diff --git a/types/vote_set_test.go b/types/vote_set_test.go index 9890b7464..c01342eb1 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -564,15 +564,15 @@ func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { numValidators int threshold int }{ - { // threshold allowing 1 vote + { // single node network llmqType: btcjson.LLMQType_100_67, - numValidators: 100, + numValidators: 1, threshold: 1, }, - { // single node network + { // two node network llmqType: btcjson.LLMQType_100_67, numValidators: 2, - threshold: 1, + threshold: 2, }, { // normal network llmqType: btcjson.LLMQType_100_67, @@ -581,7 +581,7 @@ func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { }, { // network below threshold llmqType: btcjson.LLMQType_100_67, - numValidators: 66, + numValidators: 67, threshold: 67, }, } @@ -601,9 +601,6 @@ func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { tt.threshold, ¶ms, ) - assert.GreaterOrEqual(t, len(privValidators), tt.threshold+3, - "need at least %d validators", tt.threshold+3) - blockHash := crypto.CRandBytes(32) stateID := RandStateID() blockPartSetHeader := PartSetHeader{uint32(123), crypto.CRandBytes(32)} @@ -617,19 +614,25 @@ func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { } // we add null vote - blockMaj, anyMaj := castVote(t, BlockID{}, height, round, privValidators, int32(tt.threshold), voteSet) - assert.False(t, blockMaj, "no block majority expected after nil vote") - assert.True(t, anyMaj, "'any' majority expected after nil vote at threshold") - - // at threshold - blockMaj, anyMaj = castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+1), voteSet) - assert.True(t, blockMaj, "block majority expected") - assert.True(t, anyMaj, "'any' majority expected") + if tt.numValidators > tt.threshold { + // we add null vote + blockMaj, anyMaj := castVote(t, BlockID{}, height, round, privValidators, int32(tt.threshold), voteSet) + assert.False(t, blockMaj, "no block majority expected after nil vote") + assert.True(t, anyMaj, "'any' majority expected after nil vote at threshold") + } + if tt.numValidators > tt.threshold+1 { + // at threshold + blockMaj, anyMaj := castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+1), voteSet) + assert.True(t, blockMaj, "block majority expected") + assert.True(t, anyMaj, "'any' majority expected") + } // above threshold - blockMaj, anyMaj = castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+2), voteSet) - assert.True(t, blockMaj, "block majority expected") - assert.True(t, anyMaj, "'any' majority expected") + if tt.numValidators > tt.threshold+2 { + blockMaj, anyMaj := castVote(t, votedBlock, height, round, privValidators, int32(tt.threshold+2), voteSet) + assert.True(t, blockMaj, "block majority expected") + assert.True(t, anyMaj, "'any' majority expected") + } }) } } From 17b131c81617fa23fdf6b1e181a29a84fd648a0a Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 14 Feb 2025 11:22:32 +0100 Subject: [PATCH 4/4] chore: validator threshold optional handling - WIP, panics in gogoproto --- internal/consensus/replayer.go | 14 +++++--- internal/consensus/replayer_test.go | 3 ++ internal/state/current_round_state.go | 4 +++ internal/test/factory/validator_set.go | 9 ++++- proto/tendermint/types/params.proto | 6 ++-- types/params.go | 49 ++++++++++++-------------- types/params_test.go | 2 +- types/validator_set.go | 6 ++-- types/vote_set_test.go | 3 +- 9 files changed, 57 insertions(+), 39 deletions(-) diff --git a/internal/consensus/replayer.go b/internal/consensus/replayer.go index 4fd5e8645..45b39cbd4 100644 --- a/internal/consensus/replayer.go +++ b/internal/consensus/replayer.go @@ -342,14 +342,18 @@ func (r *BlockReplayer) execInitChain(ctx context.Context, rs *replayState, stat quorumType = r.genDoc.QuorumType } + var valParams *types.ValidatorParams + if res.ConsensusParams != nil && res.ConsensusParams.Validator != nil { + params := types.ValidatorParamsFromProto(res.ConsensusParams.Validator) + valParams = ¶ms + } + if len(res.ValidatorSetUpdate.ValidatorUpdates) != 0 { // we replace existing validator with the one from InitChain instead of applying it as a diff - var valParams *types.ValidatorParams - if res.ConsensusParams != nil && res.ConsensusParams.Validator != nil { - params := types.ValidatorParamsFromProto(res.ConsensusParams.Validator) - valParams = ¶ms - } state.Validators = types.NewValidatorSet(nil, nil, quorumType, nil, false, valParams) + // } else if valParams != nil && valParams.VotingPowerThreshold != nil { + // // we update the existing validator set with the new voting threshold + // state.Validators.VotingPowerThreshold = *valParams.VotingPowerThreshold } // we only update state when we are in initial state diff --git a/internal/consensus/replayer_test.go b/internal/consensus/replayer_test.go index 14c952f41..440c2719e 100644 --- a/internal/consensus/replayer_test.go +++ b/internal/consensus/replayer_test.go @@ -198,6 +198,9 @@ func TestInitChainGenesisTime(t *testing.T) { QuorumHash: make([]byte, 32), GenesisTime: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), } + // We must synchronize genesis validator set voting threshold with the one from the validator set. + // In normal cicrumstances, the threshold is set in genesis or in ResponseInitChain. + genDoc.ConsensusParams.Validator.VotingPowerThreshold = &vset.VotingPowerThreshold err = genDoc.ValidateAndComplete() require.NoError(t, err) diff --git a/internal/state/current_round_state.go b/internal/state/current_round_state.go index 3842de27a..6e328ebcc 100644 --- a/internal/state/current_round_state.go +++ b/internal/state/current_round_state.go @@ -340,6 +340,10 @@ func valsetUpdate( if thresholdPubKey != nil { nValSet.ThresholdPublicKey = thresholdPubKey } + + if params.VotingPowerThreshold != nil { + nValSet.VotingPowerThreshold = *params.VotingPowerThreshold + } } return nValSet, nValSet.ValidateBasic() diff --git a/internal/test/factory/validator_set.go b/internal/test/factory/validator_set.go index 624c2f5fc..de2d2e8f8 100644 --- a/internal/test/factory/validator_set.go +++ b/internal/test/factory/validator_set.go @@ -44,5 +44,12 @@ func MockValidatorSet() (*types.ValidatorSet, []types.PrivValidator) { false, ) } - return types.NewValidatorSet(valz, thPubKey, btcjson.LLMQType_5_60, quorumHash, true, nil), privVals + votingPowerThreshold := uint64(len(valz)) * uint64(types.DefaultDashVotingPower) + return types.NewValidatorSet(valz, + thPubKey, + btcjson.LLMQType_5_60, + quorumHash, + true, + &types.ValidatorParams{VotingPowerThreshold: &votingPowerThreshold}, + ), privVals } diff --git a/proto/tendermint/types/params.proto b/proto/tendermint/types/params.proto index c549b2fcc..7e063e349 100644 --- a/proto/tendermint/types/params.proto +++ b/proto/tendermint/types/params.proto @@ -72,9 +72,11 @@ message ValidatorParams { // 3. For validator sets containing more than 3 validators, the threshold MUST be greater than 2/3 of the // total voting power. // - // If set to 0 or absent, default value is determined based on LLMQ quorum type defined for the network, + // If set to 0, default value is determined based on LLMQ quorum type defined for the network, // with a fall back to 2/3 + 1 of the total voting power. - optional uint64 voting_power_threshold = 2; + // + // If absent, + optional uint64 voting_power_threshold = 2; //[(gogoproto.nullable) = true] } // VersionParams contains the ABCI application version. diff --git a/types/params.go b/types/params.go index b6239f95c..cd12ea9bb 100644 --- a/types/params.go +++ b/types/params.go @@ -4,7 +4,6 @@ import ( "crypto/sha256" "errors" "fmt" - "math" "os" "time" @@ -71,7 +70,7 @@ type EvidenceParams struct { // NOTE: uses ABCI pubkey naming, not Amino names. type ValidatorParams struct { PubKeyTypes []string `json:"pub_key_types"` - VotingPowerThreshold uint64 `json:"threshold"` + VotingPowerThreshold *uint64 `json:"threshold,omitempty"` } type VersionParams struct { @@ -144,7 +143,7 @@ func DefaultEvidenceParams() EvidenceParams { func DefaultValidatorParams() ValidatorParams { return ValidatorParams{ PubKeyTypes: []string{ABCIPubKeyTypeBLS12381}, - VotingPowerThreshold: 0, + VotingPowerThreshold: nil, } } @@ -246,12 +245,16 @@ func (val *ValidatorParams) ToProto() *tmproto.ValidatorParams { if val == nil { return nil } + var threshold *tmproto.ValidatorParams_VotingPowerThreshold + if val.VotingPowerThreshold != nil { + threshold = &tmproto.ValidatorParams_VotingPowerThreshold{ + VotingPowerThreshold: uint64(*val.VotingPowerThreshold), + } + } return &tmproto.ValidatorParams{ - PubKeyTypes: val.PubKeyTypes, - XVotingPowerThreshold: &tmproto.ValidatorParams_VotingPowerThreshold{ - VotingPowerThreshold: val.VotingPowerThreshold, - }, + PubKeyTypes: val.PubKeyTypes, + XVotingPowerThreshold: threshold, } } @@ -259,36 +262,26 @@ func (val *ValidatorParams) ToProto() *tmproto.ValidatorParams { // If pbValParams is nil, it returns a ValidatorParams with empty PubKeyTypes and 0 VotingPowerThreshold. func ValidatorParamsFromProto(pbValParams *tmproto.ValidatorParams) ValidatorParams { if pbValParams != nil { + var threshold *uint64 + if pbValParams.XVotingPowerThreshold != nil { + val := pbValParams.GetVotingPowerThreshold() + threshold = &val + } + return ValidatorParams{ // Copy Validator.PubkeyTypes, and set result's value to the copy. // This avoids having to initialize the slice to 0 values, and then write to it again. PubKeyTypes: append([]string{}, pbValParams.PubKeyTypes...), - VotingPowerThreshold: pbValParams.GetVotingPowerThreshold(), + VotingPowerThreshold: threshold, } } return ValidatorParams{ PubKeyTypes: []string{}, - VotingPowerThreshold: 0, + VotingPowerThreshold: nil, } } -// GetVotingPowerThreshold returns the voting power threshold for the validator set. -// For nil ValidatorParams, it returns 0. -// -// Value of 0 means that the threshold is not set. -func (val *ValidatorParams) GetVotingPowerThreshold() int64 { - if val != nil { - //nolint:goosec - if val.VotingPowerThreshold > math.MaxInt64 || int64(val.VotingPowerThreshold) > MaxTotalVotingPower { - // this should never happen - panic(fmt.Sprintf("VotingPowerThreshold %d is too big", val.VotingPowerThreshold)) - } - return int64(val.VotingPowerThreshold) - } - return 0 -} - func (params *ConsensusParams) Complete() { if params.Synchrony == (SynchronyParams{}) { params.Synchrony = DefaultSynchronyParams() @@ -528,9 +521,13 @@ func ConsensusParamsFromProto(pbParams tmproto.ConsensusParams) ConsensusParams } if pbParams.Validator != nil { + var threshold *uint64 + if n, ok := pbParams.Validator.XVotingPowerThreshold.(*tmproto.ValidatorParams_VotingPowerThreshold); ok && n != nil { + threshold = &n.VotingPowerThreshold + } c.Validator = ValidatorParams{ PubKeyTypes: append([]string{}, pbParams.Validator.PubKeyTypes...), - VotingPowerThreshold: pbParams.Validator.GetVotingPowerThreshold(), + VotingPowerThreshold: threshold, } } diff --git a/types/params_test.go b/types/params_test.go index 241181233..781e9d7c6 100644 --- a/types/params_test.go +++ b/types/params_test.go @@ -219,7 +219,7 @@ func makeParams(args makeParamsArgs) ConsensusParams { }, Validator: ValidatorParams{ PubKeyTypes: args.pubkeyTypes, - VotingPowerThreshold: 0, + VotingPowerThreshold: nil, }, Synchrony: SynchronyParams{ Precision: args.precision, diff --git a/types/validator_set.go b/types/validator_set.go index b81ededb6..569f87403 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -86,8 +86,8 @@ func NewValidatorSet(valz []*Validator, newThresholdPublicKey crypto.PubKey, quo QuorumType: quorumType, HasPublicKeys: hasPublicKeys, } - if validatorParams != nil { - vals.VotingPowerThreshold = validatorParams.VotingPowerThreshold + if validatorParams != nil && validatorParams.VotingPowerThreshold != nil { + vals.VotingPowerThreshold = *validatorParams.VotingPowerThreshold } err := vals.updateWithChangeSet(valz, false, newThresholdPublicKey, quorumHash) if err != nil { @@ -164,7 +164,7 @@ func (vals *ValidatorSet) ValidateBasic() error { case 1, 2: // For validator sets containing 1 or 2 validators, the threshold MUST be equal to the total voting power. if totalPower != threshold { - return fmt.Errorf("with 1 validator, quorum voting power %d must be equal to threshold %d", totalPower, threshold) + return fmt.Errorf("with 1 or 2 validators, quorum voting power %d must be equal to threshold %d", totalPower, threshold) } case 3: // For validator set with 3 validators, the threshold MUST be equal or greater than 2/3 of the total voting power. diff --git a/types/vote_set_test.go b/types/vote_set_test.go index c01342eb1..8be26dad0 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -589,8 +589,9 @@ func TestVoteSet_ValidatorParams_Threshold(t *testing.T) { for ti, tt := range testCases { name := strconv.Itoa(ti) t.Run(name, func(t *testing.T) { + threshold := uint64(int64(tt.threshold) * DefaultDashVotingPower) params := ValidatorParams{ - VotingPowerThreshold: uint64(int64(tt.threshold) * DefaultDashVotingPower), + VotingPowerThreshold: &threshold, } voteSet, _, privValidators := randVoteSetWithLLMQType( height,