Skip to content

Commit

Permalink
feat: add check for payment data (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Apr 26, 2024
1 parent 4354d2e commit a165e09
Show file tree
Hide file tree
Showing 10 changed files with 606 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ func (app *App) initBridge() {

func (app *App) initStorage() {
storagemodulekeeper.RegisterCrossApps(app.StorageKeeper)
storagemodulekeeper.InitPaymentCheck(app.StorageKeeper, app.appConfig.PaymentCheck.Enabled,
app.appConfig.PaymentCheck.Interval)
}

func (app *App) initGov() {
Expand Down
20 changes: 20 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type AppConfig struct {
serverconfig.Config

CrossChain CrossChainConfig `mapstructure:"cross-chain"`

PaymentCheck PaymentCheckConfig `mapstructure:"payment-check"`
}

type CrossChainConfig struct {
Expand All @@ -18,6 +20,11 @@ type CrossChainConfig struct {
DestOpChainId uint32 `mapstructure:"dest-op-chain-id"`
}

type PaymentCheckConfig struct {
Enabled bool `mapstructure:"enabled"`
Interval uint32 `mapstructure:"interval"`
}

var CustomAppTemplate = serverconfig.DefaultConfigTemplate + `
###############################################################################
### CrossChain Config ###
Expand All @@ -29,6 +36,15 @@ src-chain-id = {{ .CrossChain.SrcChainId }}
dest-bsc-chain-id = {{ .CrossChain.DestBscChainId }}
# chain-id for op bnb destination chain
dest-op-chain-id = {{ .CrossChain.DestOpChainId }}
###############################################################################
### PaymentCheck Config ###
###############################################################################
[payment-check]
# enabled - the flag to enable/disable payment check
enabled = {{ .PaymentCheck.Enabled }}
# interval - the block interval run check payment
interval = {{ .PaymentCheck.Interval }}
`

func NewDefaultAppConfig() *AppConfig {
Expand All @@ -54,5 +70,9 @@ func NewDefaultAppConfig() *AppConfig {
DestBscChainId: 2,
DestOpChainId: 3,
},
PaymentCheck: PaymentCheckConfig{
Enabled: false,
Interval: 100,
},
}
}
2 changes: 1 addition & 1 deletion cmd/gnfd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (a appCreator) newApp(
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
a.encodingConfig,
&app.AppConfig{Config: *serverConfig, CrossChain: appConfig.CrossChain},
&app.AppConfig{Config: *serverConfig, CrossChain: appConfig.CrossChain, PaymentCheck: appConfig.PaymentCheck},
appOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetEventing(cast.ToString(appOpts.Get(server.FlagEventing))),
Expand Down
2 changes: 1 addition & 1 deletion testutil/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewTestApp(
app.DefaultNodeHome,
0,
encCfg,
&app.AppConfig{CrossChain: app.NewDefaultAppConfig().CrossChain},
&app.AppConfig{CrossChain: app.NewDefaultAppConfig().CrossChain, PaymentCheck: app.NewDefaultAppConfig().PaymentCheck},
simtestutil.EmptyAppOptions{},
options...,
)
Expand Down
2 changes: 1 addition & 1 deletion x/payment/keeper/stream_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (k Keeper) GetAllStreamRecord(ctx sdk.Context) (list []types.StreamRecord)
for ; iterator.Valid(); iterator.Next() {
var val types.StreamRecord
k.cdc.MustUnmarshal(iterator.Value(), &val)
val.Account = string(iterator.Key())
val.Account = sdk.AccAddress(iterator.Key()).String()
list = append(list, val)
}

Expand Down
9 changes: 9 additions & 0 deletions x/storage/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) {

// Permission GC
keeper.GarbageCollectResourcesStalePolicy(ctx)

// Payment Data Check
interval := int64(keeper.GetPaymentCheckInterval())
if keeper.IsPaymentCheckEnabled() && interval > 0 && ctx.BlockHeight()%interval == 0 {
err = keeper.RunPaymentCheck(ctx)
if err != nil {
panic(err)
}
}
}
17 changes: 17 additions & 0 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ type (
groupSeq sequence.Sequence[sdkmath.Uint]

authority string

// payment check config
cfg *paymentCheckConfig
}
)

type paymentCheckConfig struct {
Enabled bool
Interval uint32
}

func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
Expand All @@ -72,6 +80,7 @@ func NewKeeper(
crossChainKeeper: crossChainKeeper,
virtualGroupKeeper: virtualGroupKeeper,
authority: authority,
cfg: &paymentCheckConfig{Enabled: false, Interval: 0},
}

k.bucketSeq = sequence.NewSequence[sdkmath.Uint](types.BucketSequencePrefix)
Expand All @@ -84,6 +93,14 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

func (k Keeper) IsPaymentCheckEnabled() bool {
return k.cfg.Enabled
}

func (k Keeper) GetPaymentCheckInterval() uint32 {
return k.cfg.Interval
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
Expand Down
Loading

0 comments on commit a165e09

Please sign in to comment.