forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_configtx.go
62 lines (54 loc) · 1.62 KB
/
block_configtx.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
package miner
import (
"errors"
"fmt"
"github.com/julwil/bazo-miner/p2p"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
"time"
)
func addConfigTx(b *protocol.Block, tx *protocol.ConfigTx) error {
//No further checks needed, static checks were already done with verify().
b.ConfigTxData = append(b.ConfigTxData, tx.Hash())
logger.Printf("Added tx (%x) to the ConfigTxData slice: %v", tx.Hash(), *tx)
return nil
}
func fetchConfigTxData(block *protocol.Block, configTxSlice []*protocol.ConfigTx, initialSetup bool, errChan chan error) {
for cnt, txHash := range block.ConfigTxData {
var tx protocol.Transaction
var configTx *protocol.ConfigTx
closedTx := storage.ReadClosedTx(txHash)
if closedTx != nil {
if initialSetup {
configTx = closedTx.(*protocol.ConfigTx)
configTxSlice[cnt] = configTx
continue
} else {
errChan <- errors.New("Block validation had configTx that was already in a previous block.")
return
}
}
//TODO Optimize code (duplicated)
tx = storage.ReadOpenTx(txHash)
if tx != nil {
configTx = tx.(*protocol.ConfigTx)
} else {
err := p2p.TxReq(txHash, p2p.CONFIGTX_REQ)
if err != nil {
errChan <- errors.New(fmt.Sprintf("ConfigTx could not be read: %v", err))
return
}
select {
case configTx = <-p2p.ConfigTxChan:
case <-time.After(TXFETCH_TIMEOUT * time.Second):
errChan <- errors.New("ConfigTx fetch timed out.")
return
}
if configTx.Hash() != txHash {
errChan <- errors.New("Received ConfigtxHash did not correspond to our request.")
}
}
configTxSlice[cnt] = configTx
}
errChan <- nil
}