antd 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/antd/errors.rb CHANGED
@@ -58,6 +58,66 @@ module Antd
58
58
  def initialize(message) = super(message, status_code: 503)
59
59
  end
60
60
 
61
+ # The pointer is at its final state, transferred or frozen: nothing can
62
+ # update it or hand it over again (HTTP 409 with +code: "POINTER_FINAL"+;
63
+ # gRPC FAILED_PRECONDITION with +x-antd-error-code: POINTER_FINAL+).
64
+ # Usually refused before anything is paid: the daemon reads the pointer
65
+ # first. A write that was already paid for when the network refused it (the
66
+ # pointer became final while the write was in flight) keeps that payment, so
67
+ # read the pointer before paying again.
68
+ class PointerFinalError < AntdError
69
+ def initialize(message) = super(message, status_code: 409)
70
+ end
71
+
72
+ # The pointer's owner signed two different final states and no one of them
73
+ # is held by a majority of the close group, so there is no answer to give
74
+ # (HTTP 409 with +code: "POINTER_FORKED"+; gRPC FAILED_PRECONDITION with
75
+ # +x-antd-error-code: POINTER_FORKED+). Subclasses +ForkError+, so
76
+ # +rescue Antd::ForkError+ catches it too.
77
+ class PointerForkedError < ForkError
78
+ end
79
+
80
+ # The request conflicts with the resource's current state (HTTP 409 with
81
+ # +code: "CONFLICT"+; gRPC ABORTED with +x-antd-error-code: CONFLICT+). For
82
+ # pointers, two cases share this code, and it does not say which one
83
+ # happened or whether anything was paid: a write while a prepared write to
84
+ # the same pointer awaits payment is refused before paying (finalize the
85
+ # prepared write, or wait for it to expire); an update that lost to a newer
86
+ # state while it was in flight may already have been paid for. Read the
87
+ # pointer again and check what was paid before submitting another paid
88
+ # write.
89
+ class ConflictError < AntdError
90
+ def initialize(message) = super(message, status_code: 409)
91
+ end
92
+
93
+ # Too few of an address's close group answered, or took a write, for the
94
+ # result to be the network's (HTTP 503 with +code: "CLOSE_GROUP_SHORTFALL"+;
95
+ # gRPC UNAVAILABLE with +x-antd-error-code: CLOSE_GROUP_SHORTFALL+).
96
+ #
97
+ # Reads are free to retry; one during an update in flight or churn usually
98
+ # clears. A paid write is not, unless +retryable+ is true: a pointer
99
+ # finalize then kept the paid state under the same +upload_id+, and
100
+ # repeating that finalize stores it against the same payment. Over REST
101
+ # +retryable+ is the body's +retryable+ (true only for JSON +true+); over
102
+ # gRPC it is read from the "(paid attempt retained...)" hint that closes
103
+ # the status details.
104
+ #
105
+ # Not a +ServiceUnavailableError+ (which means a daemon without a wallet):
106
+ # rescue it by name. Daemons without the pointer API reported a shortfall as
107
+ # a 500, an +InternalError+.
108
+ class CloseGroupShortfallError < AntdError
109
+ attr_reader :retryable
110
+
111
+ # @param message [String]
112
+ # @param retryable [Boolean] whether a pointer finalize kept the paid state
113
+ def initialize(message, retryable: false)
114
+ @retryable = retryable == true
115
+ super(message, status_code: 503)
116
+ end
117
+
118
+ alias retryable? retryable
119
+ end
120
+
61
121
  # A finalize stored some chunks while others remained unstored after the
62
122
  # daemon's retries (HTTP 502 with +code: "PARTIAL_UPLOAD"+; gRPC ABORTED).
63
123
  # The on-chain payment persists and the stored chunks stay on the network.
@@ -222,13 +282,77 @@ module Antd
222
282
  }
223
283
  end
224
284
 
