From 822eb972b429bce58acdfba5e0d85e9aec9c88ce Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Mon, 9 Dec 2024 14:28:35 +0000 Subject: [PATCH 01/11] chore: fixed vulnerability --- chain/repository.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/repository.go b/chain/repository.go index df622bd30..631591f8c 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -177,6 +177,7 @@ func (r *Repository) saveBlock(block *block.Block, receipts tx.Receipts, conflic } r.caches.txs.Add(string(keyBuf), tx) } + metricTransactionRepositoryCounter().AddWithLabel(int64(len(txs)), map[string]string{"type": "write", "target": "db"}) // save receipts for i, receipt := range receipts { From a751385061f12ce9f2c43a556b7319b579194c4e Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Mon, 9 Dec 2024 14:31:18 +0000 Subject: [PATCH 02/11] chore: fixed vulnerability --- state/state.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/state/state.go b/state/state.go index 5312e297c..ef23db60a 100644 --- a/state/state.go +++ b/state/state.go @@ -538,6 +538,8 @@ func (s *State) Stage(newVer trie.Version) (*Stage, error) { return err } } + // Just once for the account trie. + metricAccountCounter().AddWithLabel(int64(len(changes)), map[string]string{"type": "write", "target": "trie"}) return nil }, }, nil From e5df4aaa415944b1227ac581e061cb0016ea7ec8 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:00:19 +0000 Subject: [PATCH 03/11] first commit --- chain/cache.go | 1 + chain/chain.go | 2 ++ chain/metrics.go | 3 +++ chain/persist.go | 1 + chain/repository.go | 1 + metrics/prometheus.go | 12 ++++++------ state/metrics.go | 10 ++++++++++ 7 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 state/metrics.go diff --git a/chain/cache.go b/chain/cache.go index c1e9aae06..2b995491b 100644 --- a/chain/cache.go +++ b/chain/cache.go @@ -20,6 +20,7 @@ func newCache(maxSize int) *cache { func (c *cache) GetOrLoad(key interface{}, load func() (interface{}, error)) (interface{}, error) { if value, ok := c.Get(key); ok { + metricBlockRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "cache"}) return value, nil } value, err := load() diff --git a/chain/chain.go b/chain/chain.go index c7e30d9e9..107665cd8 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -241,6 +241,7 @@ func (c *Chain) GetTransaction(id thor.Bytes32) (*tx.Transaction, *TxMeta, error if err != nil { return nil, nil, err } + metricTransactionRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read"}) return tx, txMeta, nil } @@ -256,6 +257,7 @@ func (c *Chain) GetTransactionReceipt(txID thor.Bytes32) (*tx.Receipt, error) { if err != nil { return nil, err } + metricReceiptRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read"}) return receipt, nil } diff --git a/chain/metrics.go b/chain/metrics.go index 8c9a764d4..2e2a45d4c 100644 --- a/chain/metrics.go +++ b/chain/metrics.go @@ -9,4 +9,7 @@ import "github.com/vechain/thor/v2/metrics" var ( metricCacheHitMiss = metrics.LazyLoadCounterVec("repo_cache_hit_miss_count", []string{"type", "event"}) + metricBlockRepositoryCounter = metrics.LazyLoadCounterVec("block_repository_count", []string{"type", "target"}) + metricTransactionRepositoryCounter = metrics.LazyLoadCounterVec("transaction_repository_count", []string{"type", "target"}) + metricReceiptRepositoryCounter = metrics.LazyLoadCounterVec("receipt_repository_count", []string{"type", "target"}) ) diff --git a/chain/persist.go b/chain/persist.go index 0a73b98ac..7b9e8e75c 100644 --- a/chain/persist.go +++ b/chain/persist.go @@ -90,5 +90,6 @@ func loadBlockSummary(r kv.Getter, id thor.Bytes32) (*BlockSummary, error) { if err := loadRLP(r, id[:], &summary); err != nil { return nil, err } + metricBlockRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "db"}) return &summary, nil } diff --git a/chain/repository.go b/chain/repository.go index 631591f8c..cd0997fd3 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -187,6 +187,7 @@ func (r *Repository) saveBlock(block *block.Block, receipts tx.Receipts, conflic } r.caches.receipts.Add(string(keyBuf), receipt) } + metricReceiptRepositoryCounter().AddWithLabel(int64(len(receipts)), map[string]string{"type": "write", "target": "db"}) } if err := indexChainHead(headPutter, header); err != nil { return nil, err diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 15447f6dc..b048189ea 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -204,7 +204,7 @@ func (o *prometheusMetrics) newHistogramMeter(name string, buckets []int64) Hist err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promHistogramMeter{ @@ -237,7 +237,7 @@ func (o *prometheusMetrics) newHistogramVecMeter(name string, labels []string, b err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promHistogramVecMeter{ @@ -263,7 +263,7 @@ func (o *prometheusMetrics) newCountMeter(name string) CountMeter { err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promCountMeter{ counter: meter, @@ -281,7 +281,7 @@ func (o *prometheusMetrics) newCountVecMeter(name string, labels []string) Count err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promCountVecMeter{ counter: meter, @@ -298,7 +298,7 @@ func (o *prometheusMetrics) newGaugeMeter(name string) GaugeMeter { err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promGaugeMeter{ gauge: meter, @@ -316,7 +316,7 @@ func (o *prometheusMetrics) newGaugeVecMeter(name string, labels []string) Gauge err := prometheus.Register(meter) if err != nil { - logger.Warn("unable to register metric", "err", err) + logger.Warn(fmt.Sprintf("unable to register metric %s", name), "err", err) } return &promGaugeVecMeter{ gauge: meter, diff --git a/state/metrics.go b/state/metrics.go new file mode 100644 index 000000000..6ddfd4af7 --- /dev/null +++ b/state/metrics.go @@ -0,0 +1,10 @@ +// Copyright (c) 2024 The VeChainThor developers +// +// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying +// file LICENSE or + +package state + +import "github.com/vechain/thor/v2/metrics" + +var metricAccountCounter = metrics.LazyLoadCounterVec("account_state_count", []string{"type", "target"}) \ No newline at end of file From d1ddff0a7e19c55c044fce9ee2297f9d80b51dd5 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:02:39 +0000 Subject: [PATCH 04/11] second commit --- chain/repository.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/repository.go b/chain/repository.go index cd0997fd3..c8a1bce14 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -336,6 +336,7 @@ func (r *Repository) GetBlockTransactions(id thor.Bytes32) (tx.Transactions, err return nil, err } } + metricTransactionRepositoryCounter().AddWithLabel(int64(n), map[string]string{"type": "read"}) return txs, nil } return nil, nil From f801be24c2d43edf6e3abd96d2fcb8ac5d7b8e5e Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:05:22 +0000 Subject: [PATCH 05/11] added receipt --- chain/repository.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/repository.go b/chain/repository.go index c8a1bce14..62b440fd2 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -393,6 +393,7 @@ func (r *Repository) GetBlockReceipts(id thor.Bytes32) (tx.Receipts, error) { return nil, err } } + metricReceiptRepositoryCounter().AddWithLabel(int64(n), map[string]string{"type": "read", "target": "db"}) return receipts, nil } return nil, nil From 4bb278abcb4395bb070a1b7d27c72e8cfaceeb50 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:06:51 +0000 Subject: [PATCH 06/11] added receipt --- state/state.go | 1 + 1 file changed, 1 insertion(+) diff --git a/state/state.go b/state/state.go index ef23db60a..e63a67154 100644 --- a/state/state.go +++ b/state/state.go @@ -120,6 +120,7 @@ func (s *State) getCachedObject(addr thor.Address) (*cachedObject, error) { if err != nil { return nil, err } + metricAccountCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "trie"}) co := newCachedObject(s.db, addr, a, am) s.cache[addr] = co return co, nil From 8d1488a67df14894be02802b365b061faf8bcea0 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:07:23 +0000 Subject: [PATCH 07/11] state commit --- state/state.go | 1 + 1 file changed, 1 insertion(+) diff --git a/state/state.go b/state/state.go index e63a67154..ef81f6a16 100644 --- a/state/state.go +++ b/state/state.go @@ -132,6 +132,7 @@ func (s *State) getAccount(addr thor.Address) (*Account, error) { if err != nil { return nil, err } + metricAccountCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "stackedmap"}) return v.(*Account), nil } From 64b9219f66c91e1c1b3d71fe73c22592935066b8 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Date: Tue, 10 Dec 2024 09:30:54 +0000 Subject: [PATCH 08/11] fix: Get Devnet ID after the Thor flags are set (#915) * first commit * using bytes32 method --- cmd/thor/utils.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/thor/utils.go b/cmd/thor/utils.go index 3be6d4186..561b1276a 100644 --- a/cmd/thor/utils.go +++ b/cmd/thor/utils.go @@ -57,7 +57,7 @@ import ( "gopkg.in/urfave/cli.v1" ) -var devNetGenesisID = genesis.NewDevnet().ID() +var devNetGenesisID thor.Bytes32 func initLogger(lvl int, jsonLogs bool) *slog.LevelVar { logLevel := log.FromLegacyLevel(lvl) @@ -630,6 +630,13 @@ func printStartupMessage1( ) } +func getOrCreateDevnetID() thor.Bytes32 { + if devNetGenesisID.IsZero() { + devNetGenesisID = genesis.NewDevnet().ID() + } + return devNetGenesisID +} + func printStartupMessage2( gene *genesis.Genesis, apiURL string, @@ -668,7 +675,7 @@ func printStartupMessage2( }(), func() string { // print default dev net's dev accounts info - if gene.ID() == devNetGenesisID { + if gene.ID() == getOrCreateDevnetID() { return ` ┌──────────────────┬───────────────────────────────────────────────────────────────────────────────┐ │ Mnemonic Words │ denial kitchen pet squirrel other broom bar gas better priority spoil cross │ From 98228a590455a0122a223591e4504320db9b9380 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:22:42 +0000 Subject: [PATCH 09/11] added more dashboard --- chain/repository.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/repository.go b/chain/repository.go index 62b440fd2..ed959faae 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -231,6 +231,7 @@ func (r *Repository) AddBlock(newBlock *block.Block, receipts tx.Receipts, confl if _, err := r.saveBlock(newBlock, receipts, conflicts, asBest); err != nil { return err } + metricBlockRepositoryCounter().AddWithLabel(1, map[string]string{"type": "write", "target": "db"}) return nil } From 2b7e90988e9b6fe68ae327649f7af80376778ae5 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Tue, 10 Dec 2024 11:34:51 +0000 Subject: [PATCH 10/11] added target --- chain/chain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chain/chain.go b/chain/chain.go index 107665cd8..52a8bc0cd 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -241,7 +241,7 @@ func (c *Chain) GetTransaction(id thor.Bytes32) (*tx.Transaction, *TxMeta, error if err != nil { return nil, nil, err } - metricTransactionRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read"}) + metricTransactionRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "db"}) return tx, txMeta, nil } @@ -257,7 +257,7 @@ func (c *Chain) GetTransactionReceipt(txID thor.Bytes32) (*tx.Receipt, error) { if err != nil { return nil, err } - metricReceiptRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read"}) + metricReceiptRepositoryCounter().AddWithLabel(1, map[string]string{"type": "read", "target": "db"}) return receipt, nil } From 6dd54f7d42082b99f6d74db0495fa33f5300f35e Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Mon, 13 Jan 2025 15:46:25 +0000 Subject: [PATCH 11/11] linter --- chain/metrics.go | 6 +++--- state/metrics.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/chain/metrics.go b/chain/metrics.go index f0684e073..6b17fd913 100644 --- a/chain/metrics.go +++ b/chain/metrics.go @@ -8,8 +8,8 @@ package chain import "github.com/vechain/thor/v2/metrics" var ( - metricCacheHitMiss = metrics.LazyLoadGaugeVec("repo_cache_hit_miss_count", []string{"type", "event"}) - metricBlockRepositoryCounter = metrics.LazyLoadCounterVec("block_repository_count", []string{"type", "target"}) + metricCacheHitMiss = metrics.LazyLoadGaugeVec("repo_cache_hit_miss_count", []string{"type", "event"}) + metricBlockRepositoryCounter = metrics.LazyLoadCounterVec("block_repository_count", []string{"type", "target"}) metricTransactionRepositoryCounter = metrics.LazyLoadCounterVec("transaction_repository_count", []string{"type", "target"}) - metricReceiptRepositoryCounter = metrics.LazyLoadCounterVec("receipt_repository_count", []string{"type", "target"}) + metricReceiptRepositoryCounter = metrics.LazyLoadCounterVec("receipt_repository_count", []string{"type", "target"}) ) diff --git a/state/metrics.go b/state/metrics.go index 6ddfd4af7..b40edd98c 100644 --- a/state/metrics.go +++ b/state/metrics.go @@ -7,4 +7,4 @@ package state import "github.com/vechain/thor/v2/metrics" -var metricAccountCounter = metrics.LazyLoadCounterVec("account_state_count", []string{"type", "target"}) \ No newline at end of file +var metricAccountCounter = metrics.LazyLoadCounterVec("account_state_count", []string{"type", "target"})