generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from BuxOrg/chore-431-miners-apis-config
chore(BUX-431) Configure & select `mapi` or `arc` with arc-gorillapool as default
- Loading branch information
Showing
9 changed files
with
243 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
broadcastclient "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client" | ||
"github.com/tonicpow/go-minercraft/v2" | ||
) | ||
|
||
// NodesProtocol is the protocol/api_type used to communicate with the miners | ||
type NodesProtocol string | ||
|
||
const ( | ||
// NodesProtocolMapi represents the mapi protocol provided by minercraft | ||
NodesProtocolMapi NodesProtocol = "mapi" | ||
|
||
// NodesProtocolArc represents the arc protocol provided by go-broadcast-client | ||
NodesProtocolArc NodesProtocol = "arc" | ||
) | ||
|
||
// Validate whether the protocol is known | ||
func (n NodesProtocol) Validate() error { | ||
switch n { | ||
case NodesProtocolMapi, NodesProtocolArc: | ||
return nil | ||
default: | ||
return errors.New("invalid nodes protocol") | ||
} | ||
} | ||
|
||
func (nodes *NodesConfig) toMinercraftMapi() []*minercraft.MinerAPIs { | ||
minerApis := []*minercraft.MinerAPIs{} | ||
if nodes.Apis != nil { | ||
for _, api := range nodes.Apis { | ||
if api.MapiURL == "" { | ||
continue | ||
} | ||
minerApis = append(minerApis, &minercraft.MinerAPIs{ | ||
MinerID: api.MinerID, | ||
APIs: []minercraft.API{ | ||
{ | ||
Token: api.Token, | ||
URL: api.MapiURL, | ||
Type: minercraft.MAPI, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
return minerApis | ||
} | ||
|
||
func (nodes *NodesConfig) toBroadcastClientArc() []*broadcastclient.ArcClientConfig { | ||
minerApis := []*broadcastclient.ArcClientConfig{} | ||
if nodes.Apis != nil { | ||
for _, cfg := range nodes.Apis { | ||
if cfg.ArcURL == "" { | ||
continue | ||
} | ||
minerApis = append(minerApis, &broadcastclient.ArcClientConfig{ | ||
Token: cfg.Token, | ||
APIUrl: cfg.ArcURL, | ||
}) | ||
} | ||
} | ||
return minerApis | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"slices" | ||
) | ||
|
||
// Validate checks the configuration for specific rules | ||
func (n *NodesConfig) Validate() error { | ||
if n == nil { | ||
return errors.New("nodes are not configured") | ||
} | ||
|
||
err := n.Protocol.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if n.Apis == nil || len(n.Apis) == 0 { | ||
return errors.New("no miner apis configured") | ||
} | ||
|
||
// check if at least one mapi url is configured | ||
if n.Protocol == NodesProtocolMapi { | ||
found := slices.IndexFunc(n.Apis, func(el *MinerAPI) bool { | ||
return el.MapiURL != "" | ||
}) | ||
if found == -1 { | ||
return errors.New("no mapi urls configured") | ||
} | ||
} | ||
|
||
// check if at least one arc url is configured | ||
if n.Protocol == NodesProtocolArc { | ||
found := slices.IndexFunc(n.Apis, func(el *MinerAPI) bool { | ||
return el.ArcURL != "" | ||
}) | ||
if found == -1 { | ||
return errors.New("no arc urls configured") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestNewRelicConfig_Validate will test the method Validate() | ||
func TestNodesConfig_Validate(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("valid default nodes config", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
assert.NoError(t, n.Validate()) | ||
}) | ||
|
||
t.Run("wrong protocol", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
n.Protocol = "wrong" | ||
assert.Error(t, n.Validate()) | ||
}) | ||
|
||
t.Run("empty list of apis", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
|
||
n.Apis = nil | ||
assert.Error(t, n.Validate()) | ||
|
||
n.Apis = []*MinerAPI{} | ||
assert.Error(t, n.Validate()) | ||
}) | ||
|
||
t.Run("no mapi url", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
|
||
n.Apis = []*MinerAPI{ | ||
{ | ||
MapiURL: "", | ||
}, | ||
} | ||
assert.Error(t, n.Validate()) | ||
}) | ||
|
||
t.Run("no mapi url", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
|
||
n.Protocol = NodesProtocolMapi | ||
n.Apis[0].MapiURL = "" | ||
assert.Error(t, n.Validate()) | ||
}) | ||
|
||
t.Run("no arc url", func(t *testing.T) { | ||
n := getNodesDefaults() | ||
|
||
n.Protocol = NodesProtocolArc | ||
n.Apis[0].ArcURL = "" | ||
assert.Error(t, n.Validate()) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.