GetBlockList get a list of blocks with height in [from, to).
(since, page, size int)
| 31 | |
| 32 | // GetBlockList get a list of blocks with height in [from, to). |
| 33 | func (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. |
| 80 | func (m *BlocksModel) GetBlockByHeight(height int) (block *Block, err error) { |
no test coverage detected