Skip to content

Commit

Permalink
Merge branch 'main' into downgrade-leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre authored Oct 1, 2024
2 parents 6fd4cb5 + 97919a9 commit 0afab7e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 30 deletions.
4 changes: 1 addition & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,14 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
// The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.
// TODO: write test for me.
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool), enableDebugger bool) (
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error,
) {
conf, gen, err := MakeConfig(workingDir)
if err != nil {
return nil, nil, err
}

conf.HTTP.EnableDebugger = enableDebugger

defaultWalletPath := PactusDefaultWalletPath(workingDir)
walletInstance, err := wallet.Open(defaultWalletPath, true,
wallet.WithCustomServers([]string{conf.GRPC.Listen}))
Expand Down
4 changes: 1 addition & 3 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func buildStartCmd(parentCmd *cobra.Command) {
passwordOpt := startCmd.Flags().StringP("password", "p", "",
"the wallet password")

debuggerOpt := startCmd.Flags().BoolP("debug", "d", false, "enable pprof debugger")

startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
// change working directory
Expand Down Expand Up @@ -60,7 +58,7 @@ func buildStartCmd(parentCmd *cobra.Command) {
return password, true
}
node, _, err := cmd.StartNode(
workingDir, passwordFetcher, *debuggerOpt)
workingDir, passwordFetcher)
cmd.FatalErrorCheck(err)

cmd.TrapSignal(func() {
Expand Down
4 changes: 1 addition & 3 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const appID = "com.github.pactus-project.pactus.pactus-gui"

var (
workingDirOpt *string
debuggerOpt *bool
passwordOpt *string
testnetOpt *bool
)
Expand All @@ -34,7 +33,6 @@ func init() {
workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path")
passwordOpt = flag.String("password", "", "wallet password")
testnetOpt = flag.Bool("testnet", false, "initializing for the testnet")
debuggerOpt = flag.Bool("debug", false, "enable pprof debugger")
version.NodeAgent.AppType = "gui"
// the gtk on macos should run on main thread.
if runtime.GOOS == "darwin" {
Expand Down Expand Up @@ -157,7 +155,7 @@ func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {

return getWalletPassword(wlt)
}
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher, *debuggerOpt)
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func DefaultConfigMainnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = false
conf.HTTP.Listen = "127.0.0.1:80"
conf.HTTP.EnablePprof = false
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://127.0.0.1:40899"

Expand Down Expand Up @@ -187,6 +188,7 @@ func DefaultConfigTestnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = false
conf.HTTP.Listen = "[::]:80"
conf.HTTP.EnablePprof = false
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://[::]:40799"

Expand Down Expand Up @@ -214,6 +216,7 @@ func DefaultConfigLocalnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = true
conf.HTTP.Listen = "[::]:0"
conf.HTTP.EnablePprof = true
conf.Nanomsg.Enable = true
conf.Nanomsg.Listen = "tcp://[::]:40799"

Expand Down
5 changes: 5 additions & 0 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
# `listen` is the address to listen for incoming connections for HTTP server.
listen = "127.0.0.1:80"

# `enable_pprof` Enables Golang's pprof debugger for profiling CPU, memory, and goroutines,
# providing key performance insights. Be cautious as it exposes sensitive
# data, so enable only in secure environments with restricted access.
enable_pprof = false

# Nanomsg configuration.
[nanomsg]

Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (s *store) RecentTransaction(id tx.ID) bool {
s.lk.Lock()
defer s.lk.Unlock()

return s.txStore.RecentTransaction(id)
return s.txStore.recentTransaction(id)
}

func (s *store) HasAccount(addr crypto.Address) bool {
Expand Down
2 changes: 1 addition & 1 deletion store/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ts *txStore) pruneCache(currentHeight uint32) {
}
}

func (ts *txStore) RecentTransaction(id tx.ID) bool {
func (ts *txStore) recentTransaction(id tx.ID) bool {
return ts.txCache.Has(id)
}

Expand Down
37 changes: 26 additions & 11 deletions www/grpc/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetBlock(t *testing.T) {
assert.Empty(t, res.Txs)
})

t.Run("Should return object with (verbosity: 1)", func(t *testing.T) {
t.Run("Should return object with (verbosity: 1)", func(t *testing.T) {
res, err := client.GetBlock(context.Background(),
&pactus.GetBlockRequest{Height: height, Verbosity: pactus.BlockVerbosity_BLOCK_INFO})

Expand All @@ -64,7 +64,7 @@ func TestGetBlock(t *testing.T) {
}
})

t.Run("Should return object with (verbosity: 2)", func(t *testing.T) {
t.Run("Should return object with (verbosity: 2)", func(t *testing.T) {
res, err := client.GetBlock(context.Background(),
&pactus.GetBlockRequest{Height: height, Verbosity: pactus.BlockVerbosity_BLOCK_TRANSACTIONS})

Expand Down Expand Up @@ -335,22 +335,37 @@ func TestConsensusInfo(t *testing.T) {
td := setup(t, nil)
conn, client := td.blockchainClient(t)

v1, _ := td.GenerateTestPrepareVote(100, 2)
v2, _ := td.GenerateTestPrepareVote(100, 2)
td.consMocks[1].Active = true
td.consMocks[1].Height = 100
td.consMocks[0].AddVote(v1)
td.consMocks[1].AddVote(v2)
consHeight := td.RandHeight()
consRound := td.RandRound()
vote1, _ := td.GenerateTestPrepareVote(consHeight, consRound)
vote2, _ := td.GenerateTestPrecommitVote(consHeight, consRound)

td.consMocks[0].Active = true
td.consMocks[0].Height = consHeight
td.consMocks[0].Round = consRound
td.consMocks[0].AddVote(vote1)
td.consMocks[0].AddVote(vote2)

td.consMocks[1].Active = false
td.consMocks[1].Height = consHeight
td.consMocks[1].Round = consRound

t.Run("Should return the consensus info", func(t *testing.T) {
res, err := client.GetConsensusInfo(context.Background(), &pactus.GetConsensusInfoRequest{})

assert.NoError(t, err)
assert.NotNil(t, res)
assert.False(t, res.Instances[0].Active, true)
assert.True(t, res.Instances[1].Active, true)
assert.Equal(t, uint32(100), res.Instances[1].Height)

assert.True(t, res.Instances[0].Active)
assert.Equal(t, consHeight, res.Instances[0].Height)
assert.Equal(t, int32(consRound), res.Instances[0].Round)
assert.Len(t, res.Instances[0].Votes, 2)
assert.Equal(t, pactus.VoteType_VOTE_PREPARE, res.Instances[0].Votes[0].Type)
assert.Equal(t, pactus.VoteType_VOTE_PRECOMMIT, res.Instances[0].Votes[1].Type)

assert.False(t, res.Instances[1].Active)
assert.Equal(t, consHeight, res.Instances[1].Height)
assert.Equal(t, int32(consRound), res.Instances[1].Round)
})

assert.Nil(t, conn.Close(), "Error closing connection")
Expand Down
12 changes: 6 additions & 6 deletions www/http/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package http

type Config struct {
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnableDebugger bool `toml:"-"` // EnableDebugger is private configs
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnablePprof bool `toml:"enable_pprof"`
}

func DefaultConfig() *Config {
return &Config{
Enable: false,
Listen: "",
EnableDebugger: false,
Enable: false,
Listen: "",
EnablePprof: false,
}
}

Expand Down
13 changes: 11 additions & 2 deletions www/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type Server struct {
logger *logger.SubLogger
}

// init disables default pprof handlers registered by importing net/http/pprof.
// Your pprof is showing (https://mmcloughlin.com/posts/your-pprof-is-showing)
func init() {
http.DefaultServeMux = http.NewServeMux()
}

func NewServer(conf *Config, enableAuth bool) *Server {
ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -87,8 +93,11 @@ func (s *Server) StartServer(grpcServer string) error {
s.router.HandleFunc("/validator/number/{number}", s.GetValidatorByNumberHandler)
s.router.HandleFunc("/metrics/prometheus", promhttp.Handler().ServeHTTP)

if s.config.EnableDebugger {
s.router.HandleFunc("/debug/pprof/", pprof.Index)
if s.config.EnablePprof {
http.HandleFunc("/debug/pprof/", pprof.Index)
http.HandleFunc("/debug/pprof/profile", pprof.Profile)
http.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
http.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

if s.enableAuth {
Expand Down

0 comments on commit 0afab7e

Please sign in to comment.