code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/protocol_upgrade_proposal_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package sqlstore_test 17 18 import ( 19 "context" 20 "testing" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/sqlstore" 24 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func addTestPUP(t *testing.T, 31 ctx context.Context, 32 status entities.ProtocolUpgradeProposalStatus, 33 height uint64, 34 tag string, 35 approvers []string, 36 store *sqlstore.ProtocolUpgradeProposals, 37 block entities.Block, 38 ) entities.ProtocolUpgradeProposal { 39 t.Helper() 40 pup := entities.ProtocolUpgradeProposal{ 41 UpgradeBlockHeight: height, 42 VegaReleaseTag: tag, 43 Approvers: approvers, 44 Status: status, 45 VegaTime: block.VegaTime, 46 TxHash: generateTxHash(), 47 } 48 err := store.Add(ctx, pup) 49 require.NoError(t, err) 50 if pup.Approvers == nil { 51 pup.Approvers = []string{} 52 } 53 return pup 54 } 55 56 func TestProtocolUpgradeProposal(t *testing.T) { 57 ctx := tempTransaction(t) 58 59 pupPending := entities.ProtocolUpgradeProposalStatus(eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_PENDING) 60 pupApproved := entities.ProtocolUpgradeProposalStatus(eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_APPROVED) 61 pupRejected := entities.ProtocolUpgradeProposalStatus(eventspb.ProtocolUpgradeProposalStatus_PROTOCOL_UPGRADE_PROPOSAL_STATUS_REJECTED) 62 63 blockStore := sqlstore.NewBlocks(connectionSource) 64 block1 := addTestBlock(t, ctx, blockStore) 65 block2 := addTestBlock(t, ctx, blockStore) 66 block3 := addTestBlock(t, ctx, blockStore) 67 store := sqlstore.NewProtocolUpgradeProposals(connectionSource) 68 69 var pup1a, pup1b, pup2a, pup2b, pup3, pup4 entities.ProtocolUpgradeProposal 70 71 t.Run("adding", func(t *testing.T) { 72 pup1a = addTestPUP(t, ctx, pupPending, 1, "1.1", []string{"phil"}, store, block1) 73 pup1b = addTestPUP(t, ctx, pupApproved, 1, "1.1", []string{"phil", "dave"}, store, block1) // Updated in same block 74 pup2a = addTestPUP(t, ctx, pupPending, 2, "2.2", []string{"dave", "jim"}, store, block1) 75 pup2b = addTestPUP(t, ctx, pupPending, 2, "2.2", []string{"jim"}, store, block2) // Updated in next block 76 pup3 = addTestPUP(t, ctx, pupApproved, 3, "3.3", []string{"roger", "fred"}, store, block2) // Updated in next block 77 pup4 = addTestPUP(t, ctx, pupRejected, 4, "3.4", nil, store, block3) // Updated in next block 78 }) 79 80 t.Run("list all", func(t *testing.T) { 81 fetched, _, err := store.List(ctx, nil, nil, entities.CursorPagination{}) 82 require.NoError(t, err) 83 84 expected := []entities.ProtocolUpgradeProposal{pup1b, pup2b, pup3, pup4} 85 assert.Equal(t, expected, fetched) 86 }) 87 88 t.Run("GetByTxHash", func(t *testing.T) { 89 fetched, err := store.GetByTxHash(ctx, pup1b.TxHash) 90 require.NoError(t, err) 91 expected := []entities.ProtocolUpgradeProposal{pup1b} 92 assert.Equal(t, expected, fetched) 93 94 fetched, err = store.GetByTxHash(ctx, pup2b.TxHash) 95 require.NoError(t, err) 96 expected = []entities.ProtocolUpgradeProposal{pup2b} 97 assert.Equal(t, expected, fetched) 98 }) 99 100 t.Run("list all paged", func(t *testing.T) { 101 cursor := pup1b.Cursor().Encode() 102 var one int32 = 1 103 p, err := entities.NewCursorPagination(&one, &cursor, nil, nil, false) 104 require.NoError(t, err) 105 106 fetched, pageInfo, err := store.List(ctx, nil, nil, p) 107 require.NoError(t, err) 108 109 expected := []entities.ProtocolUpgradeProposal{pup2b} 110 assert.Equal(t, expected, fetched) 111 assert.True(t, pageInfo.ToProto().HasNextPage) 112 }) 113 114 t.Run("list approved", func(t *testing.T) { 115 fetched, _, err := store.List(ctx, &pupApproved, nil, entities.CursorPagination{}) 116 require.NoError(t, err) 117 118 expected := []entities.ProtocolUpgradeProposal{pup1b, pup3} 119 assert.Equal(t, expected, fetched) 120 }) 121 122 t.Run("list approved by", func(t *testing.T) { 123 dave := "dave" 124 fetched, _, err := store.List(ctx, nil, &dave, entities.CursorPagination{}) 125 require.NoError(t, err) 126 127 expected := []entities.ProtocolUpgradeProposal{pup1b} 128 assert.Equal(t, expected, fetched) 129 }) 130 131 _, _ = pup1a, pup2a 132 } 133 134 func TestProtocolUpdateProposalStatusEnum(t *testing.T) { 135 var protocolUpgradeProposalStatus eventspb.ProtocolUpgradeProposalStatus 136 states := getEnums(t, protocolUpgradeProposalStatus) 137 assert.Len(t, states, 4) 138 for s, state := range states { 139 t.Run(state, func(t *testing.T) { 140 ctx := tempTransaction(t) 141 height := uint64(1) 142 tag := "1.1" 143 approvers := []string{"phil"} 144 blockStore := sqlstore.NewBlocks(connectionSource) 145 block := addTestBlock(t, ctx, blockStore) 146 store := sqlstore.NewProtocolUpgradeProposals(connectionSource) 147 148 pup := entities.ProtocolUpgradeProposal{ 149 UpgradeBlockHeight: height, 150 VegaReleaseTag: tag, 151 Approvers: approvers, 152 Status: entities.ProtocolUpgradeProposalStatus(s), 153 VegaTime: block.VegaTime, 154 TxHash: generateTxHash(), 155 } 156 require.NoError(t, store.Add(ctx, pup)) 157 got, err := store.GetByTxHash(ctx, pup.TxHash) 158 require.NoError(t, err) 159 assert.Len(t, got, 1) 160 assert.Equal(t, pup.Status, got[0].Status) 161 }) 162 } 163 }