285
+ # Metadata key the daemon (0.16.0 or later) sets on gRPC
286
+ # statuses whose code is shared with older errors, carrying the REST +code+.
287
+ ANTD_ERROR_CODE_METADATA = "x-antd-error-code"
288
+
289
+ # The gRPC status code the daemon pairs each tagged code with
290
+ # (antd/src/error.rs). A tag on any other status code is not trusted.
291
+ GRPC_STATUS_FOR_ERROR_CODE = {
292
+ "POINTER_FINAL" => 9, # FAILED_PRECONDITION
293
+ "POINTER_FORKED" => 9, # FAILED_PRECONDITION
294
+ "CONFLICT" => 10, # ABORTED
295
+ "CLOSE_GROUP_SHORTFALL" => 14 # UNAVAILABLE
296
+ }.freeze
297
+ private_constant :GRPC_STATUS_FOR_ERROR_CODE
298
+
299
+ # The hint that closes a CLOSE_GROUP_SHORTFALL message when a pointer
300
+ # finalize kept the paid state: "(paid attempt retained...)" at the very
301
+ # end of the details, as for a retained partial upload.
302
+ CLOSE_GROUP_RETAINED_TAIL = /\(#{Regexp.escape(PARTIAL_UPLOAD_RETAINED_HINT)}[^()]*\)\z/
303
+ private_constant :CLOSE_GROUP_RETAINED_TAIL
304
+
305
+ # The typed error for a daemon error +code+ that shares its HTTP status (and
306
+ # gRPC code) with older errors: the 409s POINTER_FINAL, POINTER_FORKED and
307
+ # CONFLICT, and CLOSE_GROUP_SHORTFALL's 503. nil for any other code, which
308
+ # keeps the status-based mapping.
309
+ #
310
+ # @param code [Object] the daemon's error code
311
+ # @param message [String]
312
+ # @param retryable [Boolean] CLOSE_GROUP_SHORTFALL only
313
+ # @return [AntdError, nil]
314
+ def self.error_for_code(code, message, retryable: false)
315
+ case code
316
+ when "POINTER_FINAL" then PointerFinalError.new(message)
317
+ when "POINTER_FORKED" then PointerForkedError.new(message)
318
+ when "CONFLICT" then ConflictError.new(message)
319
+ when "CLOSE_GROUP_SHORTFALL" then CloseGroupShortfallError.new(message, retryable: retryable)
320
+ end
321
+ end
322
+
323
+ # The typed error for a gRPC status the daemon tagged with
324
+ # +x-antd-error-code+ (in the trailing metadata, +GRPC::BadStatus#metadata+),
325
+ # when the tag agrees with the status code: FAILED_PRECONDITION for
326
+ # POINTER_FINAL / POINTER_FORKED, ABORTED for CONFLICT, UNAVAILABLE for
327
+ # CLOSE_GROUP_SHORTFALL. nil for an untagged status, a tag this SDK does not
328
+ # know, or a tag on another status code; the caller then keeps the
329
+ # code-based mapping (so an untagged FAILED_PRECONDITION is still a
330
+ # +PaymentError+).
331
+ #
332
+ # @param code [Integer] gRPC status code
333
+ # @param details [String, nil] the status details as the daemon sent them,
334
+ # read for CLOSE_GROUP_SHORTFALL's retained hint
335
+ # @param message [String] the message for the raised error
336
+ # @param metadata [Hash, nil] trailing metadata
337
+ # @return [AntdError, nil]
338
+ def self.error_for_grpc_metadata(code, details, message, metadata)
339
+ tag = metadata.is_a?(Hash) ? metadata[ANTD_ERROR_CODE_METADATA] : nil
340
+ tag = tag.first if tag.is_a?(Array)
341
+ return nil unless tag.is_a?(String) && GRPC_STATUS_FOR_ERROR_CODE[tag] == code
342
+
343
+ error_for_code(tag, message, retryable: CLOSE_GROUP_RETAINED_TAIL.match?(details.to_s))
344
+ end
345
+
225
346
  # Maps a non-2xx REST response onto a typed error, preferring the body's
226
347
  # machine-readable +code+ over the bare HTTP status where they diverge:
227
348
  # +PARTIAL_UPLOAD+ arrives as a 502 that would otherwise read as a generic
228
- # +NetworkError+. Every other code keeps the status-based mapping of
229
- # +error_for_status+. A JSON object body's +error+ field becomes the
230
- # message when it is a string; otherwise (non-JSON body, a JSON value that
231
- # is not an object, a non-string +error+) the raw body is the message.
349
+ # +NetworkError+, POINTER_FINAL, POINTER_FORKED and CONFLICT are 409s that
350
+ # must not read as +AlreadyExistsError+, and CLOSE_GROUP_SHORTFALL is a 503
351
+ # that must not read as +ServiceUnavailableError+ (see +error_for_code+).
352
+ # Every other code keeps the status-based mapping of +error_for_status+. A
353
+ # JSON object body's +error+ field becomes the message when it is a string;
354
+ # otherwise (non-JSON body, a JSON value that is not an object, a non-string
355
+ # +error+) the raw body is the message.
232
356
  #
233
357
  # Never raises on a malformed body: every field is type-checked, not
234
358
  # coerced. Only a +code+ that is exactly the string "PARTIAL_UPLOAD"
@@ -264,6 +388,11 @@ module Antd
264
388
  retention_known: [true, false].include?(parsed["retryable"])
265
389
  )
