From 3f7f3dd37f7ba5c731098052d15a6484dc250657 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 10 Jul 2015 15:50:58 +0000 Subject: [PATCH] add git commit hash to nodeInfo --- .gitignore | 1 + Makefile | 7 ++++--- common/os.go | 6 ++++++ crawler/crawl.go | 2 +- node/node.go | 8 ++++++++ p2p/switch.go | 5 +++++ rpc/core/net.go | 4 +--- rpc/core/types/responses.go | 14 ++++++-------- rpc/test/tests.go | 4 ++-- types/node.go | 7 ++++--- 10 files changed, 38 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 36b24c7eb..e526480e6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ rpc/test/.tendermint .debora .tendermint remote_dump +.revision diff --git a/Makefile b/Makefile index 63c1240c6..88b3350b3 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ install: go install github.com/tendermint/tendermint/cmd/debora go install github.com/tendermint/tendermint/cmd/stdinwriter go install github.com/tendermint/tendermint/cmd/logjack + echo -n `git rev-parse --verify HEAD` > .revision build: go build -o build/tendermint github.com/tendermint/tendermint/cmd/tendermint @@ -42,11 +43,11 @@ gen_client: go install github.com/ebuchman/go-rpc-gen go generate rpc/core_client/*.go +revision: + echo -n `git rev-parse --verify HEAD` > .revision + tendermint_root/priv_validator.json: tendermint_root/priv_validator.json.orig cp $< $@ -economy: tendermint_root/priv_validator.json - docker run -v $(CURDIR)/tendermint_root:/tendermint_root -p 46656:46656 tendermint - clean: rm -f tendermint tendermint_root/priv_validator.json diff --git a/common/os.go b/common/os.go index 8060ba5ed..51c6d2b6a 100644 --- a/common/os.go +++ b/common/os.go @@ -5,10 +5,16 @@ import ( "io/ioutil" "os" "os/signal" + "path" "sync" "time" ) +var ( + GoPath = os.Getenv("GOPATH") + TendermintRepo = path.Join(GoPath, "src", "github.com", "tendermint", "tendermint") +) + func TrapSignal(cb func()) { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) diff --git a/crawler/crawl.go b/crawler/crawl.go index 973b27ee8..e79440305 100644 --- a/crawler/crawl.go +++ b/crawler/crawl.go @@ -50,7 +50,7 @@ func (n *Node) Address() string { // Set the basic status and chain_id info for a node from RPC responses func (n *Node) SetInfo(status *rpctypes.ResponseStatus, netinfo *rpctypes.ResponseNetInfo) { n.LastSeen = time.Now() - n.ChainID = status.ChainID + n.ChainID = status.NodeInfo.ChainID n.BlockHeight = status.LatestBlockHeight n.NetInfo = netinfo // n.Validator diff --git a/node/node.go b/node/node.go index a7688095d..9d00e4c12 100644 --- a/node/node.go +++ b/node/node.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "path" "strconv" "strings" "time" @@ -242,9 +243,16 @@ func makeNodeInfo(sw *p2p.Switch) *types.NodeInfo { Moniker: config.GetString("moniker"), Version: config.GetString("version"), } + + // include git hash in the nodeInfo if available + if rev, err := ReadFile(path.Join(TendermintRepo, ".revision")); err == nil { + nodeInfo.Revision = string(rev) + } + if !sw.IsListening() { return nodeInfo } + p2pListener := sw.Listeners()[0] p2pHost := p2pListener.ExternalAddress().IP.String() p2pPort := p2pListener.ExternalAddress().Port diff --git a/p2p/switch.go b/p2p/switch.go index f522a9a23..2702f5930 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -119,6 +119,11 @@ func (sw *Switch) SetNodeInfo(nodeInfo *types.NodeInfo) { sw.nodeInfo = nodeInfo } +// Not goroutine safe. +func (sw *Switch) NodeInfo() *types.NodeInfo { + return sw.nodeInfo +} + func (sw *Switch) Start() { if atomic.CompareAndSwapUint32(&sw.running, 0, 1) { // Start reactors diff --git a/rpc/core/net.go b/rpc/core/net.go index 203048777..ef0cc8344 100644 --- a/rpc/core/net.go +++ b/rpc/core/net.go @@ -31,9 +31,7 @@ func Status() (*ctypes.ResponseStatus, error) { } return &ctypes.ResponseStatus{ - Moniker: config.GetString("moniker"), - ChainID: config.GetString("chain_id"), - Version: config.GetString("version"), + NodeInfo: p2pSwitch.NodeInfo(), GenesisHash: genesisHash, PubKey: privValidator.PubKey, LatestBlockHash: latestBlockHash, diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 02dc267dc..b6c294511 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -49,14 +49,12 @@ type Receipt struct { } type ResponseStatus struct { - Moniker string `json:"moniker"` - ChainID string `json:"chain_id"` - Version string `json:"version"` - GenesisHash []byte `json:"genesis_hash"` - PubKey account.PubKey `json:"pub_key"` - LatestBlockHash []byte `json:"latest_block_hash"` - LatestBlockHeight int `json:"latest_block_height"` - LatestBlockTime int64 `json:"latest_block_time"` // nano + NodeInfo *types.NodeInfo `json:"node_info"` + GenesisHash []byte `json:"genesis_hash"` + PubKey account.PubKey `json:"pub_key"` + LatestBlockHash []byte `json:"latest_block_hash"` + LatestBlockHeight int `json:"latest_block_height"` + LatestBlockTime int64 `json:"latest_block_time"` // nano } type ResponseNetInfo struct { diff --git a/rpc/test/tests.go b/rpc/test/tests.go index 3dbd17d5a..f335fd3ea 100644 --- a/rpc/test/tests.go +++ b/rpc/test/tests.go @@ -16,9 +16,9 @@ func testStatus(t *testing.T, typ string) { if err != nil { t.Fatal(err) } - if resp.ChainID != chainID { + if resp.NodeInfo.ChainID != chainID { t.Fatal(fmt.Errorf("ChainID mismatch: got %s expected %s", - resp.ChainID, chainID)) + resp.NodeInfo.ChainID, chainID)) } } diff --git a/types/node.go b/types/node.go index 8cb1255a0..b13506f78 100644 --- a/types/node.go +++ b/types/node.go @@ -6,9 +6,10 @@ import ( ) type NodeInfo struct { - Moniker string `json:"moniker"` - ChainID string `json:"chain_id"` - Version string `json:"version"` + Moniker string `json:"moniker"` + ChainID string `json:"chain_id"` + Version string `json:"version"` + Revision string `json:"revision"` Host string `json:"host"` P2PPort uint16 `json:"p2p_port"`