-
Notifications
You must be signed in to change notification settings - Fork 13
/
get.go
43 lines (35 loc) · 1.19 KB
/
get.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockheader
import (
"encoding/binary"
"github.com/bitmark-inc/bitmarkd/blockdigest"
"github.com/bitmark-inc/bitmarkd/blockrecord"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/genesis"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/storage"
)
// DigestForBlock - return the digest for a specific block number
func DigestForBlock(number uint64) (blockdigest.Digest, error) {
globalData.Lock()
defer globalData.Unlock()
// valid block number
if number <= genesis.BlockNumber {
if mode.IsTesting() {
return genesis.TestGenesisDigest, nil
}
return genesis.LiveGenesisDigest, nil
}
// fetch block and compute digest
n := make([]byte, 8)
binary.BigEndian.PutUint64(n, number)
packed := storage.Pool.Blocks.Get(n) // ***** FIX THIS: possible optimisation is to store the block hashes in a separate index
if nil == packed {
return blockdigest.Digest{}, fault.ErrBlockNotFound
}
_, digest, _, err := blockrecord.ExtractHeader(packed, 0)
return digest, err
}