266
390
  end
391
+
392
+ # retryable is present, and true, only on a pointer finalize that kept
393
+ # the paid state; anything else reads as not retained.
394
+ coded = error_for_code(parsed["code"], message, retryable: parsed["retryable"] == true)
395
+ return coded if coded
267
396
  end
268
397
 
269
398
  error_for_status(code, message)
@@ -17,6 +17,8 @@ require_relative "v1/chunks_services_pb"
17
17
  require_relative "v1/files_services_pb"
18
18
  require_relative "v1/upload_services_pb"
19
19
  require_relative "v1/wallet_services_pb"
20
+ require_relative "v1/pointers_services_pb"
21
+ require "base64"
20
22
 
21
23
  module Antd
22
24
  DEFAULT_GRPC_TARGET = "localhost:50051"
@@ -47,6 +49,7 @@ module Antd
47
49
  @file_stub = Antd::V1::FileService::Stub.new(target, :this_channel_is_insecure)
48
50
  @upload_stub = Antd::V1::UploadService::Stub.new(target, :this_channel_is_insecure)
49
51
  @wallet_stub = Antd::V1::WalletService::Stub.new(target, :this_channel_is_insecure)
52
+ @pointer_stub = Antd::V1::PointerService::Stub.new(target, :this_channel_is_insecure)
50
53
  end
51
54
 
52
55
  # --- Health ---
@@ -76,7 +79,8 @@ module Antd
76
79
  def data_put(data, payment_mode: PaymentMode::AUTO)
77
80
  req = Antd::V1::PutDataRequest.new(data: data.b, payment_mode: payment_mode)
78
81
  resp = grpc_call { @data_stub.put(req) }
79
- DataPutResult.new(data_map: resp.data_map, chunks_stored: resp.chunks_stored, payment_mode_used: resp.payment_mode_used)
82
+ DataPutResult.new(data_map: resp.data_map, chunks_stored: resp.chunks_stored, payment_mode_used: resp.payment_mode_used,
83
+ sha256: presence(resp.sha256))
80
84
  end
81
85
 
82
86
  # Retrieve private data from a caller-held DataMap (hex).
@@ -95,7 +99,8 @@ module Antd
95
99
  def data_put_public(data, payment_mode: PaymentMode::AUTO)
96
100
  req = Antd::V1::PutPublicDataRequest.new(data: data.b, payment_mode: payment_mode)
97
101
  resp = grpc_call { @data_stub.put_public(req) }
98
- DataPutPublicResult.new(address: resp.address, chunks_stored: resp.chunks_stored, payment_mode_used: resp.payment_mode_used)
102
+ DataPutPublicResult.new(address: resp.address, chunks_stored: resp.chunks_stored, payment_mode_used: resp.payment_mode_used,
103
+ sha256: presence(resp.sha256))
99
104
  end
100
105
 
101
106
  # Retrieve public data by address.
@@ -226,7 +231,8 @@ module Antd
226
231
  storage_cost_atto: resp.storage_cost_atto,
227
232
  gas_cost_wei: resp.gas_cost_wei,
228
233
  chunks_stored: resp.chunks_stored,
229
- payment_mode_used: resp.payment_mode_used
234
+ payment_mode_used: resp.payment_mode_used,
235
+ sha256: presence(resp.sha256)
230
236
  )
231
237
  end
232
238
 
@@ -252,7 +258,8 @@ module Antd
252
258
  storage_cost_atto: resp.storage_cost_atto,
253
259
  gas_cost_wei: resp.gas_cost_wei,
254
260
  chunks_stored: resp.chunks_stored,
255
- payment_mode_used: resp.payment_mode_used
261
+ payment_mode_used: resp.payment_mode_used,
262
+ sha256: presence(resp.sha256)
256
263
  )
