forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslashing.go
111 lines (98 loc) · 3.3 KB
/
slashing.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package miner
import (
"errors"
"github.com/julwil/bazo-miner/p2p"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
"sync"
"time"
)
type SlashingProof struct {
ConflictingBlockHash1 [32]byte
ConflictingBlockHash2 [32]byte
ConflictingBlockHashWithoutTx1 [32]byte
ConflictingBlockHashWithoutTx2 [32]byte
}
var SameChainMutex = sync.Mutex{}
//Find a proof where a validator votes on two different chains within the slashing window
func seekSlashingProof(block *protocol.Block) error {
//check if block is being added to your chain
lastClosedBlock := storage.ReadLastClosedBlock()
if lastClosedBlock == nil {
return errors.New("Latest block not found.")
}
//When the block is added ontop of your chain then there is no slashing needed
if lastClosedBlock.Hash == block.Hash || lastClosedBlock.Hash == block.PrevHash {
return nil
} else {
//Get the latest blocks and check if there is proof for multi-voting within the slashing window
prevBlocks := storage.ReadAllClosedBlocks()
if prevBlocks == nil {
return nil
}
for _, prevBlock := range prevBlocks {
if IsInSameChain(prevBlock, block) {
return nil
}
if prevBlock.Beneficiary == block.Beneficiary &&
(uint64(prevBlock.Height) < uint64(block.Height)+activeParameters.SlashingWindowSize ||
uint64(block.Height) < uint64(prevBlock.Height)+activeParameters.SlashingWindowSize) {
slashingDict[block.Beneficiary] = SlashingProof{ConflictingBlockHash1: block.Hash, ConflictingBlockHash2: prevBlock.Hash, ConflictingBlockHashWithoutTx1: block.HashWithoutTx, ConflictingBlockHashWithoutTx2: block.PrevHashWithoutTx}
}
}
}
return nil
}
//Check if two blocks are part of the same chain or if they appear in two competing chains
func IsInSameChain(b1, b2 *protocol.Block) bool {
SameChainMutex.Lock()
defer SameChainMutex.Unlock()
var higherBlock, lowerBlock *protocol.Block
if b1.Height == b2.Height {
return false
}
if b1.Height > b2.Height {
higherBlock = b1
lowerBlock = b2
} else {
higherBlock = b2
lowerBlock = b1
}
for higherBlock.Height > 0 {
newHigherBlock := storage.ReadClosedBlock(higherBlock.PrevHash)
//Check blocks without transactions
if newHigherBlock == nil {
newHigherBlock = storage.ReadClosedBlockWithoutTx(higherBlock.PrevHashWithoutTx)
}
if newHigherBlock == nil {
p2p.BlockReq(higherBlock.PrevHash, higherBlock.PrevHashWithoutTx)
//Blocking wait
select {
case encodedBlock := <-p2p.BlockReqChan:
newHigherBlock = newHigherBlock.Decode(encodedBlock)
storage.WriteToReceivedStash(newHigherBlock)
//Limit waiting time to BLOCKFETCH_TIMEOUT seconds before aborting.
case <-time.After(BLOCKFETCH_TIMEOUT * time.Second):
if p2p.BlockAlreadyReceived(storage.ReadReceivedBlockStash(), higherBlock.PrevHash) {
for _, block := range storage.ReadReceivedBlockStash() {
if block.Hash == higherBlock.PrevHash {
newHigherBlock = block
break
}
}
logger.Printf("Block %x received Before", higherBlock.PrevHash)
break
}
logger.Printf("Higher Block %x, %x is nil --> Break", higherBlock.PrevHash, higherBlock.PrevHashWithoutTx)
break
}
}
if higherBlock != nil {
higherBlock = newHigherBlock
if higherBlock.Hash == lowerBlock.Hash {
return true
}
}
}
return false
}