-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
137 lines (103 loc) · 3.09 KB
/
setup.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockheader
import (
"sync"
"github.com/bitmark-inc/bitmarkd/blockdigest"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/genesis"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/logger"
)
// globals for header
type blockData struct {
sync.RWMutex // to allow locking
log *logger.L
height uint64 // this is the current block Height
previousBlock blockdigest.Digest // and its digest
previousVersion uint16 // plus its version
previousTimestamp uint64 // plus its timestamp
// set once during initialise
initialised bool
}
// global data
var globalData blockData
// Initialise - setup the current block data
func Initialise() error {
globalData.Lock()
defer globalData.Unlock()
// no need to start if already started
if globalData.initialised {
return fault.AlreadyInitialised
}
log := logger.New("blockheader")
globalData.log = log
log.Info("starting…")
setGenesis()
log.Infof("block height: %d", globalData.height)
log.Infof("previous block: %v", globalData.previousBlock)
log.Infof("previous version: %d", globalData.previousVersion)
// all data initialised
globalData.initialised = true
return nil
}
// Finalise - shutdown the block header system
func Finalise() error {
if !globalData.initialised {
return fault.NotInitialised
}
globalData.log.Info("shutting down…")
globalData.log.Flush()
// finally...
globalData.initialised = false
globalData.log.Info("finished")
globalData.log.Flush()
return nil
}
// SetGenesis - reset the block data
func SetGenesis() {
globalData.Lock()
setGenesis()
globalData.Unlock()
}
// internal: must hold lock
func setGenesis() {
globalData.height = genesis.BlockNumber
globalData.previousBlock = genesis.LiveGenesisDigest
globalData.previousVersion = 1
globalData.previousTimestamp = 0
if mode.IsTesting() {
globalData.previousBlock = genesis.TestGenesisDigest
}
}
// Set - set current header data
func Set(height uint64, digest blockdigest.Digest, version uint16, timestamp uint64) {
globalData.Lock()
globalData.height = height
globalData.previousBlock = digest
globalData.previousVersion = version
globalData.previousTimestamp = timestamp
globalData.Unlock()
}
// Get - return all header data
func Get() (uint64, blockdigest.Digest, uint16, uint64) {
globalData.RLock()
defer globalData.RUnlock()
return globalData.height, globalData.previousBlock, globalData.previousVersion, globalData.previousTimestamp
}
// GetNew - return block data for initialising a new block
// returns: previous block digest and the number for the new block
func GetNew() (blockdigest.Digest, uint64) {
globalData.RLock()
defer globalData.RUnlock()
nextBlockNumber := globalData.height + 1
return globalData.previousBlock, nextBlockNumber
}
// Height - return current height
func Height() uint64 {
globalData.RLock()
defer globalData.RUnlock()
return globalData.height
}