forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs_transact.go
217 lines (178 loc) · 5.11 KB
/
jobs_transact.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package jobs
import (
"encoding/csv"
"fmt"
"io"
"os"
"github.com/hyperledger/burrow/txs/payload"
"github.com/hyperledger/burrow/deploy/def"
"github.com/hyperledger/burrow/deploy/util"
log "github.com/sirupsen/logrus"
)
func FormulateSendJob(send *def.Send, account string, client *def.Client) (*payload.SendTx, error) {
// Use Default
send.Source = useDefault(send.Source, account)
// Formulate tx
log.WithFields(log.Fields{
"source": send.Source,
"destination": send.Destination,
"amount": send.Amount,
}).Info("Sending Transaction")
return client.Send(&def.SendArg{
Input: send.Source,
Output: send.Destination,
Amount: send.Amount,
Sequence: send.Sequence,
})
}
func SendJob(send *def.Send, tx *payload.SendTx, account string, client *def.Client) (string, error) {
// Sign, broadcast, display
txe, err := client.SignAndBroadcast(tx)
if err != nil {
return "", util.ChainErrorHandler(account, err)
}
util.ReadTxSignAndBroadcast(txe, err)
if err != nil {
return "", err
}
return txe.Receipt.TxHash.String(), nil
}
func FormulateRegisterNameJob(name *def.RegisterName, do *def.DeployArgs, account string, client *def.Client) ([]*payload.NameTx, error) {
txs := make([]*payload.NameTx, 0)
// If a data file is given it should be in csv format and
// it will be read first. Once the file is parsed and sent
// to the chain then a single nameRegTx will be sent if that
// has been populated.
if name.DataFile != "" {
// open the file and use a reader
fileReader, err := os.Open(name.DataFile)
if err != nil {
return nil, err
}
defer fileReader.Close()
r := csv.NewReader(fileReader)
// loop through the records
for {
// Read the record
record, err := r.Read()
// Catch the errors
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// Sink the Amount into the third slot in the record if
// it doesn't exist
if len(record) <= 2 {
record = append(record, name.Amount)
}
// Send an individual Tx for the record
// [TODO]: move these to async using goroutines?
r, err := registerNameTx(&def.RegisterName{
Source: name.Source,
Name: record[0],
Data: record[1],
Amount: record[2],
Fee: name.Fee,
Sequence: name.Sequence,
}, do, account, client)
if err != nil {
return nil, err
}
txs = append(txs, r)
n := fmt.Sprintf("%s:%s", record[0], record[1])
// TODO: write smarter
if err = WriteJobResultCSV(n, r.String()); err != nil {
return nil, err
}
}
}
// If the data field is populated then there is a single
// nameRegTx to send. So do that *now*.
if name.Data != "" {
tx, err := registerNameTx(name, do, account, client)
if err != nil {
return nil, err
}
txs = append(txs, tx)
}
if len(txs) == 0 {
return nil, fmt.Errorf("nothing to do")
}
return txs, nil
}
// Runs an individual nametx.
func registerNameTx(name *def.RegisterName, do *def.DeployArgs, account string, client *def.Client) (*payload.NameTx, error) {
// Set Defaults
name.Source = useDefault(name.Source, account)
name.Fee = useDefault(name.Fee, do.DefaultFee)
name.Amount = useDefault(name.Amount, do.DefaultAmount)
// Formulate tx
log.WithFields(log.Fields{
"name": name.Name,
"data": name.Data,
"amount": name.Amount,
}).Info("NameReg Transaction")
return client.Name(&def.NameArg{
Input: name.Source,
Sequence: name.Sequence,
Name: name.Name,
Amount: name.Amount,
Data: name.Data,
Fee: name.Fee,
})
}
func RegisterNameJob(name *def.RegisterName, do *def.DeployArgs, script *def.Playbook, txs []*payload.NameTx, client *def.Client) (string, error) {
var result string
for _, tx := range txs {
// Sign, broadcast, display
txe, err := client.SignAndBroadcast(tx)
if err != nil {
return "", util.ChainErrorHandler(script.Account, err)
}
util.ReadTxSignAndBroadcast(txe, err)
if err != nil {
return "", err
}
result = txe.Receipt.TxHash.String()
}
return result, nil
}
func FormulatePermissionJob(perm *def.Permission, account string, client *def.Client) (*payload.PermsTx, error) {
// Set defaults
perm.Source = useDefault(perm.Source, account)
log.Debug("Target: ", perm.Target)
log.Debug("Marmots Deny: ", perm.Role)
log.Debug("Action: ", perm.Action)
// Populate the transaction appropriately
// Formulate tx
return client.Permissions(&def.PermArg{
Input: perm.Source,
Sequence: perm.Sequence,
Action: perm.Action,
Target: perm.Target,
Permission: perm.Permission,
Role: perm.Role,
Value: perm.Value,
})
}
func PermissionJob(perm *def.Permission, account string, tx *payload.PermsTx, client *def.Client) (string, error) {
log.Debug("What are the args returned in transaction: ", tx.PermArgs)
// Sign, broadcast, display
txe, err := client.SignAndBroadcast(tx)
if err != nil {
return "", util.ChainErrorHandler(account, err)
}
util.ReadTxSignAndBroadcast(txe, err)
if err != nil {
return "", err
}
return txe.Receipt.TxHash.String(), nil
}
func useDefault(thisOne, defaultOne string) string {
if thisOne == "" {
return defaultOne
}
return thisOne
}