forked from bazo-blockchain/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
100 lines (80 loc) · 2.46 KB
/
utils.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
package storage
import (
"errors"
"fmt"
"github.com/bazo-blockchain/bazo-miner/protocol"
"io"
"log"
"os"
"time"
)
func InitLogger() *log.Logger {
//Create a Log-file (Logger.Miner.log) and write all logger.printf(...) Statements into it.
//use this two lines, if all miners should have distinct names for their log files.
time.Now().Format("030405")
filename := "LoggerMiner"+time.Now().Format("150405")+".log"
//use this line when all miners should have the same log file name.
//filename := "LoggerMiner.log"
LogFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
wrt := io.MultiWriter(os.Stdout, LogFile)
log.SetOutput(wrt)
return log.New(wrt, "INFO: ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
}
//Needed by miner and p2p package
func GetAccount(hash [32]byte) (acc *protocol.Account, err error) {
if acc = State[hash]; acc != nil {
return acc, nil
} else {
return nil, errors.New(fmt.Sprintf("Acc (%x) not in the state.", hash[0:8]))
}
}
func GetRootAccount(hash [32]byte) (acc *protocol.Account, err error) {
if IsRootKey(hash) {
acc, err = GetAccount(hash)
return acc, err
}
return nil, err
}
func IsRootKey(hash [32]byte) bool {
_, exists := RootKeys[hash]
return exists
}
//Get all pubKeys involved in AccTx, FundsTx of a given block
func GetTxPubKeys(block *protocol.Block) (txPubKeys [][32]byte) {
txPubKeys = GetAccTxPubKeys(block.AccTxData)
txPubKeys = append(txPubKeys, GetFundsTxPubKeys(block.FundsTxData)...)
return txPubKeys
}
//Get all pubKey involved in AccTx
func GetAccTxPubKeys(accTxData [][32]byte) (accTxPubKeys [][32]byte) {
for _, txHash := range accTxData {
var tx protocol.Transaction
var accTx *protocol.AccTx
tx = ReadClosedTx(txHash)
if tx == nil {
tx = ReadOpenTx(txHash)
}
accTx = tx.(*protocol.AccTx)
accTxPubKeys = append(accTxPubKeys, accTx.Issuer)
accTxPubKeys = append(accTxPubKeys, protocol.SerializeHashContent(accTx.PubKey))
}
return accTxPubKeys
}
//Get all pubKey involved in FundsTx
func GetFundsTxPubKeys(fundsTxData [][32]byte) (fundsTxPubKeys [][32]byte) {
for _, txHash := range fundsTxData {
var tx protocol.Transaction
var fundsTx *protocol.FundsTx
tx = ReadClosedTx(txHash)
if tx == nil {
tx = ReadOpenTx(txHash)
}
fundsTx = tx.(*protocol.FundsTx)
fundsTxPubKeys = append(fundsTxPubKeys, fundsTx.From)
fundsTxPubKeys = append(fundsTxPubKeys, fundsTx.To)
}
return fundsTxPubKeys
}