forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp2p_interface.go
94 lines (73 loc) · 2.65 KB
/
p2p_interface.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
package miner
import (
"github.com/julwil/bazo-miner/p2p"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
)
//The code in this source file communicates with the p2p package via channels
//Constantly listen to incoming data from the network
func incomingData() {
for {
block := <-p2p.BlockIn
processBlock(block)
}
}
//ReceivedBlockStash is a stash with all Blocks received such that we can prevent forking
func processBlock(payload []byte) {
var block, closedBlock *protocol.Block
block = block.Decode(payload)
closedBlock = storage.ReadClosedBlock(block.Hash)
logger.Printf("Received block:%v\n", block.String())
// We already received this block previously.
if closedBlock != nil {
// No updates were performed on the block. Nothing to do.
if closedBlock.NrUpdates == block.NrUpdates {
logger.Printf("Received block (%x) has already been validated.\n", block.Hash[0:8])
return
}
// There has been an update on the block in the meantime. We need to replace the block.
storage.WriteClosedBlock(block)
logger.Printf("Replaced block:\n%v with:\n%v", closedBlock.String(), block.String())
return
}
//Append received Block to stash
storage.WriteToReceivedStash(block)
//Start validation process
receivedBlockInTheMeantime = true
err := validate(block, false)
receivedBlockInTheMeantime = false
if err == nil {
go broadcastBlock(block)
logger.Printf("Validated block (received): %vState:\n%v", block, getState())
} else {
logger.Printf("Received block (%x) could not be validated: %v\n", block.Hash[0:8], err)
}
}
//p2p.BlockOut is a channel whose data get consumed by the p2p package
func broadcastBlock(block *protocol.Block) {
p2p.BlockOut <- block.Encode()
//Make a deep copy of the block (since it is a pointer and will be saved to db later).
//Otherwise the block's bloom filter is initialized on the original block.
var blockCopy = *block
blockCopy.InitBloomFilter(append(storage.GetTxPubKeys(&blockCopy)))
p2p.BlockHeaderOut <- blockCopy.EncodeHeader()
}
func broadcastVerifiedFundsTxs(txs []*protocol.FundsTx) {
var verifiedTxs [][]byte
for _, tx := range txs {
verifiedTxs = append(verifiedTxs, tx.Encode()[:])
}
p2p.VerifiedTxsOut <- protocol.Encode(verifiedTxs, protocol.FUNDSTX_SIZE)
}
func broadcastVerifiedAggTxsToOtherMiners(txs []*protocol.AggTx) {
for _, tx := range txs {
toBrdcst := p2p.BuildPacket(p2p.AGGTX_BRDCST, tx.Encode())
p2p.VerifiedTxsBrdcstOut <- toBrdcst
}
}
func broadcastVerifiedFundsTxsToOtherMiners(txs []*protocol.FundsTx) {
for _, tx := range txs {
toBrdcst := p2p.BuildPacket(p2p.FUNDSTX_BRDCST, tx.Encode())
p2p.VerifiedTxsBrdcstOut <- toBrdcst
}
}