Skip to content

Commit

Permalink
test: vote extension test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Jan 29, 2024
1 parent a10c82a commit 638d475
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
18 changes: 4 additions & 14 deletions crypto/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ func (i *SignItem) UpdateSignHash(reverse bool) {
messageHash = tmbytes.Reverse(messageHash)
}

// if testing.Testing() {
// fmt.Printf("generating sign ID using bls.BuildSignHash for %d %X %X %X\n", llmqType, quorumHash, requestID, messageHash)
// out := append([]byte{byte(llmqType)}, quorumHash...)
// out = append(out, requestID...)
// out = append(out, messageHash...)

// fmt.Printf("data before sha256: %X\n", out)
// fmt.Printf("sha256(sha256(data)): %X\n", crypto.Checksum((crypto.Checksum(out))))
// }

var blsQuorumHash bls.Hash
copy(blsQuorumHash[:], quorumHash)

Expand All @@ -123,10 +113,10 @@ func (i *SignItem) UpdateSignHash(reverse bool) {
var blsMessageHash bls.Hash
copy(blsMessageHash[:], messageHash)

fmt.Printf("LlmqType: %x + ", llmqType)
fmt.Printf("QuorumHash: %x + ", blsQuorumHash)
fmt.Printf("RequestID: %x + ", blsRequestID)
fmt.Printf("MsgHash: %x\n", blsMessageHash)
// fmt.Printf("LlmqType: %x + ", llmqType)
// fmt.Printf("QuorumHash: %x + ", blsQuorumHash)
// fmt.Printf("RequestID: %x + ", blsRequestID)
// fmt.Printf("MsgHash: %x\n", blsMessageHash)

blsSignHash := bls.BuildSignHash(uint8(llmqType), blsQuorumHash, blsRequestID, blsMessageHash)

Expand Down
3 changes: 3 additions & 0 deletions types/vote_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ func (e ThresholdRawVoteExtension) Copy() VoteExtensionIf {
return &ThresholdRawVoteExtension{ThresholdVoteExtension: *inner}
}

// SignItem creates a SignItem for a threshold raw vote extension
//
// Note: signItem.Msg left empty by purpose, as we don't want hash to be checked in Verify()
func (e ThresholdRawVoteExtension) SignItem(_ string, height int64, round int32, quorumType btcjson.LLMQType, quorumHash []byte) (crypto.SignItem, error) {
var signRequestID []byte
var err error
Expand Down
23 changes: 15 additions & 8 deletions types/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestVoteExtension(t *testing.T) {
XSignRequestId: &tmproto.VoteExtension_SignRequestId{
SignRequestId: []byte("\x06plwdtx"),
},
Extension: []byte("extension")}),
Extension: bytes.Repeat([]byte("extensio"), 4)}), // must be 32 bytes
includeSignature: true,
expectError: false,
},
Expand All @@ -356,7 +356,7 @@ func TestVoteExtension(t *testing.T) {
XSignRequestId: &tmproto.VoteExtension_SignRequestId{
SignRequestId: []byte("dpevote"),
},
Extension: []byte("extension")}),
Extension: bytes.Repeat([]byte("extensio"), 4)}), // must be 32 bytes
includeSignature: true,
expectError: false,
},
Expand Down Expand Up @@ -684,22 +684,29 @@ func TestVoteExtensionsSignBytes(t *testing.T) {
//
// Given some vote extension, SignBytes or THRESHOLD_RECOVER_RAW returns that extension.
func TestVoteExtensionsSignBytesRaw(t *testing.T) {
expect := []byte{1, 2, 3, 4, 5, 6, 7, 8}
extension := bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 4)
quorumHash := bytes.Repeat([]byte{8, 7, 6, 5, 4, 3, 2, 1}, 4)
expectedSignHash := []byte{0xe, 0x88, 0x8d, 0xa8, 0x97, 0xf1, 0xc0, 0xfd, 0x6a, 0xe8, 0x3b, 0x77, 0x9b, 0x5, 0xdd,
0x28, 0xc, 0xe2, 0x58, 0xf6, 0x4c, 0x86, 0x1, 0x34, 0xfa, 0x4, 0x27, 0xe1, 0xaa, 0xab, 0x1a, 0xde}

assert.Len(t, extension, 32)

ve := tmproto.VoteExtension{
Extension: expect,
Extension: extension,
Signature: []byte{},
Type: tmproto.VoteExtensionType_THRESHOLD_RECOVER_RAW,
XSignRequestId: &tmproto.VoteExtension_SignRequestId{
SignRequestId: []byte("dpevote-someSignRequestID"),
},
}
signItem, err := VoteExtensionFromProto(ve).SignItem("some-chain", 1, 2, btcjson.LLMQType_TEST_PLATFORM, crypto.RandQuorumHash())

signItem, err := VoteExtensionFromProto(ve).SignItem("some-chain", 1, 2, btcjson.LLMQType_TEST_PLATFORM, quorumHash)
assert.NoError(t, err)

actual := signItem.Msg
actual := signItem.SignHash

t.Logf("sign bytes: %x", actual)
assert.EqualValues(t, expect, actual)
t.Logf("sign hash: %x", actual)
assert.EqualValues(t, expectedSignHash, actual)
}

func TestVoteProtobuf(t *testing.T) {
Expand Down

0 comments on commit 638d475

Please sign in to comment.