This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
blockchain.go
260 lines (227 loc) · 7.31 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bcm
import (
"bytes"
"fmt"
"sync"
"time"
"github.com/hyperledger/burrow/encoding"
"github.com/tendermint/tendermint/types"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/logging"
dbm "github.com/tendermint/tendermint/libs/db"
)
var stateKey = []byte("BlockchainState")
type BlockchainInfo interface {
GenesisHash() []byte
GenesisDoc() genesis.GenesisDoc
ChainID() string
LastBlockHeight() uint64
LastBlockTime() time.Time
LastCommitTime() time.Time
LastCommitDuration() time.Duration
LastBlockHash() []byte
AppHashAfterLastBlock() []byte
// Gets the BlockHash at a height (or nil if no BlockStore mounted or block could not be found)
BlockHash(height uint64) []byte
// GetBlockHash returns hash of the specific block
GetBlockHeader(blockNumber uint64) (*types.Header, error)
}
type Blockchain struct {
sync.RWMutex
persistedState PersistedState
// Non-persisted state
db dbm.DB
blockStore *BlockStore
genesisDoc genesis.GenesisDoc
lastBlockHash []byte
lastCommitTime time.Time
lastCommitDuration time.Duration
}
var _ BlockchainInfo = &Blockchain{}
// LoadOrNewBlockchain returns true if state already exists
func LoadOrNewBlockchain(db dbm.DB, genesisDoc *genesis.GenesisDoc, logger *logging.Logger) (_ *Blockchain, exists bool, _ error) {
logger = logger.WithScope("LoadOrNewBlockchain")
logger.InfoMsg("Trying to load blockchain state from database",
"database_key", stateKey)
bc, err := loadBlockchain(db, genesisDoc)
if err != nil {
return nil, false, fmt.Errorf("error loading blockchain state from database: %v", err)
}
if bc != nil {
dbHash := bc.GenesisHash()
argHash := genesisDoc.Hash()
if !bytes.Equal(dbHash, argHash) {
return nil, false, fmt.Errorf("GenesisDoc passed to LoadOrNewBlockchain has hash: 0x%X, which does not "+
"match the one found in database: 0x%X, database genesis:\n%v\npassed genesis:\n%v\n",
argHash, dbHash, bc.genesisDoc.JSONString(), genesisDoc.JSONString())
}
if bc.LastBlockTime().Before(genesisDoc.GenesisTime) {
return nil, false, fmt.Errorf("LastBlockTime %v from loaded Blockchain is before GenesisTime %v",
bc.LastBlockTime(), genesisDoc.GenesisTime)
}
return bc, true, nil
}
logger.InfoMsg("No existing blockchain state found in database, making new blockchain")
return NewBlockchain(db, genesisDoc), false, nil
}
// NewBlockchain returns a pointer to blockchain state initialised from genesis
func NewBlockchain(db dbm.DB, genesisDoc *genesis.GenesisDoc) *Blockchain {
bc := &Blockchain{
db: db,
persistedState: PersistedState{
AppHashAfterLastBlock: genesisDoc.Hash(),
GenesisHash: genesisDoc.Hash(),
LastBlockTime: genesisDoc.GenesisTime,
},
genesisDoc: *genesisDoc,
}
return bc
}
func GetSyncInfo(blockchain BlockchainInfo) *SyncInfo {
return &SyncInfo{
LatestBlockHeight: blockchain.LastBlockHeight(),
LatestBlockHash: blockchain.LastBlockHash(),
LatestAppHash: blockchain.AppHashAfterLastBlock(),
LatestBlockTime: blockchain.LastBlockTime(),
LatestBlockSeenTime: blockchain.LastCommitTime(),
LatestBlockDuration: blockchain.LastCommitDuration(),
}
}
func loadBlockchain(db dbm.DB, genesisDoc *genesis.GenesisDoc) (*Blockchain, error) {
buf := db.Get(stateKey)
if len(buf) == 0 {
return nil, nil
}
bc, err := decodeBlockchain(buf, genesisDoc)
if err != nil {
return nil, err
}
bc.db = db
return bc, nil
}
func (bc *Blockchain) CommitBlock(blockTime time.Time, blockHash, appHash []byte) error {
return bc.CommitBlockAtHeight(blockTime, blockHash, appHash, bc.persistedState.LastBlockHeight+1)
}
func (bc *Blockchain) CommitBlockAtHeight(blockTime time.Time, blockHash, appHash []byte, height uint64) error {
bc.Lock()
defer bc.Unlock()
// Checkpoint on the _previous_ block. If we die, this is where we will resume since we know all intervening state
// has been written successfully since we are committing the next block.
// If we fall over we can resume a safe committed state and Tendermint will catch us up
err := bc.save()
if err != nil {
return err
}
bc.lastCommitDuration = blockTime.Sub(bc.persistedState.LastBlockTime)
bc.lastBlockHash = blockHash
bc.persistedState.LastBlockHeight = height
bc.persistedState.LastBlockTime = blockTime
bc.persistedState.AppHashAfterLastBlock = appHash
bc.lastCommitTime = time.Now().UTC()
return nil
}
func (bc *Blockchain) CommitWithAppHash(appHash []byte) error {
bc.persistedState.AppHashAfterLastBlock = appHash
bc.Lock()
defer bc.Unlock()
return bc.save()
}
func (bc *Blockchain) save() error {
if bc.db != nil {
encodedState, err := bc.Encode()
if err != nil {
return err
}
bc.db.SetSync(stateKey, encodedState)
}
return nil
}
func (bc *Blockchain) Encode() ([]byte, error) {
return encoding.Encode(&bc.persistedState)
}
func decodeBlockchain(encodedState []byte, genesisDoc *genesis.GenesisDoc) (*Blockchain, error) {
bc := NewBlockchain(nil, genesisDoc)
err := encoding.Decode(encodedState, &bc.persistedState)
if err != nil {
return nil, err
}
return bc, nil
}
func (bc *Blockchain) GenesisHash() []byte {
return bc.persistedState.GenesisHash
}
func (bc *Blockchain) GenesisDoc() genesis.GenesisDoc {
return bc.genesisDoc
}
func (bc *Blockchain) ChainID() string {
return bc.genesisDoc.ChainID()
}
func (bc *Blockchain) LastBlockHeight() uint64 {
if bc == nil {
return 0
}
bc.RLock()
defer bc.RUnlock()
return bc.persistedState.LastBlockHeight
}
func (bc *Blockchain) LastBlockTime() time.Time {
bc.RLock()
defer bc.RUnlock()
return bc.persistedState.LastBlockTime
}
func (bc *Blockchain) LastCommitTime() time.Time {
bc.RLock()
defer bc.RUnlock()
return bc.lastCommitTime
}
func (bc *Blockchain) LastCommitDuration() time.Duration {
bc.RLock()
defer bc.RUnlock()
return bc.lastCommitDuration
}
func (bc *Blockchain) LastBlockHash() []byte {
bc.RLock()
defer bc.RUnlock()
return bc.lastBlockHash
}
func (bc *Blockchain) AppHashAfterLastBlock() []byte {
bc.RLock()
defer bc.RUnlock()
return bc.persistedState.AppHashAfterLastBlock
}
// Tendermint block access
func (bc *Blockchain) SetBlockStore(bs *BlockStore) {
bc.blockStore = bs
}
func (bc *Blockchain) BlockHash(height uint64) []byte {
header, err := bc.GetBlockHeader(height)
if err != nil {
return nil
}
return header.Hash()
}
func (bc *Blockchain) GetBlockHeader(height uint64) (*types.Header, error) {
const errHeader = "GetBlockHeader():"
if bc == nil {
return nil, fmt.Errorf("%s could not get block hash because Blockchain has not been given access to "+
"tendermint BlockStore", errHeader)
}
blockMeta, err := bc.blockStore.BlockMeta(int64(height))
if err != nil {
return nil, fmt.Errorf("%s could not get BlockMeta: %v", errHeader, err)
}
return &blockMeta.Header, nil
}