Skip to content

Commit eac9b34

Browse files
author
auxten
committed
Merge remote-tracking branch 'origin/develop' into beta
2 parents 6098142 + 6eda4b5 commit eac9b34

124 files changed

Lines changed: 13244 additions & 851 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitlab-ci.yml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ variables:
77

88
before_script:
99
# Setup dependency management tool
10-
# - curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o $GOPATH/bin/dep
11-
# - chmod +x $GOPATH/bin/dep
12-
# - go get github.com/mattn/goveralls
13-
# - go get github.com/haya14busa/goverage
14-
# - go get github.com/golang/lint/golint
15-
# - go get github.com/haya14busa/reviewdog/cmd/reviewdog
16-
# - go get github.com/wadey/gocovmerge
10+
# - curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o $GOPATH/bin/dep
11+
# - chmod +x $GOPATH/bin/dep
12+
# - go get github.com/mattn/goveralls
13+
# - go get github.com/haya14busa/goverage
14+
# - go get github.com/golang/lint/golint
15+
# - go get github.com/haya14busa/reviewdog/cmd/reviewdog
16+
# - go get github.com/wadey/gocovmerge
1717
- mkdir -p $GOPATH/src/github.com/CovenantSQL
1818
- cp -r /builds/thunderdb/CovenantSQL $GOPATH/src/github.com/CovenantSQL/
1919
- cd $GOPATH/src/github.com/CovenantSQL/CovenantSQL
20-
# - dep ensure
20+
# - dep ensure
2121
- mkdir -p ~/bin/ && export PATH="~/bin/:$PATH"
2222
- ulimit -n 8192
2323
# - curl -fSL https://github.com/haya14busa/reviewdog/releases/download/$REVIEWDOG_VERSION/reviewdog_linux_amd64 -o ~/bin/reviewdog && chmod +x ~/bin/reviewdog
2424

