From d6460bc521382d71464c0ca0686d996037acc60c Mon Sep 17 00:00:00 2001 From: Marius Scurtescu Date: Tue, 12 Mar 2024 18:29:40 -0700 Subject: [PATCH] extract version logic to config package (#107) * Modify the wrong default volumes value to /root/.config (#109) * move extract version logic to new version.go in config package * update version extraction command --------- Co-authored-by: talentbuilder --- Taskfile.yaml | 2 +- node/app/db_console.go | 4 +- node/config/version.go | 22 +++++++++ node/config/version_test.go | 47 +++++++++++++++++++ .../consensus/ceremony/broadcast_messaging.go | 8 ++-- .../ceremony_data_clock_consensus_engine.go | 8 ++-- node/consensus/ceremony/consensus_frames.go | 5 +- node/consensus/consensus_engine.go | 14 ------ node/main.go | 2 +- 9 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 node/config/version.go create mode 100644 node/config/version_test.go diff --git a/Taskfile.yaml b/Taskfile.yaml index 7dd9c36..d73c0d6 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -7,7 +7,7 @@ dotenv: vars: VERSION: - sh: cat node/main.go | grep "Quilibrium Node - v" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' + sh: cat node/config/version.go | grep -A 1 "func GetVersion() \[\]byte {" | grep -Eo '0x[0-9a-fA-F]+' | xargs printf "%d.%d.%d" PROJECT_NAME: quilibrium SERVICE_NAME: node GIT_REPO: diff --git a/node/app/db_console.go b/node/app/db_console.go index 7405843..60282d0 100644 --- a/node/app/db_console.go +++ b/node/app/db_console.go @@ -887,11 +887,11 @@ func logoVersion(width int) string { out += " ''---.. ...---'' ##\n" out += " ''----------''\n" out += " \n" - out += " Quilibrium Node - v1.4.6 – Sunset\n" + out += " Quilibrium Node - v" + config.GetVersionString() + " – Sunset\n" out += " \n" out += " DB Console\n" } else { - out = "Quilibrium Node - v1.4.6 – Sunset - DB Console\n" + out = "Quilibrium Node - v" + config.GetVersionString() + " – Sunset - DB Console\n" } return out } diff --git a/node/config/version.go b/node/config/version.go new file mode 100644 index 0000000..b4bcacb --- /dev/null +++ b/node/config/version.go @@ -0,0 +1,22 @@ +package config + +import ( + "fmt" + "time" +) + +func GetMinimumVersionCutoff() time.Time { + return time.Date(2024, time.March, 10, 5, 30, 0, 0, time.UTC) +} + +func GetMinimumVersion() []byte { + return []byte{0x01, 0x04, 0x05} +} + +func GetVersion() []byte { + return []byte{0x01, 0x04, 0x06} +} + +func GetVersionString() string { + return fmt.Sprintf("%d.%d.%d", GetVersion()[0], GetVersion()[1], GetVersion()[2]) +} diff --git a/node/config/version_test.go b/node/config/version_test.go new file mode 100644 index 0000000..ec7d3a0 --- /dev/null +++ b/node/config/version_test.go @@ -0,0 +1,47 @@ +package config + +import ( + "github.com/stretchr/testify/assert" + "regexp" + "testing" + "time" +) + +func TestGetMinimumVersion(t *testing.T) { + minVersion := GetMinimumVersion() + + assert.Equal(t, 3, len(minVersion), "Expected version to have exactly 3 bytes; got %v", len(minVersion)) + + version := GetVersion() + + assert.GreaterOrEqual(t, version[0], minVersion[0]) + + if minVersion[0] == version[0] { + assert.GreaterOrEqual(t, version[1], minVersion[1]) + + if minVersion[1] == version[1] { + assert.GreaterOrEqual(t, version[2], minVersion[2]) + } + } +} + +func TestGetVersion(t *testing.T) { + version := GetVersion() + + assert.Equal(t, 3, len(version), "Expected version to have exactly 3 bytes; got %v", len(version)) +} + +func TestGetMinimumVersionCutoff(t *testing.T) { + cutoff := GetMinimumVersionCutoff() + + assert.True(t, cutoff.Before(time.Now())) + assert.True(t, cutoff.After(time.Date(2024, time.March, 1, 0, 0, 0, 0, time.UTC))) +} + +func TestGetVersionString(t *testing.T) { + version := GetVersionString() + + versionRegexp := regexp.MustCompile("[0-9]+\\.[0-9]+\\.[0-9]+") + + assert.Regexp(t, versionRegexp, version) +} diff --git a/node/consensus/ceremony/broadcast_messaging.go b/node/consensus/ceremony/broadcast_messaging.go index 66ef533..66a6941 100644 --- a/node/consensus/ceremony/broadcast_messaging.go +++ b/node/consensus/ceremony/broadcast_messaging.go @@ -3,6 +3,7 @@ package ceremony import ( "bytes" "encoding/binary" + "source.quilibrium.com/quilibrium/monorepo/node/config" "strings" "time" @@ -15,7 +16,6 @@ import ( "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" "source.quilibrium.com/quilibrium/monorepo/go-libp2p-blossomsub/pb" - "source.quilibrium.com/quilibrium/monorepo/node/consensus" "source.quilibrium.com/quilibrium/monorepo/node/keys" "source.quilibrium.com/quilibrium/monorepo/node/protobufs" ) @@ -98,7 +98,7 @@ func (e *CeremonyDataClockConsensusEngine) handleMessage( case protobufs.ClockFrameType: e.peerMapMx.RLock() if peer, ok := e.peerMap[string(message.From)]; !ok || - bytes.Compare(peer.version, consensus.GetMinimumVersion()) < 0 { + bytes.Compare(peer.version, config.GetMinimumVersion()) < 0 { return nil } e.peerMapMx.RUnlock() @@ -179,8 +179,8 @@ func (e *CeremonyDataClockConsensusEngine) handleCeremonyPeerListAnnounce( continue } - if bytes.Compare(p.Version, consensus.GetMinimumVersion()) < 0 && - p.Timestamp > consensus.GetMinimumVersionCutoff().UnixMilli() { + if bytes.Compare(p.Version, config.GetMinimumVersion()) < 0 && + p.Timestamp > config.GetMinimumVersionCutoff().UnixMilli() { e.logger.Debug( "peer provided outdated version, penalizing app score", zap.Binary("peer_id", p.PeerId), diff --git a/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go b/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go index d67fb03..7471878 100644 --- a/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go +++ b/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go @@ -313,7 +313,7 @@ func (e *CeremonyDataClockConsensusEngine) Start() <-chan error { timestamp := time.Now().UnixMilli() msg := binary.BigEndian.AppendUint64([]byte{}, frame.FrameNumber) - msg = append(msg, consensus.GetVersion()...) + msg = append(msg, config.GetVersion()...) msg = binary.BigEndian.AppendUint64(msg, uint64(timestamp)) sig, err := e.pubSub.SignMessage(msg) if err != nil { @@ -325,7 +325,7 @@ func (e *CeremonyDataClockConsensusEngine) Start() <-chan error { peerId: e.pubSub.GetPeerID(), multiaddr: "", maxFrame: frame.FrameNumber, - version: consensus.GetVersion(), + version: config.GetVersion(), signature: sig, publicKey: e.pubSub.GetPublicKey(), timestamp: timestamp, @@ -338,7 +338,7 @@ func (e *CeremonyDataClockConsensusEngine) Start() <-chan error { PeerId: e.pubSub.GetPeerID(), Multiaddr: "", MaxFrame: frame.FrameNumber, - Version: consensus.GetVersion(), + Version: config.GetVersion(), Signature: sig, PublicKey: e.pubSub.GetPublicKey(), Timestamp: timestamp, @@ -369,7 +369,7 @@ func (e *CeremonyDataClockConsensusEngine) Start() <-chan error { PeerId: e.pubSub.GetPeerID(), Multiaddrs: []string{""}, MaxFrame: frame.FrameNumber, - Version: consensus.GetVersion(), + Version: config.GetVersion(), Signature: sig, PublicKey: e.pubSub.GetPublicKey(), Timestamp: timestamp, diff --git a/node/consensus/ceremony/consensus_frames.go b/node/consensus/ceremony/consensus_frames.go index 425a294..befd26d 100644 --- a/node/consensus/ceremony/consensus_frames.go +++ b/node/consensus/ceremony/consensus_frames.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "io" + "source.quilibrium.com/quilibrium/monorepo/node/config" "time" "github.com/pkg/errors" @@ -163,8 +164,8 @@ func (e *CeremonyDataClockConsensusEngine) GetMostAheadPeer() ( for _, v := range e.peerMap { _, ok := e.uncooperativePeersMap[string(v.peerId)] if v.maxFrame > max && - v.timestamp > consensus.GetMinimumVersionCutoff().UnixMilli() && - bytes.Compare(v.version, consensus.GetMinimumVersion()) >= 0 && !ok { + v.timestamp > config.GetMinimumVersionCutoff().UnixMilli() && + bytes.Compare(v.version, config.GetMinimumVersion()) >= 0 && !ok { peer = v.peerId max = v.maxFrame } diff --git a/node/consensus/consensus_engine.go b/node/consensus/consensus_engine.go index a4b8464..fab7cf9 100644 --- a/node/consensus/consensus_engine.go +++ b/node/consensus/consensus_engine.go @@ -2,8 +2,6 @@ package consensus import ( "crypto" - "time" - "source.quilibrium.com/quilibrium/monorepo/node/config" "source.quilibrium.com/quilibrium/monorepo/node/execution" "source.quilibrium.com/quilibrium/monorepo/node/keys" @@ -49,15 +47,3 @@ type DataConsensusEngine interface { IsInProverTrie(key []byte) bool GetPeerInfo() *protobufs.PeerInfoResponse } - -func GetMinimumVersionCutoff() time.Time { - return time.Date(2024, time.March, 10, 5, 30, 0, 0, time.UTC) -} - -func GetMinimumVersion() []byte { - return []byte{0x01, 0x04, 0x05} -} - -func GetVersion() []byte { - return []byte{0x01, 0x04, 0x06} -} diff --git a/node/main.go b/node/main.go index 8999dc0..8452675 100644 --- a/node/main.go +++ b/node/main.go @@ -491,5 +491,5 @@ func printLogo() { func printVersion() { fmt.Println(" ") - fmt.Println(" Quilibrium Node - v1.4.6 – Sunset") + fmt.Println(" Quilibrium Node - v" + config.GetVersionString() + " – Sunset") }