Skip to content

Commit 326349b

Browse files
committed
Make work source configurable for atto-safesign program
1 parent 2699472 commit 326349b

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

cmd/atto-safesign/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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-safesign/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ Environment:
5757
Authentication.
5858
`
5959

60+
type workSourceType int
61+
62+
const (
63+
workSourceLocal workSourceType = iota
64+
workSourceNode
65+
workSourceLocalFallback
66+
)
67+
6068
var accountIndexFlag uint
6169
var yFlag bool
6270

@@ -153,7 +161,7 @@ func receive() error {
153161
if err != nil {
154162
return err
155163
}
156-
if err = block.FetchWork(node); err != nil {
164+
if err = fillWork(&block, node); err != nil {
157165
return err
158166
}
159167
blockJSON, err := json.Marshal(block)
@@ -186,7 +194,7 @@ func change() error {
186194
if err != nil {
187195
return err
188196
}
189-
if err = block.FetchWork(node); err != nil {
197+
if err = fillWork(&block, node); err != nil {
190198
return err
191199
}
192200
blockJSON, err := json.Marshal(block)
@@ -215,7 +223,7 @@ func send() error {
215223
if err != nil {
216224
return err
217225
}
218-
if err = block.FetchWork(node); err != nil {
226+
if err = fillWork(&block, node); err != nil {
219227
return err
220228
}
221229
blockJSON, err := json.Marshal(block)

cmd/atto-safesign/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,19 @@ func letUserVerifyBlock(block atto.Block) (err error) {
136136
}
137137
return
138138
}
139+
140+
func fillWork(block *atto.Block, node string) error {
141+
switch workSource {
142+
case workSourceLocal:
143+
return block.GenerateWork()
144+
case workSourceNode:
145+
return block.FetchWork(node)
146+
case workSourceLocalFallback:
147+
if err := block.FetchWork(node); err != nil {
148+
fmt.Fprintf(os.Stderr, "Could not fetch work from node (error: %v); generating it on CPU...\n", err)
149+
return block.GenerateWork()
150+
}
151+
return nil
152+
}
153+
return fmt.Errorf("unknown work source")
154+
}

0 commit comments

Comments
 (0)