Skip to content
Merged
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
11 changes: 11 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49749,6 +49749,7 @@ paths:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down Expand Up @@ -49946,6 +49947,7 @@ paths:
- DKG_REQUEST_STATUS_PENDING: DKG_REQUEST_STATUS_PENDING defines the status of the DKG request which is pending
- DKG_REQUEST_STATUS_COMPLETED: DKG_REQUEST_STATUS_COMPLETED defines the status of the DKG request which is completed
- DKG_REQUEST_STATUS_FAILED: DKG_REQUEST_STATUS_FAILED defines the status of the DKG request which failed
- DKG_REQUEST_STATUS_TIMEDOUT: DKG_REQUEST_STATUS_TIMEDOUT defines the status of the DKG request which timed out
in: query
required: false
type: string
Expand All @@ -49954,6 +49956,7 @@ paths:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
tags:
- Query
Expand Down Expand Up @@ -50006,6 +50009,7 @@ paths:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down Expand Up @@ -50259,6 +50263,7 @@ paths:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down Expand Up @@ -82532,6 +82537,7 @@ definitions:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
side.btcbridge.DKGRequestStatus:
Expand All @@ -82541,13 +82547,15 @@ definitions:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: >-
- DKG_REQUEST_STATUS_UNSPECIFIED: DKG_REQUEST_STATUS_UNSPECIFIED defines
the unknown DKG request status
- DKG_REQUEST_STATUS_PENDING: DKG_REQUEST_STATUS_PENDING defines the status of the DKG request which is pending
- DKG_REQUEST_STATUS_COMPLETED: DKG_REQUEST_STATUS_COMPLETED defines the status of the DKG request which is completed
- DKG_REQUEST_STATUS_FAILED: DKG_REQUEST_STATUS_FAILED defines the status of the DKG request which failed
- DKG_REQUEST_STATUS_TIMEDOUT: DKG_REQUEST_STATUS_TIMEDOUT defines the status of the DKG request which timed out
side.btcbridge.MsgCompleteDKGResponse:
type: object
description: MsgCompleteDKGResponse defines the Msg/CompleteDKG response type.
Expand Down Expand Up @@ -82760,6 +82768,7 @@ definitions:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down Expand Up @@ -82912,6 +82921,7 @@ definitions:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down Expand Up @@ -82961,6 +82971,7 @@ definitions:
- DKG_REQUEST_STATUS_PENDING
- DKG_REQUEST_STATUS_COMPLETED
- DKG_REQUEST_STATUS_FAILED
- DKG_REQUEST_STATUS_TIMEDOUT
default: DKG_REQUEST_STATUS_UNSPECIFIED
title: DKG Request
description: >-
Expand Down
2 changes: 2 additions & 0 deletions proto/side/btcbridge/bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ enum DKGRequestStatus {
DKG_REQUEST_STATUS_COMPLETED = 2;
// DKG_REQUEST_STATUS_FAILED defines the status of the DKG request which failed
DKG_REQUEST_STATUS_FAILED = 3;
// DKG_REQUEST_STATUS_TIMEDOUT defines the status of the DKG request which timed out
DKG_REQUEST_STATUS_TIMEDOUT = 4;
}

// DKG Request
Expand Down
6 changes: 2 additions & 4 deletions proto/side/btcbridge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ message MsgSubmitWithdrawTransaction {
// this is relayer address who submit the bitcoin transaction to the side chain
string sender = 1;
string blockhash = 2;
// the previous tx bytes in base64 format
string prev_tx_bytes = 3;
// the tx bytes in base64 format
string tx_bytes = 4;
repeated string proof = 5;
string tx_bytes = 3;
repeated string proof = 4;
}

// MsgSubmitWithdrawTransactionResponse defines the Msg/SubmitWithdrawTransaction response type.
Expand Down
2 changes: 1 addition & 1 deletion x/btcbridge/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
for _, req := range pendingDKGRequests {
// check if the DKG request expired
if !ctx.BlockTime().Before(*req.Expiration) {
req.Status = types.DKGRequestStatus_DKG_REQUEST_STATUS_FAILED
req.Status = types.DKGRequestStatus_DKG_REQUEST_STATUS_TIMEDOUT
continue
}

Expand Down
3 changes: 2 additions & 1 deletion x/btcbridge/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)

