2020-06-03 18:54:31 +00:00
<!--
order: 6
-->
2020-04-23 20:57:25 +00:00
# Begin Block
2020-05-12 20:15:38 +00:00
At the start of each block, atomic swaps that meet certain criteria are expired or deleted.
2020-04-23 20:57:25 +00:00
```go
2020-05-12 20:15:38 +00:00
func BeginBlocker(ctx sdk.Context, k Keeper) {
k.UpdateExpiredAtomicSwaps(ctx)
k.DeleteClosedAtomicSwapsFromLongtermStorage(ctx)
}
```
## Expiration
If an atomic swap's `ExpireHeight` is greater than the current block height, it will be expired. The logic to expire atomic swaps is as follows:
```go
var expiredSwapIDs []string
2020-04-23 20:57:25 +00:00
k.IterateAtomicSwapsByBlock(ctx, uint64(ctx.BlockHeight()), func(id []byte) bool {
2020-05-12 20:15:38 +00:00
atomicSwap, found := k.GetAtomicSwap(ctx, id)
if !found {
return false
}
// Expire the uncompleted swap and update both indexes
atomicSwap.Status = types.Expired
k.RemoveFromByBlockIndex(ctx, atomicSwap)
k.SetAtomicSwap(ctx, atomicSwap)
expiredSwapIDs = append(expiredSwapIDs, hex.EncodeToString(atomicSwap.GetSwapID()))
2020-04-23 20:57:25 +00:00
return false
})
2020-05-12 20:15:38 +00:00
```
2020-04-23 20:57:25 +00:00
2020-05-12 20:15:38 +00:00
## Deletion
Atomic swaps are deleted 86400 blocks (one week, assuming a block time of 7 seconds) after being completed. The logic to delete atomic swaps is as follows:
```go
k.IterateAtomicSwapsLongtermStorage(ctx, uint64(ctx.BlockHeight()), func(id []byte) bool {
swap, found := k.GetAtomicSwap(ctx, id)
if !found {
return false
2020-04-23 20:57:25 +00:00
}
2020-05-12 20:15:38 +00:00
k.RemoveAtomicSwap(ctx, swap.GetSwapID())
k.RemoveFromLongtermStorage(ctx, swap)
return false
})
```