Skip to content

Commit

Permalink
feat: fbridge transfer (#1347)
Browse files Browse the repository at this point in the history
* impl bridge transfer tx/query

* add CLI
  • Loading branch information
tkxkd0159 authored May 2, 2024
1 parent 82e8936 commit 7b421db
Show file tree
Hide file tree
Showing 19 changed files with 676 additions and 123 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (consensus) [\#1178](https://github.com/Finschia/finschia-sdk/pull/1178) change the consensus from Ostracon to Tendermint v0.34.24
* (x/fswap) [\#1336](https://github.com/Finschia/finschia-sdk/pull/1336) Initialize fswap module
* (x/fswap) [\#1339](https://github.com/Finschia/finschia-sdk/pull/1339) Implement fswap module's genesis
* (x/fbridge) [\#1336](https://github.com/Finschia/finschia-sdk/pull/1340) Initialize fbridge module
* (x/fbridge) [\#1340](https://github.com/Finschia/finschia-sdk/pull/1340) Initialize fbridge module
* (x/fbridge) [\#1347](https://github.com/Finschia/finschia-sdk/pull/1347) Implement bridge transfer feature (sending side)

### Improvements
* (docs) [\#1120](https://github.com/Finschia/finschia-sdk/pull/1120) Update links in x/foundation README.md
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mocks: $(MOCKS_DIR)
mockgen -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger
mockgen -source=x/stakingplus/expected_keepers.go -package testutil -destination x/stakingplus/testutil/expected_keepers_mocks.go
mockgen -source=x/fswap/types/expected_keepers.go -package testutil -destination x/fswap/testutil/expected_keepers_mocks.go
mockgen -source=x/fbridge/types/expected_keepers.go -package testutil -destination x/fbridge/testutil/expected_keepers_mocks.go
.PHONY: mocks

$(MOCKS_DIR):
Expand Down
6 changes: 3 additions & 3 deletions proto/lbm/fbridge/v1/fbridge.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import "gogoproto/gogo.proto";

message Params {
// ratio of how many operators' confirmations are needed to be valid.
Fraction operator_trust_level = 1;
Fraction operator_trust_level = 1 [(gogoproto.nullable) = false];
// ratio of how many guardians' confirmations are needed to be valid.
Fraction guardian_trust_level = 2;
Fraction guardian_trust_level = 2 [(gogoproto.nullable) = false];
// ratio of how many judges' confirmations are needed to be valid.
Fraction judge_trust_level = 3;
Fraction judge_trust_level = 3 [(gogoproto.nullable) = false];
// default timelock period for each provision (unix timestamp)
uint64 timelock_period = 4;
// default period of the proposal to update the role
Expand Down
14 changes: 14 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ import (
"github.com/Finschia/finschia-sdk/x/evidence"
evidencekeeper "github.com/Finschia/finschia-sdk/x/evidence/keeper"
evidencetypes "github.com/Finschia/finschia-sdk/x/evidence/types"
fbridgekeeper "github.com/Finschia/finschia-sdk/x/fbridge/keeper"
fbridgemodule "github.com/Finschia/finschia-sdk/x/fbridge/module"
fbridgetypes "github.com/Finschia/finschia-sdk/x/fbridge/types"
"github.com/Finschia/finschia-sdk/x/feegrant"
feegrantkeeper "github.com/Finschia/finschia-sdk/x/feegrant/keeper"
feegrantmodule "github.com/Finschia/finschia-sdk/x/feegrant/module"
Expand Down Expand Up @@ -142,6 +145,7 @@ var (
tokenmodule.AppModuleBasic{},
collectionmodule.AppModuleBasic{},
fswap.AppModuleBasic{},
fbridgemodule.AppModuleBasic{},
)

// module account permissions
Expand All @@ -154,11 +158,13 @@ var (
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
fbridgetypes.ModuleName: {authtypes.Burner},
}

// module accounts that are allowed to receive tokens
allowedReceivingModAcc = map[string]bool{
// govtypes.ModuleName: true, // TODO: uncomment it when authority is ready
fbridgetypes.ModuleName: true,
}
)

Expand Down Expand Up @@ -202,6 +208,7 @@ type SimApp struct {
TokenKeeper tokenkeeper.Keeper
CollectionKeeper collectionkeeper.Keeper
FswapKeeper fswapkeeper.Keeper
FbridgeKeeper fbridgekeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -256,6 +263,7 @@ func NewSimApp(
collection.StoreKey,
authzkeeper.StoreKey,
fswaptypes.StoreKey,
fbridgetypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
Expand Down Expand Up @@ -362,6 +370,8 @@ func NewSimApp(
fswapConfig := fswaptypes.DefaultConfig()
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper, fswapConfig)

app.FbridgeKeeper = fbridgekeeper.NewKeeper(appCodec, keys[fbridgetypes.StoreKey], app.AccountKeeper, app.BankKeeper, "stake", authtypes.NewModuleAddress(govtypes.ModuleName).String())

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -394,6 +404,7 @@ func NewSimApp(
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
fswap.NewAppModule(appCodec, app.FswapKeeper, app.AccountKeeper, app.BankKeeper),
fbridgemodule.NewAppModule(appCodec, app.FbridgeKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -422,6 +433,7 @@ func NewSimApp(
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
fbridgetypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -444,6 +456,7 @@ func NewSimApp(
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
fbridgetypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -472,6 +485,7 @@ func NewSimApp(
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
fbridgetypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down
31 changes: 30 additions & 1 deletion x/fbridge/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,45 @@ package cli
import (
"github.com/spf13/cobra"

"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/client/flags"
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

// NewQueryCmd returns the query commands for fbridge module
func NewQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the fbridge module",
}

cmd.AddCommand()
cmd.AddCommand(
NewQueryNextSeqSendCmd(),
)

return cmd
}

func NewQueryNextSeqSendCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "nextseq-send",
Short: "Query the next sequence number for sending",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
qc := types.NewQueryClient(clientCtx)
res, err := qc.NextSeqSend(cmd.Context(), &types.QueryNextSeqSendRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
48 changes: 46 additions & 2 deletions x/fbridge/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"github.com/spf13/cobra"

"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/client/flags"
"github.com/Finschia/finschia-sdk/client/tx"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

// GetTxCmd returns the transaction commands for this module
// NewTxCmd returns the transaction commands for fbridge module
func NewTxCmd() *cobra.Command {
TxCmd := &cobra.Command{
Use: types.ModuleName,
Expand All @@ -17,7 +21,47 @@ func NewTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

TxCmd.AddCommand()
TxCmd.AddCommand(
NewTransferTxCmd(),
)

return TxCmd
}

func NewTransferTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "transfer [to_address] [amount]",
Short: `Transfer token from current chain to counterparty chain`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
fromAddr := clientCtx.GetFromAddress().String()
if _, err := sdk.AccAddressFromBech32(fromAddr); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid address: %s", fromAddr)
}
toAddr := args[0]
coins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
if len(coins) != 1 {
return sdkerrors.ErrInvalidRequest.Wrapf("only one native coin type is allowed")
}

msg := types.MsgTransfer{
Sender: fromAddr,
Receiver: toAddr,
Amount: coins[0].Amount,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
8 changes: 7 additions & 1 deletion x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import (
)

func (k Keeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) error {
k.setNextSequence(ctx, gs.SendingState.NextSeq)

return nil
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return nil
return &types.GenesisState{
SendingState: types.SendingState{
NextSeq: k.GetNextSequence(ctx),
},
}
}
15 changes: 13 additions & 2 deletions x/fbridge/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package keeper
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

Expand All @@ -12,8 +16,15 @@ func (k Keeper) Params(ctx context.Context, request *types.QueryParamsRequest) (
panic("implement me")
}

func (k Keeper) NextSeqSend(ctx context.Context, request *types.QueryNextSeqSendRequest) (*types.QueryNextSeqSendResponse, error) {
panic("implement me")
func (k Keeper) NextSeqSend(goCtx context.Context, req *types.QueryNextSeqSendRequest) (*types.QueryNextSeqSendResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
seq := k.GetNextSequence(ctx)

return &types.QueryNextSeqSendResponse{Seq: seq}, nil
}

func (k Keeper) GreatestSeqByOperator(ctx context.Context, request *types.QueryGreatestSeqByOperatorRequest) (*types.QueryGreatestSeqByOperatorResponse, error) {
Expand Down
59 changes: 58 additions & 1 deletion x/fbridge/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
package keeper

type Keeper struct{}
import (
"errors"

"github.com/tendermint/tendermint/libs/log"

"github.com/Finschia/finschia-sdk/codec"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper

// the target denom for the bridge
targetDenom string

// the authority address that can execute privileged operations only if the guardian group is not set
// - UpdateParams
// - SuggestRole
authority string
}

func NewKeeper(
cdc codec.BinaryCodec,
key sdk.StoreKey,
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
targetDenom string,
authority string,
) Keeper {
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(errors.New("fbridge module account has not been set"))
}

if authority != types.DefaultAuthority().String() {
panic(errors.New("authority must be gov module"))
}

return Keeper{
storeKey: key,
cdc: cdc,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
targetDenom: targetDenom,
authority: authority,
}
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

func (k Keeper) GetAuthority() string {
return k.authority
}
Loading

0 comments on commit 7b421db

Please sign in to comment.