forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslashing_test.go
78 lines (63 loc) · 2.7 KB
/
slashing_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package miner
import (
"github.com/julwil/bazo-miner/crypto"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
"reflect"
"testing"
)
func TestSlashingCondition(t *testing.T) {
cleanAndPrepare()
myAcc, _ := storage.GetAccount(protocol.SerializeHashContent(validatorAccAddress))
initBalance := myAcc.Balance
forkBlock := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
if err := finalizeBlock(forkBlock); err != nil {
t.Errorf("Block finalization for b1 (%v) failed: %v\n", forkBlock, err)
}
if err := validate(forkBlock, false); err != nil {
t.Errorf("Block validation for (%v) failed: %v\n", forkBlock, err)
}
// genesis <- forkBlock <- b
b := newBlock(forkBlock.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, 2)
if err := finalizeBlock(b); err != nil {
t.Errorf("Block finalization for b1 (%v) failed: %v\n", b, err)
}
if err := validate(b, false); err != nil {
t.Errorf("Block validation for (%v) failed: %v\n", b, err)
}
//reference to an old block
lastBlock = forkBlock
// genesis <- forkBlock <- b2
b2 := newBlock(forkBlock.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, 2)
if err := finalizeBlock(b2); err != nil {
t.Errorf("Block finalization for b2 (%v) failed: %v\n", b2, err)
}
//t.Logf("\ninit block:%v\nb1:%v\nb2:%v\n", forkBlock.Hash, b.Hash, b2.Hash)
if err := validate(b2, false); err != nil {
t.Errorf("Block validation for b2 (%v) failed: %v\n", b2, err)
}
slashingDict2 := make(map[[32]byte]SlashingProof)
slashingDict2[b.Beneficiary] = SlashingProof{b2.Hash, b.Hash}
if !reflect.DeepEqual(slashingDict, slashingDict2) {
t.Error("Slashing dictionary was not built correctly.", slashingDict, slashingDict2)
}
//third block contains the slashing proof
b3 := newBlock(b2.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, 3)
if err := finalizeBlock(b3); err != nil {
t.Errorf("Block finalization for b3 (%v) failed: %v\n", b3, err)
}
//Check whether the right proof was included in b3
slashingDict3 := make(map[[32]byte]SlashingProof)
slashingDict3[b3.Beneficiary] = SlashingProof{b3.ConflictingBlockHash1, b3.ConflictingBlockHash2}
if !reflect.DeepEqual(slashingDict, slashingDict3) {
t.Error("Slashing proof was not correctly included in b3.", slashingDict, slashingDict3)
}
if err := validate(b3, false); err != nil {
t.Errorf("Block validation for b3 (%v) failed: %v\n", b3, err)
}
//Check whether the slashing reward is added after a slashing proof is provided
expectedBalance := initBalance + 4*activeParameters.BlockReward + activeParameters.SlashReward - activeParameters.StakingMinimum
if !reflect.DeepEqual(expectedBalance, myAcc.Balance) {
t.Error("Slashing reward is not properly added.", initBalance, myAcc.Balance, expectedBalance)
}
}