forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocks.go
More file actions
101 lines (88 loc) · 2.66 KB
/
blocks.go
File metadata and controls
101 lines (88 loc) · 2.66 KB
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
package models
import (
"database/sql"
"time"
"github.com/go-gorp/gorp"
)
// BlocksModel groups operations on Blocks.
type BlocksModel struct{}
// Block is a block.
type Block struct {
Height int `db:"height" json:"height"` // pk
Hash string `db:"hash" json:"hash"`
Timestamp int64 `db:"timestamp" json:"timestamp"`
TimestampHuman time.Time `db:"-" json:"timestamp_human"`
Version int32 `db:"version" json:"version"`
Producer string `db:"producer" json:"producer"`
MerkleRoot string `db:"merkle_root" json:"merkle_root"`
Parent string `db:"parent" json:"parent"`
TxCount int `db:"tx_count" json:"tx_count"`
}
// PostGet is the hook after SELECT query.
func (b *Block) PostGet(s gorp.SqlExecutor) error {
b.TimestampHuman = time.Unix(0, b.Timestamp)
return nil
}
// GetBlockList get a list of blocks with height in [from, to).
func (m *BlocksModel) GetBlockList(since, page, size int) (blocks []*Block, pagination *Pagination, err error) {
var (
querySQL = `
SELECT
height,
hash,
timestamp,
version,
producer,
merkle_root,
parent,
tx_count
FROM
indexed_blocks
`
countSQL = buildCountSQL(querySQL)
conds []string
args []interface{}
)
pagination = NewPagination(page, size)
if since > 0 {
conds = append(conds, "height < ?")
args = append(args, since)
}
querySQL, countSQL = buildSQLWithConds(querySQL, countSQL, conds)
count, err := chaindb.SelectInt(countSQL, args...)
if err != nil {
return nil, pagination, err
}
pagination.SetTotal(int(count))
blocks = make([]*Block, 0)
if pagination.Offset() > pagination.Total {
return blocks, pagination, nil
}
querySQL += " ORDER BY height DESC"
querySQL += " LIMIT ? OFFSET ?"
args = append(args, pagination.Limit(), pagination.Offset())
_, err = chaindb.Select(&blocks, querySQL, args...)
return blocks, pagination, err
}
// GetBlockByHeight get a block by its height.
func (m *BlocksModel) GetBlockByHeight(height int) (block *Block, err error) {
block = &Block{}
query := `SELECT height, hash, timestamp, version, producer, merkle_root, parent, tx_count
FROM indexed_blocks WHERE height = ?`
err = chaindb.SelectOne(block, query, height)
if err == sql.ErrNoRows {
return nil, nil
}
return block, err
}
// GetBlockByHash get a block by its hash.
func (m *BlocksModel) GetBlockByHash(hash string) (block *Block, err error) {
block = &Block{}
query := `SELECT height, hash, timestamp, version, producer, merkle_root, parent, tx_count
FROM indexed_blocks WHERE hash = ?`
err = chaindb.SelectOne(block, query, hash)
if err == sql.ErrNoRows {
return nil, nil
}
return block, err
}