2525
test-my-project:
26+
stage: test
27+
script: ./alltest.sh
28+
29+
compatibility-testnet:
2630
stage: test
2731
script:
2832
- make clean
29-
- make use_all_cores
30-
- go test -v -race -failfast -parallel 16 -cpu 16 $(go list ./... | grep -v "/vendor/") -coverprofile cover.out
31-
- go test -bench=^BenchmarkPersistentCaller_Call$ -run ^$ ./rpc/
32-
- bash cleanupDB.sh || true
33-
- go test -bench=^BenchmarkMinerTwo$ -benchtime=5s -run ^$ ./cmd/cql-minerd/
33+
- make -j8 client
3434
- go test -bench=^BenchmarkTestnetMiner2$ -benchtime=5s -run ^$ ./cmd/cql-minerd/
35-
- gocovmerge cover.out $(find cmd -name "*.cover.out") | grep -F -v '_gen.go' > coverage.txt && rm -f cover.out
36-
- bash <(curl -s https://codecov.io/bash)
35+
- bash test/testnet_client/run.sh

Gopkg.lock

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
name = "github.com/CovenantSQL/xurls"
6262
branch = "master"
6363

64+
[[override]]
65+
name = "github.com/zserge/metric"
66+
branch = "master"
67+
6468
[[override]]
6569
name = "github.com/xtaci/smux"
6670
branch = "master"

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ push_bench:
100100
docker tag $(IMAGE):$(VERSION) $(IMAGE):bench
101101
docker push $(IMAGE):bench
102102

103+
push_staging:
104+
docker tag $(OB_IMAGE):$(VERSION) $(OB_IMAGE):staging
105+
docker push $(OB_IMAGE):staging
106+
docker tag $(IMAGE):$(VERSION) $(IMAGE):staging
107+
docker push $(IMAGE):staging
108+
109+
103110
push:
104111
docker push $(OB_IMAGE):$(VERSION)
105112
docker push $(OB_IMAGE):latest
@@ -221,6 +228,8 @@ all: bp miner observer client
221228

222229
clean:
223230
rm -rf bin/cql*
231+
rm -f *.cover.out
232+
rm -f coverage.txt
224233

225234
.PHONY: status start stop logs push push_testnet clean \
226235
bin/cqld.test bin/cqld bin/cql-minerd.test bin/cql-minerd bin/cql-utils bin/cql-observer bin/cql-observer.test \

alltest.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o pipefail
5+
set -o nounset
6+
7+
test::package() {
8+
local package="${1:-notset}"
9+
10+
if [[ "${package}" == "notset" ]]; then
11+
&>2 echo "empty package name"
12+
exit 1
13+
fi
14+
15+
local coverage_file="${package//\//.}.cover.out"
16+
echo "[TEST] package=${package}, coverage=${coverage_file}"
17+
go test -race -failfast -parallel 16 -cpu 16 -coverpkg="github.com/CovenantSQL/CovenantSQL/..." -coverprofile "${coverage_file}" "${package}"
18+
}
19+
20+
main() {
21+
make clean
22+
make -j6 bp miner observer
23+
24+
# test package by package
25+
for package in $(go list ./... | grep -v "/vendor/"); do
26+
test::package "${package}"
27+
done
28+
29+
gocovmerge *.cover.out $(find cmd -name "*.cover.out") | grep -F -v '_gen.go' > coverage.txt && rm -f *.cover.out
30+
bash <(curl -s https://codecov.io/bash)
31+
32+
# some benchmarks
33+
go test -bench=^BenchmarkPersistentCaller_Call$ -run ^$ ./rpc/
34+
bash cleanupDB.sh || true
35+
go test -bench=^BenchmarkMinerTwo$ -benchtime=5s -run ^$ ./cmd/cql-minerd/
36+
bash cleanupDB.sh || true
37+
}
38+
39+
main "$@"
40+

api/blocks.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/CovenantSQL/CovenantSQL/api/models"
8+
"github.com/sourcegraph/jsonrpc2"
9+
)
10+
11+
func init() {
12+
rpc.RegisterMethod("bp_getBlockList", bpGetBlockList, bpGetBlockListParams{})
13+
rpc.RegisterMethod("bp_getBlockByHeight", bpGetBlockByHeight, bpGetBlockByHeightParams{})
14+
rpc.RegisterMethod("bp_getBlockByHash", bpGetBlockByHash, bpGetBlockByHashParams{})
15+
}
16+
17+
type bpGetBlockListParams struct {
18+
Since int `json:"since"`
19+
Page int `json:"page"`
20+
Size int `json:"size"`
21+
}
22+
23+
func (params *bpGetBlockListParams) Validate() error {
24+
if params.Size > 1000 {
25+
return errors.New("max size is 1000")
26+
}
27+
return nil
28+
}
29+
30+
// BPGetBlockListResponse is the response for method bp_getBlockList.
31+
type BPGetBlockListResponse struct {
32+
Blocks []*models.Block `json:"blocks"`
33+
Pagination *models.Pagination `json:"pagination"`
34+
}
35+
36+
func bpGetBlockList(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
37+
result interface{}, err error,
38+
) {
39+
params := ctx.Value("_params").(*bpGetBlockListParams)
40+
model := models.BlocksModel{}
41+
blocks, pagination, err := model.GetBlockList(params.Since, params.Page, params.Size)
42+
if err != nil {
43+
return nil, err
44+
}
45+
result = &BPGetBlockListResponse{
46+
Blocks: blocks,
47+
Pagination: pagination,
48+
}
49+
return result, nil
50+
}
51+
52+
type bpGetBlockByHeightParams struct {
53+
Height int `json:"height"`
54+
}
55+
56+
func bpGetBlockByHeight(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
57+
result interface{}, err error,
58+
) {
59+
params := ctx.Value("_params").(*bpGetBlockByHeightParams)
60+
model := models.BlocksModel{}
61+
return model.GetBlockByHeight(params.Height)
62+
}
63+
64+
type bpGetBlockByHashParams struct {
65+
Hash string `json:"hash"`
66+
}
67+
68+
func bpGetBlockByHash(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
69+
result interface{}, err error,
70+
) {
71+
params := ctx.Value("_params").(*bpGetBlockByHashParams)
72+
model := models.BlocksModel{}
73+
return model.GetBlockByHash(params.Hash)
74+
}

api/models/blocks.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package models
2+
3+
import (
4+
"database/sql"
5+
"time"
6+
7+
"github.com/go-gorp/gorp"
8+
)
9+
10+
// BlocksModel groups operations on Blocks.
11+
type BlocksModel struct{}
12+
13+
// Block is a block.
14+
type Block struct {
15+
Height int `db:"height" json:"height"` // pk
16+
Hash string `db:"hash" json:"hash"`
17+
Timestamp int64 `db:"timestamp" json:"timestamp"`
18+
TimestampHuman time.Time `db:"-" json:"timestamp_human"`
19+
Version int32 `db:"version" json:"version"`
20+
Producer string `db:"producer" json:"producer"`
21+
MerkleRoot string `db:"merkle_root" json:"merkle_root"`
22+
Parent string `db:"parent" json:"parent"`
23+
TxCount int `db:"tx_count" json:"tx_count"`
24+
}
25+
26+
// PostGet is the hook after SELECT query.
27+
func (b *Block) PostGet(s gorp.SqlExecutor) error {
28+
b.TimestampHuman = time.Unix(0, b.Timestamp)
29+
return nil
30+
}
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) {
81+
block = &Block{}
82+
query := `SELECT height, hash, timestamp, version, producer, merkle_root, parent, tx_count
83+
FROM indexed_blocks WHERE height = ?`
84+
err = chaindb.SelectOne(block, query, height)
85+
if err == sql.ErrNoRows {
86+
return nil, nil
87+
}
88+
return block, err
89+
}
90+
91+
// GetBlockByHash get a block by its hash.
92+
func (m *BlocksModel) GetBlockByHash(hash string) (block *Block, err error) {
93+
block = &Block{}
94+
query := `SELECT height, hash, timestamp, version, producer, merkle_root, parent, tx_count
95+
FROM indexed_blocks WHERE hash = ?`
96+
err = chaindb.SelectOne(block, query, hash)
97+
if err == sql.ErrNoRows {
98+
return nil, nil
99+
}
100+
return block, err
101+
}

0 commit comments

Comments
 (0)