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

Unmarshalling perf #466

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
15 changes: 6 additions & 9 deletions types/btc_schnorr_sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ type BIP340Signature []byte
const BIP340SignatureLen = schnorr.SignatureSize

func NewBIP340Signature(data []byte) (*BIP340Signature, error) {

var sig BIP340Signature
err := sig.Unmarshal(data)

if _, err := sig.ToBTCSig(); err != nil {
return nil, errors.New("bytes cannot be converted to a *schnorr.Signature object")
}

return &sig, err
}

Expand Down Expand Up @@ -69,15 +75,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
Comment on lines 77 to 78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't perform verification here, NewBIP340Signature won't perform any verification either. Since NewBIP340Signature will be the function that takes inputs from the external users, we need to move the verification removed here to NewBIP340Signature.

return nil
}
Expand Down
6 changes: 0 additions & 6 deletions x/btcstaking/types/btc_slashing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
28 changes: 28 additions & 0 deletions x/btcstaking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,19 @@ 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")
}

if _, err := m.DelegatorSlashingSig.ToBTCSig(); err != nil {
return fmt.Errorf("invalid delegator slashing signature: %w", err)
}

if _, err := sdk.AccAddressFromBech32(m.Signer); err != nil {
return err
}
Expand Down Expand Up @@ -111,6 +121,15 @@ 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)
}

if _, err := m.DelegatorUnbondingSlashingSig.ToBTCSig(); err != nil {
return fmt.Errorf("invalid delegator unbonding slashing signature: %w", err)
}

unbondingTxMsg, err := bbn.NewBTCTxFromBytes(m.UnbondingTx)
if err != nil {
return err
Expand Down Expand Up @@ -145,6 +164,11 @@ func (m *MsgAddCovenantSigs) ValidateBasic() error {
if m.UnbondingTxSig == nil {
return fmt.Errorf("empty covenant signature")
}

if _, err := m.UnbondingTxSig.ToBTCSig(); err != nil {
return fmt.Errorf("invalid covenant unbonding signature: %w", err)
}

if m.SlashingUnbondingTxSigs == nil {
return fmt.Errorf("empty covenant signature")
}
Expand All @@ -161,5 +185,9 @@ func (m *MsgBTCUndelegate) ValidateBasic() error {
return fmt.Errorf("empty signature from the delegator")
}

if _, err := m.UnbondingTxSig.ToBTCSig(); err != nil {
return fmt.Errorf("invalid delegator unbonding signature: %w", err)
}

return nil
}