0g-chain/x/bep3/spec/06_begin_block.md
Denali Marsh 6c68e41758
[R4R] bep3 module audit revisions (#505)
* 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
2020-05-12 13:15:38 -07:00

1.3 KiB

Begin Block

At the start of each block, atomic swaps that meet certain criteria are expired or deleted.

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:

	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:

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
})