From 0168bec6e56875e7072f3ab4124f3428782b9aa0 Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:36:34 +0100 Subject: [PATCH 01/10] Added epoch unbondings export / import at genesis time --- proto/lum/network/millions/genesis.proto | 2 + x/millions/genesis.go | 4 + x/millions/genesis_test.go | 23 ++++ x/millions/keeper/keeper_epoch.go | 22 ++++ x/millions/types/genesis.pb.go | 150 ++++++++++++++++------- 5 files changed, 158 insertions(+), 43 deletions(-) diff --git a/proto/lum/network/millions/genesis.proto b/proto/lum/network/millions/genesis.proto index 28974bc7..024d64d1 100644 --- a/proto/lum/network/millions/genesis.proto +++ b/proto/lum/network/millions/genesis.proto @@ -29,4 +29,6 @@ message GenesisState { repeated Prize prizes = 9 [ (gogoproto.nullable) = false ]; repeated Withdrawal withdrawals = 10 [ (gogoproto.nullable) = false ]; repeated EpochTracker epoch_trackers = 11 [ (gogoproto.nullable) = false ]; + repeated EpochUnbonding epoch_unbondings = 12 + [ (gogoproto.nullable) = false ]; } diff --git a/x/millions/genesis.go b/x/millions/genesis.go index 01366ba4..bc9162bb 100644 --- a/x/millions/genesis.go +++ b/x/millions/genesis.go @@ -47,6 +47,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) k.SetEpochTracker(ctx, epochTracker, types.WithdrawalTrackerType) } + for _, epochUnbonding := range genState.EpochUnbondings { + k.SetEpochUnbonding(ctx, epochUnbonding) + } } func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { @@ -63,5 +66,6 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { Prizes: k.ListPrizes(ctx), Withdrawals: k.ListWithdrawals(ctx), EpochTrackers: k.ListEpochTrackers(ctx), + EpochUnbondings: k.ListEpochUnbondings(ctx), } } diff --git a/x/millions/genesis_test.go b/x/millions/genesis_test.go index 6b2ae5bd..7c1bfc07 100644 --- a/x/millions/genesis_test.go +++ b/x/millions/genesis_test.go @@ -109,6 +109,9 @@ var testGenesis = millionstypes.GenesisState{ EpochTrackers: []millionstypes.EpochTracker{ {EpochTrackerType: epochstypes.DAY_EPOCH, EpochIdentifier: epochstypes.DAY_EPOCH, NextEpochNumber: uint64(2), PreviousEpochNumber: uint64(0), NextEpochStartTime: future}, }, + EpochUnbondings: []millionstypes.EpochUnbonding{ + {PoolId: 1, EpochIdentifier: "test", EpochNumber: 1, TotalAmount: sdk.NewCoin("ulum", sdk.NewInt(1000000)), WithdrawalIds: []uint64{1, 2}, WithdrawalIdsCount: 2}, + }, } func TestInitGenesis(t *testing.T) { @@ -298,6 +301,16 @@ func TestInitGenesis(t *testing.T) { require.Equal(t, testGenesis.EpochTrackers[i].NextEpochStartTime, epochTracker.NextEpochStartTime) } + // Make sure epoch unbondings have been imported + epochUnbondings := app.MillionsKeeper.ListEpochUnbondings(ctx) + require.Len(t, epochUnbondings, len(testGenesis.EpochUnbondings)) + for i, epochUnbonding := range epochUnbondings { + require.Equal(t, testGenesis.EpochUnbondings[i].EpochIdentifier, epochUnbonding.EpochIdentifier) + require.Equal(t, testGenesis.EpochUnbondings[i].EpochNumber, epochUnbonding.EpochNumber) + require.Equal(t, testGenesis.EpochUnbondings[i].WithdrawalIds, epochUnbonding.WithdrawalIds) + require.Equal(t, testGenesis.EpochUnbondings[i].WithdrawalIdsCount, epochUnbonding.WithdrawalIdsCount) + require.Equal(t, testGenesis.EpochUnbondings[i].TotalAmount.Amount.Int64(), epochUnbonding.TotalAmount.Amount.Int64()) + } } func TestExportGenesis(t *testing.T) { @@ -407,4 +420,14 @@ func TestExportGenesis(t *testing.T) { require.Equal(t, testGenesis.EpochTrackers[i].NextEpochNumber, epochTracker.NextEpochNumber) require.Equal(t, testGenesis.EpochTrackers[i].NextEpochStartTime, epochTracker.NextEpochStartTime) } + + // Test epoch unbondings export + require.Len(t, exportGenesis.EpochUnbondings, len(testGenesis.EpochUnbondings)) + for i, epochUnbonding := range testGenesis.EpochUnbondings { + require.Equal(t, testGenesis.EpochUnbondings[i].EpochIdentifier, epochUnbonding.EpochIdentifier) + require.Equal(t, testGenesis.EpochUnbondings[i].EpochNumber, epochUnbonding.EpochNumber) + require.Equal(t, testGenesis.EpochUnbondings[i].WithdrawalIds, epochUnbonding.WithdrawalIds) + require.Equal(t, testGenesis.EpochUnbondings[i].WithdrawalIdsCount, epochUnbonding.WithdrawalIdsCount) + require.Equal(t, testGenesis.EpochUnbondings[i].TotalAmount.Amount.Int64(), epochUnbonding.TotalAmount.Amount.Int64()) + } } diff --git a/x/millions/keeper/keeper_epoch.go b/x/millions/keeper/keeper_epoch.go index 3c063dff..9f1aabdc 100644 --- a/x/millions/keeper/keeper_epoch.go +++ b/x/millions/keeper/keeper_epoch.go @@ -395,6 +395,14 @@ func (k Keeper) GetEpochUnbondings(ctx sdk.Context, epochID uint64) (epochUnbond return } +// SetEpochUnbonding sets the internal epoch unbondings +func (k Keeper) SetEpochUnbonding(ctx sdk.Context, epochUnbonding types.EpochUnbonding) { + store := ctx.KVStore(k.storeKey) + key := types.GetEpochUnbondingsKey(epochUnbonding.GetEpochNumber()) + encodedEpochUnbonding := k.cdc.MustMarshal(&epochUnbonding) + store.Set(key, encodedEpochUnbonding) +} + func (k Keeper) ListEpochTrackers(ctx sdk.Context) (list []types.EpochTracker) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.EpochTrackerPrefix) @@ -408,3 +416,17 @@ func (k Keeper) ListEpochTrackers(ctx sdk.Context) (list []types.EpochTracker) { return } + +func (k Keeper) ListEpochUnbondings(ctx sdk.Context) (list []types.EpochUnbonding) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.EpochUnbondingPrefix) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + var val types.EpochUnbonding + k.cdc.MustUnmarshal(iterator.Value(), &val) + list = append(list, val) + } + + return +} diff --git a/x/millions/types/genesis.pb.go b/x/millions/types/genesis.pb.go index 3a93d1b8..a078966b 100644 --- a/x/millions/types/genesis.pb.go +++ b/x/millions/types/genesis.pb.go @@ -25,17 +25,18 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type GenesisState struct { - Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params" yaml:"params"` - NextPoolId uint64 `protobuf:"varint,2,opt,name=next_pool_id,json=nextPoolId,proto3" json:"next_pool_id,omitempty"` - NextDepositId uint64 `protobuf:"varint,3,opt,name=next_deposit_id,json=nextDepositId,proto3" json:"next_deposit_id,omitempty"` - NextPrizeId uint64 `protobuf:"varint,4,opt,name=next_prize_id,json=nextPrizeId,proto3" json:"next_prize_id,omitempty"` - NextWithdrawalId uint64 `protobuf:"varint,5,opt,name=next_withdrawal_id,json=nextWithdrawalId,proto3" json:"next_withdrawal_id,omitempty"` - Pools []Pool `protobuf:"bytes,6,rep,name=pools,proto3" json:"pools"` - Deposits []Deposit `protobuf:"bytes,7,rep,name=deposits,proto3" json:"deposits"` - Draws []Draw `protobuf:"bytes,8,rep,name=draws,proto3" json:"draws"` - Prizes []Prize `protobuf:"bytes,9,rep,name=prizes,proto3" json:"prizes"` - Withdrawals []Withdrawal `protobuf:"bytes,10,rep,name=withdrawals,proto3" json:"withdrawals"` - EpochTrackers []EpochTracker `protobuf:"bytes,11,rep,name=epoch_trackers,json=epochTrackers,proto3" json:"epoch_trackers"` + Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params" yaml:"params"` + NextPoolId uint64 `protobuf:"varint,2,opt,name=next_pool_id,json=nextPoolId,proto3" json:"next_pool_id,omitempty"` + NextDepositId uint64 `protobuf:"varint,3,opt,name=next_deposit_id,json=nextDepositId,proto3" json:"next_deposit_id,omitempty"` + NextPrizeId uint64 `protobuf:"varint,4,opt,name=next_prize_id,json=nextPrizeId,proto3" json:"next_prize_id,omitempty"` + NextWithdrawalId uint64 `protobuf:"varint,5,opt,name=next_withdrawal_id,json=nextWithdrawalId,proto3" json:"next_withdrawal_id,omitempty"` + Pools []Pool `protobuf:"bytes,6,rep,name=pools,proto3" json:"pools"` + Deposits []Deposit `protobuf:"bytes,7,rep,name=deposits,proto3" json:"deposits"` + Draws []Draw `protobuf:"bytes,8,rep,name=draws,proto3" json:"draws"` + Prizes []Prize `protobuf:"bytes,9,rep,name=prizes,proto3" json:"prizes"` + Withdrawals []Withdrawal `protobuf:"bytes,10,rep,name=withdrawals,proto3" json:"withdrawals"` + EpochTrackers []EpochTracker `protobuf:"bytes,11,rep,name=epoch_trackers,json=epochTrackers,proto3" json:"epoch_trackers"` + EpochUnbondings []EpochUnbonding `protobuf:"bytes,12,rep,name=epoch_unbondings,json=epochUnbondings,proto3" json:"epoch_unbondings"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -148,6 +149,13 @@ func (m *GenesisState) GetEpochTrackers() []EpochTracker { return nil } +func (m *GenesisState) GetEpochUnbondings() []EpochUnbonding { + if m != nil { + return m.EpochUnbondings + } + return nil +} + func init() { proto.RegisterType((*GenesisState)(nil), "lum.network.millions.GenesisState") } @@ -157,38 +165,40 @@ func init() { } var fileDescriptor_ff43331bdfdad888 = []byte{ - // 487 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0x41, 0x6b, 0x13, 0x41, - 0x18, 0x86, 0xb3, 0x36, 0x8d, 0x75, 0xb6, 0x51, 0x19, 0x2a, 0x2c, 0x51, 0x37, 0xeb, 0x82, 0x92, - 0x83, 0xdd, 0x85, 0x0a, 0x82, 0x5e, 0x84, 0x50, 0xa9, 0xc1, 0x83, 0x25, 0x0a, 0x82, 0x97, 0x30, - 0xcd, 0x0e, 0xc9, 0xd0, 0xd9, 0x9d, 0x65, 0x66, 0x42, 0x5a, 0x7f, 0x85, 0x3f, 0xab, 0xc7, 0x1e, - 0x3d, 0x15, 0x49, 0xfe, 0x81, 0x3f, 0x40, 0xe4, 0x9b, 0x99, 0x34, 0x3d, 0x4c, 0x72, 0x5b, 0xbe, - 0x7d, 0xde, 0x77, 0xde, 0xef, 0xe5, 0x43, 0x29, 0x9f, 0x95, 0x79, 0x45, 0xf5, 0x5c, 0xc8, 0xf3, - 0xbc, 0x64, 0x9c, 0x33, 0x51, 0xa9, 0x7c, 0x42, 0x2b, 0xaa, 0x98, 0xca, 0x6a, 0x29, 0xb4, 0xc0, - 0x07, 0x7c, 0x56, 0x66, 0x8e, 0xc9, 0x56, 0x4c, 0xe7, 0x60, 0x22, 0x26, 0xc2, 0x00, 0x39, 0x7c, - 0x59, 0xb6, 0xe3, 0xf7, 0x2b, 0x68, 0x2d, 0x14, 0xd3, 0x8e, 0xe9, 0xfa, 0x19, 0x49, 0xe6, 0x0e, - 0x78, 0xe1, 0x05, 0x6a, 0x22, 0x49, 0xa9, 0xb6, 0x7a, 0xd4, 0x42, 0x70, 0x07, 0x24, 0x7e, 0x40, - 0xb2, 0x9f, 0xd4, 0x11, 0x2f, 0xbd, 0xc4, 0x9c, 0xe9, 0x29, 0x44, 0x21, 0xdb, 0x8d, 0x68, 0x2d, - 0xc6, 0x53, 0x4b, 0xa4, 0xff, 0x9a, 0x68, 0xff, 0xc4, 0x36, 0xf6, 0x55, 0x13, 0x4d, 0xf1, 0x67, - 0xd4, 0xb2, 0x61, 0xa3, 0x20, 0x09, 0x7a, 0xe1, 0xd1, 0xb3, 0xcc, 0xd7, 0x60, 0x76, 0x6a, 0x98, - 0xfe, 0x93, 0xab, 0x9b, 0x6e, 0xe3, 0xef, 0x4d, 0xb7, 0x7d, 0x49, 0x4a, 0xfe, 0x3e, 0xb5, 0xca, - 0x74, 0xe8, 0x2c, 0x70, 0x82, 0xf6, 0x2b, 0x7a, 0xa1, 0x47, 0xb0, 0xdb, 0x88, 0x15, 0xd1, 0xbd, - 0x24, 0xe8, 0x35, 0x87, 0x08, 0x66, 0xa7, 0x42, 0xf0, 0x41, 0x81, 0x5f, 0xa1, 0x47, 0x86, 0x70, - 0x2d, 0x03, 0xb4, 0x63, 0xa0, 0x36, 0x8c, 0x8f, 0xed, 0x74, 0x50, 0xe0, 0x14, 0xb5, 0xad, 0x13, - 0x94, 0x00, 0x54, 0xd3, 0x50, 0xa1, 0xb1, 0x82, 0xd9, 0xa0, 0xc0, 0xaf, 0x11, 0x36, 0xcc, 0xba, - 0x06, 0x00, 0x77, 0x0d, 0xf8, 0x18, 0xfe, 0x7c, 0xbf, 0xfd, 0x31, 0x28, 0xf0, 0x5b, 0xb4, 0x0b, - 0xb1, 0x54, 0xd4, 0x4a, 0x76, 0x7a, 0xe1, 0x51, 0x67, 0xc3, 0x9e, 0x42, 0xf0, 0x7e, 0x13, 0xb6, - 0x1c, 0x5a, 0x1c, 0x7f, 0x40, 0x7b, 0x2e, 0xac, 0x8a, 0xee, 0x1b, 0xe9, 0x73, 0xbf, 0xd4, 0x85, - 0x77, 0xea, 0x5b, 0x11, 0x3c, 0x0c, 0x21, 0x54, 0xb4, 0xb7, 0xed, 0xe1, 0x63, 0x49, 0xe6, 0xab, - 0x87, 0x0d, 0x8e, 0xdf, 0xa1, 0x96, 0xd9, 0x5e, 0x45, 0x0f, 0x8c, 0xf0, 0xe9, 0x86, 0xc4, 0xc0, - 0x38, 0xa5, 0x13, 0xe0, 0x4f, 0x28, 0x5c, 0x97, 0xa2, 0x22, 0x64, 0xf4, 0x89, 0x5f, 0xbf, 0x2e, - 0xc9, 0x99, 0xdc, 0x95, 0xe2, 0x2f, 0xe8, 0xa1, 0x39, 0x9f, 0x91, 0x96, 0x64, 0x7c, 0x4e, 0xa5, - 0x8a, 0x42, 0x63, 0x96, 0xfa, 0xcd, 0x3e, 0x02, 0xfb, 0xcd, 0xa2, 0xce, 0xae, 0x4d, 0xef, 0xcc, - 0x54, 0xff, 0xe4, 0x6a, 0x11, 0x07, 0xd7, 0x8b, 0x38, 0xf8, 0xb3, 0x88, 0x83, 0x5f, 0xcb, 0xb8, - 0x71, 0xbd, 0x8c, 0x1b, 0xbf, 0x97, 0x71, 0xe3, 0xc7, 0xe1, 0x84, 0xe9, 0xe9, 0xec, 0x2c, 0x1b, - 0x8b, 0x32, 0xe7, 0xb3, 0xf2, 0x70, 0x75, 0xc7, 0xe3, 0x29, 0x61, 0x55, 0x7e, 0xb1, 0xbe, 0x67, - 0x7d, 0x59, 0x53, 0x75, 0xd6, 0x32, 0x07, 0xfd, 0xe6, 0x7f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe4, - 0xff, 0xe6, 0xac, 0x16, 0x04, 0x00, 0x00, + // 515 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x93, 0x4f, 0x6b, 0x13, 0x41, + 0x18, 0x87, 0xb3, 0xb6, 0x89, 0x75, 0x36, 0xb1, 0x65, 0xa8, 0x30, 0x44, 0xdd, 0xac, 0x8b, 0x4a, + 0x0e, 0x76, 0x17, 0x2a, 0x08, 0x7a, 0x11, 0x42, 0xa5, 0x06, 0x0f, 0x96, 0x68, 0x11, 0xbc, 0x84, + 0x49, 0x76, 0xd8, 0x0c, 0x9d, 0xdd, 0x59, 0x76, 0x66, 0x49, 0xeb, 0xa7, 0xf0, 0x63, 0xf5, 0xd8, + 0x8b, 0xe0, 0xa9, 0x48, 0xf2, 0x0d, 0xfc, 0x04, 0x32, 0x7f, 0xd2, 0x54, 0x98, 0xe6, 0xb6, 0xbc, + 0xf3, 0xfc, 0x9e, 0x79, 0xdf, 0x97, 0x1d, 0x10, 0xb1, 0x3a, 0x4f, 0x0a, 0x22, 0xe7, 0xbc, 0x3a, + 0x4b, 0x72, 0xca, 0x18, 0xe5, 0x85, 0x48, 0x32, 0x52, 0x10, 0x41, 0x45, 0x5c, 0x56, 0x5c, 0x72, + 0xb8, 0xcf, 0xea, 0x3c, 0xb6, 0x4c, 0xbc, 0x62, 0xba, 0xfb, 0x19, 0xcf, 0xb8, 0x06, 0x12, 0xf5, + 0x65, 0xd8, 0xae, 0xdb, 0x97, 0x92, 0x92, 0x0b, 0x2a, 0x2d, 0xd3, 0x73, 0x33, 0x15, 0x9e, 0x5b, + 0xe0, 0x99, 0x13, 0x28, 0x71, 0x85, 0x73, 0xb1, 0xd1, 0x51, 0x72, 0xce, 0x2c, 0x10, 0xba, 0x81, + 0x8a, 0xfe, 0x20, 0x96, 0x78, 0xe1, 0x24, 0xe6, 0x54, 0xce, 0x54, 0x2b, 0x78, 0xb3, 0x88, 0x94, + 0x7c, 0x3a, 0x33, 0x44, 0xf4, 0xab, 0x09, 0xda, 0xc7, 0x66, 0x63, 0x5f, 0x24, 0x96, 0x04, 0x7e, + 0x02, 0x2d, 0xd3, 0x2c, 0xf2, 0x42, 0xaf, 0xef, 0x1f, 0x3e, 0x89, 0x5d, 0x1b, 0x8c, 0x4f, 0x34, + 0x33, 0x78, 0x74, 0x79, 0xdd, 0x6b, 0xfc, 0xbd, 0xee, 0x75, 0x2e, 0x70, 0xce, 0xde, 0x45, 0x26, + 0x19, 0x8d, 0xac, 0x02, 0x86, 0xa0, 0x5d, 0x90, 0x73, 0x39, 0x56, 0xb3, 0x8d, 0x69, 0x8a, 0xee, + 0x85, 0x5e, 0x7f, 0x7b, 0x04, 0x54, 0xed, 0x84, 0x73, 0x36, 0x4c, 0xe1, 0x4b, 0xb0, 0xab, 0x09, + 0xbb, 0x65, 0x05, 0x6d, 0x69, 0xa8, 0xa3, 0xca, 0x47, 0xa6, 0x3a, 0x4c, 0x61, 0x04, 0x3a, 0xc6, + 0xa4, 0x96, 0xa0, 0xa8, 0x6d, 0x4d, 0xf9, 0x5a, 0xa5, 0x6a, 0xc3, 0x14, 0xbe, 0x02, 0x50, 0x33, + 0xeb, 0x35, 0x28, 0xb0, 0xa9, 0xc1, 0x3d, 0x75, 0xf2, 0xed, 0xe6, 0x60, 0x98, 0xc2, 0x37, 0xa0, + 0xa9, 0xda, 0x12, 0xa8, 0x15, 0x6e, 0xf5, 0xfd, 0xc3, 0xee, 0x1d, 0x73, 0x72, 0xce, 0x06, 0xdb, + 0x6a, 0xca, 0x91, 0xc1, 0xe1, 0x7b, 0xb0, 0x63, 0x9b, 0x15, 0xe8, 0xbe, 0x8e, 0x3e, 0x75, 0x47, + 0x6d, 0xf3, 0x36, 0x7d, 0x13, 0x52, 0x17, 0xab, 0x26, 0x04, 0xda, 0xd9, 0x74, 0xf1, 0x51, 0x85, + 0xe7, 0xab, 0x8b, 0x35, 0x0e, 0xdf, 0x82, 0x96, 0x9e, 0x5e, 0xa0, 0x07, 0x3a, 0xf8, 0xf8, 0x8e, + 0x8e, 0x15, 0x63, 0x93, 0x36, 0x00, 0x3f, 0x02, 0x7f, 0xbd, 0x14, 0x81, 0x80, 0xce, 0x87, 0xee, + 0xfc, 0x7a, 0x49, 0x56, 0x72, 0x3b, 0x0a, 0x3f, 0x83, 0x87, 0xfa, 0xf7, 0x19, 0xcb, 0x0a, 0x4f, + 0xcf, 0x48, 0x25, 0x90, 0xaf, 0x65, 0x91, 0x5b, 0xf6, 0x41, 0xb1, 0x5f, 0x0d, 0x6a, 0x75, 0x1d, + 0x72, 0xab, 0x26, 0xe0, 0x29, 0xd8, 0x33, 0xc2, 0xba, 0x98, 0xf0, 0x22, 0xa5, 0x45, 0x26, 0x50, + 0x5b, 0x2b, 0x9f, 0x6f, 0x50, 0x9e, 0xae, 0x60, 0x2b, 0xdd, 0x25, 0xff, 0x55, 0xc5, 0xe0, 0xf8, + 0x72, 0x11, 0x78, 0x57, 0x8b, 0xc0, 0xfb, 0xb3, 0x08, 0xbc, 0x9f, 0xcb, 0xa0, 0x71, 0xb5, 0x0c, + 0x1a, 0xbf, 0x97, 0x41, 0xe3, 0xfb, 0x41, 0x46, 0xe5, 0xac, 0x9e, 0xc4, 0x53, 0x9e, 0x27, 0xac, + 0xce, 0x0f, 0x56, 0xcf, 0x63, 0x3a, 0xc3, 0xb4, 0x48, 0xce, 0xd7, 0xcf, 0x44, 0x5e, 0x94, 0x44, + 0x4c, 0x5a, 0xfa, 0x9d, 0xbc, 0xfe, 0x17, 0x00, 0x00, 0xff, 0xff, 0x25, 0xb2, 0x31, 0xe9, 0x6d, + 0x04, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -211,6 +221,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.EpochUnbondings) > 0 { + for iNdEx := len(m.EpochUnbondings) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.EpochUnbondings[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x62 + } + } if len(m.EpochTrackers) > 0 { for iNdEx := len(m.EpochTrackers) - 1; iNdEx >= 0; iNdEx-- { { @@ -395,6 +419,12 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if len(m.EpochUnbondings) > 0 { + for _, e := range m.EpochUnbondings { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } return n } @@ -746,6 +776,40 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 12: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field EpochUnbondings", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.EpochUnbondings = append(m.EpochUnbondings, EpochUnbonding{}) + if err := m.EpochUnbondings[len(m.EpochUnbondings)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) From a1da82cad2aac76df241633e3dd476e421fb1be4 Mon Sep 17 00:00:00 2001 From: Zakaria Lounes Date: Thu, 9 Nov 2023 17:20:29 +0100 Subject: [PATCH 02/10] chore(dev-deps): update ledger-cosmos-go from v0.12.2 to v0.12.4 and ledger-go from v0.14.1 to v0.14.3 --- go.mod | 6 +++--- go.sum | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 7cdc58aa..e519e8a9 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,7 @@ require ( github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect - github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect + github.com/cosmos/ledger-cosmos-go v0.12.4 // indirect github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect github.com/creachadair/taskgroup v0.4.2 // indirect github.com/danieljoos/wincred v1.1.2 // indirect @@ -152,8 +152,8 @@ require ( github.com/tendermint/go-amino v0.16.0 // indirect github.com/tidwall/btree v1.6.0 // indirect github.com/ulikunitz/xz v0.5.11 // indirect - github.com/zondax/hid v0.9.1 // indirect - github.com/zondax/ledger-go v0.14.1 // indirect + github.com/zondax/hid v0.9.2 // indirect + github.com/zondax/ledger-go v0.14.3 // indirect go.etcd.io/bbolt v1.3.7 // indirect go.opencensus.io v0.24.0 // indirect golang.org/x/crypto v0.11.0 // indirect diff --git a/go.sum b/go.sum index 645d6237..5ecbcd80 100644 --- a/go.sum +++ b/go.sum @@ -400,8 +400,8 @@ github.com/cosmos/ibc-go/v7 v7.2.0 h1:dx0DLUl7rxdyZ8NiT6UsrbzKOJx/w7s+BOaewFRH6c github.com/cosmos/ibc-go/v7 v7.2.0/go.mod h1:OOcjKIRku/j1Xs1RgKK0yvKRrJ5iFuZYMetR1n3yMlc= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM= github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0= -github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA= -github.com/cosmos/ledger-cosmos-go v0.12.2/go.mod h1:ZcqYgnfNJ6lAXe4HPtWgarNEY+B74i+2/8MhZw4ziiI= +github.com/cosmos/ledger-cosmos-go v0.12.4 h1:drvWt+GJP7Aiw550yeb3ON/zsrgW0jgh5saFCr7pDnw= +github.com/cosmos/ledger-cosmos-go v0.12.4/go.mod h1:fjfVWRf++Xkygt9wzCsjEBdjcf7wiiY35fv3ctT+k4M= github.com/cosmos/rosetta-sdk-go v0.10.0 h1:E5RhTruuoA7KTIXUcMicL76cffyeoyvNybzUGSKFTcM= github.com/cosmos/rosetta-sdk-go v0.10.0/go.mod h1:SImAZkb96YbwvoRkzSMQB6noNJXFgWl/ENIznEoYQI4= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= @@ -1160,10 +1160,10 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zondax/hid v0.9.1 h1:gQe66rtmyZ8VeGFcOpbuH3r7erYtNEAezCAYu8LdkJo= -github.com/zondax/hid v0.9.1/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM= -github.com/zondax/ledger-go v0.14.1 h1:Pip65OOl4iJ84WTpA4BKChvOufMhhbxED3BaihoZN4c= -github.com/zondax/ledger-go v0.14.1/go.mod h1:fZ3Dqg6qcdXWSOJFKMG8GCTnD7slO/RL2feOQv8K320= +github.com/zondax/hid v0.9.2 h1:WCJFnEDMiqGF64nlZz28E9qLVZ0KSJ7xpc5DLEyma2U= +github.com/zondax/hid v0.9.2/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM= +github.com/zondax/ledger-go v0.14.3 h1:wEpJt2CEcBJ428md/5MgSLsXLBos98sBOyxNmCjfUCw= +github.com/zondax/ledger-go v0.14.3/go.mod h1:IKKaoxupuB43g4NxeQmbLXv7T9AlQyie1UpHb342ycI= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ= go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= From 05862f14256330117798d654726dca3d41daa55d Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:14:24 +0100 Subject: [PATCH 03/10] Temporary context on ICA callbacks --- x/millions/keeper/callbacks_bank_send.go | 27 ++++++++--- x/millions/keeper/callbacks_claim.go | 25 ++++++++--- x/millions/keeper/callbacks_delegate.go | 25 ++++++++--- x/millions/keeper/callbacks_redelegate.go | 27 ++++++++--- .../keeper/callbacks_set_withdraw_address.go | 22 ++++++--- .../keeper/callbacks_transfer_from_native.go | 45 ++++++++++++++----- .../keeper/callbacks_transfer_to_native.go | 25 ++++++++--- x/millions/keeper/callbacks_undelegate.go | 40 ++++++++++++----- 8 files changed, 176 insertions(+), 60 deletions(-) diff --git a/x/millions/keeper/callbacks_bank_send.go b/x/millions/keeper/callbacks_bank_send.go index 9a2903e2..ea47a916 100644 --- a/x/millions/keeper/callbacks_bank_send.go +++ b/x/millions/keeper/callbacks_bank_send.go @@ -32,26 +32,39 @@ func (k Keeper) UnmarshalBankSendCallbackArgs(ctx sdk.Context, bankSendCallback } func BankSendCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - bankSendCallback, err := k.UnmarshalBankSendCallbackArgs(ctx, args) + bankSendCallback, err := k.UnmarshalBankSendCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal bank send callback args: %s", err.Error())) } // Acquire the pool instance from the callback - pool, err := k.GetPool(ctx, bankSendCallback.GetPoolId()) + pool, err := k.GetPool(cacheCtx, bankSendCallback.GetPoolId()) if err != nil { return err } if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a bank send to native packet") + k.Logger(cacheCtx).Debug("Received timeout for a bank send to native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a bank send to native packet") - return k.OnTransferWithdrawalToRecipientCompleted(ctx, pool.PoolId, bankSendCallback.GetWithdrawalId(), true) + k.Logger(cacheCtx).Debug("Received failure for a bank send to native packet") + if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, pool.PoolId, bankSendCallback.GetWithdrawalId(), true); err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a bank send to native packet") - return k.OnTransferWithdrawalToRecipientCompleted(ctx, pool.PoolId, bankSendCallback.GetWithdrawalId(), false) + k.Logger(cacheCtx).Debug("Received success for a bank send to native packet") + if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, pool.PoolId, bankSendCallback.GetWithdrawalId(), false); err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_claim.go b/x/millions/keeper/callbacks_claim.go index 8b47dd0a..18d8f7bb 100644 --- a/x/millions/keeper/callbacks_claim.go +++ b/x/millions/keeper/callbacks_claim.go @@ -33,8 +33,11 @@ func (k Keeper) UnmarshalClaimCallbackArgs(ctx sdk.Context, claimCallback []byte } func ClaimCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - claimCallback, err := k.UnmarshalClaimCallbackArgs(ctx, args) + claimCallback, err := k.UnmarshalClaimCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal claim callback args: %s", err.Error())) } @@ -42,16 +45,26 @@ func ClaimCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackRes // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a claim packet") + k.Logger(cacheCtx).Debug("Received timeout for a claim packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a claim packet") - _, err = k.OnClaimYieldOnRemoteZoneCompleted(ctx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), true) - return err + k.Logger(cacheCtx).Debug("Received failure for a claim packet") + _, err = k.OnClaimYieldOnRemoteZoneCompleted(cacheCtx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), true) + if err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { k.Logger(ctx).Debug("Received success for a claim packet") _, err = k.OnClaimYieldOnRemoteZoneCompleted(ctx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), false) - return err + if err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_delegate.go b/x/millions/keeper/callbacks_delegate.go index 3841c21b..a60faeeb 100644 --- a/x/millions/keeper/callbacks_delegate.go +++ b/x/millions/keeper/callbacks_delegate.go @@ -33,8 +33,11 @@ func (k Keeper) UnmarshalDelegateCallbackArgs(ctx sdk.Context, delegateCallback } func DelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - delegateCallback, err := k.UnmarshalDelegateCallbackArgs(ctx, args) + delegateCallback, err := k.UnmarshalDelegateCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal delegate callback args: %s", err.Error())) } @@ -42,13 +45,23 @@ func DelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ack // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a delegate packet") + k.Logger(cacheCtx).Debug("Received timeout for a delegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a delegate packet") - return k.OnDelegateDepositOnRemoteZoneCompleted(ctx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), true) + k.Logger(cacheCtx).Debug("Received failure for a delegate packet") + if err := k.OnDelegateDepositOnRemoteZoneCompleted(cacheCtx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), true); err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a delegate packet") - return k.OnDelegateDepositOnRemoteZoneCompleted(ctx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), false) + k.Logger(cacheCtx).Debug("Received success for a delegate packet") + if err := k.OnDelegateDepositOnRemoteZoneCompleted(cacheCtx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), false); err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_redelegate.go b/x/millions/keeper/callbacks_redelegate.go index e17f1142..954be690 100644 --- a/x/millions/keeper/callbacks_redelegate.go +++ b/x/millions/keeper/callbacks_redelegate.go @@ -33,14 +33,17 @@ func (k Keeper) UnmarshalRedelegateCallbackArgs(ctx sdk.Context, redelegateCallb } func RedelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - redelegateCallback, err := k.UnmarshalRedelegateCallbackArgs(ctx, args) + redelegateCallback, err := k.UnmarshalRedelegateCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal redelegate callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(ctx, redelegateCallback.GetPoolId()) + _, err = k.GetPool(cacheCtx, redelegateCallback.GetPoolId()) if err != nil { return err } @@ -48,13 +51,23 @@ func RedelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, a // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a redelegate packet") + k.Logger(cacheCtx).Debug("Received timeout for a redelegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a redelegate packet") - return k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(ctx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), true) + k.Logger(cacheCtx).Debug("Received failure for a redelegate packet") + if err := k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(cacheCtx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), true); err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a redelegate packet") - return k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(ctx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), false) + k.Logger(cacheCtx).Debug("Received success for a redelegate packet") + if err := k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(cacheCtx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), false); err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_set_withdraw_address.go b/x/millions/keeper/callbacks_set_withdraw_address.go index ab319718..81d0c551 100644 --- a/x/millions/keeper/callbacks_set_withdraw_address.go +++ b/x/millions/keeper/callbacks_set_withdraw_address.go @@ -33,14 +33,17 @@ func (k Keeper) UnmarshalSetWithdrawAddressCallbackArgs(ctx sdk.Context, setWith } func SetWithdrawAddressCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - setWithdrawAddressCallback, err := k.UnmarshalSetWithdrawAddressCallbackArgs(ctx, args) + setWithdrawAddressCallback, err := k.UnmarshalSetWithdrawAddressCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal set withdraw address callback args: %s", err.Error())) } // Acquire the pool instance from the callback - pool, err := k.GetPool(ctx, setWithdrawAddressCallback.GetPoolId()) + pool, err := k.GetPool(cacheCtx, setWithdrawAddressCallback.GetPoolId()) if err != nil { return err } @@ -48,13 +51,18 @@ func SetWithdrawAddressCallback(k Keeper, ctx sdk.Context, packet channeltypes.P // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a set withdraw address packet") + k.Logger(cacheCtx).Debug("Received timeout for a set withdraw address packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a set withdraw address packet") + k.Logger(cacheCtx).Debug("Received failure for a set withdraw address packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a set withdraw address packet") - _, err := k.OnSetupPoolWithdrawalAddressCompleted(ctx, pool.PoolId) - return err + k.Logger(cacheCtx).Debug("Received success for a set withdraw address packet") + _, err := k.OnSetupPoolWithdrawalAddressCompleted(cacheCtx, pool.PoolId) + if err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_transfer_from_native.go b/x/millions/keeper/callbacks_transfer_from_native.go index 928747ca..f17d679a 100644 --- a/x/millions/keeper/callbacks_transfer_from_native.go +++ b/x/millions/keeper/callbacks_transfer_from_native.go @@ -33,14 +33,17 @@ func (k Keeper) UnmarshalTransferFromNativeCallbackArgs(ctx sdk.Context, transfe } func TransferFromNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - transferCallback, err := k.UnmarshalTransferFromNativeCallbackArgs(ctx, args) + transferCallback, err := k.UnmarshalTransferFromNativeCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal transfer from native callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(ctx, transferCallback.GetPoolId()) + _, err = k.GetPool(cacheCtx, transferCallback.GetPoolId()) if err != nil { return err } @@ -48,22 +51,42 @@ func TransferFromNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.P // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a transfer from native packet") + k.Logger(cacheCtx).Debug("Received timeout for a transfer from native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a transfer from native packet") + k.Logger(cacheCtx).Debug("Received failure for a transfer from native packet") if transferCallback.Type == types.TransferType_Claim { - _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), true) - return err + _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), true) + if err != nil { + return err + } + + // Commit the cache + writeCache() } else if transferCallback.Type == types.TransferType_Withdraw { - return k.OnTransferWithdrawalToRecipientCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), true) + if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), true); err != nil { + return err + } + + // Commit the cache + writeCache() } } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a transfer from native packet") + k.Logger(cacheCtx).Debug("Received success for a transfer from native packet") if transferCallback.Type == types.TransferType_Claim { - _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), false) - return err + _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), false) + if err != nil { + return err + } + + // Commit the cache + writeCache() } else if transferCallback.Type == types.TransferType_Withdraw { - return k.OnTransferWithdrawalToRecipientCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), false) + if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), false); err != nil { + return err + } + + // Commit the cache + writeCache() } } return nil diff --git a/x/millions/keeper/callbacks_transfer_to_native.go b/x/millions/keeper/callbacks_transfer_to_native.go index 2e4421c2..d9da6a12 100644 --- a/x/millions/keeper/callbacks_transfer_to_native.go +++ b/x/millions/keeper/callbacks_transfer_to_native.go @@ -33,8 +33,11 @@ func (k Keeper) UnmarshalTransferToNativeCallbackArgs(ctx sdk.Context, transferC } func TransferToNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - transferCallback, err := k.UnmarshalTransferToNativeCallbackArgs(ctx, args) + transferCallback, err := k.UnmarshalTransferToNativeCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal transfer to native callback args: %s", err.Error())) } @@ -42,13 +45,23 @@ func TransferToNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Pac // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for a transfer to native packet") + k.Logger(cacheCtx).Debug("Received timeout for a transfer to native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for a transfer to native packet") - return k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), true) + k.Logger(cacheCtx).Debug("Received failure for a transfer to native packet") + if err := k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), true); err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for a transfer to native packet.") - return k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), false) + k.Logger(cacheCtx).Debug("Received success for a transfer to native packet.") + if err := k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), false); err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } diff --git a/x/millions/keeper/callbacks_undelegate.go b/x/millions/keeper/callbacks_undelegate.go index fe1d4141..1ca38b48 100644 --- a/x/millions/keeper/callbacks_undelegate.go +++ b/x/millions/keeper/callbacks_undelegate.go @@ -57,14 +57,17 @@ func (k Keeper) GetUnbondingCompletionTime(ctx sdk.Context, msgResponses [][]byt } func UndelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { + // Create a custom temporary cache + cacheCtx, writeCache := ctx.CacheContext() + // Deserialize the callback args - undelegateCallback, err := k.UnmarshalUndelegateCallbackArgs(ctx, args) + undelegateCallback, err := k.UnmarshalUndelegateCallbackArgs(cacheCtx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal undelegate callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(ctx, undelegateCallback.GetPoolId()) + _, err = k.GetPool(cacheCtx, undelegateCallback.GetPoolId()) if err != nil { return err } @@ -72,30 +75,47 @@ func UndelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, a // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(ctx).Debug("Received timeout for an undelegate packet") + k.Logger(cacheCtx).Debug("Received timeout for an undelegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(ctx).Debug("Received failure for an undelegate packet") + k.Logger(cacheCtx).Debug("Received failure for an undelegate packet") // Failed OnUndelegateEpochUnbondingOnRemoteZoneCompleted - return k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( - ctx, + err := k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( + cacheCtx, undelegateCallback.PoolId, undelegateCallback.WithdrawalIds, nil, true, ) + if err != nil { + return err + } + + // Commit the cache + writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(ctx).Debug("Received success for an undelegate packet") - unbondingEndsAt, err := k.GetUnbondingCompletionTime(ctx, ackResponse.MsgResponses) + k.Logger(cacheCtx).Debug("Received success for an undelegate packet") + + // Update the completion time using the latest completion time across each message within the transaction + unbondingEndsAt, err := k.GetUnbondingCompletionTime(cacheCtx, ackResponse.MsgResponses) if err != nil { return err } - return k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( - ctx, + + // Call the callback handler + err = k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( + cacheCtx, undelegateCallback.PoolId, undelegateCallback.WithdrawalIds, unbondingEndsAt, false, ) + + if err != nil { + return err + } + + // Commit the cache + writeCache() } return nil } From 269bbf589baa28cca6640576c4a2771f550ed2e9 Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue <3663159+lebascou@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:55:32 +0300 Subject: [PATCH 04/10] [LUM-856] Restoring ICA should restore blocked entities (#62) * Fix epoch unittests * Add new entity restoration procedure in case of channel restore --------- Co-authored-by: Fabrice Bascoulergue --- x/millions/keeper/keeper_epoch_test.go | 7 +++- x/millions/keeper/keeper_withdrawal.go | 11 +++-- x/millions/keeper/msg_server_pool.go | 48 ++++++++++++++++++++++ x/millions/keeper/msg_server_withdrawal.go | 7 ++++ 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/x/millions/keeper/keeper_epoch_test.go b/x/millions/keeper/keeper_epoch_test.go index f184b26d..fc13d3c1 100644 --- a/x/millions/keeper/keeper_epoch_test.go +++ b/x/millions/keeper/keeper_epoch_test.go @@ -255,9 +255,12 @@ func (suite *KeeperTestSuite) TestEpoch_AddEpochUnbonding() { suite.Require().Equal([]uint64{1, 2}, epochUnbondingPool.WithdrawalIds) suite.Require().Equal(uint64(2), epochUnbondingPool.WithdrawalIdsCount) - // Same withdrawalID to the epoch unbonding should faild + // Same withdrawalID to the epoch unbonding should do nothing err = app.MillionsKeeper.AddEpochUnbonding(ctx, withdrawals[0], false) - suite.Require().ErrorIs(millionstypes.ErrEntityOverride, err) + suite.Require().NoError(err) + epochUnbondingPoolAfter, err := app.MillionsKeeper.GetEpochPoolUnbonding(ctx, epochTracker.EpochNumber+frequency, 1) + suite.Require().NoError(err) + suite.Require().Equal(epochUnbondingPool.WithdrawalIds, epochUnbondingPoolAfter.WithdrawalIds) // Trigger new deposits for new pool for i := 0; i < 3; i++ { diff --git a/x/millions/keeper/keeper_withdrawal.go b/x/millions/keeper/keeper_withdrawal.go index f5ebade5..22c64a24 100644 --- a/x/millions/keeper/keeper_withdrawal.go +++ b/x/millions/keeper/keeper_withdrawal.go @@ -82,13 +82,12 @@ func (k Keeper) OnUndelegateWithdrawalsOnRemoteZoneCompleted(ctx sdk.Context, po if err := k.AddEpochUnbonding(ctx, withdrawal, true); err != nil { return err } - continue + } else { + // Set the unbondingEndsAt and add withdrawal to matured queue + withdrawal.UnbondingEndsAt = unbondingEndsAt + k.UpdateWithdrawalStatus(ctx, withdrawal.PoolId, withdrawal.WithdrawalId, types.WithdrawalState_IcaUnbonding, unbondingEndsAt, false) + k.addWithdrawalToMaturedQueue(ctx, withdrawal) } - - // Set the unbondingEndsAt and add withdrawal to matured queue - withdrawal.UnbondingEndsAt = unbondingEndsAt - k.UpdateWithdrawalStatus(ctx, withdrawal.PoolId, withdrawal.WithdrawalId, types.WithdrawalState_IcaUnbonding, unbondingEndsAt, false) - k.addWithdrawalToMaturedQueue(ctx, withdrawal) } if isError { diff --git a/x/millions/keeper/msg_server_pool.go b/x/millions/keeper/msg_server_pool.go index 13113a71..58144c44 100644 --- a/x/millions/keeper/msg_server_pool.go +++ b/x/millions/keeper/msg_server_pool.go @@ -47,6 +47,7 @@ func (k msgServer) RestoreInterchainAccounts(goCtx context.Context, msg *types.M if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, pool.GetConnectionId(), icaDepositPortName, appVersion); err != nil { return nil, errorsmod.Wrapf(types.ErrFailedToRestorePool, fmt.Sprintf("Unable to trigger deposit account registration, err: %s", err.Error())) } + k.restoreICADepositEntities(ctx, pool.GetPoolId()) // Exit to prevent a double channel registration which might create unpredictable behaviours // The registration of the ICA PrizePool will either be automatically triggered once the ICA Deposit gets ACK (see keeper.OnSetupPoolICACompleted) // or can be restored by calling this method again @@ -65,7 +66,54 @@ func (k msgServer) RestoreInterchainAccounts(goCtx context.Context, msg *types.M if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, pool.GetConnectionId(), icaPrizePoolPortName, appVersion); err != nil { return nil, errorsmod.Wrapf(types.ErrFailedToRestorePool, fmt.Sprintf("Unable to trigger prizepool account registration, err: %s", err.Error())) } + k.restoreICAPrizePoolEntities(ctx, pool.GetPoolId()) } return &types.MsgRestoreInterchainAccountsResponse{}, nil } + +// restoreICADepositEntities restores all blocked entities by putting them in error state instead of locked state +// thus leaving users the opportunity to retry the operation +func (k msgServer) restoreICADepositEntities(ctx sdk.Context, poolID uint64) { + // Restore deposits ICA locked operations on ICADeposit account + deposits := k.Keeper.ListPoolDeposits(ctx, poolID) + for _, d := range deposits { + if d.State == types.DepositState_IcaDelegate { + d.ErrorState = d.State + d.State = types.DepositState_Failure + k.Keeper.setPoolDeposit(ctx, &d) + } + } + // Restore withdrawals ICA locked operations on ICADeposit account + withdrawals := k.Keeper.ListPoolWithdrawals(ctx, poolID) + for _, w := range withdrawals { + if w.State == types.WithdrawalState_IcaUndelegate { + w.ErrorState = w.State + w.State = types.WithdrawalState_Failure + k.Keeper.setPoolWithdrawal(ctx, w) + } + } + // Restore draws ICA locked operations on ICADeposit account + draws := k.Keeper.ListPoolDraws(ctx, poolID) + for _, d := range draws { + if d.State == types.DrawState_IcaWithdrawRewards { + d.ErrorState = d.State + d.State = types.DrawState_Failure + k.Keeper.SetPoolDraw(ctx, d) + } + } +} + +// restoreICAPrizePoolEntities restores all blocked entities by putting them in error state instead of locked state +// thus leaving users the opportunity to retry the operation +func (k msgServer) restoreICAPrizePoolEntities(ctx sdk.Context, poolID uint64) { + // Restore draws ICQ locked operations on ICAPrizePool account + draws := k.Keeper.ListPoolDraws(ctx, poolID) + for _, d := range draws { + if d.State == types.DrawState_IcqBalance { + d.ErrorState = d.State + d.State = types.DrawState_Failure + k.Keeper.SetPoolDraw(ctx, d) + } + } +} diff --git a/x/millions/keeper/msg_server_withdrawal.go b/x/millions/keeper/msg_server_withdrawal.go index ae00912c..ef86e744 100644 --- a/x/millions/keeper/msg_server_withdrawal.go +++ b/x/millions/keeper/msg_server_withdrawal.go @@ -130,6 +130,13 @@ func (k msgServer) WithdrawDepositRetry(goCtx context.Context, msg *types.MsgWit if err := k.TransferWithdrawalToRecipient(ctx, withdrawal.PoolId, withdrawal.WithdrawalId); err != nil { return nil, err } + } else if withdrawal.ErrorState == types.WithdrawalState_IcaUndelegate { + // Only possible following a restore account which put entities in this error state + // otherwise the retry is automatically triggered by the ICA callback + err := k.AddEpochUnbonding(ctx, withdrawal, true) + if err != nil { + return nil, err + } } else { return nil, errorsmod.Wrapf( types.ErrInvalidWithdrawalState, From 2aa0b2927f8f3c4c97769f661ef44dc34af6d64e Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:27:53 +0100 Subject: [PATCH 05/10] Revert "Temporary context on ICA callbacks" This reverts commit 05862f14256330117798d654726dca3d41daa55d. --- x/millions/keeper/callbacks_bank_send.go | 27 +++-------- x/millions/keeper/callbacks_claim.go | 25 +++-------- x/millions/keeper/callbacks_delegate.go | 25 +++-------- x/millions/keeper/callbacks_redelegate.go | 27 +++-------- .../keeper/callbacks_set_withdraw_address.go | 22 +++------ .../keeper/callbacks_transfer_from_native.go | 45 +++++-------------- .../keeper/callbacks_transfer_to_native.go | 25 +++-------- x/millions/keeper/callbacks_undelegate.go | 40 +++++------------ 8 files changed, 60 insertions(+), 176 deletions(-) diff --git a/x/millions/keeper/callbacks_bank_send.go b/x/millions/keeper/callbacks_bank_send.go index ea47a916..9a2903e2 100644 --- a/x/millions/keeper/callbacks_bank_send.go +++ b/x/millions/keeper/callbacks_bank_send.go @@ -32,39 +32,26 @@ func (k Keeper) UnmarshalBankSendCallbackArgs(ctx sdk.Context, bankSendCallback } func BankSendCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - bankSendCallback, err := k.UnmarshalBankSendCallbackArgs(cacheCtx, args) + bankSendCallback, err := k.UnmarshalBankSendCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal bank send callback args: %s", err.Error())) } // Acquire the pool instance from the callback - pool, err := k.GetPool(cacheCtx, bankSendCallback.GetPoolId()) + pool, err := k.GetPool(ctx, bankSendCallback.GetPoolId()) if err != nil { return err } if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a bank send to native packet") + k.Logger(ctx).Debug("Received timeout for a bank send to native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a bank send to native packet") - if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, pool.PoolId, bankSendCallback.GetWithdrawalId(), true); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received failure for a bank send to native packet") + return k.OnTransferWithdrawalToRecipientCompleted(ctx, pool.PoolId, bankSendCallback.GetWithdrawalId(), true) } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a bank send to native packet") - if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, pool.PoolId, bankSendCallback.GetWithdrawalId(), false); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received success for a bank send to native packet") + return k.OnTransferWithdrawalToRecipientCompleted(ctx, pool.PoolId, bankSendCallback.GetWithdrawalId(), false) } return nil } diff --git a/x/millions/keeper/callbacks_claim.go b/x/millions/keeper/callbacks_claim.go index 18d8f7bb..8b47dd0a 100644 --- a/x/millions/keeper/callbacks_claim.go +++ b/x/millions/keeper/callbacks_claim.go @@ -33,11 +33,8 @@ func (k Keeper) UnmarshalClaimCallbackArgs(ctx sdk.Context, claimCallback []byte } func ClaimCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - claimCallback, err := k.UnmarshalClaimCallbackArgs(cacheCtx, args) + claimCallback, err := k.UnmarshalClaimCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal claim callback args: %s", err.Error())) } @@ -45,26 +42,16 @@ func ClaimCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackRes // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a claim packet") + k.Logger(ctx).Debug("Received timeout for a claim packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a claim packet") - _, err = k.OnClaimYieldOnRemoteZoneCompleted(cacheCtx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), true) - if err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received failure for a claim packet") + _, err = k.OnClaimYieldOnRemoteZoneCompleted(ctx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), true) + return err } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { k.Logger(ctx).Debug("Received success for a claim packet") _, err = k.OnClaimYieldOnRemoteZoneCompleted(ctx, claimCallback.GetPoolId(), claimCallback.GetDrawId(), false) - if err != nil { - return err - } - - // Commit the cache - writeCache() + return err } return nil } diff --git a/x/millions/keeper/callbacks_delegate.go b/x/millions/keeper/callbacks_delegate.go index a60faeeb..3841c21b 100644 --- a/x/millions/keeper/callbacks_delegate.go +++ b/x/millions/keeper/callbacks_delegate.go @@ -33,11 +33,8 @@ func (k Keeper) UnmarshalDelegateCallbackArgs(ctx sdk.Context, delegateCallback } func DelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - delegateCallback, err := k.UnmarshalDelegateCallbackArgs(cacheCtx, args) + delegateCallback, err := k.UnmarshalDelegateCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal delegate callback args: %s", err.Error())) } @@ -45,23 +42,13 @@ func DelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ack // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a delegate packet") + k.Logger(ctx).Debug("Received timeout for a delegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a delegate packet") - if err := k.OnDelegateDepositOnRemoteZoneCompleted(cacheCtx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), true); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received failure for a delegate packet") + return k.OnDelegateDepositOnRemoteZoneCompleted(ctx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), true) } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a delegate packet") - if err := k.OnDelegateDepositOnRemoteZoneCompleted(cacheCtx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), false); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received success for a delegate packet") + return k.OnDelegateDepositOnRemoteZoneCompleted(ctx, delegateCallback.GetPoolId(), delegateCallback.GetDepositId(), delegateCallback.GetSplitDelegations(), false) } return nil } diff --git a/x/millions/keeper/callbacks_redelegate.go b/x/millions/keeper/callbacks_redelegate.go index 954be690..e17f1142 100644 --- a/x/millions/keeper/callbacks_redelegate.go +++ b/x/millions/keeper/callbacks_redelegate.go @@ -33,17 +33,14 @@ func (k Keeper) UnmarshalRedelegateCallbackArgs(ctx sdk.Context, redelegateCallb } func RedelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - redelegateCallback, err := k.UnmarshalRedelegateCallbackArgs(cacheCtx, args) + redelegateCallback, err := k.UnmarshalRedelegateCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal redelegate callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(cacheCtx, redelegateCallback.GetPoolId()) + _, err = k.GetPool(ctx, redelegateCallback.GetPoolId()) if err != nil { return err } @@ -51,23 +48,13 @@ func RedelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, a // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a redelegate packet") + k.Logger(ctx).Debug("Received timeout for a redelegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a redelegate packet") - if err := k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(cacheCtx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), true); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received failure for a redelegate packet") + return k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(ctx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), true) } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a redelegate packet") - if err := k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(cacheCtx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), false); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received success for a redelegate packet") + return k.OnRedelegateToActiveValidatorsOnRemoteZoneCompleted(ctx, redelegateCallback.GetPoolId(), redelegateCallback.GetOperatorAddress(), redelegateCallback.GetSplitDelegations(), false) } return nil } diff --git a/x/millions/keeper/callbacks_set_withdraw_address.go b/x/millions/keeper/callbacks_set_withdraw_address.go index 81d0c551..ab319718 100644 --- a/x/millions/keeper/callbacks_set_withdraw_address.go +++ b/x/millions/keeper/callbacks_set_withdraw_address.go @@ -33,17 +33,14 @@ func (k Keeper) UnmarshalSetWithdrawAddressCallbackArgs(ctx sdk.Context, setWith } func SetWithdrawAddressCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - setWithdrawAddressCallback, err := k.UnmarshalSetWithdrawAddressCallbackArgs(cacheCtx, args) + setWithdrawAddressCallback, err := k.UnmarshalSetWithdrawAddressCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal set withdraw address callback args: %s", err.Error())) } // Acquire the pool instance from the callback - pool, err := k.GetPool(cacheCtx, setWithdrawAddressCallback.GetPoolId()) + pool, err := k.GetPool(ctx, setWithdrawAddressCallback.GetPoolId()) if err != nil { return err } @@ -51,18 +48,13 @@ func SetWithdrawAddressCallback(k Keeper, ctx sdk.Context, packet channeltypes.P // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a set withdraw address packet") + k.Logger(ctx).Debug("Received timeout for a set withdraw address packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a set withdraw address packet") + k.Logger(ctx).Debug("Received failure for a set withdraw address packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a set withdraw address packet") - _, err := k.OnSetupPoolWithdrawalAddressCompleted(cacheCtx, pool.PoolId) - if err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received success for a set withdraw address packet") + _, err := k.OnSetupPoolWithdrawalAddressCompleted(ctx, pool.PoolId) + return err } return nil } diff --git a/x/millions/keeper/callbacks_transfer_from_native.go b/x/millions/keeper/callbacks_transfer_from_native.go index f17d679a..928747ca 100644 --- a/x/millions/keeper/callbacks_transfer_from_native.go +++ b/x/millions/keeper/callbacks_transfer_from_native.go @@ -33,17 +33,14 @@ func (k Keeper) UnmarshalTransferFromNativeCallbackArgs(ctx sdk.Context, transfe } func TransferFromNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - transferCallback, err := k.UnmarshalTransferFromNativeCallbackArgs(cacheCtx, args) + transferCallback, err := k.UnmarshalTransferFromNativeCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal transfer from native callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(cacheCtx, transferCallback.GetPoolId()) + _, err = k.GetPool(ctx, transferCallback.GetPoolId()) if err != nil { return err } @@ -51,42 +48,22 @@ func TransferFromNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.P // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a transfer from native packet") + k.Logger(ctx).Debug("Received timeout for a transfer from native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a transfer from native packet") + k.Logger(ctx).Debug("Received failure for a transfer from native packet") if transferCallback.Type == types.TransferType_Claim { - _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), true) - if err != nil { - return err - } - - // Commit the cache - writeCache() + _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), true) + return err } else if transferCallback.Type == types.TransferType_Withdraw { - if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), true); err != nil { - return err - } - - // Commit the cache - writeCache() + return k.OnTransferWithdrawalToRecipientCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), true) } } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a transfer from native packet") + k.Logger(ctx).Debug("Received success for a transfer from native packet") if transferCallback.Type == types.TransferType_Claim { - _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), false) - if err != nil { - return err - } - - // Commit the cache - writeCache() + _, err := k.OnTransferFreshPrizePoolCoinsToLocalZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDrawId(), false) + return err } else if transferCallback.Type == types.TransferType_Withdraw { - if err := k.OnTransferWithdrawalToRecipientCompleted(cacheCtx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), false); err != nil { - return err - } - - // Commit the cache - writeCache() + return k.OnTransferWithdrawalToRecipientCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetWithdrawalId(), false) } } return nil diff --git a/x/millions/keeper/callbacks_transfer_to_native.go b/x/millions/keeper/callbacks_transfer_to_native.go index d9da6a12..2e4421c2 100644 --- a/x/millions/keeper/callbacks_transfer_to_native.go +++ b/x/millions/keeper/callbacks_transfer_to_native.go @@ -33,11 +33,8 @@ func (k Keeper) UnmarshalTransferToNativeCallbackArgs(ctx sdk.Context, transferC } func TransferToNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - transferCallback, err := k.UnmarshalTransferToNativeCallbackArgs(cacheCtx, args) + transferCallback, err := k.UnmarshalTransferToNativeCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal transfer to native callback args: %s", err.Error())) } @@ -45,23 +42,13 @@ func TransferToNativeCallback(k Keeper, ctx sdk.Context, packet channeltypes.Pac // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for a transfer to native packet") + k.Logger(ctx).Debug("Received timeout for a transfer to native packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for a transfer to native packet") - if err := k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), true); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received failure for a transfer to native packet") + return k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), true) } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for a transfer to native packet.") - if err := k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), false); err != nil { - return err - } - - // Commit the cache - writeCache() + k.Logger(ctx).Debug("Received success for a transfer to native packet.") + return k.OnTransferDepositToRemoteZoneCompleted(ctx, transferCallback.GetPoolId(), transferCallback.GetDepositId(), false) } return nil } diff --git a/x/millions/keeper/callbacks_undelegate.go b/x/millions/keeper/callbacks_undelegate.go index 1ca38b48..fe1d4141 100644 --- a/x/millions/keeper/callbacks_undelegate.go +++ b/x/millions/keeper/callbacks_undelegate.go @@ -57,17 +57,14 @@ func (k Keeper) GetUnbondingCompletionTime(ctx sdk.Context, msgResponses [][]byt } func UndelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error { - // Create a custom temporary cache - cacheCtx, writeCache := ctx.CacheContext() - // Deserialize the callback args - undelegateCallback, err := k.UnmarshalUndelegateCallbackArgs(cacheCtx, args) + undelegateCallback, err := k.UnmarshalUndelegateCallbackArgs(ctx, args) if err != nil { return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal undelegate callback args: %s", err.Error())) } // Acquire the pool instance from the callback - _, err = k.GetPool(cacheCtx, undelegateCallback.GetPoolId()) + _, err = k.GetPool(ctx, undelegateCallback.GetPoolId()) if err != nil { return err } @@ -75,47 +72,30 @@ func UndelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, a // If the response status is a timeout, that's not an "error" since the relayer will retry then fail or succeed. // We just log it out and return no error if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT { - k.Logger(cacheCtx).Debug("Received timeout for an undelegate packet") + k.Logger(ctx).Debug("Received timeout for an undelegate packet") } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE { - k.Logger(cacheCtx).Debug("Received failure for an undelegate packet") + k.Logger(ctx).Debug("Received failure for an undelegate packet") // Failed OnUndelegateEpochUnbondingOnRemoteZoneCompleted - err := k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( - cacheCtx, + return k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( + ctx, undelegateCallback.PoolId, undelegateCallback.WithdrawalIds, nil, true, ) - if err != nil { - return err - } - - // Commit the cache - writeCache() } else if ackResponse.Status == icacallbackstypes.AckResponseStatus_SUCCESS { - k.Logger(cacheCtx).Debug("Received success for an undelegate packet") - - // Update the completion time using the latest completion time across each message within the transaction - unbondingEndsAt, err := k.GetUnbondingCompletionTime(cacheCtx, ackResponse.MsgResponses) + k.Logger(ctx).Debug("Received success for an undelegate packet") + unbondingEndsAt, err := k.GetUnbondingCompletionTime(ctx, ackResponse.MsgResponses) if err != nil { return err } - - // Call the callback handler - err = k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( - cacheCtx, + return k.OnUndelegateWithdrawalsOnRemoteZoneCompleted( + ctx, undelegateCallback.PoolId, undelegateCallback.WithdrawalIds, unbondingEndsAt, false, ) - - if err != nil { - return err - } - - // Commit the cache - writeCache() } return nil } From b1b24d6399ce55f096efd1c83aa13a97a4857742 Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:27:58 +0100 Subject: [PATCH 06/10] Update hooks.go --- x/millions/keeper/hooks.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/x/millions/keeper/hooks.go b/x/millions/keeper/hooks.go index c27fd0e0..46e4f45e 100644 --- a/x/millions/keeper/hooks.go +++ b/x/millions/keeper/hooks.go @@ -22,7 +22,11 @@ func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInf } func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.EpochInfo, logger log.Logger) (successCount, errorCount, skippedCount int) { - epochTracker, err := k.UpdateEpochTracker(ctx, epochInfo, types.WithdrawalTrackerType) + // Create temporary context + cacheCtx, writeCache := ctx.CacheContext() + + // Update the epoch tracker + epochTracker, err := k.UpdateEpochTracker(cacheCtx, epochInfo, types.WithdrawalTrackerType) if err != nil { logger.Error( fmt.Sprintf("Unable to update epoch tracker, err: %v", err), @@ -32,9 +36,9 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep } // Get epoch unbondings - epochUnbondings := k.GetEpochUnbondings(ctx, epochTracker.EpochNumber) + epochUnbondings := k.GetEpochUnbondings(cacheCtx, epochTracker.EpochNumber) for _, epochUnbonding := range epochUnbondings { - success, err := k.processEpochUnbonding(ctx, epochUnbonding, epochTracker, logger) + success, err := k.processEpochUnbonding(cacheCtx, epochUnbonding, epochTracker, logger) if err != nil { logger.Error( fmt.Sprintf("Error processing epoch unbonding: %v", err), @@ -49,15 +53,20 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep } } - if successCount+errorCount > 0 { - logger.Info( - "epoch unbonding undelegate started", - "nbr_success", successCount, - "nbr_error", errorCount, - "nbr_skipped", skippedCount, - ) + // If there was no error, write the cache + if errorCount > 0 { + return successCount, errorCount, skippedCount + } else { + writeCache() } + logger.Info( + "epoch unbonding undelegate processed", + "nbr_success", successCount, + "nbr_error", errorCount, + "nbr_skipped", skippedCount, + ) + return successCount, errorCount, skippedCount } From da1baa17711a23c304f872a7fce03f9b43ce687d Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:37:59 +0100 Subject: [PATCH 07/10] Added missing log --- x/millions/keeper/hooks.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x/millions/keeper/hooks.go b/x/millions/keeper/hooks.go index 46e4f45e..09e2ac28 100644 --- a/x/millions/keeper/hooks.go +++ b/x/millions/keeper/hooks.go @@ -55,6 +55,12 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep // If there was no error, write the cache if errorCount > 0 { + logger.Error( + "epoch unbonding undelegate processed with errors, cache not written", + "nbr_success", successCount, + "nbr_error", errorCount, + "nbr_skipped", skippedCount, + ) return successCount, errorCount, skippedCount } else { writeCache() From f2d50bc0ab7a127e4a87ec3060728024258f9bda Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Fri, 17 Nov 2023 15:35:03 +0100 Subject: [PATCH 08/10] Update hooks to allow for operation rollback --- x/millions/keeper/hooks.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/x/millions/keeper/hooks.go b/x/millions/keeper/hooks.go index 09e2ac28..ecd7cec0 100644 --- a/x/millions/keeper/hooks.go +++ b/x/millions/keeper/hooks.go @@ -37,6 +37,8 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep // Get epoch unbondings epochUnbondings := k.GetEpochUnbondings(cacheCtx, epochTracker.EpochNumber) + + // For each unbonding, try to proceed the undelegation otherwise, rollback the entire operation for _, epochUnbonding := range epochUnbondings { success, err := k.processEpochUnbonding(cacheCtx, epochUnbonding, epochTracker, logger) if err != nil { @@ -46,6 +48,7 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep "epoch_number", epochUnbonding.GetEpochNumber(), ) errorCount++ + break } else if success { successCount++ } else { @@ -53,19 +56,39 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep } } - // If there was no error, write the cache + // If there was an error, we are supposed to cancel the entire operation if errorCount > 0 { + // Log out the critical error logger.Error( "epoch unbonding undelegate processed with errors, cache not written", "nbr_success", successCount, "nbr_error", errorCount, "nbr_skipped", skippedCount, ) + + // Rollback the operations and put everything back up in the next epoch + // We explicitly don't use the cache context here, as we want to proceed + for _, epochUnbonding := range epochUnbondings { + for _, wid := range epochUnbonding.WithdrawalIds { + withdrawal, err := k.GetPoolWithdrawal(ctx, epochUnbonding.PoolId, wid) + if err != nil { + return 0, 1, 0 + } + if err := k.AddEpochUnbonding(ctx, withdrawal, false); err != nil { + return 0, 1, 0 + } + } + + if err := k.RemoveEpochUnbonding(ctx, epochUnbonding); err != nil { + return 0, 1, 0 + } + } + return successCount, errorCount, skippedCount - } else { - writeCache() } + // Otherwise we can just commit the cache, and return + writeCache() logger.Info( "epoch unbonding undelegate processed", "nbr_success", successCount, From 3f298d630e60eba4af4eec1cf060277f7ebf11aa Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Wed, 22 Nov 2023 17:14:12 +0100 Subject: [PATCH 09/10] Introduce temporary cache ctx for rollback operation --- x/millions/keeper/hooks.go | 39 +++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/x/millions/keeper/hooks.go b/x/millions/keeper/hooks.go index ecd7cec0..00524b8d 100644 --- a/x/millions/keeper/hooks.go +++ b/x/millions/keeper/hooks.go @@ -26,7 +26,7 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep cacheCtx, writeCache := ctx.CacheContext() // Update the epoch tracker - epochTracker, err := k.UpdateEpochTracker(cacheCtx, epochInfo, types.WithdrawalTrackerType) + epochTracker, err := k.UpdateEpochTracker(ctx, epochInfo, types.WithdrawalTrackerType) if err != nil { logger.Error( fmt.Sprintf("Unable to update epoch tracker, err: %v", err), @@ -66,36 +66,45 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep "nbr_skipped", skippedCount, ) + // Allocate new cache context + rollbackTmpCacheCtx, writeRollbackCache := ctx.CacheContext() + + // Get epoch unbondings + epochUnbondings = k.GetEpochUnbondings(rollbackTmpCacheCtx, epochTracker.EpochNumber) + // Rollback the operations and put everything back up in the next epoch // We explicitly don't use the cache context here, as we want to proceed for _, epochUnbonding := range epochUnbondings { for _, wid := range epochUnbonding.WithdrawalIds { - withdrawal, err := k.GetPoolWithdrawal(ctx, epochUnbonding.PoolId, wid) + withdrawal, err := k.GetPoolWithdrawal(rollbackTmpCacheCtx, epochUnbonding.PoolId, wid) if err != nil { + logger.Error(fmt.Sprintf("Failure in getting the pool withdrawals for pool %d, err: %v", epochUnbonding.PoolId, err)) return 0, 1, 0 } - if err := k.AddEpochUnbonding(ctx, withdrawal, false); err != nil { + if err := k.AddEpochUnbonding(rollbackTmpCacheCtx, withdrawal, false); err != nil { + logger.Error(fmt.Sprintf("Failure in adding the withdrawal to epoch unbonding for pool %d, err: %v", epochUnbonding.PoolId, err)) return 0, 1, 0 } } - if err := k.RemoveEpochUnbonding(ctx, epochUnbonding); err != nil { + if err := k.RemoveEpochUnbonding(rollbackTmpCacheCtx, epochUnbonding); err != nil { + logger.Error(fmt.Sprintf("Failure in removing the epoch unbonding for pool %d, err: %v", epochUnbonding.PoolId, err)) return 0, 1, 0 } } - return successCount, errorCount, skippedCount + // Write the cache + writeRollbackCache() + } else { + // Otherwise we can just commit the cache, and return + writeCache() + logger.Info( + "epoch unbonding undelegate processed", + "nbr_success", successCount, + "nbr_error", errorCount, + "nbr_skipped", skippedCount, + ) } - - // Otherwise we can just commit the cache, and return - writeCache() - logger.Info( - "epoch unbonding undelegate processed", - "nbr_success", successCount, - "nbr_error", errorCount, - "nbr_skipped", skippedCount, - ) - return successCount, errorCount, skippedCount } From 57c5826cf11d80744d412b6b944fbf7bfdb579b5 Mon Sep 17 00:00:00 2001 From: Segfault <5221072+Segfaultd@users.noreply.github.com> Date: Wed, 22 Nov 2023 17:16:19 +0100 Subject: [PATCH 10/10] Improve comments, add logs --- x/millions/keeper/hooks.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/x/millions/keeper/hooks.go b/x/millions/keeper/hooks.go index 00524b8d..f2416168 100644 --- a/x/millions/keeper/hooks.go +++ b/x/millions/keeper/hooks.go @@ -22,9 +22,6 @@ func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInf } func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.EpochInfo, logger log.Logger) (successCount, errorCount, skippedCount int) { - // Create temporary context - cacheCtx, writeCache := ctx.CacheContext() - // Update the epoch tracker epochTracker, err := k.UpdateEpochTracker(ctx, epochInfo, types.WithdrawalTrackerType) if err != nil { @@ -35,6 +32,9 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep return } + // Create temporary context + cacheCtx, writeCache := ctx.CacheContext() + // Get epoch unbondings epochUnbondings := k.GetEpochUnbondings(cacheCtx, epochTracker.EpochNumber) @@ -73,7 +73,7 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep epochUnbondings = k.GetEpochUnbondings(rollbackTmpCacheCtx, epochTracker.EpochNumber) // Rollback the operations and put everything back up in the next epoch - // We explicitly don't use the cache context here, as we want to proceed + // We explicitly use a custom tailored cache for this operation for _, epochUnbonding := range epochUnbondings { for _, wid := range epochUnbonding.WithdrawalIds { withdrawal, err := k.GetPoolWithdrawal(rollbackTmpCacheCtx, epochUnbonding.PoolId, wid) @@ -93,10 +93,16 @@ func (k Keeper) processEpochUnbondings(ctx sdk.Context, epochInfo epochstypes.Ep } } - // Write the cache + // Write the rollback cache writeRollbackCache() + logger.Info( + "epoch unbonding processed with failure, rollback done", + "nbr_success", successCount, + "nbr_error", errorCount, + "nbr_skipped", skippedCount, + ) } else { - // Otherwise we can just commit the cache, and return + // Write the operation succeed cache writeCache() logger.Info( "epoch unbonding undelegate processed",