Skip to content

Commit 2699472

Browse files
committed
Make work source configurable for atto program
1 parent 384bf85 commit 2699472

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

cmd/atto/config.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
var (
4-
// The node needs to support the work_generate action. See
4+
// The node that is used to interact with the nano network. See
55
// e.g. https://publicnodes.somenano.com to find public nodes
66
// or set up your own node.
77
node = "https://node.somenano.com/proxy"
@@ -10,4 +10,15 @@ var (
1010
// opening an accout, but can be changed afterwards. See e.g.
1111
// https://nanolooker.com/representatives to find representatives.
1212
defaultRepresentative = "nano_1jtx5p8141zjtukz4msp1x93st7nh475f74odj8673qqm96xczmtcnanos1o"
13+
14+
// workSource specifies where the work for block submission shall
15+
// come from. These options are available:
16+
// - workSourceLocal: The work is generated on the CPU of the
17+
// current computer.
18+
// - workSourceNode: The work is fetched from the node using the
19+
// work_generate action. Make sure that your node supports it.
20+
// - workSourceLocalFallback: It is attempted to fetch the work
21+
// from the node, but if this fails, it will be generated on
22+
// the CPU of the current computer.
23+
workSource = workSourceLocalFallback
1324
)

cmd/atto/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Environment:
5050
Authentication.
5151
`
5252

53+
type workSourceType int
54+
55+
const (
56+
workSourceLocal workSourceType = iota
57+
workSourceNode
58+
workSourceLocalFallback
59+
)
60+
5361
var accountIndexFlag uint
5462
var yFlag bool
5563

@@ -191,7 +199,7 @@ func printBalance() error {
191199
if err = block.Sign(privateKey); err != nil {
192200
return err
193201
}
194-
if err = block.FetchWork(node); err != nil {
202+
if err = fillWork(&block, node); err != nil {
195203
return err
196204
}
197205
if err = block.Submit(node); err != nil {
@@ -255,7 +263,7 @@ func changeRepresentative() error {
255263
if err = block.Sign(privateKey); err != nil {
256264
return err
257265
}
258-
if err = block.FetchWork(node); err != nil {
266+
if err = fillWork(&block, node); err != nil {
259267
return err
260268
}
261269
if err = block.Submit(node); err != nil {
@@ -296,7 +304,7 @@ func sendFunds() error {
296304
if err = block.Sign(privateKey); err != nil {
297305
return err
298306
}
299-
if err = block.FetchWork(node); err != nil {
307+
if err = fillWork(&block, node); err != nil {
300308
return err
301309
}
302310
if err = block.Submit(node); err != nil {

cmd/atto/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"runtime"
99
"strings"
10+
11+
"github.com/codesoap/atto"
1012
)
1113

1214
// getSeed returns the first line of the standard input.
@@ -61,3 +63,19 @@ func letUserVerifySend(amount, recipient string) (err error) {
6163
}
6264
return
6365
}
66+
67+
func fillWork(block *atto.Block, node string) error {
68+
switch workSource {
69+
case workSourceLocal:
70+
return block.GenerateWork()
71+
case workSourceNode:
72+
return block.FetchWork(node)
73+
case workSourceLocalFallback:
74+
if err := block.FetchWork(node); err != nil {
75+
fmt.Fprintf(os.Stderr, "Could not fetch work from node (error: %v); generating it on CPU... ", err)
76+
return block.GenerateWork()
77+
}
78+
return nil
79+
}
80+
return fmt.Errorf("unknown work source")
81+
}

0 commit comments

Comments
 (0)