MCPcopy Create free account
hub / github.com/CovenantSQL/CovenantSQL / GetBlockList

Method GetBlockList

api/models/blocks.go:33–77  ·  view source on GitHub ↗

GetBlockList get a list of blocks with height in [from, to).

(since, page, size int)

Source from the content-addressed store, hash-verified

31
32// GetBlockList get a list of blocks with height in [from, to).
33func (m *BlocksModel) GetBlockList(since, page, size int) (blocks []*Block, pagination *Pagination, err error) {
34 var (
35 querySQL = `
36 SELECT
37 height,
38 hash,
39 timestamp,
40 version,
41 producer,
42 merkle_root,
43 parent,
44 tx_count
45 FROM
46 indexed_blocks
47 `
48 countSQL = buildCountSQL(querySQL)
49 conds []string
50 args []interface{}
51 )
52
53 pagination = NewPagination(page, size)
54 if since > 0 {
55 conds = append(conds, "height < ?")
56 args = append(args, since)
57 }
58
59 querySQL, countSQL = buildSQLWithConds(querySQL, countSQL, conds)
60
61 count, err := chaindb.SelectInt(countSQL, args...)
62 if err != nil {
63 return nil, pagination, err
64 }
65 pagination.SetTotal(int(count))
66 blocks = make([]*Block, 0)
67 if pagination.Offset() > pagination.Total {
68 return blocks, pagination, nil
69 }
70
71 querySQL += " ORDER BY height DESC"
72 querySQL += " LIMIT ? OFFSET ?"
73 args = append(args, pagination.Limit(), pagination.Offset())
74
75 _, err = chaindb.Select(&blocks, querySQL, args...)
76 return blocks, pagination, err
77}
78
79// GetBlockByHeight get a block by its height.
80func (m *BlocksModel) GetBlockByHeight(height int) (block *Block, err error) {

Callers 1

bpGetBlockListFunction · 0.95

Calls 6

buildCountSQLFunction · 0.85
NewPaginationFunction · 0.85
buildSQLWithCondsFunction · 0.85
SetTotalMethod · 0.80
OffsetMethod · 0.80
LimitMethod · 0.80

Tested by

no test coverage detected