-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.go
199 lines (178 loc) · 6.14 KB
/
db.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/rand"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"golang.org/x/crypto/sha3"
)
// Graph is a collection of transactions
type Graph struct {
Transactions []Transaction `json:"transactions"`
}
// Transaction This is the structure of the transaction
type Transaction struct {
Time string `json:"time" db:"tx_time"`
Type string `json:"type" db:"tx_type"`
Hash string `json:"hash" db:"tx_hash"`
Data string `json:"data" db:"tx_data"`
Prev string `json:"prev" db:"tx_prev"`
Epoc string `json:"epoc" db:"tx_epoc"`
Subg string `json:"subg" db:"tx_subg"`
Prnt string `json:"prnt" db:"tx_prnt"`
Mile bool `json:"mile" db:"tx_mile"`
Lead bool `json:"lead" db:"tx_lead"`
}
// connect will create an active DB connection
func connect() (*sqlx.DB, error) {
connectParams := fmt.Sprintf("user=%s dbname=%s sslmode=%s", dbUser, dbName, dbSSL)
db, err := sqlx.Connect("postgres", connectParams)
return db, err
}
// addBulkTransactions adds $number of tx to the graph immediately
func addBulkTransactions(number int) {
sum := 0
for i := 1; i < number; i++ {
sum += i
msg := string(unixTimeStampNano())
createTransaction("2", msg)
}
}
// loadSingleTx outputs a single tx by hash
func loadSingleTx(hash string) []byte {
db, connectErr := connect()
defer db.Close()
handle("Error creating a DB connection: ", connectErr)
tx := Transaction{}
err := db.Get(&tx, "SELECT * FROM transactions WHERE tx_hash=$1", hash)
// if &tx.Data == nil {
txJSON, _ := json.MarshalIndent(&tx, "", " ")
switch {
case err != nil:
handle("There was a problem loading the graph: ", err)
return txJSON
default:
fmt.Printf(brightcyan+"\nReturning tx with hash:"+brightgreen+"\n%s\n"+nc, hash)
return txJSON
// }
}
}
func generateRandomTransactions() {
min := 1
max := 100
rand.Seed(time.Now().UTC().UnixNano())
number := rand.Intn(max-min) + min
fmt.Printf("+%v tx in subgraph %s.. ", number, thisSubgraphShortName)
addBulkTransactions(number)
// fmt.Printf(brightgreen+" Done! "+brightyellow+"Sleeping %v seconds."+nc, number)
if number%3 == 0 {
time.Sleep(time.Duration(number) * time.Minute)
}
time.Sleep(time.Duration(number) * time.Second)
generateRandomTransactions()
}
// createRoot Transaction channels start with a rootTx transaction always
func createRoot() error {
db, connectErr := connect()
defer db.Close()
handle("Error creating a DB connection: ", connectErr)
var count int
err := db.QueryRow("SELECT COUNT(*) FROM transactions ORDER BY $1 DESC", "tx_time").Scan(&count)
switch {
case err != nil:
handle("There was a problem counting database transactions: ", err)
return err
default:
// fmt.Printf("Found %v transactions in the db.", count)
if count == 0 {
txTime := unixTimeStampNano()
txType := "0"
txSubg := "0"
txPrnt := "0"
txData := "8d3729b91a13878508c564fbf410ae4f33fcb4cfdb99677f4b23d4c4adb447650964b4fe9da16299831b9cc17aaabd5b8d81fb05460be92af99d128584101a30" // ?
txPrev := "c66f4851618cd53104d4a395212958abf88d96962c0c298a0c7a7c1242fac5c2ee616c8c4f140a2e199558ead6d18ae263b2311b590b0d7bf3777be5b3623d9c" // RockSteady was here
hash := sha512.Sum512([]byte(txTime + txType + txData + txPrev))
txHash := hex.EncodeToString(hash[:])
txMile := true
txLead := false
txEpoc := txHash
tx := db.MustBegin()
tx.MustExec("INSERT INTO transactions (tx_time, tx_type, tx_hash, tx_data, tx_prev, tx_epoc, tx_subg, tx_prnt, tx_mile, tx_lead ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", txTime, txType, txHash, txData, txPrev, txEpoc, txSubg, txPrnt, txMile, txLead)
tx.Commit()
return nil
} else if count > 0 {
fmt.Printf("%s%s\n"+brightmagenta+"%v"+brightpurple+" transactions known", brightcyan+"\nRoot Tx is ", brightgreen+"present", count)
return errors.New("Root tx already present. ")
}
}
return nil
}
// createTransaction This will add a transaction to the graph
func createTransaction(txType, data string) bool {
var txData string
var txSubg string
var txPrev string
var txPrnt string
var txLead bool
// if isCoordinator && txType == "2" {
if txType == "2" {
parsePayload := json.Valid([]byte(data))
if !parsePayload {
txData = hex.EncodeToString([]byte(data))
} else if parsePayload {
txData = data
}
db, connectErr := connect()
defer db.Close()
handle("Error creating a DB connection: ", connectErr)
_ = db.QueryRow("SELECT tx_hash FROM transactions ORDER BY tx_time DESC LIMIT 1").Scan(&txPrev)
txTime := unixTimeStampNano()
txHash := hashTransaction(txTime, txType, txData, txPrev)
txEpoc := "0"
txMile := false
tx := db.MustBegin()
if txCount == 0 {
txLead = true
txSubg = txHash
txPrnt = txEpoc
thisSubgraph = txHash
txPrnt = thisSubgraph
thisSubgraphShortName = thisSubgraph[0:4]
go newSubGraphTimer()
} else if txCount > 0 {
txLead = false
txPrnt = thisSubgraph
txSubg = thisSubgraph
thisSubgraphShortName = thisSubgraph[0:4]
}
tx.MustExec("INSERT INTO transactions (tx_time, tx_type, tx_hash, tx_data, tx_prev, tx_epoc, tx_subg, tx_prnt, tx_mile, tx_lead ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", txTime, txType, txHash, txData, txPrev, txEpoc, txSubg, txPrnt, txMile, txLead)
tx.Commit()
txCount++
}
return true
}
// newSubGraphTimer timer for collection interval
func newSubGraphTimer() {
// fmt.Printf(brightcyan+"\nSubgraph created:"+brightgreen+" %s.."+brightcyan+" SubGraph Interval: "+brightgreen+"%vs\n"+nc, thisSubgraph[0:8], poolInterval)
fmt.Printf(brightcyan+"\nSubgraph created:"+brightgreen+" %s.. "+nc, thisSubgraph[0:8])
time.Sleep(time.Duration(poolInterval) * time.Second)
txCount = 0
// fmt.Printf(brightyellow + "\nInterval concluded" + nc)
}
// hashTransaction takes elements of a transaction and computes a hash using SHA512
func hashTransaction(txTime, txType, txData, txPrev string) string {
hashedData := []byte(txTime + txType + txData + txPrev)
slot := make([]byte, 64)
sha3.ShakeSum256(slot, hashedData)
// fmt.Printf("%x\n", slot)
txHash := hex.EncodeToString(slot[:])
// legacy sha512
// hash := sha512.Sum512(hashedData)
// txHash := hex.EncodeToString(hash[:])
return txHash
}