// import utxos
for _, utxo := range genState.Utxos {
k.SetUTXO(ctx, utxo)
k.SaveUTXO(ctx, utxo)
}
}

Expand All @@ -30,6 +30,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.Params = k.GetParams(ctx)
genesis.BestBlockHeader = k.GetBestBlockHeader(ctx)
genesis.BlockHeaders = k.GetAllBlockHeaders(ctx)
genesis.Utxos = k.GetAllUTXOs(ctx)

// this line is used by starport scaffolding # genesis/module/export

Expand Down
59 changes: 32 additions & 27 deletions x/btcbridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,46 +215,51 @@ func (k Keeper) ValidateTransaction(ctx sdk.Context, txBytes string, prevTxBytes
}

// Create a new transaction
var tx wire.MsgTx
err = tx.Deserialize(bytes.NewReader(rawTx))
var msgTx wire.MsgTx
err = msgTx.Deserialize(bytes.NewReader(rawTx))
if err != nil {
fmt.Println("Error deserializing transaction:", err)
return nil, nil, err
}

uTx := btcutil.NewTx(&tx)
tx := btcutil.NewTx(&msgTx)

// Validate the transaction
if err := blockchain.CheckTransactionSanity(uTx); err != nil {
if err := blockchain.CheckTransactionSanity(tx); err != nil {
fmt.Println("Transaction is not valid:", err)
return nil, nil, err
}

// Decode the previous transaction
rawPrevTx, err := base64.StdEncoding.DecodeString(prevTxBytes)
if err != nil {
fmt.Println("Error decoding transaction from base64:", err)
return nil, nil, err
}
var prevTx *btcutil.Tx

// Create a new transaction
var prevMsgTx wire.MsgTx
err = prevMsgTx.Deserialize(bytes.NewReader(rawPrevTx))
if err != nil {
fmt.Println("Error deserializing transaction:", err)
return nil, nil, err
}
// Check the previous tx if given
if len(prevTxBytes) > 0 {
// Decode the previous transaction
rawPrevTx, err := base64.StdEncoding.DecodeString(prevTxBytes)
if err != nil {
fmt.Println("Error decoding transaction from base64:", err)
return nil, nil, err
}

prevTx := btcutil.NewTx(&prevMsgTx)
// Create a new transaction
var prevMsgTx wire.MsgTx
err = prevMsgTx.Deserialize(bytes.NewReader(rawPrevTx))
if err != nil {
fmt.Println("Error deserializing transaction:", err)
return nil, nil, err
}

// Validate the transaction
if err := blockchain.CheckTransactionSanity(prevTx); err != nil {
fmt.Println("Transaction is not valid:", err)
return nil, nil, err
}
prevTx = btcutil.NewTx(&prevMsgTx)

if uTx.MsgTx().TxIn[0].PreviousOutPoint.Hash.String() != prevTx.Hash().String() {
return nil, nil, types.ErrInvalidBtcTransaction
// Validate the transaction
if err := blockchain.CheckTransactionSanity(prevTx); err != nil {
fmt.Println("Transaction is not valid:", err)
return nil, nil, err
}

if tx.MsgTx().TxIn[0].PreviousOutPoint.Hash.String() != prevTx.Hash().String() {
return nil, nil, types.ErrInvalidBtcTransaction
}
}

// check if the proof is valid
Expand All @@ -263,10 +268,10 @@ func (k Keeper) ValidateTransaction(ctx sdk.Context, txBytes string, prevTxBytes
return nil, nil, err
}

if !types.VerifyMerkleProof(proof, uTx.Hash(), root) {
if !types.VerifyMerkleProof(proof, tx.Hash(), root) {
k.Logger(ctx).Error("Invalid merkle proof", "txhash", tx, "root", root, "proof", proof)
return nil, nil, types.ErrTransactionNotIncluded
}

return uTx, prevTx, nil
return tx, prevTx, nil
}
2 changes: 1 addition & 1 deletion x/btcbridge/keeper/keeper_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (k Keeper) Mint(ctx sdk.Context, tx *btcutil.Tx, prevTx *btcutil.Tx, height
}

// check if the receiver is one of the vault addresses
vault := types.SelectVaultByBitcoinAddress(params.Vaults, addr.EncodeAddress())
vault := types.SelectVaultByAddress(params.Vaults, addr.EncodeAddress())
if vault == nil {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion x/btcbridge/keeper/keeper_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (k Keeper) CheckVaults(ctx sdk.Context, vaults []string) error {
}

for _, v := range vaults {
if types.SelectVaultByBitcoinAddress(currentVaults, v) != nil {
if types.SelectVaultByAddress(currentVaults, v) != nil {
return types.ErrInvalidDKGCompletionRequest
}
}
Expand Down
6 changes: 6 additions & 0 deletions x/btcbridge/keeper/keeper_utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ func (bk *BaseUTXOKeeper) SpendUTXOs(ctx sdk.Context, utxos []*types.UTXO) error
return nil
}

// SaveUTXO saves the given utxo
// Intended to be used out of the module, such as genesis import
func (bk *BaseUTXOKeeper) SaveUTXO(ctx sdk.Context, utxo *types.UTXO) {
bk.saveUTXO(ctx, utxo)
}

// saveUTXO saves the given utxo
func (bk *BaseUTXOKeeper) saveUTXO(ctx sdk.Context, utxo *types.UTXO) {
bk.SetUTXO(ctx, utxo)
Expand Down
12 changes: 6 additions & 6 deletions x/btcbridge/keeper/keeper_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (k Keeper) FilterWithdrawRequestsByAddr(ctx sdk.Context, req *types.QueryWi
func (k Keeper) ProcessBitcoinWithdrawTransaction(ctx sdk.Context, msg *types.MsgSubmitWithdrawTransaction) (*chainhash.Hash, error) {
ctx.Logger().Info("accept bitcoin withdraw tx", "blockhash", msg.Blockhash)

tx, _, err := k.ValidateTransaction(ctx, msg.TxBytes, msg.PrevTxBytes, msg.Blockhash, msg.Proof)
tx, _, err := k.ValidateTransaction(ctx, msg.TxBytes, "", msg.Blockhash, msg.Proof)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -294,15 +294,15 @@ func (k Keeper) spendUTXOs(ctx sdk.Context, uTx *btcutil.Tx) {
}
}

// handleTxFee performs the fee handling for the btc withdrawal tx
// handleBtcTxFee performs the network fee handling for the btc withdrawal tx
// Make sure that the given psbt is valid
// There are at most two outputs and the change output is the last one if any
// There are at most three outpus and the change output is the last one if any
func (k Keeper) handleBtcTxFee(p *psbt.Packet, changeAddr string) (*types.UTXO, error) {
recipientOut := p.UnsignedTx.TxOut[0]
recipientOut := p.UnsignedTx.TxOut[1]

changeOut := new(wire.TxOut)
if len(p.UnsignedTx.TxOut) > 1 {
changeOut = p.UnsignedTx.TxOut[1]
if len(p.UnsignedTx.TxOut) > 2 {
changeOut = p.UnsignedTx.TxOut[2]
} else {
changeOut = wire.NewTxOut(0, types.MustPkScriptFromAddress(changeAddr))
p.UnsignedTx.TxOut = append(p.UnsignedTx.TxOut, changeOut)
Expand Down
2 changes: 1 addition & 1 deletion x/btcbridge/types/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
MinRelayFee = 1000

// default sig hash type
DefaultSigHashType = txscript.SigHashAll
DefaultSigHashType = txscript.SigHashDefault
)

// BuildPsbt builds a bitcoin psbt from the given params.
Expand Down
Loading