Skip to content

Commit

Permalink
add btc transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
liangping committed May 17, 2024
1 parent 423f2cb commit 0955b91
Show file tree
Hide file tree
Showing 8 changed files with 748 additions and 46 deletions.
13 changes: 13 additions & 0 deletions proto/side/btclightclient/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ option go_package = "github.com/sideprotocol/side/x/btclightclient/types";
service Msg {
// SubmitBlockHeaders submits bitcoin block headers to the side chain.
rpc SubmitBlockHeaders (MsgSubmitBlockHeaderRequest) returns (MsgSubmitBlockHeadersResponse);
// SubmitTransaction submits bitcoin transaction to the side chain.
rpc SubmitTransaction (MsgSubmitTransactionRequest) returns (MsgSubmitTransactionResponse);
// UpdateSenders updates the senders of the side chain.
rpc UpdateSenders (MsgUpdateSendersRequest) returns (MsgUpdateSendersResponse);
}
Expand All @@ -25,6 +27,17 @@ message MsgSubmitBlockHeaderRequest {
message MsgSubmitBlockHeadersResponse {
}

// MsgSubmitTransactionRequest defines the Msg/SubmitTransaction request type.
message MsgSubmitTransactionRequest {
string sender = 1;
string tx = 2;
string proof = 3;
}

// MsgSubmitTransactionResponse defines the Msg/SubmitTransaction response type.
message MsgSubmitTransactionResponse {
}

// Msg defines the MsgUpdateSender service.
message MsgUpdateSendersRequest {
string sender = 1;
Expand Down
38 changes: 38 additions & 0 deletions x/btclightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package keeper

import (
"bytes"
"encoding/hex"
"fmt"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -124,6 +127,41 @@ func (k Keeper) SetBlockHeaders(ctx sdk.Context, blockHeader []*types.BlockHeade
return nil
}

// Process Bitcoin Transaction
func (k Keeper) ProcessBitcoinTransaction(ctx sdk.Context, txHexByte, proof string) error {

// Decode the hexadecimal transaction
txBytes, err := hex.DecodeString(txHexByte)
if err != nil {
fmt.Println("Error decoding hex:", err)
return 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
}

// Validate the transaction
// cfg := &chaincfg.MainNetParams // Use MainNetParams or TestNet3Params as per your network
if err := blockchain.CheckTransactionSanity(&tx); err != nil {
fmt.Println("Transaction is not valid:", err)
return err
}
if err != nil {
return err
}

ctx.Logger().Debug("Processing Transaction", tx)

// transaction.MsgTx().

return nil
}

func (k Keeper) GetBlockHeader(ctx sdk.Context, hash string) *types.BlockHeader {
store := ctx.KVStore(k.storeKey)
var blockHeader *types.BlockHeader
Expand Down
21 changes: 21 additions & 0 deletions x/btclightclient/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ func (m msgServer) SubmitBlockHeaders(goCtx context.Context, msg *types.MsgSubmi
return &types.MsgSubmitBlockHeadersResponse{}, nil
}

// SubmitBtcTransaction implements types.MsgServer.
// No Permission check required for this message
// Since everyone can submit a transaction to mint voucher tokens
// This message is usually sent by relayers
func (m msgServer) SubmitBtcTransaction(goCtx context.Context, msg *types.MsgSubmitTransactionRequest) (*types.MsgSubmitTransactionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

if err := m.ProcessBitcoinTransaction(ctx, msg.Tx, msg.Proof); err != nil {
return nil, err
}

// Emit Events

return &types.MsgSubmitTransactionResponse{}, nil

}

// UpdateSenders implements types.MsgServer.
func (m msgServer) UpdateSenders(goCtx context.Context, msg *types.MsgUpdateSendersRequest) (*types.MsgUpdateSendersResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
4 changes: 4 additions & 0 deletions x/btclightclient/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ var (
ErrForkedBlockHeader = errorsmod.Register(ModuleName, 1102, "Invalid forked block header")

ErrInvalidSenders = errorsmod.Register(ModuleName, 2100, "invalid allowed senders")

ErrInvalidBtcTransaction = errorsmod.Register(ModuleName, 3100, "invalid bitcoin transaction")
ErrNotConfirmed = errorsmod.Register(ModuleName, 3200, "transaction not confirmed")
ErrExceedMaxAcceptanceDepth = errorsmod.Register(ModuleName, 3201, "exceed max acceptance block depth")
)
58 changes: 58 additions & 0 deletions x/btclightclient/types/message_submit_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package types

import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const TypeMsgSubmitTransaction = "submit_transaction"

func NewMsgSubmitTransactionRequest(
sender string,
transaction string,
proof string,
) *MsgSubmitTransactionRequest {
return &MsgSubmitTransactionRequest{
Sender: sender,
Tx: transaction,
Proof: proof,
}
}

func (msg *MsgSubmitTransactionRequest) Route() string {
return RouterKey
}

func (msg *MsgSubmitTransactionRequest) Type() string {
return TypeMsgSubmitTransaction
}

func (msg *MsgSubmitTransactionRequest) GetSigners() []sdk.AccAddress {
Sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{Sender}
}

func (msg *MsgSubmitTransactionRequest) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgSubmitTransactionRequest) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return sdkerrors.Wrapf(err, "invalid Sender address (%s)", err)
}

if len(msg.Tx) == 0 {
return sdkerrors.Wrap(ErrInvalidBtcTransaction, "transaction cannot be empty")
}

if len(msg.Proof) == 0 {
return sdkerrors.Wrap(ErrInvalidBtcTransaction, "proof cannot be empty")
}

return nil
}
5 changes: 4 additions & 1 deletion x/btclightclient/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
// NewParams creates a new Params instance
func NewParams(senders []string) Params {
return Params{
Senders: senders,
Senders: senders,
Confirmations: 2,
MaxAcceptableBlockDepth: 100,
BtcVoucherDenom: "sat",
}
}

Expand Down
Loading

0 comments on commit 0955b91

Please sign in to comment.