257
264
  end
258
265
 
@@ -408,7 +415,8 @@ module Antd
408
415
  address: resp.address,
409
416
  chunks_stored: resp.chunks_stored.to_i,
410
417
  data_map: resp.data_map,
411
- data_map_address: resp.data_map_address
418
+ data_map_address: resp.data_map_address,
419
+ sha256: presence(resp.sha256)
412
420
  )
413
421
  end
414
422
 
@@ -440,7 +448,8 @@ module Antd
440
448
  address: resp.address,
441
449
  chunks_stored: resp.chunks_stored.to_i,
442
450
  data_map: resp.data_map,
443
- data_map_address: resp.data_map_address
451
+ data_map_address: resp.data_map_address,
452
+ sha256: presence(resp.sha256)
444
453
  )
445
454
  end
446
455
 
@@ -468,8 +477,183 @@ module Antd
468
477
  resp.approved
469
478
  end
470
479
 
480
+ # --- Pointers ---
481
+ #
482
+ # The same methods, models and errors as the REST +Client+ (see there),
483
+ # over PointerService. The pointer errors share gRPC status codes with
484
+ # older ones, so the daemon names them in the +x-antd-error-code+
485
+ # trailer, which +grpc_call+ reads (see +Antd.error_for_grpc_metadata+).
486
+ # Unset optionals arrive as "" or an absent message and read as nil, as
487
+ # REST's nulls do. Every write pays: do not retry +pointer_create+,
488
+ # +pointer_update+, +pointer_transfer+ or +pointer_finalize+
489
+ # automatically.
490
+
491
+ # Mirrors +Client#pointer_keys+.
492
+ # @return [Array<String>]
493
+ def pointer_keys
494
+ resp = grpc_call { @pointer_stub.list_keys(Antd::V1::ListPointerKeysRequest.new) }
495
+ resp.keys.map(&:address)
496
+ end
497
+
498
+ # Mirrors +Client#pointer_create+. Pays; do not retry automatically.
499
+ # @return [PointerWriteResult]
500
+ def pointer_create(owner, target, kind: nil)
501
+ req = Antd::V1::PointerWriteRequest.new(owner: owner, target: target, kind: kind.to_s)
502
+ pointer_write_result(grpc_call { @pointer_stub.create(req) })
503
+ end
504
+
505
+ # Mirrors +Client#pointer_update+. Pays; do not retry automatically.
506
+ # @return [PointerWriteResult]
507
+ def pointer_update(owner, target, kind: nil)
508
+ req = Antd::V1::PointerWriteRequest.new(owner: owner, target: target, kind: kind.to_s)
509
+ pointer_write_result(grpc_call { @pointer_stub.update(req) })
510
+ end
511
+
512
+ # Mirrors +Client#pointer_transfer+. Pays; do not retry automatically.
513
+ # @return [PointerTransferResult]
514
+ def pointer_transfer(owner, recipient)
515
+ req = Antd::V1::PointerTransferRequest.new(owner: owner, recipient: recipient)
516
+ resp = grpc_call { @pointer_stub.transfer(req) }
517
+ PointerTransferResult.new(
518
+ address: resp.address,
519
+ state_id: resp.state_id,
520
+ finality: resp.finality.nil? ? nil : pointer_finality_from(resp.finality),
521
+ finality_error: presence(resp.finality_error)
522
+ )
523
+ end
524
+
525
+ # Mirrors +Client#pointer_get+.
526
+ # @return [PointerRecord]
527
+ def pointer_get(address)
528
+ resp = grpc_call { @pointer_stub.get(Antd::V1::GetPointerRequest.new(address: address)) }
529
+ PointerRecord.new(
530
+ address: resp.address,
531
+ owner: resp.owner,
532
+ counter: Antd.parse_pointer_counter("counter", resp.counter),
533
+ kind: resp.kind,
534
+ kind_tag: resp.kind_tag,
535
+ target: resp.target,
536
+ state_id: resp.state_id,
537
+ is_final: resp.is_final,
538
+ transferred_to: presence(resp.transferred_to)
539
+ )
540
+ end
541
+
542
+ # Mirrors +Client#pointer_resolve+.
543
+ # @return [PointerTarget]
544
+ def pointer_resolve(address)
545
+ resp = grpc_call { @pointer_stub.resolve(Antd::V1::ResolvePointerRequest.new(address: address)) }
546
+ PointerTarget.new(kind: resp.kind, kind_tag: resp.kind_tag, target: resp.target)
547
+ end
548
+
549
+ # Mirrors +Client#pointer_finality+.
550
+ # @return [PointerFinality]
551
+ def pointer_finality(address)
552
+ resp = grpc_call { @pointer_stub.finality(Antd::V1::PointerFinalityRequest.new(address: address)) }
553
+ pointer_finality_from(resp)
554
+ end
555
+
556
+ # Mirrors +Client#pointer_controller+.
557
+ # @return [PointerController]
558
+ def pointer_controller(address)
559
+ resp = grpc_call { @pointer_stub.controller(Antd::V1::PointerControllerRequest.new(address: address)) }
560
+ PointerController.new(pointer: resp.pointer, transfers: resp.transfers.to_a, frozen: resp.frozen)
561
+ end
562
+
563
+ # Mirrors +Client#pointer_prepare+.
564
+ # @return [PointerPrepareResult]
565
+ def pointer_prepare(owner, op, target: nil, kind: nil, recipient: nil, include_signed_quotes: false)
566
+ req = Antd::V1::PreparePointerRequest.new(
567
+ owner: owner,
568
+ op: op.to_s,
569
+ target: target.to_s,
570
+ kind: kind.to_s,
571
+ recipient: recipient.to_s,
572
+ include_signed_quotes: include_signed_quotes ? true : false
573
+ )
574
+ resp = grpc_call { @pointer_stub.prepare(req) }
575
+ PointerPrepareResult.new(
576
+ upload_id: resp.upload_id,
577
+ op: resp.op,
578
+ address: resp.address,
579
+ state_id: resp.state_id,
580
+ counter: Antd.parse_pointer_counter("counter", resp.counter),
581
+ payment_type: resp.payment_type,
582
+ payments: resp.payments.map do |p|
583
+ PaymentInfo.new(quote_hash: p.quote_hash, rewards_address: p.rewards_address, amount: p.amount)
584
+ end,
585
+ total_amount: resp.total_amount,
586
+ payment_vault_address: resp.payment_vault_address,
587
+ payment_token_address: resp.payment_token_address,
588
+ rpc_url: resp.rpc_url,
589
+ # Bytes on the wire; base64 here, the same form REST returns.
590
+ signed_quotes: resp.signed_quotes.map do |q|
591
+ SignedQuoteEntry.new(
592
+ quote_hash: q.quote_hash,
593
+ quote: Base64.strict_encode64(q.quote),
594
+ commitment_sidecar: q.commitment_sidecar.empty? ? nil : Base64.strict_encode64(q.commitment_sidecar)
595
+ )
596
+ end
597
+ )
598
+ end
599
+
600
+ # Mirrors +Client#pointer_finalize+. A +CloseGroupShortfallError+ with
601
+ # +retryable+ means the daemon kept the paid state: repeat the call with
602
+ # the same +upload_id+.
603
+ # @return [PointerFinalizeResult]
604
+ def pointer_finalize(upload_id, tx_hashes)
605
+ req = Antd::V1::FinalizePointerRequest.new(upload_id: upload_id, tx_hashes: tx_hashes)
606
+ resp = grpc_call { @pointer_stub.finalize(req) }
607
+ PointerFinalizeResult.new(
608
+ address: resp.address,
609
+ state_id: resp.state_id,
610
+ counter: Antd.parse_pointer_counter("counter", resp.counter),
611
+ finality: resp.finality.nil? ? nil : pointer_finality_from(resp.finality)
612
+ )
613
+ end
614
+
471
615
  private
