-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathfile_update_transaction.go
More file actions
349 lines (294 loc) · 10.8 KB
/
Copy pathfile_update_transaction.go
File metadata and controls
349 lines (294 loc) · 10.8 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package hiero
// SPDX-License-Identifier: Apache-2.0
import (
"time"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
)
// FileUpdateTransaction
// Modify the metadata and/or contents of a file. If a field is not set in the transaction body, the
// corresponding file attribute will be unchanged. This transaction must be signed by all the keys
// in the top level of a key list (M-of-M) of the file being updated. If the keys themselves are
// being updated, then the transaction must also be signed by all the new keys. If the keys contain
// additional KeyList or ThresholdKey then M-of-M secondary KeyList or ThresholdKey signing
// requirements must be meet
type FileUpdateTransaction struct {
*Transaction[*FileUpdateTransaction]
fileID *FileID
keys *KeyList
expirationTime *time.Time
contents []byte
memo string
maxChunks uint64
chunkSize int
}
// NewFileUpdateTransaction creates a FileUpdateTransaction which modifies the metadata and/or contents of a file.
// If a field is not set in the transaction body, the corresponding file attribute will be unchanged.
// tx transaction must be signed by all the keys in the top level of a key list (M-of-M) of the file being updated.
// If the keys themselves are being updated, then the transaction must also be signed by all the new keys. If the keys contain
// additional KeyList or ThresholdKey then M-of-M secondary KeyList or ThresholdKey signing
// requirements must be meet
func NewFileUpdateTransaction() *FileUpdateTransaction {
tx := &FileUpdateTransaction{
maxChunks: 20,
chunkSize: 2048,
}
tx.Transaction = _NewTransaction(tx)
return tx
}
func _FileUpdateTransactionFromProtobuf(tx Transaction[*FileUpdateTransaction], pb *services.TransactionBody) FileUpdateTransaction {
var keys *KeyList
if pb.GetFileUpdate().GetKeys() != nil {
keysVal, _ := _KeyListFromProtobuf(pb.GetFileUpdate().GetKeys())
keys = &keysVal
}
var expiration *time.Time
if pb.GetFileUpdate().GetExpirationTime() != nil {
expirationVal := _TimeFromProtobuf(pb.GetFileUpdate().GetExpirationTime())
expiration = &expirationVal
}
fileUpdateTransaction := FileUpdateTransaction{
fileID: _FileIDFromProtobuf(pb.GetFileUpdate().GetFileID()),
keys: keys,
expirationTime: expiration,
contents: pb.GetFileUpdate().GetContents(),
memo: pb.GetFileUpdate().GetMemo().Value,
maxChunks: 20,
chunkSize: 2048,
}
tx.childTransaction = &fileUpdateTransaction
fileUpdateTransaction.Transaction = &tx
return fileUpdateTransaction
}
// SetFileID Sets the FileID to be updated
func (tx *FileUpdateTransaction) SetFileID(fileID FileID) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.fileID = &fileID
return tx
}
// GetFileID returns the FileID to be updated
func (tx *FileUpdateTransaction) GetFileID() FileID {
if tx.fileID == nil {
return FileID{}
}
return *tx.fileID
}
// SetKeys Sets the new list of keys that can modify or delete the file
func (tx *FileUpdateTransaction) SetKeys(keys ...Key) *FileUpdateTransaction {
tx._RequireNotFrozen()
if tx.keys == nil {
tx.keys = &KeyList{keys: []Key{}}
}
keyList := NewKeyList()
keyList.AddAll(keys)
tx.keys = keyList
return tx
}
func (tx *FileUpdateTransaction) GetKeys() KeyList {
if tx.keys != nil {
return *tx.keys
}
return KeyList{}
}
// SetExpirationTime Sets the new expiry time
func (tx *FileUpdateTransaction) SetExpirationTime(expiration time.Time) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.expirationTime = &expiration
return tx
}
// GetExpirationTime returns the new expiry time
func (tx *FileUpdateTransaction) GetExpirationTime() time.Time {
if tx.expirationTime != nil {
return *tx.expirationTime
}
return time.Time{}
}
// SetContents Sets the new contents that should overwrite the file's current contents
func (tx *FileUpdateTransaction) SetContents(contents []byte) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.contents = contents
return tx
}
// GetContents returns the new contents that should overwrite the file's current contents
func (tx *FileUpdateTransaction) GetContents() []byte {
return tx.contents
}
// SetMaxChunkSize sets the maximum size of each chunk used by ExecuteAll when the contents are
// larger than a single transaction can hold. Defaults to 2048 bytes, matching FileAppendTransaction.
func (tx *FileUpdateTransaction) SetMaxChunkSize(size int) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.chunkSize = size
return tx
}
// GetMaxChunkSize returns the maximum size of each chunk used by ExecuteAll.
func (tx *FileUpdateTransaction) GetMaxChunkSize() int {
return tx.chunkSize
}
// SetMaxChunks sets the maximum number of chunks ExecuteAll is allowed to split the contents into.
// Defaults to 20, matching FileAppendTransaction. ExecuteAll returns ErrMaxChunksExceeded if the
// contents require more chunks than this.
func (tx *FileUpdateTransaction) SetMaxChunks(size uint64) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.maxChunks = size
return tx
}
// GetMaxChunks returns the maximum number of chunks ExecuteAll is allowed to split the contents into.
func (tx *FileUpdateTransaction) GetMaxChunks() uint64 {
return tx.maxChunks
}
// SetFileMemo Sets the new memo to be associated with the file (UTF-8 encoding max 100 bytes)
func (tx *FileUpdateTransaction) SetFileMemo(memo string) *FileUpdateTransaction {
tx._RequireNotFrozen()
tx.memo = memo
return tx
}
// GeFileMemo
// Deprecated
// use GetFileMemo()
func (tx *FileUpdateTransaction) GeFileMemo() string {
return tx.memo
}
func (tx *FileUpdateTransaction) GetFileMemo() string {
return tx.memo
}
// ExecuteAll updates the file, transparently splitting contents larger than a single transaction
// can hold. When the contents fit in one chunk it behaves like Execute and returns a single-element
// slice; otherwise the first chunk overwrites the file via the FileUpdate and the remainder is
// appended with a FileAppendTransaction, so a file larger than the ~6 KiB single-transaction limit
// can be updated in one call. The returned slice holds the FileUpdate response followed by one
// response per append chunk.
//
// Each sub-transaction is charged its own fee and is signed with the operator only. If the file
// requires additional keys, or you need explicit control of the FileID, per-transaction fees, error
// recovery between chunks, or scheduling, use the manual FileUpdateTransaction +
// FileAppendTransaction two-step instead. Execute keeps its single-transaction semantics and still
// returns TRANSACTION_OVERSIZE for oversized contents.
func (tx *FileUpdateTransaction) ExecuteAll(client *Client) ([]TransactionResponse, error) {
if client == nil || client.operator == nil {
return nil, errNoClientProvided
}
if tx.freezeError != nil {
return nil, tx.freezeError
}
chunkSize := tx.chunkSize
if chunkSize <= 0 {
chunkSize = 2048
}
chunks := uint64((len(tx.contents) + chunkSize - 1) / chunkSize)
if chunks == 0 {
chunks = 1
}
if chunks > tx.maxChunks {
return nil, ErrMaxChunksExceeded{Chunks: chunks, MaxChunks: tx.maxChunks}
}
// Fits in a single transaction: behave exactly like Execute.
if chunks <= 1 {
resp, err := tx.Execute(client)
return []TransactionResponse{resp}, err
}
// Chunking rebuilds the bodies and appends by FileID, so the transaction must be unfrozen and
// carry a FileID.
if tx.IsFrozen() {
return nil, errFileUpdateChunkingRequiresUnfrozen
}
if tx.fileID == nil {
return nil, errFileUpdateChunkingRequiresFileID
}
fullContents := tx.contents
firstChunkEnd := min(chunkSize, len(fullContents))
// The first chunk overwrites the file's contents via the update itself; wait for its receipt
// before appending the rest to preserve ordering.
tx.SetContents(fullContents[:firstChunkEnd])
updateResponse, err := tx.Execute(client)
if err != nil {
return []TransactionResponse{updateResponse}, err
}
if _, err := updateResponse.SetValidateStatus(true).GetReceipt(client); err != nil {
return []TransactionResponse{updateResponse}, err
}
appendTx := NewFileAppendTransaction().
SetFileID(*tx.fileID).
SetContents(fullContents[firstChunkEnd:]).
SetMaxChunkSize(chunkSize).
SetMaxChunks(tx.maxChunks)
if nodeAccountIDs := tx.GetNodeAccountIDs(); len(nodeAccountIDs) > 0 {
appendTx.SetNodeAccountIDs(nodeAccountIDs)
}
appendResponses, err := appendTx.ExecuteAll(client)
responses := append([]TransactionResponse{updateResponse}, appendResponses...)
return responses, err
}
// Schedule creates a ScheduleCreateTransaction for this FileUpdateTransaction. Chunked contents
// (more than one chunk) cannot be scheduled, mirroring FileAppendTransaction.Schedule.
func (tx *FileUpdateTransaction) Schedule() (*ScheduleCreateTransaction, error) {
chunkSize := tx.chunkSize
if chunkSize <= 0 {
chunkSize = 2048
}
chunks := uint64((len(tx.contents) + chunkSize - 1) / chunkSize)
if chunks > 1 {
return &ScheduleCreateTransaction{}, ErrMaxChunksExceeded{
Chunks: chunks,
MaxChunks: 1,
}
}
return tx.Transaction.Schedule()
}
// ----------- Overridden functions ----------------
func (tx FileUpdateTransaction) getName() string {
return "FileUpdateTransaction"
}
func (tx FileUpdateTransaction) validateNetworkOnIDs(client *Client) error {
if client == nil || !client.autoValidateChecksums {
return nil
}
if tx.fileID != nil {
if err := tx.fileID.ValidateChecksum(client); err != nil {
return err
}
}
return nil
}
func (tx FileUpdateTransaction) build() *services.TransactionBody {
body := tx.buildTransactionBody()
body.Data = &services.TransactionBody_FileUpdate{
FileUpdate: tx.buildProtoBody(),
}
return body
}
func (tx FileUpdateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
body := tx.buildSchedulableTransactionBody()
body.Data = &services.SchedulableTransactionBody_FileUpdate{
FileUpdate: tx.buildProtoBody(),
}
return body, nil
}
func (tx FileUpdateTransaction) buildProtoBody() *services.FileUpdateTransactionBody {
body := &services.FileUpdateTransactionBody{
Memo: &wrapperspb.StringValue{Value: tx.memo},
}
if tx.fileID != nil {
body.FileID = tx.fileID._ToProtobuf()
}
if tx.expirationTime != nil {
body.ExpirationTime = _TimeToProtobuf(*tx.expirationTime)
}
if tx.keys != nil {
body.Keys = tx.keys._ToProtoKeyList()
}
if tx.contents != nil {
body.Contents = tx.contents
}
return body
}
func (tx FileUpdateTransaction) getMethod(channel *_Channel) _Method {
return _Method{
transaction: channel._GetFile().UpdateFile,
}
}
func (tx FileUpdateTransaction) constructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
return tx.buildScheduled()
}
func (tx FileUpdateTransaction) getBaseTransaction() *Transaction[TransactionInterface] {
return castFromConcreteToBaseTransaction(tx.Transaction, &tx)
}