@@ -26,9 +26,6 @@ const (
2626
2727 // liquidation cet sequence
2828 LiquidationCetSequence = wire .MaxTxInSequenceNum
29-
30- // default fee rate
31- DefaultFeeRate = 1
3229)
3330
3431const (
@@ -79,7 +76,7 @@ func BuildDLCMeta(borrowerPubKey string, borrowerAuthPubKey string, dcmPubKey st
7976}
8077
8178// VerifyCets verifies the given cets
82- func VerifyCets (dlcMeta * DLCMeta , depositTxs []* psbt.Packet , vaultPkScript []byte , borrowerPubKey string , borrowerAuthPubKey string , dcmPubKey string , dlcEvent * dlctypes.DLCEvent , liquidationCet string , liquidationAdaptorSignatures []string , defaultLiquidationAdaptorSignatures []string , repaymentCet string , repaymentSignatures []string ) error {
79+ func VerifyCets (dlcMeta * DLCMeta , depositTxs []* psbt.Packet , vaultPkScript []byte , borrowerPubKey string , borrowerAuthPubKey string , dcmPubKey string , dlcEvent * dlctypes.DLCEvent , liquidationCet string , liquidationAdaptorSignatures []string , defaultLiquidationAdaptorSignatures []string , repaymentCet string , repaymentSignatures []string , currentFeeRate int64 , maxLiquidationFeeRateMultiplier int64 ) error {
8380 liquidationAdaptorPoint , err := dlctypes .GetSignaturePointFromEvent (dlcEvent , LiquidatedOutcomeIndex )
8481 if err != nil {
8582 return err
@@ -90,11 +87,11 @@ func VerifyCets(dlcMeta *DLCMeta, depositTxs []*psbt.Packet, vaultPkScript []byt
9087 return err
9188 }
9289
93- if err := VerifyLiquidationCet (dlcMeta , depositTxs , vaultPkScript , borrowerAuthPubKey , dcmPubKey , liquidationCet , liquidationAdaptorSignatures , liquidationAdaptorPoint ); err != nil {
90+ if err := VerifyLiquidationCet (dlcMeta , depositTxs , vaultPkScript , borrowerAuthPubKey , dcmPubKey , liquidationCet , liquidationAdaptorSignatures , liquidationAdaptorPoint , currentFeeRate , maxLiquidationFeeRateMultiplier ); err != nil {
9491 return errorsmod .Wrapf (ErrInvalidCET , "invalid liquidation cet: %v" , err )
9592 }
9693
97- if err := VerifyLiquidationCet (dlcMeta , depositTxs , vaultPkScript , borrowerAuthPubKey , dcmPubKey , liquidationCet , defaultLiquidationAdaptorSignatures , defaultLiquidationAdaptorPoint ); err != nil {
94+ if err := VerifyLiquidationCet (dlcMeta , depositTxs , vaultPkScript , borrowerAuthPubKey , dcmPubKey , liquidationCet , defaultLiquidationAdaptorSignatures , defaultLiquidationAdaptorPoint , currentFeeRate , maxLiquidationFeeRateMultiplier ); err != nil {
9895 return errorsmod .Wrapf (ErrInvalidCET , "invalid default liquidation cet: %v" , err )
9996 }
10097
@@ -106,7 +103,7 @@ func VerifyCets(dlcMeta *DLCMeta, depositTxs []*psbt.Packet, vaultPkScript []byt
106103}
107104
108105// VerifyLiquidationCet verifies the given liquidation cet and corresponding adaptor signatures
109- func VerifyLiquidationCet (dlcMeta * DLCMeta , depositTxs []* psbt.Packet , vaultPkScript []byte , borrowerAuthPubKey string , dcmPubKey string , liquidationCET string , adaptorSignatures []string , adaptorPoint []byte ) error {
106+ func VerifyLiquidationCet (dlcMeta * DLCMeta , depositTxs []* psbt.Packet , vaultPkScript []byte , borrowerAuthPubKey string , dcmPubKey string , liquidationCET string , adaptorSignatures []string , adaptorPoint []byte , currentFeeRate int64 , maxFeeRateMultiplier int64 ) error {
110107 p , err := psbt .NewFromRawBytes (bytes .NewReader ([]byte (liquidationCET )), true )
111108 if err != nil {
112109 return errorsmod .Wrap (ErrInvalidCET , "failed to deserialize cet" )
@@ -132,13 +129,8 @@ func VerifyLiquidationCet(dlcMeta *DLCMeta, depositTxs []*psbt.Packet, vaultPkSc
132129
133130 witnessSize := getCetWitnessSize (CetType_LIQUIDATION , script , controlBlock )
134131
135- fee , err := p .GetTxFee ()
136- if err != nil {
137- return errorsmod .Wrapf (ErrInvalidCET , "failed to get tx fee: %v" , err )
138- }
139-
140- if int64 (fee ) < GetTxVirtualSize (p .UnsignedTx , witnessSize ) {
141- return errorsmod .Wrap (ErrInvalidCET , "too low fee rate" )
132+ if err := checkCetFeeRate (p , witnessSize , currentFeeRate , maxFeeRateMultiplier ); err != nil {
133+ return err
142134 }
143135
144136 if err := CheckTransactionWeight (p .UnsignedTx , witnessSize ); err != nil {
@@ -230,13 +222,8 @@ func VerifyRepaymentCet(dlcMeta *DLCMeta, depositTxs []*psbt.Packet, vaultPkScri
230222
231223 witnessSize := getCetWitnessSize (CetType_REPAYMENT , script , controlBlock )
232224
233- fee , err := p .GetTxFee ()
234- if err != nil {
235- return errorsmod .Wrapf (ErrInvalidCET , "failed to get tx fee: %v" , err )
236- }
237-
238- if int64 (fee ) < GetTxVirtualSize (p .UnsignedTx , witnessSize ) {
239- return errorsmod .Wrap (ErrInvalidCET , "too low fee rate" )
225+ if err := checkCetFeeRate (p , witnessSize , 0 , 0 ); err != nil {
226+ return err
240227 }
241228
242229 if err := CheckTransactionWeight (p .UnsignedTx , witnessSize ); err != nil {
@@ -580,6 +567,26 @@ func getVaultUtxosFromDepositTx(depositTx *psbt.Packet, vaultPkScript []byte) ([
580567 return utxos , nil
581568}
582569
570+ // checkCetFeeRate checks the fee rate of the given cet
571+ func checkCetFeeRate (p * psbt.Packet , witnessSize int , currentFeeRate int64 , maxFeeRateMultiplier int64 ) error {
572+ virtualSize := GetTxVirtualSize (p .UnsignedTx , witnessSize )
573+
574+ fee , err := p .GetTxFee ()
575+ if err != nil {
576+ return errorsmod .Wrapf (ErrInvalidCET , "failed to get tx fee: %v" , err )
577+ }
578+
579+ if int64 (fee ) < virtualSize {
580+ return errorsmod .Wrap (ErrInvalidCET , "too low fee rate" )
581+ }
582+
583+ if maxFeeRateMultiplier > 0 && int64 (fee ) > virtualSize * currentFeeRate * maxFeeRateMultiplier {
584+ return errorsmod .Wrap (ErrInvalidCET , "too high fee rate" )
585+ }
586+
587+ return nil
588+ }
589+
583590// getCetWitnessSize gets the cet witness size according to the given params
584591// NOTE: The final signature is 64 bytes due to that the sig hash type is SigHashDefault currently.
585592func getCetWitnessSize (cetType CetType , script []byte , controlBlock []byte ) int {
0 commit comments