472
616
 
617
+ def pointer_write_result(resp)
618
+ PointerWriteResult.new(
619
+ address: resp.address,
620
+ state_id: resp.state_id,
621
+ counter: Antd.parse_pointer_counter("counter", resp.counter)
622
+ )
623
+ end
624
+
625
+ # Converts the proto finality, where an unset counter or transferred_to is
626
+ # "" and an unset state or majority an absent message: each reads as nil.
627
+ def pointer_finality_from(f)
628
+ PointerFinality.new(
629
+ address: f.address,
630
+ group: f.group,
631
+ answered: f.answered,
632
+ status: f.status,
633
+ counter: f.counter.empty? ? nil : Antd.parse_pointer_counter("finality counter", f.counter),
634
+ state: f.state.nil? ? nil : pointer_final_state_from(f.state),
635
+ states: f.states.map { |s| pointer_final_state_from(s) },
636
+ majority: f.majority.nil? ? nil : pointer_final_state_from(f.majority),
637
+ transferred_to: presence(f.transferred_to)
638
+ )
639
+ end
640
+
641
+ def pointer_final_state_from(s)
642
+ PointerFinalState.new(
643
+ state_id: s.state_id,
644
+ kind: s.kind,
645
+ kind_tag: s.kind_tag,
646
+ target: s.target,
647
+ holders: s.holders,
648
+ transferred_to: presence(s.transferred_to)
649
+ )
650
+ end
651
+
652
+ # A proto string, or nil when unset (empty).
653
+ def presence(value)
654
+ value.nil? || value.empty? ? nil : value
655
+ end
656
+
473
657
  # Drives a +return_op: true+ server-streaming call, yielding +DownloadFrame+s.
