Skip to content

Commit

Permalink
Add timeout to reject the RTGS sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
masihyeganeh committed Sep 27, 2024
1 parent 2562c25 commit 34b99aa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ But you have the option to configure it otherwise.
For example, if you want to connect to testnet instead of mainnet, you can run:

```bash
iso20022-client init --chain-id=coreum-testnet-1 --coreum-contract-address=testcore1eyky8vfdyz77zkh50zkrdw3mc9guyrfy45pd5ak9jpqgtgwgfvfqd8lkmc --coreum-grpc-url=https://full-node.testnet-1.coreum.dev:9090
iso20022-client init --chain-id=coreum-testnet-1 --coreum-contract-address=testcore1sx8h66ehjcj999t5apz5993y9n6nxwdr5jcd06mzrquxdkful4tqpugwh8 --coreum-grpc-url=https://full-node.testnet-1.coreum.dev:9090
```

### Add Key
Expand Down
2 changes: 2 additions & 0 deletions iso20022/coreum/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type Session struct {
Destination sdk.AccAddress `json:"destination"`
Messages []string `json:"messages"`
FundsInEscrow []sdk.Coin `json:"funds_in_escrow"`
StartTime uint64 `json:"start_time"`
Uetr string `json:"uetr"`
ConfirmedByInitiator bool `json:"confirmed_by_initiator"`
ConfirmedByDestination bool `json:"confirmed_by_destination"`
}
Expand Down
56 changes: 53 additions & 3 deletions iso20022/processes/contract_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,21 @@ func (p *ContractClientProcess) Start(ctx context.Context) error {
p.log.Info(ctx, "Starting the contract client process")
return parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
spawn("msg-receiver", parallel.Continue, func(ctx context.Context) error {
ticker := time.NewTicker(p.cfg.PollInterval)
expiredSessionsTicker := time.NewTicker(time.Hour)
messagesTicker := time.NewTicker(p.cfg.PollInterval)
for {
select {
case <-ticker.C:
case <-expiredSessionsTicker.C:
err := p.cancelExpiredSessions(ctx)
if err != nil {
p.log.Error(
ctx,
"Failed to cancel expired sessions",
zap.Error(err),
)
continue
}
case <-messagesTicker.C:
err := p.receiveMessages(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
Expand All @@ -101,7 +112,8 @@ func (p *ContractClientProcess) Start(ctx context.Context) error {
}
}
case <-ctx.Done():
ticker.Stop()
messagesTicker.Stop()
expiredSessionsTicker.Stop()
return errors.WithStack(ctx.Err())
}
}
Expand Down Expand Up @@ -178,6 +190,44 @@ func (p *ContractClientProcess) Start(ctx context.Context) error {
})
}

func (p *ContractClientProcess) cancelExpiredSessions(ctx context.Context) error {
limit := uint32(100)

sessions, err := p.contractClient.GetActiveSessions(
ctx,
p.cfg.ClientAddress,
coreum.UserTypeInitiator,
nil,
&limit,
)
if err != nil {
return err
}

expireTime := uint64(time.Now().Add(-24 * time.Hour).UnixNano())

cancelSessions := make([]coreum.CancelSession, 0)

for _, session := range sessions {
if session.ConfirmedByDestination == false && session.ConfirmedByInitiator == false && session.StartTime < expireTime {
cancelSessions = append(cancelSessions, coreum.CancelSession{
Uetr: session.Uetr,
Initiator: session.Initiator,
Destination: session.Destination,
})
}
}

if len(cancelSessions) > 0 {
_, err = p.contractClient.CancelSessions(ctx, p.cfg.ClientAddress, cancelSessions...)
if err != nil {
return err
}
}

return nil
}

func (p *ContractClientProcess) receiveMessages(ctx context.Context) error {
limit := uint32(10)

Expand Down
2 changes: 1 addition & 1 deletion iso20022/runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func DefaultConfig() Config {
},
Contract: CoreumContractConfig{
// TODO: Change to the contract address on mainnet before release
ContractAddress: "testcore1eyky8vfdyz77zkh50zkrdw3mc9guyrfy45pd5ak9jpqgtgwgfvfqd8lkmc",
ContractAddress: "testcore1sx8h66ehjcj999t5apz5993y9n6nxwdr5jcd06mzrquxdkful4tqpugwh8",
GasAdjustment: defaultCoreumContactConfig.GasAdjustment,
GasPriceAdjustment: defaultCoreumContactConfig.GasPriceAdjustment.MustFloat64(),
PageLimit: defaultCoreumContactConfig.PageLimit,
Expand Down
2 changes: 1 addition & 1 deletion iso20022/runner/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ coreum:
chain_id: coreum-testnet-1
denom: utestcore
contract:
contract_address: testcore1eyky8vfdyz77zkh50zkrdw3mc9guyrfy45pd5ak9jpqgtgwgfvfqd8lkmc
contract_address: testcore1sx8h66ehjcj999t5apz5993y9n6nxwdr5jcd06mzrquxdkful4tqpugwh8
gas_adjustment: 1.4
gas_price_adjustment: 1.2
page_limit: 50
Expand Down

0 comments on commit 34b99aa

Please sign in to comment.