-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
120 lines (100 loc) · 2.44 KB
/
pool.go
File metadata and controls
120 lines (100 loc) · 2.44 KB
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
package main
import (
"bytes"
"encoding/gob"
"sort"
"strings"
"time"
"golang.org/x/crypto/sha3"
)
type HashDate struct {
hash string
sent time.Time
}
type Blockpool struct {
Hashes [1000]string
queue []HashDate
}
var blockpool Blockpool // Blockpool singleton
// Returns the genesis pool, after setting the global pool to genesis
func GenesisPool() Blockpool {
var genesis Blockpool
hasher := sha3.New512()
for i := 0; i < len(genesis.Hashes); i++ {
block := make([]byte, 1)
block[0] = byte(i)
hasher.Write(block)
genesis.Hashes[i] = string(hasher.Sum(nil)[:64])
hasher.Reset()
}
blockpool = genesis
return genesis
}
// Adds a new hash to the blockpool's receive queue
func ReceiveBlockHash(hash string) {
entry := HashDate{hash, time.Now()}
blockpool.queue = append(blockpool.queue, entry)
}
func updateBlockpool() {
firstOld := -1
for i := 0; i < len(blockpool.queue); i++ {
if time.Since(blockpool.queue[i].sent) > time.Hour {
firstOld = i
break
}
}
if firstOld == -1 {
return
}
pairs := blockpool.queue[firstOld:]
hashes := make([]string, len(pairs))
for i := 0; i < len(pairs); i++ {
hashes[i] = pairs[i].hash
}
copy(blockpool.Hashes[:], append(hashes, blockpool.Hashes[:firstOld]...)[:1000])
}
// Selects a block parent based on the encrypted message
func SelectParentHash(encryptedMessage string) string {
updateBlockpool()
//Add hash of encrypted message to the end of the blockpool array
hasher := sha3.New512()
hasher.Write([]byte(encryptedMessage))
hash := string(hasher.Sum(nil)[:64])
blockpoolStrings := append(blockpool.Hashes[:], hash)
//Sorted blockpool strings
sort.Strings(blockpoolStrings)
var element int
//Determine the element after the encrypted message
for i := 0; i < len(blockpoolStrings); i++ {
if strings.Compare(blockpoolStrings[i], hash) == 0 {
if i == len(blockpoolStrings)-1 {
element = i + 1
if element == len(blockpoolStrings) {
element = i - 1
}
}
}
}
//return parent hash
return blockpoolStrings[element]
}
func StringifyBlockpool() string {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
err := encoder.Encode(blockpool)
if err != nil {
panic(err)
}
return buf.String()
}
// Returns the blockpool after assigning it
func DestringifyBlockpool(pool string) Blockpool {
var buf bytes.Buffer
buf.WriteString(pool)
decoder := gob.NewDecoder(&buf)
err := decoder.Decode(blockpool)
if err != nil {
panic(err)
}
return blockpool
}