/
blockmania.go
60 lines (52 loc) · 1.24 KB
/
blockmania.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
// Package blockmania implements the Blockmania consensus algorithm.
package blockmania // import "chainspace.io/chainspace-go/blockmania"
import (
"strconv"
)
const (
hexUpper = "0123456789ABCDEF"
)
// BlockGraph represents a block generated by a node and its dependencies.
type BlockGraph struct {
Block BlockID
Deps []Dep
Prev BlockID
}
// BlockID represents a specific block from a node in a way that can be used as
// a map key.
type BlockID struct {
Hash string
Node uint64
Round uint64
}
// Dep represents a direct dependency of a block generated by a node.
type Dep struct {
Block BlockID
Deps []BlockID
Prev BlockID
}
// Interpreted represents the results of interpreting a Graph for a particular
// round.
type Interpreted struct {
Blocks []BlockID
Consumed uint64
Round uint64
}
func (b BlockID) String() string {
var v []byte
v = strconv.AppendUint(v, b.Node, 10)
v = append(v, ' ', '|', ' ')
v = strconv.AppendUint(v, b.Round, 10)
if b.Hash != "" {
v = append(v, ' ', '|', ' ')
for i := 6; i < 12; i++ {
c := b.Hash[i]
v = append(v, hexUpper[c>>4], hexUpper[c&0x0f])
}
}
return string(v)
}
// Valid returns whether the BlockID is not the zero value.
func (b BlockID) Valid() bool {
return b.Hash != ""
}