Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoS Add Bitset to messaging schema #565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions lib/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/btcsuite/btcd/wire"
"github.com/bxcodec/faker"
"github.com/deso-protocol/core/utils/bitset"
merkletree "github.com/deso-protocol/go-merkle-tree"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -121,6 +122,7 @@ func createTestBlockHeaderVersion2(t *testing.T) *MsgDeSoHeader {
0x64, 0x65,
}

testBitset := bitset.NewBitset().Set(0, true).Set(3, true)
_, testBLSSignature := _generateValidatorVotingPublicKeyAndSignature(t)

return &MsgDeSoHeader{
Expand All @@ -137,7 +139,7 @@ func createTestBlockHeaderVersion2(t *testing.T) *MsgDeSoHeader {
BlockHash: &testBlockHash,
ProposedInView: uint64(123456789123),
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{1},
SignersList: testBitset,
Signature: testBLSSignature,
},
},
Expand All @@ -147,13 +149,13 @@ func createTestBlockHeaderVersion2(t *testing.T) *MsgDeSoHeader {
BlockHash: &testBlockHash,
ProposedInView: uint64(345678912345),
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{2},
SignersList: testBitset,
Signature: testBLSSignature,
},
},
ValidatorsTimeoutHighQCViews: []uint64{456789123456},
ValidatorsTimeoutAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{3},
SignersList: testBitset,
Signature: testBLSSignature,
},
},
Expand Down
39 changes: 32 additions & 7 deletions lib/pos_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"

"github.com/deso-protocol/core/bls"
"github.com/deso-protocol/core/utils/bitset"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -350,9 +351,7 @@ func DecodeQuorumCertificate(rr io.Reader) (*QuorumCertificate, error) {
// present in the aggregated signature. The indices of all validators are expected
// to be known by the caller.
type AggregatedBLSSignature struct {
// TODO: Switch this to a bitlist, which will result in ~8x reduction in total
// size of this construct.
SignersList []byte
SignersList *bitset.Bitset
Signature *bls.Signature
}

Expand All @@ -365,15 +364,21 @@ func (sig *AggregatedBLSSignature) Eq(other *AggregatedBLSSignature) bool {
return false
}

return bytes.Equal(sig.SignersList, other.SignersList) &&
sig.Signature.Eq(other.Signature)
if !sig.Signature.Eq(other.Signature) {
return false
}

return bytes.Equal(sig.SignersList.ToBytes(), other.SignersList.ToBytes())
}

func (sig *AggregatedBLSSignature) ToBytes() ([]byte, error) {
retBytes := []byte{}

// SignersList
retBytes = append(retBytes, EncodeByteArray(sig.SignersList)...)
if sig.SignersList == nil {
return nil, errors.New("AggregatedBLSSignature.ToBytes: SignersList must not be nil")
}
retBytes = append(retBytes, EncodeBitset(sig.SignersList)...)

// Signature
if sig.Signature == nil {
Expand All @@ -387,7 +392,7 @@ func (sig *AggregatedBLSSignature) ToBytes() ([]byte, error) {
func (sig *AggregatedBLSSignature) FromBytes(rr io.Reader) error {
var err error

sig.SignersList, err = DecodeByteArray(rr)
sig.SignersList, err = DecodeBitset(rr)
if err != nil {
return errors.Wrapf(err, "AggregatedBLSSignature.FromBytes: Error decoding SignersList")
}
Expand Down Expand Up @@ -531,3 +536,23 @@ func (aggQC *TimeoutAggregateQuorumCertificate) FromBytes(rr io.Reader) error {

return nil
}

// ==================================================================
// Bitset Utils
// ==================================================================

func EncodeBitset(b *bitset.Bitset) []byte {
var encodedBytes []byte
if b != nil {
encodedBytes = b.ToBytes()
}
return EncodeByteArray(encodedBytes)
}

func DecodeBitset(rr io.Reader) (*bitset.Bitset, error) {
encodedBytes, err := DecodeByteArray(rr)
if err != nil {
return nil, errors.Wrapf(err, "DecodeBitset: Error decoding bitset")
}
return (bitset.NewBitset()).FromBytes(encodedBytes), nil
}
5 changes: 3 additions & 2 deletions lib/pos_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/deso-protocol/core/bls"
"github.com/deso-protocol/core/utils/bitset"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func TestValidatorTimeoutEncodeDecode(t *testing.T) {
BlockHash: &BlockHash{},
ProposedInView: 999910,
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{1, 2},
SignersList: bitset.NewBitset().Set(0, true).Set(3, true),
Signature: aggregateSignature,
},
},
Expand All @@ -68,7 +69,7 @@ func TestValidatorTimeoutEncodeDecode(t *testing.T) {
// Encode the message and verify the length is correct.
encodedMsgBytes, err := originalMsg.ToBytes(false)
require.NoError(t, err)
require.Equal(t, 270, len(encodedMsgBytes))
require.Equal(t, 269, len(encodedMsgBytes))

// Decode the message.
decodedMsg := &MsgDeSoValidatorTimeout{}
Expand Down
3 changes: 2 additions & 1 deletion utils/bitset/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func (b *Bitset) ToBytes() []byte {
}

// Populates the BitSet from a big-endian byte slice.
func (b *Bitset) FromBytes(bytes []byte) {
func (b *Bitset) FromBytes(bytes []byte) *Bitset {
if b.store == nil {
b.store = big.NewInt(0)
}
b.store.SetBytes(bytes)
return b
}