474
658
  # The byte denominator is surfaced first as a leading +DownloadFrame+ (its
475
659
  # +meta+ set), read from the response's +x-content-length+ initial metadata;
@@ -560,8 +744,21 @@ module Antd
560
744
 
561
745
 
562
746
  # Executes a gRPC call and translates errors to Antd error types.
747
+ #
748
+ # The daemon's +x-antd-error-code+ trailer comes first: where it names a
749
+ # code that shares its gRPC status with older errors (POINTER_FINAL,
750
+ # POINTER_FORKED, CONFLICT, CLOSE_GROUP_SHORTFALL) and agrees with the
751
+ # status code, it selects the typed error. Anything else is re-raised to
752
+ # the status-code mapping below.
563
753
  def grpc_call
564
- yield
754
+ begin
755
+ yield
756
+ rescue GRPC::BadStatus => e
757
+ tagged = Antd.error_for_grpc_metadata(e.code, e.details, e.message, e.metadata)
758
+ raise tagged unless tagged.nil?
759
+
760
+ raise
761
+ end
565
762
  rescue GRPC::InvalidArgument => e
566
763
  raise BadRequestError, e.message
567
764
  rescue GRPC::NotFound => e
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "manifests"
4
+ require_relative "grpc_client"
5
+ require_relative "v1/manifests_services_pb"
6
+
7
+ # The manifest methods on +Antd::GrpcClient+, over ManifestService. The
8
+ # models and the entry check are in manifests.rb, shared with REST.
9
+ module Antd
10
+ # Channel arguments for the manifest stub's own channel. grpc-ruby has no
11
+ # per-call message limit, so ManifestService gets a channel of its own
12
+ # with both limits raised to +MANIFEST_GRPC_MAX_MESSAGE+; the other
13
+ # services keep gRPC's defaults.
14
+ MANIFEST_GRPC_CHANNEL_ARGS = {
15
+ "grpc.max_receive_message_length" => MANIFEST_GRPC_MAX_MESSAGE,
16
+ "grpc.max_send_message_length" => MANIFEST_GRPC_MAX_MESSAGE
17
+ }.freeze
18
+
19
+ class GrpcClient
20
+ # --- Manifests ---
21
+ #
22
+ # The same methods and models as the REST +Client+ (see there). The
23
+ # `.ant` bytes travel raw over gRPC; REST's base64 never shows. Unset
24
+ # optionals (proto3 +optional+ without a value, or an empty string) read
25
+ # as nil, as REST's omitted fields do.
26
+
27
+ # Mirrors +Client#build_manifest+. Nothing is paid for.
28
+ #
29
+ # Needs antd 0.16.0 or later.
30
+ #
31
+ # @return [BuiltManifest]
32
+ # @raise [BadRequestError] an entry with both or neither of +data_map+ and
33
+ # +address+, or a bad +size+, before anything is sent
34
+ # @raise [BadRequestError] an entry the daemon rejects (INVALID_ARGUMENT);
35
+ # the message names +entries[i]+
36
+ def build_manifest(entries, name: nil, torrent_info_hashes: nil, embed_roots: true)
37
+ inputs = Antd.manifest_entry_inputs(entries)
38
+ req = Antd::V1::BuildManifestRequest.new(
39
+ torrent_info_hashes: Array(torrent_info_hashes),
40
+ # Always sent, so "unset means true" never depends on the daemon.
41
+ embed_roots: embed_roots ? true : false,
42
+ entries: inputs.map { |e| manifest_entry_input_pb(e) }
43
+ )
44
+ req.name = name unless name.nil? || name.empty?
45
+
46
+ resp = grpc_call { manifest_stub.build(req) }
47
+ BuiltManifest.new(
48
+ manifest: resp.manifest,
49
+ link: manifest_pb_string(resp.link),
50
+ entries: resp.entries.map { |e| manifest_entry_info_from_pb(e) }
51
+ )
52
+ end
53
+
54
+ # Mirrors +Client#decode_manifest+. The bytes are sent raw.
55
+ #
56
+ # Needs antd 0.16.0 or later.
57
+ #
58
+ # @return [DecodedManifest]
59
+ def decode_manifest(manifest, resolve: false)
60
+ raise BadRequestError, MANIFEST_DECODE_NEEDS_INPUT if manifest.nil? || manifest.empty?
61
+
62
+ decode_manifest_pb(Antd::V1::DecodeManifestRequest.new(manifest: manifest.b, resolve: resolve ? true : false))
63
+ end
64
+
65
+ # Mirrors +Client#decode_manifest_link+.
66
+ #
67
+ # Needs antd 0.16.0 or later.
68
+ #
69
+ # @return [DecodedManifest]
70
+ def decode_manifest_link(link, resolve: false)
71
+ raise BadRequestError, MANIFEST_DECODE_NEEDS_INPUT if link.nil? || link.empty?
72
+
73
+ decode_manifest_pb(Antd::V1::DecodeManifestRequest.new(link: link, resolve: resolve ? true : false))
74
+ end
75
+
76
+ private
77
+
78
+ # ManifestService on its own channel, with +MANIFEST_GRPC_CHANNEL_ARGS+.
79
+ # Made on first use, so +initialize+ stays as it is.
80
+ def manifest_stub
81
+ @manifest_stub ||= Antd::V1::ManifestService::Stub.new(
82
+ @target, :this_channel_is_insecure, channel_args: MANIFEST_GRPC_CHANNEL_ARGS
83
+ )
84
+ end
85
+
86
+ def decode_manifest_pb(req)
87
+ resp = grpc_call { manifest_stub.decode(req) }
88
+ DecodedManifest.new(
89
+ source: resp.source,
90
+ name: resp.has_name? ? manifest_pb_string(resp.name) : nil,
91
+ torrent_info_hash_v1: manifest_pb_string(resp.torrent_info_hash_v1),
92
+ torrent_info_hash_v2: manifest_pb_string(resp.torrent_info_hash_v2),
93
+ entries: resp.entries.map { |e| manifest_entry_info_from_pb(e) }
94
+ )
95
+ end
96
+
97
+ def manifest_entry_input_pb(e)
98
+ attrs = {}
99
+ attrs[:path] = e.path unless e.path.nil? || e.path.empty?
100
+ attrs[:size] = e.size unless e.size.nil?
101
+ if e.data_map.nil? || e.data_map.empty?
102
+ attrs[:address] = e.address
103
+ else
104
+ attrs[:data_map] = e.data_map
105
+ end
106
+ attrs[:sha256] = e.sha256 unless e.sha256.nil? || e.sha256.empty?
107
+ Antd::V1::ManifestEntryInput.new(**attrs)
108
+ end
109
+
110
+ # proto3 +optional+ sizes are checked for presence: unset is nil, not 0.
111
+ def manifest_entry_info_from_pb(e)
112
+ ManifestEntryInfo.new(
113
+ effective_name: e.effective_name,
114
+ path: e.has_path? ? manifest_pb_string(e.path) : nil,
115
+ size: e.has_size? ? e.size : nil,
116
+ kind: e.kind,
117
+ content_address: e.content_address,
118
+ data_map: manifest_pb_string(e.data_map),
119
+ exact_size: e.has_exact_size? ? e.exact_size : nil,
120
+ error: manifest_pb_string(e.error),
121
+ sha256: manifest_pb_string(e.sha256)
122
+ )
123
+ end
124
+
125
+ # A proto string, or nil when empty.
126
+ def manifest_pb_string(value)
127
+ value.nil? || value.empty? ? nil : value
128
+ end
129
+ end
130
+ end