From f765f1ef9fbac935a25a86c9236065f08931c4be Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Fri, 9 Feb 2024 14:34:01 +0100 Subject: [PATCH] Unmarshalling perf --- types/btc_schnorr_sig.go | 10 -------- x/btcstaking/types/btc_slashing_tx.go | 6 ----- x/btcstaking/types/msg.go | 36 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/types/btc_schnorr_sig.go b/types/btc_schnorr_sig.go index 58342323f..1fe61888e 100644 --- a/types/btc_schnorr_sig.go +++ b/types/btc_schnorr_sig.go @@ -2,7 +2,6 @@ package types import ( "encoding/hex" - "errors" "github.com/btcsuite/btcd/btcec/v2/schnorr" ) @@ -69,15 +68,6 @@ func (sig BIP340Signature) MarshalTo(data []byte) (int, error) { } func (sig *BIP340Signature) Unmarshal(data []byte) error { - newSig := BIP340Signature(data) - - // ensure that the bytes can be transformed to a *schnorr.Signature object - // this includes all format checks - _, err := newSig.ToBTCSig() - if err != nil { - return errors.New("bytes cannot be converted to a *schnorr.Signature object") - } - *sig = data return nil } diff --git a/x/btcstaking/types/btc_slashing_tx.go b/x/btcstaking/types/btc_slashing_tx.go index afb1da6e9..6fae8be20 100644 --- a/x/btcstaking/types/btc_slashing_tx.go +++ b/x/btcstaking/types/btc_slashing_tx.go @@ -63,12 +63,6 @@ func (tx BTCSlashingTx) MarshalTo(data []byte) (int, error) { func (tx *BTCSlashingTx) Unmarshal(data []byte) error { *tx = data - - // ensure data can be decoded to a tx - if _, err := tx.ToMsgTx(); err != nil { - return err - } - return nil } diff --git a/x/btcstaking/types/msg.go b/x/btcstaking/types/msg.go index 73247bc0c..7583a951b 100644 --- a/x/btcstaking/types/msg.go +++ b/x/btcstaking/types/msg.go @@ -73,9 +73,21 @@ func (m *MsgCreateBTCDelegation) ValidateBasic() error { if m.SlashingTx == nil { return fmt.Errorf("empty slashing tx") } + + if _, err := m.SlashingTx.ToMsgTx(); err != nil { + return fmt.Errorf("invalid slashing tx: %w", err) + } + if m.DelegatorSlashingSig == nil { return fmt.Errorf("empty delegator signature") } + + _, err := m.DelegatorSlashingSig.ToBTCSig() + + if err != nil { + return fmt.Errorf("invalid delegator slashing signature: %w", err) + } + if _, err := sdk.AccAddressFromBech32(m.Signer); err != nil { return err } @@ -111,6 +123,17 @@ func (m *MsgCreateBTCDelegation) ValidateBasic() error { if m.DelegatorUnbondingSlashingSig == nil { return fmt.Errorf("empty delegator signature") } + + if _, err := m.UnbondingSlashingTx.ToMsgTx(); err != nil { + return fmt.Errorf("invalid unbonding slashing tx: %w", err) + } + + _, err = m.DelegatorUnbondingSlashingSig.ToBTCSig() + + if err != nil { + return fmt.Errorf("invalid delegator unbonding slashing signature: %w", err) + } + unbondingTxMsg, err := bbn.NewBTCTxFromBytes(m.UnbondingTx) if err != nil { return err @@ -145,6 +168,13 @@ func (m *MsgAddCovenantSigs) ValidateBasic() error { if m.UnbondingTxSig == nil { return fmt.Errorf("empty covenant signature") } + + _, err := m.UnbondingTxSig.ToBTCSig() + + if err != nil { + return fmt.Errorf("invalid covenant unbonding signature: %w", err) + } + if m.SlashingUnbondingTxSigs == nil { return fmt.Errorf("empty covenant signature") } @@ -161,5 +191,11 @@ func (m *MsgBTCUndelegate) ValidateBasic() error { return fmt.Errorf("empty signature from the delegator") } + _, err := m.UnbondingTxSig.ToBTCSig() + + if err != nil { + return fmt.Errorf("invalid delegator unbonding signature: %w", err) + } + return nil }