Skip to content

Commit dcf7e3b

Browse files
committed
add test and fix a test
added test for median time and fixed the invalid signatures check, which now fails before the block is validated via the median time calculation
1 parent 9c09042 commit dcf7e3b

2 files changed

Lines changed: 86 additions & 8 deletions

File tree

state/state_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/big"
88
"os"
99
"testing"
10+
"time"
1011

1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
@@ -1124,3 +1125,86 @@ func TestStateProto(t *testing.T) {
11241125
}
11251126
}
11261127
}
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+
}

state/validation_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,8 @@ func TestValidateBlockCommit(t *testing.T) {
196196
#2589: test len(block.LastCommit.Signatures) == state.LastValidators.Size()
197197
*/
198198
block, err = makeBlock(state, height, wrongSigsCommit)
199-
require.NoError(t, err)
200-
err = blockExec.ValidateBlock(state, block)
201-
_, isErrInvalidCommitSignatures := err.(types.ErrInvalidCommitSignatures)
202-
require.True(t, isErrInvalidCommitSignatures,
203-
"expected ErrInvalidCommitSignatures at height %d, but got: %v",
204-
height,
205-
err,
206-
)
199+
require.Error(t, err)
200+
require.ErrorContains(t, err, "error making block")
207201
}
208202

209203
/*

0 commit comments

Comments
 (0)