-
Notifications
You must be signed in to change notification settings - Fork 61
/
transaction.go
247 lines (190 loc) · 7.31 KB
/
transaction.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package hedera
import (
"bytes"
"math"
"math/rand"
"time"
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/proto"
)
// Transaction contains the protobuf of a prepared transaction which can be signed and executed.
type Transaction struct {
pb *proto.Transaction
id TransactionID
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (transaction *Transaction) UnmarshalBinary(txBytes []byte) error {
transaction.pb = new(proto.Transaction)
if err := protobuf.Unmarshal(txBytes, transaction.pb); err != nil {
return err
}
var txBody proto.TransactionBody
if err := protobuf.Unmarshal(transaction.pb.GetBodyBytes(), &txBody); err != nil {
return err
}
transaction.id = transactionIDFromProto(txBody.TransactionID)
return nil
}
// Sign uses the provided privateKey to sign the transaction.
func (transaction Transaction) Sign(privateKey Ed25519PrivateKey) Transaction {
return transaction.SignWith(privateKey.PublicKey(), privateKey.Sign)
}
func (transaction Transaction) signWithOperator(operator operator) Transaction {
// If the transaction is not signed by the operator, we need
// to sign the transaction with the operator
var signedByOperator bool
operatorPublicKey := operator.publicKey.keyData
for _, sigPair := range transaction.pb.SigMap.SigPair {
if bytes.Equal(sigPair.PubKeyPrefix, operatorPublicKey) {
signedByOperator = true
break
}
}
if !signedByOperator {
if operator.privateKey != nil {
transaction.Sign(*operator.privateKey)
} else {
transaction.SignWith(operator.publicKey, operator.signer)
}
}
return transaction
}
// SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
// with the publicKey as the map key.
func (transaction Transaction) SignWith(publicKey Ed25519PublicKey, signer TransactionSigner) Transaction {
signature := signer(transaction.pb.GetBodyBytes())
transaction.pb.SigMap.SigPair = append(transaction.pb.SigMap.SigPair, &proto.SignaturePair{
PubKeyPrefix: publicKey.keyData,
Signature: &proto.SignaturePair_Ed25519{Ed25519: signature},
})
return transaction
}
func (transaction Transaction) executeForResponse(client *Client) (TransactionID, *proto.TransactionResponse, error) {
if client.operator != nil {
transaction.signWithOperator(*client.operator)
}
transactionBody := transaction.body()
id := transactionIDFromProto(transactionBody.TransactionID)
nodeAccountID := accountIDFromProto(transactionBody.NodeAccountID)
node := client.node(nodeAccountID)
if node == nil {
return id, nil, newErrLocalValidationf("NodeAccountID %v not found on Client", nodeAccountID)
}
methodName, err := getMethodName(transaction.body())
if err != nil {
return id, nil, err
}
validUntil := time.Now().Add(time.Duration(transactionBody.TransactionValidDuration.Seconds) * time.Second)
resp := new(proto.TransactionResponse)
for attempt := 0; true; attempt++ {
if attempt > 0 && time.Now().After(validUntil) {
// Timed out
break
}
if attempt > 0 {
// After the first attempt, start an exponentially increasing delay
delay := 500.0 * rand.Float64() * ((math.Pow(2, float64(attempt))) - 1)
time.Sleep(time.Duration(delay) * time.Millisecond)
}
err := node.invoke(methodName, transaction.pb, resp)
if err != nil {
return id, resp, err
}
if resp.NodeTransactionPrecheckCode == proto.ResponseCodeEnum_BUSY {
// Try again (in a flash) on BUSY
continue
}
return id, resp, nil
}
// Timed out
return id, nil, newErrHederaPreCheckStatus(transaction.id, Status(resp.NodeTransactionPrecheckCode))
}
// Execute executes the Transaction with the provided client
func (transaction Transaction) Execute(client *Client) (TransactionID, error) {
id, resp, err := transaction.executeForResponse(client)
if err != nil {
return id, err
}
status := Status(resp.NodeTransactionPrecheckCode)
if status.isExceptional(true) {
// precheck failed
return id, newErrHederaPreCheckStatus(transaction.id, status)
}
// success
return id, nil
}
func (transaction Transaction) String() string {
return protobuf.MarshalTextString(transaction.pb) +
protobuf.MarshalTextString(transaction.body())
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (transaction Transaction) MarshalBinary() ([]byte, error) {
return protobuf.Marshal(transaction.pb)
}
// ID returns the transaction ID of the transaction
func (transaction Transaction) ID() TransactionID {
// Provide an accessor function to prevent the user from mutating the
// ID which would result in undefined behavior.
return transaction.id
}
// The protobuf stores the transaction body as raw bytes so we need to first
// decode what we have to inspect the Kind, TransactionID, and the NodeAccountID so we know how to
// properly execute it
func (transaction Transaction) body() *proto.TransactionBody {
transactionBody := new(proto.TransactionBody)
err := protobuf.Unmarshal(transaction.pb.GetBodyBytes(), transactionBody)
if err != nil {
// The bodyBytes inside of the transaction at this point have been verified and this should be impossible
panic(err)
}
return transactionBody
}
// getMethodName returns the proto method name of the transaction body
func getMethodName(transactionBody *proto.TransactionBody) (string, error) {
switch transactionBody.Data.(type) {
case *proto.TransactionBody_CryptoCreateAccount:
return "/proto.CryptoService/createAccount", nil
case *proto.TransactionBody_CryptoTransfer:
return "/proto.CryptoService/cryptoTransfer", nil
case *proto.TransactionBody_CryptoUpdateAccount:
return "/proto.CryptoService/updateAccount", nil
case *proto.TransactionBody_CryptoDelete:
return "/proto.CryptoService/cryptoDelete", nil
// FileServices
case *proto.TransactionBody_FileCreate:
return "/proto.FileService/createFile", nil
case *proto.TransactionBody_FileUpdate:
return "/proto.FileService/updateFile", nil
case *proto.TransactionBody_FileAppend:
return "/proto.FileService/appendContent", nil
case *proto.TransactionBody_FileDelete:
return "/proto.FileService/deleteFile", nil
// Contract
case *proto.TransactionBody_ContractCreateInstance:
return "/proto.SmartContractService/createContract", nil
case *proto.TransactionBody_ContractDeleteInstance:
return "/proto.SmartContractService/deleteContract", nil
case *proto.TransactionBody_ContractUpdateInstance:
return "/proto.SmartContractService/updateContract", nil
case *proto.TransactionBody_ContractCall:
return "/proto.SmartContractService/contractCallMethod", nil
// System
case *proto.TransactionBody_Freeze:
return "/proto.FreezeService/freeze", nil
case *proto.TransactionBody_SystemDelete:
return "/proto.FileService/systemDelete", nil
case *proto.TransactionBody_SystemUndelete:
return "/proto.FileService/systemUndelete", nil
// HCS
case *proto.TransactionBody_ConsensusCreateTopic:
return "/proto.ConsensusService/createTopic", nil
case *proto.TransactionBody_ConsensusDeleteTopic:
return "/proto.ConsensusService/deleteTopic", nil
case *proto.TransactionBody_ConsensusUpdateTopic:
return "/proto.ConsensusService/updateTopic", nil
case *proto.TransactionBody_ConsensusSubmitMessage:
return "/proto.ConsensusService/submitMessage", nil
default:
return "", newErrLocalValidationf("Could not find method name for: %T", transactionBody.Data)
}
}