This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action_blockheader.go
116 lines (90 loc) · 3.39 KB
/
action_blockheader.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
package bux
import (
"context"
"fmt"
"github.com/libsv/go-bc"
"github.com/mrz1836/go-datastore"
)
// RecordBlockHeader will save a block header into the Datastore
//
// hash is the hash of the block header
// bh is the block header data
// opts are model options and can include "metadata"
func (c *Client) RecordBlockHeader(ctx context.Context, hash string, height uint32, bh bc.BlockHeader,
opts ...ModelOps) (*BlockHeader, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "record_block_header")
// Create the model & set the default options (gives options from client->model)
newOpts := c.DefaultModelOptions(append(opts, New())...)
blockHeader := newBlockHeader(hash, height, bh, newOpts...)
// Ensure that we have a transaction id (created from the txHex)
id := blockHeader.GetID()
if len(id) == 0 {
return nil, ErrMissingBlockHeaderHash
}
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyRecordBlockHeader, id), c.Cachestore(),
)
defer unlock()
if err != nil {
return nil, err
}
// Process & save the transaction model
if err = blockHeader.Save(ctx); err != nil {
return nil, err
}
// Return the response
return blockHeader, nil
}
// GetBlockHeaders will get all the block headers from the Datastore
func (c *Client) GetBlockHeaders(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*BlockHeader, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_block_headers")
// Get the block headers
blockHeaders, err := getBlockHeaders(
ctx, metadataConditions, conditions, queryParams,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
return blockHeaders, nil
}
// GetBlockHeadersCount will get a count of all the block headers from the Datastore
func (c *Client) GetBlockHeadersCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_block_headers_count")
// Get the block headers count
count, err := getBlockHeadersCount(
ctx, metadataConditions, conditions,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}
// GetUnsyncedBlockHeaders get all unsynced block headers
func (c *Client) GetUnsyncedBlockHeaders(ctx context.Context) ([]*BlockHeader, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_unsynced_block_headers")
// Get the unsynced block headers
return getUnsyncedBlockHeaders(ctx, c.DefaultModelOptions()...)
}
// GetLastBlockHeader get last block header
func (c *Client) GetLastBlockHeader(ctx context.Context) (*BlockHeader, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_last_block_header")
// Get the last block header
return getLastBlockHeader(ctx, c.DefaultModelOptions()...)
}
// GetBlockHeaderByHeight get the block header by height
func (c *Client) GetBlockHeaderByHeight(ctx context.Context, height uint32) (*BlockHeader, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_block_header_by_height")
// Get the block header by height
return getBlockHeaderByHeight(ctx, height, c.DefaultModelOptions()...)
}