|
7 | 7 | "math/big" |
8 | 8 | "os" |
9 | 9 | "testing" |
| 10 | + "time" |
10 | 11 |
|
11 | 12 | "github.com/stretchr/testify/assert" |
12 | 13 | "github.com/stretchr/testify/require" |
@@ -1124,3 +1125,86 @@ func TestStateProto(t *testing.T) { |
1124 | 1125 | } |
1125 | 1126 | } |
1126 | 1127 | } |
| 1128 | + |
| 1129 | +func TestMedianTime(t *testing.T) { |
| 1130 | + val1 := types.NewValidator(ed25519.GenPrivKey().PubKey(), 30) |
| 1131 | + val2 := types.NewValidator(ed25519.GenPrivKey().PubKey(), 30) |
| 1132 | + val3 := types.NewValidator(ed25519.GenPrivKey().PubKey(), 30) |
| 1133 | + |
| 1134 | + vals := types.NewValidatorSet([]*types.Validator{val1, val2, val3}) |
| 1135 | + |
| 1136 | + t.Run("all validators present", func(t *testing.T) { |
| 1137 | + now := time.Now() |
| 1138 | + commit := &types.Commit{ |
| 1139 | + Height: 1, |
| 1140 | + Signatures: []types.CommitSig{ |
| 1141 | + { |
| 1142 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1143 | + ValidatorAddress: val1.Address, |
| 1144 | + Timestamp: now, |
| 1145 | + }, |
| 1146 | + { |
| 1147 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1148 | + ValidatorAddress: val2.Address, |
| 1149 | + Timestamp: now.Add(1 * time.Minute), |
| 1150 | + }, |
| 1151 | + { |
| 1152 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1153 | + ValidatorAddress: val3.Address, |
| 1154 | + Timestamp: now.Add(2 * time.Minute), |
| 1155 | + }, |
| 1156 | + }, |
| 1157 | + } |
| 1158 | + |
| 1159 | + medianTime, err := sm.MedianTime(commit, vals) |
| 1160 | + require.NoError(t, err) |
| 1161 | + require.False(t, medianTime.IsZero()) |
| 1162 | + }) |
| 1163 | + |
| 1164 | + t.Run("validator not in validator set", func(t *testing.T) { |
| 1165 | + unknownVal := ed25519.GenPrivKey().PubKey().Address() |
| 1166 | + now := time.Now() |
| 1167 | + commit := &types.Commit{ |
| 1168 | + Height: 1, |
| 1169 | + Signatures: []types.CommitSig{ |
| 1170 | + { |
| 1171 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1172 | + ValidatorAddress: val1.Address, |
| 1173 | + Timestamp: now, |
| 1174 | + }, |
| 1175 | + { |
| 1176 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1177 | + ValidatorAddress: unknownVal, |
| 1178 | + Timestamp: now.Add(1 * time.Minute), |
| 1179 | + }, |
| 1180 | + }, |
| 1181 | + } |
| 1182 | + |
| 1183 | + _, err := sm.MedianTime(commit, vals) |
| 1184 | + require.Error(t, err) |
| 1185 | + require.Contains(t, err.Error(), "commit validator not found in validator set") |
| 1186 | + }) |
| 1187 | + |
| 1188 | + t.Run("not all validators present", func(t *testing.T) { |
| 1189 | + now := time.Now() |
| 1190 | + commit := &types.Commit{ |
| 1191 | + Height: 1, |
| 1192 | + Signatures: []types.CommitSig{ |
| 1193 | + { |
| 1194 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1195 | + ValidatorAddress: val1.Address, |
| 1196 | + Timestamp: now, |
| 1197 | + }, |
| 1198 | + { |
| 1199 | + BlockIDFlag: types.BlockIDFlagCommit, |
| 1200 | + ValidatorAddress: val2.Address, |
| 1201 | + Timestamp: now.Add(1 * time.Minute), |
| 1202 | + }, |
| 1203 | + }, |
| 1204 | + } |
| 1205 | + |
| 1206 | + medianTime, err := sm.MedianTime(commit, vals) |
| 1207 | + require.NoError(t, err) |
| 1208 | + require.False(t, medianTime.IsZero()) |
| 1209 | + }) |
| 1210 | +} |
0 commit comments