github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/executor/backend_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package executor 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 "go.uber.org/mock/gomock" 12 13 "github.com/MetalBlockchain/metalgo/database" 14 "github.com/MetalBlockchain/metalgo/ids" 15 "github.com/MetalBlockchain/metalgo/vms/platformvm/block" 16 "github.com/MetalBlockchain/metalgo/vms/platformvm/state" 17 ) 18 19 func TestGetState(t *testing.T) { 20 require := require.New(t) 21 ctrl := gomock.NewController(t) 22 23 var ( 24 mockState = state.NewMockState(ctrl) 25 onAcceptState = state.NewMockDiff(ctrl) 26 blkID1 = ids.GenerateTestID() 27 blkID2 = ids.GenerateTestID() 28 b = &backend{ 29 state: mockState, 30 blkIDToState: map[ids.ID]*blockState{ 31 blkID1: { 32 onAcceptState: onAcceptState, 33 }, 34 blkID2: {}, 35 }, 36 } 37 ) 38 39 { 40 // Case: block is in the map and onAcceptState isn't nil. 41 gotState, ok := b.GetState(blkID1) 42 require.True(ok) 43 require.Equal(onAcceptState, gotState) 44 } 45 46 { 47 // Case: block is in the map and onAcceptState is nil. 48 _, ok := b.GetState(blkID2) 49 require.False(ok) 50 } 51 52 { 53 // Case: block is not in the map and block isn't last accepted. 54 mockState.EXPECT().GetLastAccepted().Return(ids.GenerateTestID()) 55 _, ok := b.GetState(ids.GenerateTestID()) 56 require.False(ok) 57 } 58 59 { 60 // Case: block is not in the map and block is last accepted. 61 blkID := ids.GenerateTestID() 62 mockState.EXPECT().GetLastAccepted().Return(blkID) 63 gotState, ok := b.GetState(blkID) 64 require.True(ok) 65 require.Equal(mockState, gotState) 66 } 67 } 68 69 func TestBackendGetBlock(t *testing.T) { 70 require := require.New(t) 71 ctrl := gomock.NewController(t) 72 73 var ( 74 blkID1 = ids.GenerateTestID() 75 statelessBlk = block.NewMockBlock(ctrl) 76 state = state.NewMockState(ctrl) 77 b = &backend{ 78 state: state, 79 blkIDToState: map[ids.ID]*blockState{ 80 blkID1: { 81 statelessBlock: statelessBlk, 82 }, 83 }, 84 } 85 ) 86 87 { 88 // Case: block is in the map. 89 gotBlk, err := b.GetBlock(blkID1) 90 require.NoError(err) 91 require.Equal(statelessBlk, gotBlk) 92 } 93 94 { 95 // Case: block isn't in the map or database. 96 blkID := ids.GenerateTestID() 97 state.EXPECT().GetStatelessBlock(blkID).Return(nil, database.ErrNotFound) 98 _, err := b.GetBlock(blkID) 99 require.Equal(database.ErrNotFound, err) 100 } 101 102 { 103 // Case: block isn't in the map and is in database. 104 blkID := ids.GenerateTestID() 105 state.EXPECT().GetStatelessBlock(blkID).Return(statelessBlk, nil) 106 gotBlk, err := b.GetBlock(blkID) 107 require.NoError(err) 108 require.Equal(statelessBlk, gotBlk) 109 } 110 } 111 112 func TestGetTimestamp(t *testing.T) { 113 type test struct { 114 name string 115 backendF func(*gomock.Controller) *backend 116 expectedTimestamp time.Time 117 } 118 119 blkID := ids.GenerateTestID() 120 tests := []test{ 121 { 122 name: "block is in map", 123 backendF: func(*gomock.Controller) *backend { 124 return &backend{ 125 blkIDToState: map[ids.ID]*blockState{ 126 blkID: { 127 timestamp: time.Unix(1337, 0), 128 }, 129 }, 130 } 131 }, 132 expectedTimestamp: time.Unix(1337, 0), 133 }, 134 { 135 name: "block isn't map", 136 backendF: func(ctrl *gomock.Controller) *backend { 137 state := state.NewMockState(ctrl) 138 state.EXPECT().GetTimestamp().Return(time.Unix(1337, 0)) 139 return &backend{ 140 state: state, 141 } 142 }, 143 expectedTimestamp: time.Unix(1337, 0), 144 }, 145 } 146 147 for _, tt := range tests { 148 t.Run(tt.name, func(t *testing.T) { 149 ctrl := gomock.NewController(t) 150 151 backend := tt.backendF(ctrl) 152 gotTimestamp := backend.getTimestamp(blkID) 153 require.Equal(t, tt.expectedTimestamp, gotTimestamp) 154 }) 155 } 156 }