Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jun 30, 2024
2 parents ec927fd + d8a0b1a commit 5b798ee
Show file tree
Hide file tree
Showing 18 changed files with 883 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.swl
*.swm
*.swn
# .vscode
.vscode
.idea
*.pyc
*.exe
Expand Down
16 changes: 16 additions & 0 deletions proto/side/btcbridge/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ service Query {
rpc QuerySigningRequest(QuerySigningRequestRequest) returns (QuerySigningRequestResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/signing/request";
}
// QuerySigningRequestByAddress queries the signing request by the given address.
rpc QuerySigningRequestByAddress(QuerySigningRequestByAddressRequest) returns (QuerySigningRequestByAddressResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/signing/request/{address}";
}
// UTXOs queries all utxos.
rpc QueryUTXOs(QueryUTXOsRequest) returns (QueryUTXOsResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/utxos";
Expand All @@ -53,6 +57,18 @@ message QuerySigningRequestResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySigningRequestByAddressRequest is request type for the Query/SigningRequestByAddress RPC method.
message QuerySigningRequestByAddressRequest {
string address = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySigningRequestByAddressResponse is response type for the Query/SigningRequestByAddress RPC method.
message QuerySigningRequestByAddressResponse {
repeated BitcoinSigningRequest requests = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

Expand Down
2 changes: 1 addition & 1 deletion proto/side/btcbridge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ message MsgWithdrawBitcoinRequest {
// withdraw amount in satoshi, etc: 100000000sat = 1btc
string amount = 2;
// fee rate in sats/vB
int64 fee_rate = 3;
string fee_rate = 3;
}

// MsgWithdrawBitcoinResponse defines the Msg/WithdrawBitcoin response type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/stretchr/testify/require"
)

func BtcLightClientKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
func BtcBridgeKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
app := app.InitSideTestApp(false)

storeKey := sdk.NewKVStoreKey(types.StoreKey)
Expand Down
16 changes: 13 additions & 3 deletions x/btcbridge/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func CmdQueryBlock() *cobra.Command {
// CmdQuerySigningRequest returns the command to query signing request
func CmdQuerySigningRequest() *cobra.Command {
cmd := &cobra.Command{
Use: "signing-request [status]",
Short: "Query all signing requests",
Use: "signing-request [status or address]",
Short: "Query signing requests by status or address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand All @@ -144,7 +144,17 @@ func CmdQuerySigningRequest() *cobra.Command {

status, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return err
_, err = sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

res, err := queryClient.QuerySigningRequestByAddress(cmd.Context(), &types.QuerySigningRequestByAddressRequest{Address: args[0]})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}

res, err := queryClient.QuerySigningRequest(cmd.Context(), &types.QuerySigningRequestRequest{Status: types.SigningStatus(status)})
Expand Down
8 changes: 1 addition & 7 deletions x/btcbridge/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -129,15 +128,10 @@ func CmdWithdrawBitcoin() *cobra.Command {
return fmt.Errorf("invalid amount")
}

feeRate, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return fmt.Errorf("invalid fee rate")
}

msg := types.NewMsgWithdrawBitcoinRequest(
clientCtx.GetFromAddress().String(),
args[0],
feeRate,
args[1],
)

if err := msg.ValidateBasic(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/btcbridge/genesis.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package btclightclient
package btcbridge

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
14 changes: 7 additions & 7 deletions x/btcbridge/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package btclightclient_test
package btcbridge_test

// Path: x/btclightclient/genesis_test.go
// Path: x/btcbridge/genesis_test.go

import (
"bytes"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/btcsuite/btcd/wire"
keepertest "github.com/sideprotocol/side/testutil/keeper"
"github.com/sideprotocol/side/testutil/nullify"
btclightclient "github.com/sideprotocol/side/x/btcbridge"
"github.com/sideprotocol/side/x/btcbridge"
"github.com/sideprotocol/side/x/btcbridge/types"
"github.com/stretchr/testify/require"
)
Expand All @@ -26,9 +26,9 @@ func TestGenesis(t *testing.T) {
// this line is used by starport scaffolding # genesis/test/state
}

k, ctx := keepertest.BtcLightClientKeeper(t)
btclightclient.InitGenesis(ctx, *k, genesisState)
got := btclightclient.ExportGenesis(ctx, *k)
k, ctx := keepertest.BtcBridgeKeeper(t)
btcbridge.InitGenesis(ctx, *k, genesisState)
got := btcbridge.ExportGenesis(ctx, *k)
require.NotNil(t, got)

nullify.Fill(&genesisState)
Expand All @@ -42,7 +42,7 @@ func TestGenesis(t *testing.T) {

// // test tx: https://blockchain.info/tx/b657e22827039461a9493ede7bdf55b01579254c1630b0bfc9185ec564fc05ab?format=json

// k, ctx := keepertest.BtcLightClientKeeper(t)
// k, ctx := keepertest.BtcbridgeClientKeeper(t)

// txHex := "02000000000101e5df234dbeff74d6da14754ebeea8ab0e2f60b1884566846cf6b36e8ceb5f5350100000000fdffffff02f0490200000000001600142ac13073e8d491428790481321a636696d00107681d7e205000000001600142bf3aa186cbdcbe88b70a67edcd5a32ce5e8e6d8024730440220081ee61d749ce8cedcf6eedde885579af2eb65ca67d29e6ae2c37109d81cbbb202203a1891ce45f91f159ccf04348ef37a3d1a12d89e5e01426e061326057e6c128d012103036bbdd77c9a932f37bd66175967c7fb7eb75ece06b87c1ad1716770cb3ca4ee79fc2a00"
// prevTxHex := "0200000000010183372652f2af9ab34b3a003efada6b054c75583185ac130de72599dfdf4e462b0100000000fdffffff02f0490200000000001600142ac13073e8d491428790481321a636696d001076a64ee50500000000160014a03614eef338681373de94a2dc2574de55da1980024730440220274250f6036bea0947daf4455ab4976f81721257d163fd952fb5b0c70470edc602202fba816be260219bbc40a8983c459cf05cf2209bf1e62e7ccbf78aec54db607f0121031cee21ef69fe68b240c3032616fa310c6a60a856c0a7e0c1298815c92fb2c61788fb2a00"
Expand Down
40 changes: 20 additions & 20 deletions x/btcbridge/keeper/keeper_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ import (
)

// Process Bitcoin Deposit Transaction
func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.MsgSubmitDepositTransactionRequest) error {
func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.MsgSubmitDepositTransactionRequest) (*chainhash.Hash, btcutil.Address, error) {
ctx.Logger().Info("accept bitcoin deposit tx", "blockhash", msg.Blockhash)

param := k.GetParams(ctx)

if !param.IsAuthorizedSender(msg.Sender) {
return types.ErrSenderAddressNotAuthorized
return nil, nil, types.ErrSenderAddressNotAuthorized
}

header := k.GetBlockHeader(ctx, msg.Blockhash)
// Check if block confirmed
if header == nil || header.Height == 0 {
return types.ErrBlockNotFound
return nil, nil, types.ErrBlockNotFound
}

best := k.GetBestBlockHeader(ctx)
// Check if the block is confirmed
if best.Height-header.Height < uint64(param.Confirmations) {
return types.ErrNotConfirmed
return nil, nil, types.ErrNotConfirmed
}
// Check if the block is within the acceptable depth
// if best.Height-header.Height > param.MaxAcceptableBlockDepth {
Expand All @@ -44,62 +44,62 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg
txBytes, err := base64.StdEncoding.DecodeString(msg.TxBytes)
if err != nil {
fmt.Println("Error decoding transaction from base64:", err)
return err
return nil, nil, err
}

// Create a new transaction
var tx wire.MsgTx
err = tx.Deserialize(bytes.NewReader(txBytes))
if err != nil {
fmt.Println("Error deserializing transaction:", err)
return err
return nil, nil, err
}
uTx := btcutil.NewTx(&tx)
if len(uTx.MsgTx().TxIn) < 1 {
return types.ErrInvalidBtcTransaction
return nil, nil, types.ErrInvalidBtcTransaction
}

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

// Decode the previous transaction
prevTxBytes, err := base64.StdEncoding.DecodeString(msg.PrevTxBytes)
if err != nil {
fmt.Println("Error decoding transaction from base64:", err)
return err
return nil, nil, err
}

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

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

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

chainCfg := sdk.GetConfig().GetBtcChainCfg()

// Extract the recipient address
recipient, err := types.ExtractRecipientAddr(&tx, &prevMsgTx, param.Vaults, chainCfg)
if err != nil {
return err
return nil, nil, err
}

// if pk.Class() != txscript.WitnessV1TaprootTy || pk.Class() != txscript.WitnessV0PubKeyHashTy || pk.Class() != txscript.WitnessV0ScriptHashTy {
Expand All @@ -110,25 +110,25 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg
// check if the proof is valid
root, err := chainhash.NewHashFromStr(header.MerkleRoot)
if err != nil {
return err
return nil, nil, err
}

txhash := uTx.MsgTx().TxHash()
if !types.VerifyMerkleProof(msg.Proof, &txhash, root) {
k.Logger(ctx).Error("Invalid merkle proof", "txhash", tx, "root", root, "proof", msg.Proof)
return types.ErrTransactionNotIncluded
return nil, nil, types.ErrTransactionNotIncluded
}

// mint voucher token and save utxo if the receiver is a vault address
for i, out := range uTx.MsgTx().TxOut {
// check if the output is a valid address
pks, err := txscript.ParsePkScript(out.PkScript)
if err != nil {
return err
return nil, nil, err
}
addr, err := pks.Address(chainCfg)
if err != nil {
return err
return nil, nil, err
}
// check if the receiver is one of the voucher addresses
vault := types.SelectVaultByBitcoinAddress(param.Vaults, addr.EncodeAddress())
Expand All @@ -142,14 +142,14 @@ func (k Keeper) ProcessBitcoinDepositTransaction(ctx sdk.Context, msg *types.Msg
case types.AssetType_ASSET_TYPE_BTC:
err := k.mintBTC(ctx, uTx, header.Height, recipient.EncodeAddress(), vault, out, i, param.BtcVoucherDenom)
if err != nil {
return err
return nil, nil, err
}
case types.AssetType_ASSET_TYPE_RUNE:
k.mintRUNE(ctx, uTx, header.Height, recipient.EncodeAddress(), vault, out, i, "rune")
}
}

return nil
return &txhash, recipient, nil
}

func (k Keeper) mintBTC(ctx sdk.Context, uTx *btcutil.Tx, height uint64, sender string, vault *types.Vault, out *wire.TxOut, vout int, denom string) error {
Expand Down
Loading

0 comments on commit 5b798ee

Please sign in to comment.