mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
6c68e41758
* most audit revisions * remove expected income * update begin block spec * filter queryAtomicSwaps, add queryAssetSupplies * update old address * update test * Remove legacy method * remove legacy comment * address PR comments * IsValid for SwapDirection, SwapStatus * fix rng logging * query asset supplies * return [64]byte from rng * remove cross chain field from MsgCreateAtomicSwap * move swap filtering to querier * rename Limit field to SupplyLimit
46 lines
1.3 KiB
Markdown
46 lines
1.3 KiB
Markdown
# Begin Block
|
|
|
|
At the start of each block, atomic swaps that meet certain criteria are expired or deleted.
|
|
|
|
```go
|
|
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
|
|
k.IterateAtomicSwapsByBlock(ctx, uint64(ctx.BlockHeight()), func(id []byte) bool {
|
|
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()))
|
|
return false
|
|
})
|
|
```
|
|
|
|
## 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
|
|
}
|
|
k.RemoveAtomicSwap(ctx, swap.GetSwapID())
|
|
k.RemoveFromLongtermStorage(ctx, swap)
|
|
return false
|
|
})
|
|
``` |