|
| 1 | +package monitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + "github.com/ethereum/go-ethereum/core/types" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMaliciousVoteMonitor(t *testing.T) { |
| 12 | + //log.Root().SetHandler(log.StdoutHandler) |
| 13 | + // case 1, different voteAddress |
| 14 | + { |
| 15 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 16 | + pendingBlockNumber := uint64(1000) |
| 17 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 18 | + voteAddress := types.BLSPublicKey{} |
| 19 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 20 | + vote1 := &types.VoteEnvelope{ |
| 21 | + VoteAddress: voteAddress, |
| 22 | + Signature: types.BLSSignature{}, |
| 23 | + Data: &types.VoteData{ |
| 24 | + SourceNumber: uint64(0), |
| 25 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 26 | + TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, |
| 27 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 28 | + }, |
| 29 | + } |
| 30 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 31 | + voteAddress[0] = 4 |
| 32 | + vote2 := &types.VoteEnvelope{ |
| 33 | + VoteAddress: voteAddress, |
| 34 | + Signature: types.BLSSignature{}, |
| 35 | + Data: &types.VoteData{ |
| 36 | + SourceNumber: uint64(0), |
| 37 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 38 | + TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, |
| 39 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 40 | + }, |
| 41 | + } |
| 42 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 43 | + } |
| 44 | + |
| 45 | + // case 2, target number not in maliciousVoteSlashScope |
| 46 | + { |
| 47 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 48 | + pendingBlockNumber := uint64(1000) |
| 49 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 50 | + voteAddress := types.BLSPublicKey{} |
| 51 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 52 | + vote1 := &types.VoteEnvelope{ |
| 53 | + VoteAddress: voteAddress, |
| 54 | + Signature: types.BLSSignature{}, |
| 55 | + Data: &types.VoteData{ |
| 56 | + SourceNumber: uint64(0), |
| 57 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 58 | + TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, |
| 59 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 60 | + }, |
| 61 | + } |
| 62 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 63 | + vote2 := &types.VoteEnvelope{ |
| 64 | + VoteAddress: voteAddress, |
| 65 | + Signature: types.BLSSignature{}, |
| 66 | + Data: &types.VoteData{ |
| 67 | + SourceNumber: uint64(0), |
| 68 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 69 | + TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, |
| 70 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 71 | + }, |
| 72 | + } |
| 73 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 74 | + } |
| 75 | + |
| 76 | + // case 3, violate rule1 |
| 77 | + { |
| 78 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 79 | + pendingBlockNumber := uint64(1000) |
| 80 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 81 | + voteAddress := types.BLSPublicKey{} |
| 82 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 83 | + vote1 := &types.VoteEnvelope{ |
| 84 | + VoteAddress: voteAddress, |
| 85 | + Signature: types.BLSSignature{}, |
| 86 | + Data: &types.VoteData{ |
| 87 | + SourceNumber: uint64(0), |
| 88 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 89 | + TargetNumber: pendingBlockNumber - 1, |
| 90 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 91 | + }, |
| 92 | + } |
| 93 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 94 | + vote2 := &types.VoteEnvelope{ |
| 95 | + VoteAddress: voteAddress, |
| 96 | + Signature: types.BLSSignature{}, |
| 97 | + Data: &types.VoteData{ |
| 98 | + SourceNumber: uint64(0), |
| 99 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 100 | + TargetNumber: pendingBlockNumber - 1, |
| 101 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 102 | + }, |
| 103 | + } |
| 104 | + assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 105 | + } |
| 106 | + |
| 107 | + // case 4, violate rule2, vote with smaller range first |
| 108 | + { |
| 109 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 110 | + pendingBlockNumber := uint64(1000) |
| 111 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 112 | + voteAddress := types.BLSPublicKey{} |
| 113 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 114 | + vote1 := &types.VoteEnvelope{ |
| 115 | + VoteAddress: voteAddress, |
| 116 | + Signature: types.BLSSignature{}, |
| 117 | + Data: &types.VoteData{ |
| 118 | + SourceNumber: pendingBlockNumber - 4, |
| 119 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 120 | + TargetNumber: pendingBlockNumber - 1, |
| 121 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 122 | + }, |
| 123 | + } |
| 124 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 125 | + vote2 := &types.VoteEnvelope{ |
| 126 | + VoteAddress: voteAddress, |
| 127 | + Signature: types.BLSSignature{}, |
| 128 | + Data: &types.VoteData{ |
| 129 | + SourceNumber: pendingBlockNumber - 2, |
| 130 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 131 | + TargetNumber: pendingBlockNumber - 3, |
| 132 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 133 | + }, |
| 134 | + } |
| 135 | + assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 136 | + } |
| 137 | + |
| 138 | + // case 5, violate rule2, vote with larger range first |
| 139 | + { |
| 140 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 141 | + pendingBlockNumber := uint64(1000) |
| 142 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 143 | + voteAddress := types.BLSPublicKey{} |
| 144 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 145 | + vote1 := &types.VoteEnvelope{ |
| 146 | + VoteAddress: voteAddress, |
| 147 | + Signature: types.BLSSignature{}, |
| 148 | + Data: &types.VoteData{ |
| 149 | + SourceNumber: pendingBlockNumber - 2, |
| 150 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 151 | + TargetNumber: pendingBlockNumber - 3, |
| 152 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 153 | + }, |
| 154 | + } |
| 155 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 156 | + vote2 := &types.VoteEnvelope{ |
| 157 | + VoteAddress: voteAddress, |
| 158 | + Signature: types.BLSSignature{}, |
| 159 | + Data: &types.VoteData{ |
| 160 | + SourceNumber: pendingBlockNumber - 4, |
| 161 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 162 | + TargetNumber: pendingBlockNumber - 1, |
| 163 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 164 | + }, |
| 165 | + } |
| 166 | + assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 167 | + } |
| 168 | + |
| 169 | + // case 6, normal case |
| 170 | + { |
| 171 | + maliciousVoteMonitor := NewMaliciousVoteMonitor() |
| 172 | + pendingBlockNumber := uint64(1000) |
| 173 | + voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) |
| 174 | + voteAddress := types.BLSPublicKey{} |
| 175 | + copy(voteAddress[:], voteAddrBytes[:]) |
| 176 | + vote1 := &types.VoteEnvelope{ |
| 177 | + VoteAddress: voteAddress, |
| 178 | + Signature: types.BLSSignature{}, |
| 179 | + Data: &types.VoteData{ |
| 180 | + SourceNumber: pendingBlockNumber - 4, |
| 181 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 182 | + TargetNumber: pendingBlockNumber - 3, |
| 183 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), |
| 184 | + }, |
| 185 | + } |
| 186 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) |
| 187 | + vote2 := &types.VoteEnvelope{ |
| 188 | + VoteAddress: voteAddress, |
| 189 | + Signature: types.BLSSignature{}, |
| 190 | + Data: &types.VoteData{ |
| 191 | + SourceNumber: pendingBlockNumber - 3, |
| 192 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 193 | + TargetNumber: pendingBlockNumber - 2, |
| 194 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 195 | + }, |
| 196 | + } |
| 197 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) |
| 198 | + vote3 := &types.VoteEnvelope{ |
| 199 | + VoteAddress: voteAddress, |
| 200 | + Signature: types.BLSSignature{}, |
| 201 | + Data: &types.VoteData{ |
| 202 | + SourceNumber: pendingBlockNumber - 2, |
| 203 | + SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), |
| 204 | + TargetNumber: pendingBlockNumber - 1, |
| 205 | + TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), |
| 206 | + }, |
| 207 | + } |
| 208 | + assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote3, pendingBlockNumber)) |
| 209 | + } |
| 210 | +} |
0 commit comments