From 71a5b5fc4b1e8a652f1bc8ca7c04eacaa289faf3 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 18 Jun 2023 21:12:18 +0300 Subject: can specify my id --- internal/vote/vote.go | 12 +++++------- internal/vote/vote_test.go | 29 ++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) (limited to 'internal/vote') diff --git a/internal/vote/vote.go b/internal/vote/vote.go index 6fa2510..5378655 100644 --- a/internal/vote/vote.go +++ b/internal/vote/vote.go @@ -2,8 +2,9 @@ package vote import ( "encoding/json" - "os" "time" + + "codeberg.org/snonux/gorum/internal/config" ) const Expiry = 11 * time.Second @@ -14,15 +15,12 @@ type Vote struct { ExpiresAt time.Time `json:"-"` } -func New(ids []string) (Vote, error) { +func New(conf config.Config, ids []string) (Vote, error) { var v Vote - hostname, err := os.Hostname() - if err != nil { - return v, err - } - v.FromID = hostname + v.FromID = conf.MyID v.IDs = ids + return v, nil } diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index cccb1be..b5480b9 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -1,18 +1,23 @@ package vote import ( - "os" "testing" "time" + + "codeberg.org/snonux/gorum/internal/config" ) func TestVote(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) - hostname, _ := os.Hostname() - if v.FromID != hostname { - t.Errorf("Expected vote to come from earth but came from %s", v.FromID) + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) + + if v.FromID != "foo.zone" { + t.Errorf("Expected vote to come from foo.zone but came from %s", v.FromID) } if len(v.IDs) != 4 { @@ -30,7 +35,12 @@ func TestVote(t *testing.T) { func TestVoteExpiry(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) + + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) // Set expiry 1h into the future v.ExpiresAt = time.Now().Add(1 * time.Hour) @@ -47,7 +57,12 @@ func TestVoteExpiry(t *testing.T) { func TestMarshalling(t *testing.T) { t.Parallel() - v, _ := New([]string{"foo", "bar", "baz", "bay"}) + + conf := config.Config{ + MyID: "foo.zone", + } + + v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) jsonStr, err := v.ToJSON() if err != nil { t.Errorf("unable to serialize vote to json: %v", err) -- cgit v1.2.3