From 049202c3164641403b88c754873e2463c3948e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=AA?= Date: Thu, 7 Jan 2021 18:36:28 +0800 Subject: [PATCH 1/2] refactor export function --- modules/htlc/genesis.go | 8 -------- modules/random/genesis.go | 4 ++-- modules/random/genesis_test.go | 5 ++--- modules/random/keeper/grpc_query_test.go | 2 +- modules/random/keeper/keeper.go | 5 +++-- modules/random/keeper/querier.go | 2 +- simapp/test_helpers.go | 2 +- 7 files changed, 10 insertions(+), 18 deletions(-) diff --git a/modules/htlc/genesis.go b/modules/htlc/genesis.go index c817fdaa..9c50291d 100644 --- a/modules/htlc/genesis.go +++ b/modules/htlc/genesis.go @@ -2,8 +2,6 @@ package htlc import ( "encoding/hex" - "fmt" - tmbytes "github.com/tendermint/tendermint/libs/bytes" sdk "github.com/cosmos/cosmos-sdk/types" @@ -32,14 +30,8 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { k.IterateHTLCs(ctx, func(hlock tmbytes.HexBytes, h types.HTLC) (stop bool) { if h.State == types.Open { - h.ExpirationHeight = h.ExpirationHeight - uint64(ctx.BlockHeight()) + 1 pendingHtlcs[hlock.String()] = h - } else if h.State == types.Expired { - if err := k.RefundHTLC(ctx, hlock); err != nil { - panic(fmt.Errorf("failed to export the HTLC genesis state: %s", hlock.String())) - } } - return false }) diff --git a/modules/random/genesis.go b/modules/random/genesis.go index 33c5c494..f566da99 100644 --- a/modules/random/genesis.go +++ b/modules/random/genesis.go @@ -27,8 +27,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) { func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { pendingRequests := make(map[string]types.Requests) - k.IterateRandomRequestQueue(ctx, func(height int64, request types.Request) bool { - leftHeight := fmt.Sprintf("%d", height-ctx.BlockHeight()+1) + k.IterateRandomRequestQueue(ctx, func(height int64, reqID []byte, request types.Request) bool { + leftHeight := fmt.Sprintf("%d", height) heightRequests, ok := pendingRequests[leftHeight] if ok { heightRequests.Requests = append(heightRequests.Requests, request) diff --git a/modules/random/genesis_test.go b/modules/random/genesis_test.go index 7fa36526..33921f6a 100644 --- a/modules/random/genesis_test.go +++ b/modules/random/genesis_test.go @@ -62,7 +62,7 @@ func (suite *GenesisTestSuite) TestExportGenesis() { // get the pending requests from queue storedRequests := make(map[int64][]types.Request) - suite.keeper.IterateRandomRequestQueue(suite.ctx, func(h int64, r types.Request) bool { + suite.keeper.IterateRandomRequestQueue(suite.ctx, func(h int64, reqID []byte, r types.Request) bool { storedRequests[h] = append(storedRequests[h], r) return false }) @@ -76,7 +76,6 @@ func (suite *GenesisTestSuite) TestExportGenesis() { // assert that exported requests are consistent with requests in queue for height, requests := range exportedRequests { h, _ := strconv.ParseInt(height, 10, 64) - storedHeight := h + testNewHeight - 1 - suite.Equal(storedRequests[storedHeight], requests.Requests) + suite.Equal(storedRequests[h], requests.Requests) } } diff --git a/modules/random/keeper/grpc_query_test.go b/modules/random/keeper/grpc_query_test.go index 67014586..a0ea5a60 100644 --- a/modules/random/keeper/grpc_query_test.go +++ b/modules/random/keeper/grpc_query_test.go @@ -51,7 +51,7 @@ func (suite *KeeperTestSuite) TestGRPCRandomRequestQueue() { suite.Require().NoError(err) var requests = make([]types.Request, 0) - app.RandomKeeper.IterateRandomRequestQueue(ctx, func(h int64, r types.Request) (stop bool) { + app.RandomKeeper.IterateRandomRequestQueue(ctx, func(h int64, reqID []byte, r types.Request) (stop bool) { requests = append(requests, r) return false }) diff --git a/modules/random/keeper/keeper.go b/modules/random/keeper/keeper.go index e6c8ab43..b1f6b699 100644 --- a/modules/random/keeper/keeper.go +++ b/modules/random/keeper/keeper.go @@ -169,7 +169,7 @@ func (k Keeper) IterateRandomRequestQueueByHeight(ctx sdk.Context, height int64) } // IterateRandomRequestQueue iterates through the random number request queue -func (k Keeper) IterateRandomRequestQueue(ctx sdk.Context, op func(h int64, r types.Request) (stop bool)) { +func (k Keeper) IterateRandomRequestQueue(ctx sdk.Context, op func(h int64, reqID []byte, r types.Request) (stop bool)) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.PrefixRandomRequestQueue) @@ -178,11 +178,12 @@ func (k Keeper) IterateRandomRequestQueue(ctx sdk.Context, op func(h int64, r ty for ; iterator.Valid(); iterator.Next() { keyParts := bytes.Split(iterator.Key(), types.KeyDelimiter) height, _ := strconv.ParseInt(string(keyParts[1]), 10, 64) + reqID := keyParts[2] var request types.Request k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &request) - if stop := op(height, request); stop { + if stop := op(height, reqID, request); stop { break } } diff --git a/modules/random/keeper/querier.go b/modules/random/keeper/querier.go index 2bc727e1..0e518cdb 100644 --- a/modules/random/keeper/querier.go +++ b/modules/random/keeper/querier.go @@ -97,7 +97,7 @@ func queryRandomRequestQueueByHeight(ctx sdk.Context, height int64, k Keeper) [] func queryAllRandomRequestsInQueue(ctx sdk.Context, k Keeper) []types.Request { var requests = make([]types.Request, 0) - k.IterateRandomRequestQueue(ctx, func(h int64, r types.Request) (stop bool) { + k.IterateRandomRequestQueue(ctx, func(h int64, reqID []byte, r types.Request) (stop bool) { requests = append(requests, r) return false }) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 963dd679..22354d29 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -21,10 +21,10 @@ import ( bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" servertypes "github.com/cosmos/cosmos-sdk/server/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/simapp/helpers" storetypes "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/testutil" From 49677d31b4918226fa7171a9c3e56bf6096a5200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=AA?= Date: Sun, 10 Jan 2021 15:06:54 +0800 Subject: [PATCH 2/2] add method PrepForZeroHeightGenesis to htlc and random --- modules/htlc/genesis.go | 17 +++++++++++++++++ modules/random/genesis.go | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/modules/htlc/genesis.go b/modules/htlc/genesis.go index 9c50291d..71620b29 100644 --- a/modules/htlc/genesis.go +++ b/modules/htlc/genesis.go @@ -2,6 +2,7 @@ package htlc import ( "encoding/hex" + "fmt" tmbytes "github.com/tendermint/tendermint/libs/bytes" sdk "github.com/cosmos/cosmos-sdk/types" @@ -39,3 +40,19 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { PendingHtlcs: pendingHtlcs, } } + +func PrepForZeroHeightGenesis(ctx sdk.Context, k keeper.Keeper) { + k.IterateHTLCs( + ctx, + func(hlock tmbytes.HexBytes, h types.HTLC) (stop bool) { + if h.State == types.Open { + h.ExpirationHeight = h.ExpirationHeight - uint64(ctx.BlockHeight()) + 1 + k.SetHTLC(ctx,h,hlock) + } else if h.State == types.Expired { + if err := k.RefundHTLC(ctx, hlock); err != nil { + panic(fmt.Errorf("failed to export the HTLC genesis state: %s", hlock.String())) + } + } + return false + }) +} diff --git a/modules/random/genesis.go b/modules/random/genesis.go index f566da99..83a97e03 100644 --- a/modules/random/genesis.go +++ b/modules/random/genesis.go @@ -43,3 +43,12 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { return &types.GenesisState{PendingRandomRequests: pendingRequests} } + +func PrepForZeroHeightGenesis(ctx sdk.Context, k keeper.Keeper) { + k.IterateRandomRequestQueue(ctx, func(height int64, reqID []byte, request types.Request) bool { + leftHeight := height-ctx.BlockHeight()+1 + k.DequeueRandomRequest(ctx, height, reqID) + k.EnqueueRandomRequest(ctx, leftHeight, reqID, request) + return false + }) +} \ No newline at end of file