Skip to content

Commit

Permalink
add back pruner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Oct 29, 2024
1 parent 960f4f0 commit ef814c0
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 419 deletions.
2 changes: 1 addition & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestStorageRangeFunc(t *testing.T) {

func TestStorageRangeMaxResult(t *testing.T) {
db := muxdb.NewMem()
state := state.New(db, thor.Bytes32{}, 0, 0, 0)
state := state.New(db, trie.Root{})

addr := thor.BytesToAddress([]byte("account1"))
for i := 0; i < 1001; i++ {
Expand Down
148 changes: 76 additions & 72 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package api

import (
"bytes"
"crypto/rand"
"io"
"math"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -18,91 +20,93 @@ import (
"github.com/gorilla/websocket"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/api/accounts"
"github.com/vechain/thor/v2/api/subscriptions"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/cmd/thor/solo"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/metrics"
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/txpool"
)

func init() {
metrics.InitializePrometheusMetrics()
}

// TODO: add back the test
// func TestMetricsMiddleware(t *testing.T) {
// db := muxdb.NewMem()
// stater := state.NewStater(db)
// gene := genesis.NewDevnet()

// b, _, _, err := gene.Build(stater)
// if err != nil {
// t.Fatal(err)
// }
// repo, _ := chain.NewRepository(db, b)

// // inject some invalid data to db
// data := db.NewStore("chain.data")
// var blkID thor.Bytes32
// rand.Read(blkID[:])
// data.Put(blkID[:], []byte("invalid data"))

// // get summary should fail since the block data is not rlp encoded
// _, err = repo.GetBlockSummary(blkID)
// assert.NotNil(t, err)

// router := mux.NewRouter()
// acc := accounts.New(repo, stater, math.MaxUint64, thor.NoFork, solo.NewBFTEngine(repo))
// acc.Mount(router, "/accounts")
// router.PathPrefix("/metrics").Handler(metrics.HTTPHandler())
// router.Use(metricsMiddleware)
// ts := httptest.NewServer(router)

// httpGet(t, ts.URL+"/accounts/0x")
// httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String())

// _, code := httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String()+"?revision="+blkID.String())
// assert.Equal(t, 500, code)

// body, _ := httpGet(t, ts.URL+"/metrics")
// parser := expfmt.TextParser{}
// metrics, err := parser.TextToMetricFamilies(bytes.NewReader(body))
// assert.Nil(t, err)

// m := metrics["thor_metrics_api_request_count"].GetMetric()
// assert.Equal(t, 3, len(m), "should be 3 metric entries")
// assert.Equal(t, float64(1), m[0].GetCounter().GetValue())
// assert.Equal(t, float64(1), m[1].GetCounter().GetValue())

// labels := m[0].GetLabel()
// assert.Equal(t, 3, len(labels))
// assert.Equal(t, "code", labels[0].GetName())
// assert.Equal(t, "200", labels[0].GetValue())
// assert.Equal(t, "method", labels[1].GetName())
// assert.Equal(t, "GET", labels[1].GetValue())
// assert.Equal(t, "name", labels[2].GetName())
// assert.Equal(t, "accounts_get_account", labels[2].GetValue())

// labels = m[1].GetLabel()
// assert.Equal(t, 3, len(labels))
// assert.Equal(t, "code", labels[0].GetName())
// assert.Equal(t, "400", labels[0].GetValue())
// assert.Equal(t, "method", labels[1].GetName())
// assert.Equal(t, "GET", labels[1].GetValue())
// assert.Equal(t, "name", labels[2].GetName())
// assert.Equal(t, "accounts_get_account", labels[2].GetValue())

// labels = m[2].GetLabel()
// assert.Equal(t, 3, len(labels))
// assert.Equal(t, "code", labels[0].GetName())
// assert.Equal(t, "500", labels[0].GetValue())
// assert.Equal(t, "method", labels[1].GetName())
// assert.Equal(t, "GET", labels[1].GetValue())
// assert.Equal(t, "name", labels[2].GetName())
// assert.Equal(t, "accounts_get_account", labels[2].GetValue())
// }
func TestMetricsMiddleware(t *testing.T) {
db := muxdb.NewMem()
stater := state.NewStater(db)
gene := genesis.NewDevnet()

b, _, _, err := gene.Build(stater)
if err != nil {
t.Fatal(err)
}
repo, _ := chain.NewRepository(db, b)

// inject some invalid data to db
data := db.NewStore("chain.hdr")
var blkID thor.Bytes32
rand.Read(blkID[:])
data.Put(blkID[:], []byte("invalid data"))

// get summary should fail since the block data is not rlp encoded
_, err = repo.GetBlockSummary(blkID)
assert.NotNil(t, err)

router := mux.NewRouter()
acc := accounts.New(repo, stater, math.MaxUint64, thor.NoFork, solo.NewBFTEngine(repo))
acc.Mount(router, "/accounts")
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler())
router.Use(metricsMiddleware)
ts := httptest.NewServer(router)

httpGet(t, ts.URL+"/accounts/0x")
httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String())

_, code := httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String()+"?revision="+blkID.String())
assert.Equal(t, 500, code)

body, _ := httpGet(t, ts.URL+"/metrics")
parser := expfmt.TextParser{}
metrics, err := parser.TextToMetricFamilies(bytes.NewReader(body))
assert.Nil(t, err)

m := metrics["thor_metrics_api_request_count"].GetMetric()
assert.Equal(t, 3, len(m), "should be 3 metric entries")
assert.Equal(t, float64(1), m[0].GetCounter().GetValue())
assert.Equal(t, float64(1), m[1].GetCounter().GetValue())

labels := m[0].GetLabel()
assert.Equal(t, 3, len(labels))
assert.Equal(t, "code", labels[0].GetName())
assert.Equal(t, "200", labels[0].GetValue())
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())

labels = m[1].GetLabel()
assert.Equal(t, 3, len(labels))
assert.Equal(t, "code", labels[0].GetName())
assert.Equal(t, "400", labels[0].GetValue())
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())

labels = m[2].GetLabel()
assert.Equal(t, 3, len(labels))
assert.Equal(t, "code", labels[0].GetName())
assert.Equal(t, "500", labels[0].GetValue())
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())
}

func TestWebsocketMetrics(t *testing.T) {
db := muxdb.NewMem()
Expand Down
2 changes: 1 addition & 1 deletion api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func initChainMultipleBlocks(t *testing.T, blockCount int) (*chain.Repository, [
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(blk, receipts, 0); err != nil {
if err := repo.AddBlock(blk, receipts, 0, false); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(blk.Header().ID()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bft/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (engine *Engine) findCheckpointByQuality(target uint32, finalized, headID t
return c.GetBlockID(searchStart + uint32(num)*thor.CheckpointInterval)
}

func (engine *BFTEngine) getMaxBlockProposers(sum *chain.BlockSummary) (uint64, error) {
func (engine *Engine) getMaxBlockProposers(sum *chain.BlockSummary) (uint64, error) {
state := engine.stater.NewState(sum.Root())
params, err := builtin.Params.Native(state).Get(thor.KeyMaxBlockProposers)
if err != nil {
Expand Down
Loading

0 comments on commit ef814c0

Please sign in to comment.