Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tholonious committed Jun 21, 2023
1 parent 31e2f20 commit 6f188a2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 93 deletions.
2 changes: 1 addition & 1 deletion lib/db_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func _GetTestBlockNode() *BlockNode {

// Header (make a copy)
bs.Header = NewMessage(MsgTypeHeader).(*MsgDeSoHeader)
headerBytes, _ := expectedBlockHeader.ToBytes(false)
headerBytes, _ := expectedBlockHeaderVersion1.ToBytes(false)
bs.Header.FromBytes(headerBytes)

// Status
Expand Down
92 changes: 79 additions & 13 deletions lib/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lib
import (
"bytes"
"encoding/hex"
"github.com/holiman/uint256"
"math/big"
"math/rand"
"reflect"
Expand All @@ -12,6 +11,8 @@ import (
"testing"
"time"

"github.com/holiman/uint256"

"github.com/btcsuite/btcd/btcec"

"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestVerack(t *testing.T) {
require.Equal(&MsgDeSoVerack{Nonce: nonce}, testMsg)
}

var expectedBlockHeader = &MsgDeSoHeader{
var expectedBlockHeaderVersion1 = &MsgDeSoHeader{
Version: 1,
PrevBlockHash: &BlockHash{
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11,
Expand All @@ -104,14 +105,76 @@ var expectedBlockHeader = &MsgDeSoHeader{
ExtraNonce: uint64(101234123456789),
}

// Creates fully formatted a PoS block header with random signatures
// and block hashes
func createTestBlockHeaderVersion2(t *testing.T) *MsgDeSoHeader {
testBlockHash := BlockHash{
0x00, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21,
0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31,
0x32, 0x33,
}
testMerkleRoot := BlockHash{
0x00, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43,
0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53,
0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60, 0x61, 0x62, 0x63,
0x64, 0x65,
}

_, testBLSSignature := _generateValidatorVotingPublicKeyAndSignature(t)

return &MsgDeSoHeader{
Version: 2,
PrevBlockHash: &testBlockHash,
TransactionMerkleRoot: &testMerkleRoot,
TstampSecs: uint64(1678943210),
Height: uint64(1321012345),
// Nonce and ExtraNonce are unused and set to 0 starting in version 2.
Nonce: uint64(0),
ExtraNonce: uint64(0),
// Use real signatures and public keys for the PoS fields
ValidatorsVoteQC: &QuorumCertificate{
BlockHash: &testBlockHash,
ProposedInView: uint64(123456789123),
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{1},
Signature: testBLSSignature,
},
},
ValidatorsTimeoutAggregateQC: &TimeoutAggregateQuorumCertificate{
TimedOutView: uint64(234567891234),
ValidatorsHighQC: &QuorumCertificate{
BlockHash: &testBlockHash,
ProposedInView: uint64(345678912345),
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{2},
Signature: testBLSSignature,
},
},
ValidatorsTimeoutHighQCViews: []uint64{456789123456},
ValidatorsTimeoutAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{3},
Signature: testBLSSignature,
},
},
}
}

func TestHeaderConversionAndReadWriteMessage(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
_ = assert
_ = require
networkType := NetworkType_MAINNET

{
expectedBlockHeadersToTest := []*MsgDeSoHeader{
expectedBlockHeaderVersion1,
createTestBlockHeaderVersion2(t),
}

// Performs a full E2E byte encode and decode of all the block header
// version we want to test.
for _, expectedBlockHeader := range expectedBlockHeadersToTest {
data, err := expectedBlockHeader.ToBytes(false)
assert.NoError(err)

Expand Down Expand Up @@ -143,12 +206,12 @@ func TestHeaderConversionAndReadWriteMessage(t *testing.T) {
hdrPayload, err := expectedBlockHeader.ToBytes(false)
assert.NoError(err)
assert.Equal(hdrPayload, data)
}

assert.Equalf(7, reflect.TypeOf(expectedBlockHeader).Elem().NumField(),
"Number of fields in HEADER message is different from expected. "+
"Did you add a new field? If so, make sure the serialization code "+
"works, add the new field to the test case, and fix this error.")
assert.Equalf(9, reflect.TypeOf(expectedBlockHeader).Elem().NumField(),
"Number of fields in HEADER message is different from expected. "+
"Did you add a new field? If so, make sure the serialization code "+
"works, add the new field to the test case, and fix this error.")
}
}

func TestGetHeadersSerialization(t *testing.T) {
Expand All @@ -157,8 +220,8 @@ func TestGetHeadersSerialization(t *testing.T) {
_ = assert
_ = require

hash1 := expectedBlockHeader.PrevBlockHash
hash2 := expectedBlockHeader.TransactionMerkleRoot
hash1 := expectedBlockHeaderVersion1.PrevBlockHash
hash2 := expectedBlockHeaderVersion1.TransactionMerkleRoot

getHeaders := &MsgDeSoGetHeaders{
StopHash: hash1,
Expand All @@ -179,10 +242,13 @@ func TestHeaderBundleSerialization(t *testing.T) {
_ = assert
_ = require

hash1 := expectedBlockHeader.PrevBlockHash
hash1 := expectedBlockHeaderVersion1.PrevBlockHash

headerBundle := &MsgDeSoHeaderBundle{
Headers: []*MsgDeSoHeader{expectedBlockHeader, expectedBlockHeader},
Headers: []*MsgDeSoHeader{
expectedBlockHeaderVersion1,
createTestBlockHeaderVersion2(t),
},
TipHash: hash1,
TipHeight: 12345,
}
Expand Down Expand Up @@ -256,7 +322,7 @@ func TestReadWrite(t *testing.T) {
}

var expectedBlock = &MsgDeSoBlock{
Header: expectedBlockHeader,
Header: expectedBlockHeaderVersion1,
Txns: expectedTransactions(true), // originally was effectively false

BlockProducerInfo: &BlockProducerInfo{
Expand Down
58 changes: 43 additions & 15 deletions lib/pos_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,43 @@ type TimeoutAggregateQuorumCertificate struct {
ValidatorsHighQC *QuorumCertificate

// Here we include a list of the HighQC.View values we got from each of the
// validators in the ValidatorTimeoutHighQCViews field. In addition, for each
// validators in the ValidatorsTimeoutHighQCViews field. In addition, for each
// unique HighQC.View value we received, we combine all the partial signatures
// for that HighQC.View into a single BLSMultiSignature.
//
// Note: The aggregated signature is made up of partial signatures for all
// present validators, each of whom signed a payload with the pair
// (current view, the validator's local HighQC.View).
ValidatorTimeoutHighQCViews []uint64
ValidatorTimeoutAggregatedSignature *AggregatedBLSSignature
ValidatorsTimeoutHighQCViews []uint64
ValidatorsTimeoutAggregatedSignature *AggregatedBLSSignature
}

// Performs a deep equality check between two TimeoutAggregateQuorumCertificate, and
// returns true if the values of the two are identical.
func (aggQC *TimeoutAggregateQuorumCertificate) Eq(
other *TimeoutAggregateQuorumCertificate,
) bool {
if aggQC == nil && other == nil {
return true
}

if (aggQC == nil) != (other == nil) {
return false
}

if len(aggQC.ValidatorsTimeoutHighQCViews) != len(other.ValidatorsTimeoutHighQCViews) {
return false
}

for i := 0; i < len(aggQC.ValidatorsTimeoutHighQCViews); i++ {
if aggQC.ValidatorsTimeoutHighQCViews[i] != other.ValidatorsTimeoutHighQCViews[i] {
return false
}
}

return aggQC.TimedOutView == other.TimedOutView &&
aggQC.ValidatorsHighQC.Eq(other.ValidatorsHighQC) &&
aggQC.ValidatorsTimeoutAggregatedSignature.Eq(other.ValidatorsTimeoutAggregatedSignature)
}

func (aggQC *TimeoutAggregateQuorumCertificate) ToBytes() ([]byte, error) {
Expand All @@ -450,18 +478,18 @@ func (aggQC *TimeoutAggregateQuorumCertificate) ToBytes() ([]byte, error) {
}
retBytes = append(retBytes, encodedValidatorsHighQC...)

// ValidatorTimeoutHighQCViews
retBytes = append(retBytes, EncodeUint64Array(aggQC.ValidatorTimeoutHighQCViews)...)
// ValidatorsTimeoutHighQCViews
retBytes = append(retBytes, EncodeUint64Array(aggQC.ValidatorsTimeoutHighQCViews)...)

// ValidatorTimeoutAggregatedSignature
if aggQC.ValidatorTimeoutAggregatedSignature == nil {
return nil, errors.New("TimeoutAggregateQuorumCertificate.ToBytes: ValidatorTimeoutAggregatedSignature must not be nil")
// ValidatorsTimeoutAggregatedSignature
if aggQC.ValidatorsTimeoutAggregatedSignature == nil {
return nil, errors.New("TimeoutAggregateQuorumCertificate.ToBytes: ValidatorsTimeoutAggregatedSignature must not be nil")
}
encodedValidatorTimeoutAggregatedSignature, err := aggQC.ValidatorTimeoutAggregatedSignature.ToBytes()
encodedValidatorsTimeoutAggregatedSignature, err := aggQC.ValidatorsTimeoutAggregatedSignature.ToBytes()
if err != nil {
return nil, errors.Wrapf(err, "TimeoutAggregateQuorumCertificate.ToBytes: Error encoding ValidatorTimeoutAggregatedSignature")
return nil, errors.Wrapf(err, "TimeoutAggregateQuorumCertificate.ToBytes: Error encoding ValidatorsTimeoutAggregatedSignature")
}
retBytes = append(retBytes, encodedValidatorTimeoutAggregatedSignature...)
retBytes = append(retBytes, encodedValidatorsTimeoutAggregatedSignature...)

return retBytes, nil
}
Expand All @@ -480,14 +508,14 @@ func DecodeTimeoutAggregateQuorumCertificate(rr io.Reader) (*TimeoutAggregateQuo
return nil, errors.Wrapf(err, "DecodeTimeoutAggregateQuorumCertificate: Error decoding ValidatorsHighQC")
}

aggQC.ValidatorTimeoutHighQCViews, err = DecodeUint64Array(rr)
aggQC.ValidatorsTimeoutHighQCViews, err = DecodeUint64Array(rr)
if err != nil {
return nil, errors.Wrapf(err, "DecodeTimeoutAggregateQuorumCertificate: Error decoding ValidatorTimeoutHighQCViews")
return nil, errors.Wrapf(err, "DecodeTimeoutAggregateQuorumCertificate: Error decoding ValidatorsTimeoutHighQCViews")
}

aggQC.ValidatorTimeoutAggregatedSignature, err = DecodeAggregatedBLSSignature(rr)
aggQC.ValidatorsTimeoutAggregatedSignature, err = DecodeAggregatedBLSSignature(rr)
if err != nil {
return nil, errors.Wrapf(err, "DecodeTimeoutAggregateQuorumCertificate: Error decoding ValidatorTimeoutAggregatedSignature")
return nil, errors.Wrapf(err, "DecodeTimeoutAggregateQuorumCertificate: Error decoding ValidatorsTimeoutAggregatedSignature")
}

return &aggQC, nil
Expand Down
64 changes: 0 additions & 64 deletions lib/pos_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,70 +90,6 @@ func TestValidatorTimeoutEncodeDecode(t *testing.T) {
)
}

func TestMsgDeSoHeaderVersion2EncodeDecode(t *testing.T) {
_, votePartialSignature1 := _generateValidatorVotingPublicKeyAndSignature(t)
_, votePartialSignature2 := _generateValidatorVotingPublicKeyAndSignature(t)
_, votePartialSignature3 := _generateValidatorVotingPublicKeyAndSignature(t)

originalMsg := MsgDeSoHeader{
Version: HeaderVersion2,
PrevBlockHash: &BlockHash{},
TransactionMerkleRoot: &BlockHash{},
TstampSecs: 9910,
Height: 9911,
Nonce: 0,
ExtraNonce: 0,
ValidatorsVoteQC: &QuorumCertificate{
BlockHash: &BlockHash{},
ProposedInView: 9912,
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{3},
Signature: votePartialSignature1,
},
},
ValidatorsTimeoutAggregateQC: &TimeoutAggregateQuorumCertificate{
TimedOutView: 9913,
ValidatorsHighQC: &QuorumCertificate{
BlockHash: &BlockHash{},
ProposedInView: 9914,
ValidatorsVoteAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{4},
Signature: votePartialSignature2,
},
},
ValidatorTimeoutHighQCViews: []uint64{9915, 9916},
ValidatorTimeoutAggregatedSignature: &AggregatedBLSSignature{
SignersList: []byte{5},
Signature: votePartialSignature3,
},
},
}

// Encode the message and verify the length is correct.
encodedMsgBytes, err := originalMsg.ToBytes(false)
require.NoError(t, err)
require.Equal(t, 312, len(encodedMsgBytes))

// Decode the message.
decodedMsg := &MsgDeSoHeader{}
err = decodedMsg.FromBytes(encodedMsgBytes)
require.NoError(t, err)

// Check that the message versions are the same.
require.Equal(t, originalMsg.Version, decodedMsg.Version)
require.Equal(t, originalMsg.PrevBlockHash, decodedMsg.PrevBlockHash)
require.Equal(t, originalMsg.TransactionMerkleRoot, decodedMsg.TransactionMerkleRoot)
require.Equal(t, originalMsg.TstampSecs, decodedMsg.TstampSecs)
require.Equal(t, originalMsg.Height, decodedMsg.Height)
require.Equal(t, originalMsg.Nonce, decodedMsg.Nonce)
require.Equal(t, originalMsg.ExtraNonce, decodedMsg.ExtraNonce)
require.Equal(t, originalMsg.ValidatorsVoteQC.BlockHash, decodedMsg.ValidatorsVoteQC.BlockHash)
require.Equal(t, originalMsg.ValidatorsVoteQC.ProposedInView, decodedMsg.ValidatorsVoteQC.ProposedInView)
require.Equal(t, originalMsg.ValidatorsTimeoutAggregateQC.TimedOutView, decodedMsg.ValidatorsTimeoutAggregateQC.TimedOutView)
require.Equal(t, originalMsg.ValidatorsTimeoutAggregateQC.ValidatorsHighQC.BlockHash, decodedMsg.ValidatorsTimeoutAggregateQC.ValidatorsHighQC.BlockHash)
// require.True(t, originalMsg.VotePartialSignature.Eq(decodedMsg.VotePartialSignature))
}

// Creates an arbitrary BLS public key and signature for testing.
func _generateValidatorVotingPublicKeyAndSignature(t *testing.T) (*bls.PublicKey, *bls.Signature) {
blsPrivateKey, err := bls.NewPrivateKey()
Expand Down

0 comments on commit 6f188a2

Please sign in to comment.