// Package version carries build-time identity for the node binary. // // All four variables below are overridable at link time via -ldflags -X. The // canonical build command is: // // VERSION_TAG=$(git describe --tags --always --dirty) // VERSION_COMMIT=$(git rev-parse HEAD) // VERSION_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ) // VERSION_DIRTY=$(git diff --quiet HEAD -- 2>/dev/null && echo false || echo true) // // go build -ldflags "\ // -X go-blockchain/node/version.Tag=$VERSION_TAG \ // -X go-blockchain/node/version.Commit=$VERSION_COMMIT \ // -X go-blockchain/node/version.Date=$VERSION_DATE \ // -X go-blockchain/node/version.Dirty=$VERSION_DIRTY" ./cmd/node // // Both Dockerfile and Dockerfile.slim run this at image build time (see ARG // VERSION_* + RUN go build … -ldflags lines). A naked `go build ./...` // without ldflags falls back to the zero defaults — that's fine for local // dev, just not shipped to users. // // Protocol versions come from a different source: see node.ProtocolVersion, // which is a compile-time const that only bumps on wire-protocol breaking // changes. Protocol version can be the same across many build tags. package version // Tag is a human-readable version label, typically `git describe --tags // --always --dirty`. Examples: "v0.5.1", "v0.5.1-3-gabc1234", "abc1234-dirty". // "dev" is the fallback when built without ldflags. var Tag = "dev" // Commit is the full 40-char git SHA of the HEAD at build time. "none" when // unset. Useful for precise bisect / release-channel lookups even when Tag // is a non-unique label like "dev". var Commit = "none" // Date is the build timestamp in RFC3339 (UTC). "unknown" when unset. // Exposed in /api/well-known-version so operators can see how stale a // deployed binary is without `docker exec`. var Date = "unknown" // Dirty is the string "true" if the working tree had uncommitted changes at // build time, "false" otherwise. Always a string (not bool) because ldflags // can only inject strings — parse with == "true" at call sites. var Dirty = "false" // String returns a one-line summary suitable for `node --version` output. // Format: "dchain-node (commit= date= dirty=)". func String() string { short := Commit if len(short) > 8 { short = short[:8] } return "dchain-node " + Tag + " (commit=" + short + " date=" + Date + " dirty=" + Dirty + ")" } // Info returns the version fields as a map, suitable for JSON embedding in // /api/well-known-version and similar introspection endpoints. func Info() map[string]string { return map[string]string{ "tag": Tag, "commit": Commit, "date": Date, "dirty": Dirty, } } // IsDirty reports whether the build was from an unclean working tree. // Convenience over parsing Dirty == "true" at call sites. func IsDirty() bool { return Dirty == "true" }