From d9eca5e0905bb7c67d85bcb0712309dfd4dc3351 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 3 Jan 2024 14:38:27 +0100 Subject: [PATCH] chore: simplify code --- types/vote_extension.go | 49 +++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/types/vote_extension.go b/types/vote_extension.go index fd026b5b7d..fc8aa79ef1 100644 --- a/types/vote_extension.go +++ b/types/vote_extension.go @@ -330,7 +330,24 @@ func (e GenericVoteExtension) ToProto() tmproto.VoteExtension { } func (e GenericVoteExtension) SignItem(chainID string, height int64, round int32, quorumType btcjson.LLMQType, quorumHash []byte) (crypto.SignItem, error) { - return signItem(&e.VoteExtension, chainID, height, round, quorumType, quorumHash) + requestID, err := voteExtensionRequestID(height, round) + if err != nil { + return crypto.SignItem{}, err + } + canonical, err := CanonicalizeVoteExtension(chainID, &e.VoteExtension, height, round) + if err != nil { + panic(err) + } + + signBytes, err := protoio.MarshalDelimited(&canonical) + if err != nil { + panic(err) + } + + si := crypto.NewSignItem(quorumType, quorumHash, requestID, signBytes) + // we do not reverse fields when calculating SignHash for vote extensions + // si.UpdateSignHash(false) + return si, nil } func (e GenericVoteExtension) IsThresholdRecoverable() bool { @@ -466,33 +483,3 @@ func (e ThresholdRawVoteExtension) SignItem(_ string, height int64, round int32, func voteExtensionRequestID(height int64, round int32) ([]byte, error) { return heightRoundRequestID("dpevote", height, round), nil } - -// voteExtensionSignBytes returns the proto-encoding of the canonicalized vote -// extension for signing. Panics if the marshaling fails. -// -// Similar to VoteSignBytes, the encoded Protobuf message is varint -// length-prefixed for backwards-compatibility with the Amino encoding. -func voteExtensionSignBytes(chainID string, height int64, round int32, ext *tmproto.VoteExtension) []byte { - canonical, err := CanonicalizeVoteExtension(chainID, ext, height, round) - if err != nil { - panic(err) - } - bz, err := protoio.MarshalDelimited(&canonical) - if err != nil { - panic(err) - } - - return bz -} - -func signItem(ext *tmproto.VoteExtension, chainID string, height int64, round int32, quorumType btcjson.LLMQType, quorumHash []byte) (crypto.SignItem, error) { - requestID, err := voteExtensionRequestID(height, round) - if err != nil { - return crypto.SignItem{}, err - } - signBytes := voteExtensionSignBytes(chainID, height, round, ext) - si := crypto.NewSignItem(quorumType, quorumHash, requestID, signBytes) - // we do not reverse fields when calculating SignHash for vote extensions - // si.UpdateSignHash(false) - return si, nil -}