-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.go
88 lines (74 loc) · 2.22 KB
/
blockchain.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
package ncoin_wallet
import (
"fmt"
"github.com/NeironTeam/ncoin/internal"
"github.com/go-redis/redis"
"sort"
)
const REDIS_PATH = "localhost"
const REDIS_PORT = "6379"
// Blockchain struct contains all blockchain blocks and reference to last block
type Blockchain struct {
blocks blockList
lastBlock block
}
type blockList []block
func (bs blockList) Len() int { return len(bs) }
func (bs blockList) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] }
func (bs blockList) Less(i, j int) bool { return bs[i].Timestamp < bs[j].Timestamp }
// AddBlock function appends new block to the blockchain and reference that
// block as lastBlock.
func (b *Blockchain) AddBlock(n block) {
b.blocks = append(b.blocks, n)
b.lastBlock = n
}
func (b *Blockchain) Store() {
var (
redisPath string = internal.Getenv("REDIS_PATH", REDIS_PATH)
redisPort string = internal.Getenv("REDIS_PORT", REDIS_PORT)
)
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", redisPath, redisPort),
})
sort.Sort(b.blocks)
for _, bl := range b.blocks {
var hash string = bl.CalculateHash()
if exists, err := client.HExists("blocks", hash).Result(); err != nil {
panic(err)
} else if exists {
break
}
if jbl, err := bl.ToJson(); err != nil {
panic(err)
} else if err := client.HSet("blocks", hash, jbl); err != nil {
fmt.Println(err)
}
}
}
func (b *Blockchain) Load() {
var (
redisPath string = internal.Getenv("REDIS_PATH", REDIS_PATH)
redisPort string = internal.Getenv("REDIS_PORT", REDIS_PORT)
)
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", redisPath, redisPort),
})
if hashes, err := client.HKeys("blocks").Result(); err != nil {
panic(err)
} else {
for _, hash := range hashes {
var val *redis.StringCmd = client.HGet("blocks", hash)
if res, err := val.Result(); err != nil {
panic(err)
} else if bl, err := BlockFromJson([]byte(res)); err != nil {
panic(b)
} else {
b.blocks = append(b.blocks, bl)
}
}
}
}
// GetMerkleTreeRoot function iterates over whole blockchain blocks and
// generates its hash. Then combines that hash and returns the root of blcks
// hash Merkle Tree.
func (b *Blockchain) GetMerkleTreeRoot() (hash string) { return }