Skip to content

Commit 8921e5a

Browse files
committed
init commit
1 parent 3ca33ec commit 8921e5a

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

x/ccv/provider/keeper/proposal.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ func (k Keeper) BeginBlockInit(ctx sdk.Context) {
374374
propsToExecute := k.GetConsumerAdditionPropsToExecute(ctx)
375375

376376
for _, prop := range propsToExecute {
377+
if prop.Top_N == 0 && len(k.GetAllOptedIn(ctx, prop.ChainId)) == 0 {
378+
// drop the proposal
379+
ctx.Logger().Info("consumer client could not be created because no validator has"+
380+
" opted in the Opt-In chain: %s", prop.ChainId)
381+
continue
382+
}
383+
377384
// create consumer client in a cached context to handle errors
378385
cachedCtx, writeFn, err := k.CreateConsumerClientInCachedCtx(ctx, prop)
379386
if err != nil {
@@ -384,7 +391,7 @@ func (k Keeper) BeginBlockInit(ctx sdk.Context) {
384391

385392
// Only set Top N at the moment a chain starts. If we were to do this earlier (e.g., during the proposal),
386393
// then someone could create a bogus ConsumerAdditionProposal to override the Top N for a specific chain.
387-
k.SetTopN(ctx, prop.ChainId, prop.Top_N)
394+
k.SetTopN(cachedCtx, prop.ChainId, prop.Top_N)
388395

389396
// The cached context is created with a new EventManager so we merge the event
390397
// into the original context

x/ccv/provider/keeper/proposal_test.go

+75-12
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ func TestBeginBlockInit(t *testing.T) {
926926
100000000000,
927927
100000000000,
928928
100000000000,
929-
67,
929+
50,
930930
).(*providertypes.ConsumerAdditionProposal),
931931
providertypes.NewConsumerAdditionProposal(
932932
"title", "spawn time passed", "chain2", clienttypes.NewHeight(3, 4), []byte{}, []byte{},
@@ -938,7 +938,7 @@ func TestBeginBlockInit(t *testing.T) {
938938
100000000000,
939939
100000000000,
940940
100000000000,
941-
0,
941+
50,
942942
).(*providertypes.ConsumerAdditionProposal),
943943
providertypes.NewConsumerAdditionProposal(
944944
"title", "spawn time not passed", "chain3", clienttypes.NewHeight(3, 4), []byte{}, []byte{},
@@ -950,7 +950,7 @@ func TestBeginBlockInit(t *testing.T) {
950950
100000000000,
951951
100000000000,
952952
100000000000,
953-
0,
953+
50,
954954
).(*providertypes.ConsumerAdditionProposal),
955955
providertypes.NewConsumerAdditionProposal(
956956
"title", "invalid proposal: chain id already exists", "chain2", clienttypes.NewHeight(4, 5), []byte{}, []byte{},
@@ -962,46 +962,109 @@ func TestBeginBlockInit(t *testing.T) {
962962
100000000000,
963963
100000000000,
964964
100000000000,
965+
50,
966+
).(*providertypes.ConsumerAdditionProposal),
967+
providertypes.NewConsumerAdditionProposal(
968+
"title", "opt-in chain with no validator opted in", "chain4", clienttypes.NewHeight(3, 4), []byte{}, []byte{},
969+
now.Add(-time.Hour*2).UTC(),
970+
"0.75",
971+
10,
972+
"",
973+
10000,
974+
100000000000,
975+
100000000000,
976+
100000000000,
977+
0,
978+
).(*providertypes.ConsumerAdditionProposal),
979+
providertypes.NewConsumerAdditionProposal(
980+
"title", "opt-in chain with at least one validator opted in", "chain5", clienttypes.NewHeight(3, 4), []byte{}, []byte{},
981+
now.Add(-time.Hour*1).UTC(),
982+
"0.75",
983+
10,
984+
"",
985+
10000,
986+
100000000000,
987+
100000000000,
988+
100000000000,
965989
0,
966990
).(*providertypes.ConsumerAdditionProposal),
967991
}
968992

969-
// Expect client creation for only for the 1st and second proposals (spawn time already passed and valid)
970-
gomock.InOrder(
971-
append(testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain1", clienttypes.NewHeight(3, 4)),
972-
testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain2", clienttypes.NewHeight(3, 4))...)...,
973-
)
993+
// Expect client creation for only the first, second, and sixth proposals (spawn time already passed and valid)
994+
expectedCalls := testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain1", clienttypes.NewHeight(3, 4))
995+
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain2", clienttypes.NewHeight(3, 4))...)
996+
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain5", clienttypes.NewHeight(3, 4))...)
997+
998+
gomock.InOrder(expectedCalls...)
974999

9751000
for _, prop := range pendingProps {
9761001
providerKeeper.SetPendingConsumerAdditionProp(ctx, prop)
9771002
}
9781003

1004+
// opt in a sample validator so the chain's proposal can successfully execute
1005+
providerKeeper.SetOptedIn(ctx, pendingProps[5].ChainId, providertypes.OptedInValidator{
1006+
ProviderAddr: []byte{1},
1007+
BlockHeight: int64(2),
1008+
Power: int64(3),
1009+
PublicKey: []byte{4},
1010+
})
1011+
9791012
providerKeeper.BeginBlockInit(ctx)
9801013

981-
// Only the third proposal is still stored as pending
1014+
// first proposal is not pending anymore because its spawn time already passed and was executed
9821015
_, found := providerKeeper.GetPendingConsumerAdditionProp(
9831016
ctx, pendingProps[0].SpawnTime, pendingProps[0].ChainId)
9841017
require.False(t, found)
1018+
// first proposal was successfully executed and hence consumer genesis was created
1019+
_, found = providerKeeper.GetConsumerGenesis(ctx, pendingProps[0].ChainId)
1020+
require.True(t, found)
9851021

1022+
// second proposal is not pending anymore because its spawn time already passed and was executed
9861023
_, found = providerKeeper.GetPendingConsumerAdditionProp(
9871024
ctx, pendingProps[1].SpawnTime, pendingProps[1].ChainId)
9881025
require.False(t, found)
1026+
// second proposal was successfully executed and hence consumer genesis was created
1027+
_, found = providerKeeper.GetConsumerGenesis(ctx, pendingProps[1].ChainId)
1028+
require.True(t, found)
9891029

1030+
// third proposal is still stored as pending because its spawn time has not passed
9901031
_, found = providerKeeper.GetPendingConsumerAdditionProp(
9911032
ctx, pendingProps[2].SpawnTime, pendingProps[2].ChainId)
9921033
require.True(t, found)
1034+
// because the proposal is still pending, no consumer genesis was created
1035+
_, found = providerKeeper.GetConsumerGenesis(ctx, pendingProps[2].ChainId)
1036+
require.False(t, found)
9931037

994-
// check that the invalid proposal was dropped
1038+
// check that the invalid proposals were dropped
9951039
_, found = providerKeeper.GetPendingConsumerAdditionProp(
9961040
ctx, pendingProps[3].SpawnTime, pendingProps[3].ChainId)
9971041
require.False(t, found)
1042+
// Note that we do not check that `GetConsumerGenesis(ctx, pendingProps[3].ChainId)` returns `false` here because
1043+
//`pendingProps[3]` is an invalid proposal due to the chain id already existing so the consumer genesis also exists
1044+
1045+
// fifth proposal is dropped due to it being an Opt-In chain with no validators opted in
1046+
_, found = providerKeeper.GetPendingConsumerAdditionProp(
1047+
ctx, pendingProps[4].SpawnTime, pendingProps[4].ChainId)
1048+
require.False(t, found)
1049+
// because the proposal is dropped, no consumer genesis was created
1050+
_, found = providerKeeper.GetConsumerGenesis(ctx, pendingProps[4].ChainId)
1051+
require.False(t, found)
1052+
1053+
// sixth proposal corresponds to an Opt-In chain with one opted-in validator and hence the proposal gets
1054+
// successfully executed
1055+
_, found = providerKeeper.GetPendingConsumerAdditionProp(
1056+
ctx, pendingProps[5].SpawnTime, pendingProps[5].ChainId)
1057+
require.False(t, found)
1058+
// sixth proposal was successfully executed and hence consumer genesis was created
1059+
_, found = providerKeeper.GetConsumerGenesis(ctx, pendingProps[5].ChainId)
1060+
require.True(t, found)
9981061

9991062
// test that Top N is set correctly
10001063
require.True(t, providerKeeper.IsTopN(ctx, "chain1"))
10011064
topN, found := providerKeeper.GetTopN(ctx, "chain1")
1002-
require.Equal(t, uint32(67), topN)
1065+
require.Equal(t, uint32(50), topN)
10031066

1004-
require.True(t, providerKeeper.IsOptIn(ctx, "chain2"))
1067+
require.True(t, providerKeeper.IsOptIn(ctx, "chain4"))
10051068
}
10061069

10071070
// TestBeginBlockCCR tests BeginBlockCCR against the spec.

0 commit comments

Comments
 (0)