forked from soteria-dag/soterd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
103 lines (84 loc) · 3.29 KB
/
dag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2018-2019 The Soteria DAG developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpcclient
import (
"encoding/json"
"github.com/totaloutput/soterd/soterjson"
)
// FutureGetDAGTipsResult is a promise to deliver the result of a
// GetDAGTipsAsync RPC invocation (or an applicable error).
type FutureGetDAGTipsResult chan *response
// Receive waits for the response promised by the future and returns chain info
// result provided by the server.
func (r FutureGetDAGTipsResult) Receive() (*soterjson.GetDAGTipsResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
var dagSnapshot soterjson.GetDAGTipsResult
if err := json.Unmarshal(res, &dagSnapshot); err != nil {
return nil, err
}
return &dagSnapshot, nil
}
// GetDAGTipsAsync returns an instance of a type that can be used to get
// the result of the RPC at some future time by invoking the Receive function
// on the returned instance.
//
// See GetDAGTips for the blocking version and more details.
func (c *Client) GetDAGTipsAsync() FutureGetDAGTipsResult {
cmd := soterjson.NewGetDAGTipsCmd()
return c.sendCmd(cmd)
}
// GetDAGTips returns information about the tip of the block DAG
func (c *Client) GetDAGTips() (*soterjson.GetDAGTipsResult, error) {
return c.GetDAGTipsAsync().Receive()
}
// FutureRenderDagResult is a promise to deliver the result of a RenderDagAsync RPC invocation (or error).
type FutureRenderDagResult chan *response
// Receive waits for the response promised by the future and returns rendered dag result provided by the RPC server.
func (r FutureRenderDagResult) Receive() (*soterjson.RenderDagResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
var renderedDag soterjson.RenderDagResult
if err := json.Unmarshal(res, &renderedDag); err != nil {
return nil, err
}
return &renderedDag, nil
}
// RenderDagAsync returns a promise that can be used to retrieve the result of the RPC call at some future time by
// invoking the Receive function on the promise.
func (c *Client) RenderDagAsync() FutureRenderDagResult {
cmd := soterjson.NewRenderDagCmd()
return c.sendCmd(cmd)
}
// RenderDag returns a dag rendered in graphviz DOT file format.
func (c *Client) RenderDag() (*soterjson.RenderDagResult, error) {
return c.RenderDagAsync().Receive()
}
// FutureGetDAGColoringResult is a promise to deliver the result of a GetDAGColoringAsync RPC invocation (or error).
type FutureGetDAGColoringResult chan *response
// Receive waits for the response promised by the future and returns the DAG coloring provided by the RPC server.
func (r FutureGetDAGColoringResult) Receive() ([]*soterjson.GetDAGColoringResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
var dagColoring []*soterjson.GetDAGColoringResult
if err := json.Unmarshal(res, &dagColoring); err != nil {
return nil, err
}
return dagColoring, nil
}
// GetDAGColoringAsync is the async version of GetDAGColoring.
func (c *Client) GetDAGColoringAsync() FutureGetDAGColoringResult {
cmd := soterjson.NewGetDAGColoringCmd()
return c.sendCmd(cmd)
}
// GetDAGColoring returns the coloring of the block DAG
func (c *Client) GetDAGColoring() ([]*soterjson.GetDAGColoringResult, error) {
return c.GetDAGColoringAsync().Receive()
}