Commit 4abf1ae69db203ed58f6c0a7c64b1fff6a36cada

Authored by Marius Hanne
1 parent a30a6a0f8f

command api: only subscribe one channel at a time, don't send parameters in response

Showing 2 changed files with 75 additions and 95 deletions Side-by-side Diff

lib/bitcoin/network/command_handler.rb
... ... @@ -96,11 +96,8 @@
96 96 # didn't previously receive as unconfirmed. To make sure you receive all
97 97 # transactions, also subscribe to the tx_1 channel.
98 98 def handle_monitor request, params = {}
99   - params[:channels].each do |channel|
100   - # @node.subscribe(channel) {|*data| respond(request, [channel, *data]) }
101   - send("handle_monitor_#{channel[:name]}", request, channel)
102   - log.info { "Client subscribed to channel #{channel.inspect}" }
103   - end
  99 + send("handle_monitor_#{params[:channel]}", request, params)
  100 + log.info { "Client subscribed to channel #{channel.inspect}" }
104 101 nil
105 102 end
106 103  
107 104  
... ... @@ -127,14 +124,13 @@
127 124  
128 125 def respond_monitor_block request, params, block, depth = nil
129 126 depth ||= block.depth
130   - respond(request, { channel: params,
131   - hash: block.hash, hex: block.to_payload.hth, depth: depth })
  127 + respond(request, { hash: block.hash, hex: block.to_payload.hth, depth: depth })
132 128 end
133 129  
134 130 # TODO: params (min reorg depth)
135 131 def handle_monitor_reorg request, params
136 132 @node.subscribe(:reorg) do |new_main, new_side|
137   - respond(request, { channel: params, new_main: new_main, new_side: new_side })
  133 + respond(request, { new_main: new_main, new_side: new_side })
138 134 end
139 135 end
140 136  
... ... @@ -175,8 +171,7 @@
175 171 return unless (params[:addresses] & addrs).any?
176 172 end
177 173  
178   - respond(request, { channel: params,
179   - hash: tx.hash, nhash: tx.nhash, hex: tx.to_payload.hth, conf: conf })
  174 + respond(request, { hash: tx.hash, nhash: tx.nhash, hex: tx.to_payload.hth, conf: conf })
180 175 end
181 176  
182 177 # Handle +monitor output+ command.
... ... @@ -231,9 +226,8 @@
231 226 # filter by addresses
232 227 return if params[:addresses] && !params[:addresses].include?(addr)
233 228  
234   - respond(request, { channel: params,
235   - nhash: tx.nhash, hash: tx.hash, idx: idx, address: addr,
236   - value: out.value, conf: conf })
  229 + respond(request, { nhash: tx.nhash, hash: tx.hash, idx: idx,
  230 + address: addr, value: out.value, conf: conf })
237 231 end
238 232  
239 233 # Handle +monitor connection+ command; send current connections
spec/bitcoin/node/command_api_spec.rb
... ... @@ -315,20 +315,20 @@
315 315 end
316 316 end
317 317  
318   - def should_receive_block request, channel, block, depth, client = @client
  318 + def should_receive_block request, block, depth, client = @client
319 319 expected = { hash: block.hash, hex: block.to_payload.hth, depth: depth }
320   - should_receive(request, { channel: channel }.merge(expected), client)
  320 + should_receive(request, expected, client)
321 321 end
322 322  
323   - def should_receive_tx request, channel, tx, conf, client = @client
  323 + def should_receive_tx request, tx, conf, client = @client
324 324 expected = { hash: tx.hash, nhash: tx.nhash, hex: tx.to_payload.hth, conf: conf }
325   - should_receive(request, { channel: channel }.merge(expected), client)
  325 + should_receive(request, expected, client)
326 326 end
327 327  
328   - def should_receive_output request, channel, tx, idx, conf, client = @client
  328 + def should_receive_output request, tx, idx, conf, client = @client
329 329 expected = { hash: tx.hash, nhash: tx.nhash, idx: idx,
330 330 address: tx.out[idx].parsed_script.get_address, value: tx.out[idx].value, conf: conf }
331   - should_receive(request, { channel: channel }.merge(expected), client)
  331 + should_receive(request, expected, client)
332 332 end
333 333  
334 334 def store_block block
335 335  
336 336  
337 337  
338 338  
339 339  
340 340  
341 341  
342 342  
343 343  
344 344  
345 345  
346 346  
347 347  
... ... @@ -336,67 +336,64 @@
336 336 should_receive(request, {"queued" => block.hash })
337 337 end
338 338  
339   -
340   - @block_channel = { name: "block" }
341   - @tx_channel = { name: "tx", conf: 1 }
342   -
343 339 end
344 340  
345 341 describe :channels do
346 342  
347 343 it "should combine multiple channels" do
348   - request = send("monitor", channels: [ @block_channel, @tx_channel ])
349   - should_receive_block(request, @block_channel, @genesis, 0)
  344 + r1 = send("monitor", channel: "block")
  345 + r2 = send("monitor", channel: "tx", conf: 1)
  346 + should_receive_block(r1, @genesis, 0)
350 347  
351 348 store_block @block
352   - should_receive_block(request, @block_channel, @block, 1)
353   - should_receive_tx(request, @tx_channel, @block.tx[0], 1)
  349 + should_receive_block(r1, @block, 1)
  350 + should_receive_tx(r2, @block.tx[0], 1)
354 351 end
355 352  
356 353 it "should handle multiple clients" do
357 354 @client2 = TCPSocket.new(*@config[:command])
358 355  
359   - r1_1 = send "monitor", { channels: [ @tx_channel ] }
360   - r1_2 = send "monitor", { channels: [ @block_channel ] }, @client2
  356 + r1_1 = send "monitor", channel: "tx", conf: 1
  357 + r1_2 = send "monitor", { channel: "block" }, @client2
361 358  
362   - should_receive_block(r1_2, @block_channel, @genesis, 0, @client2)
  359 + should_receive_block(r1_2, @genesis, 0, @client2)
363 360  
364 361 store_block @block
365 362  
366   - should_receive_block(r1_2, @block_channel, @block, 1, @client2)
367   - should_receive_tx(r1_1, @tx_channel, @block.tx[0], 1)
  363 + should_receive_block(r1_2, @block, 1, @client2)
  364 + should_receive_tx(r1_1, @block.tx[0], 1)
368 365  
369 366 block = create_block @block.hash, false
370 367 store_block block
371 368  
372   - should_receive_block(r1_2, @block_channel, block, 2, @client2)
373   - should_receive_tx(r1_1, @tx_channel, block.tx[0], 1)
  369 + should_receive_block(r1_2, block, 2, @client2)
  370 + should_receive_tx(r1_1, block.tx[0], 1)
374 371  
375   - r2_2 = send "monitor", { channels: [ @tx_channel ] }, @client2
376   - r2_1 = send "monitor", { channels: [ @block_channel ] }
  372 + r2_2 = send "monitor", { channel: "tx", conf: 1 }, @client2
  373 + r2_1 = send "monitor", channel: "block"
377 374  
378   - should_receive_block(r2_1, @block_channel, block, 2)
  375 + should_receive_block(r2_1, block, 2)
379 376  
380 377 block = create_block block.hash, false
381 378 store_block block
382 379  
383   - should_receive_block(r1_2, @block_channel, block, 3, @client2)
384   - should_receive_tx(r2_2, @tx_channel, block.tx[0], 1, @client2)
  380 + should_receive_block(r1_2, block, 3, @client2)
  381 + should_receive_tx(r2_2, block.tx[0], 1, @client2)
385 382  
386   - should_receive_tx(r1_1, @tx_channel, block.tx[0], 1)
  383 + should_receive_tx(r1_1, block.tx[0], 1)
387 384  
388 385 # if something was wrong, we would now receive the last tx again
389 386  
390   - should_receive_block(r2_1, @block_channel, block, 3)
  387 + should_receive_block(r2_1, block, 3)
391 388  
392 389 block = create_block block.hash, false
393 390 store_block block
394 391  
395   - should_receive_tx(r1_1, @tx_channel, block.tx[0], 1)
  392 + should_receive_tx(r1_1, block.tx[0], 1)
396 393  
397   - should_receive_block(r2_1, @block_channel, block, 4)
398   - should_receive_block(r1_2, @block_channel, block, 4, @client2)
399   - should_receive_tx(r2_2, @tx_channel, block.tx[0], 1, @client2)
  394 + should_receive_block(r2_1, block, 4)
  395 + should_receive_block(r1_2, block, 4, @client2)
  396 + should_receive_tx(r2_2, block.tx[0], 1, @client2)
400 397 end
401 398  
402 399 end
403 400  
404 401  
... ... @@ -404,16 +401,16 @@
404 401 describe :block do
405 402  
406 403 before do
407   - @request = send "monitor", channels: [ @block_channel ]
408   - should_receive_block(@request, @block_channel, @genesis, 0)
  404 + @request = send "monitor", channel: "block"
  405 + should_receive_block(@request, @genesis, 0)
409 406 store_block @block
410   - should_receive_block(@request, @block_channel, @block, 1)
  407 + should_receive_block(@request, @block, 1)
411 408 end
412 409  
413 410 it "should monitor block" do
414 411 @block = create_block @block.hash, false
415 412 store_block @block
416   - should_receive_block(@request, @block_channel, @block, 2)
  413 + should_receive_block(@request, @block, 2)
417 414 end
418 415  
419 416 it "should not monitor side or orphan blocks" do
... ... @@ -427,7 +424,7 @@
427 424 @block = create_block @block.hash, false
428 425 store_block @block
429 426  
430   - should_receive_block(@request, @block_channel, @block, 2)
  427 + should_receive_block(@request, @block, 2)
431 428 end
432 429  
433 430 it "should received missed blocks when last height is given" do
434 431  
... ... @@ -439,12 +436,11 @@
439 436 end
440 437 sleep 0.1
441 438  
442   - c = { name: "block", last: 1 }
443   - r = send "monitor", channels: [ c ]
  439 + r = send "monitor", channel: "block", last: 1
444 440  
445   - should_receive_block(r, c, blocks[1], 2)
446   - should_receive_block(r, c, blocks[2], 3)
447   - should_receive_block(r, c, blocks[3], 4)
  441 + should_receive_block(r, blocks[1], 2)
  442 + should_receive_block(r, blocks[2], 3)
  443 + should_receive_block(r, blocks[3], 4)
448 444 end
449 445  
450 446 end
... ... @@ -452,7 +448,7 @@
452 448 describe :reorg do
453 449  
454 450 before do
455   - @request = send "monitor", channels: [{ name: "reorg" }]
  451 + @request = send "monitor", channel: "reorg"
456 452 store_block @block
457 453 end
458 454  
... ... @@ -461,8 +457,7 @@
461 457 store_block @block1
462 458 @block2 = create_block @block1.hash, false
463 459 store_block @block2
464   - should_receive @request, { channel: { name: "reorg" },
465   - new_main: [ @block1.hash ], new_side: [ @block.hash ] }
  460 + should_receive @request, { new_main: [ @block1.hash ], new_side: [ @block.hash ] }
466 461 end
467 462  
468 463 end
469 464  
470 465  
471 466  
472 467  
473 468  
474 469  
... ... @@ -470,36 +465,34 @@
470 465 describe :tx do
471 466  
472 467 it "should monitor unconfirmed tx" do
473   - c = { name: "tx" }
474   - r1 = send "monitor", channels: [ c ]
  468 + r1 = send "monitor", channel: "tx"
475 469 tx = @block.tx[0]
476 470 r2 = send "store_tx", hex: tx.to_payload.hth
477 471 should_receive r2, { "queued" => tx.hash }
478 472  
479   - should_receive_tx(r1, c, tx, 0)
  473 + should_receive_tx(r1, tx, 0)
480 474 end
481 475  
482 476 it "should monitor confirmed tx" do
483   - r = send "monitor", channels: [ @tx_channel ]
  477 + r = send "monitor", channel: "tx", conf: 1
484 478 store_block @block
485 479  
486   - should_receive_tx(r, @tx_channel, @block.tx[0], 1)
  480 + should_receive_tx(r, @block.tx[0], 1)
487 481 end
488 482  
489 483 it "should monitor tx for given confirmation level" do
490   - c = { name: "tx", conf: 3 }
491   - r = send "monitor", channels: [ c ]
  484 + r = send "monitor", channel: "tx", conf: 3
492 485 @tx = @block.tx[0]
493 486 store_block @block
494 487 @block = create_block @block.hash, false
495 488 store_block @block
496 489  
497   - should_receive_tx(r, c, @genesis.tx[0], 3)
  490 + should_receive_tx(r, @genesis.tx[0], 3)
498 491  
499 492 @block = create_block @block.hash, false
500 493 store_block @block
501 494  
502   - should_receive_tx(r, c, @tx, 3)
  495 + should_receive_tx(r, @tx, 3)
503 496 end
504 497  
505 498 it "should receive missed txs when last txhash is given" do
506 499  
... ... @@ -510,12 +503,12 @@
510 503 store_block blocks.last
511 504 end
512 505 sleep 0.1
513   - c = { name: "tx", conf: 1, last: blocks[0].tx[0].hash }
514   - r = send "monitor", channels: [ c ]
515 506  
516   - should_receive_tx(r, c, blocks[1].tx[0], 3)
517   - should_receive_tx(r, c, blocks[2].tx[0], 2)
518   - should_receive_tx(r, c, blocks[3].tx[0], 1)
  507 + r = send "monitor", channel: "tx", conf: 1, last: blocks[0].tx[0].hash
  508 +
  509 + should_receive_tx(r, blocks[1].tx[0], 3)
  510 + should_receive_tx(r, blocks[2].tx[0], 2)
  511 + should_receive_tx(r, blocks[3].tx[0], 1)
519 512 end
520 513  
521 514  
522 515  
523 516  
524 517  
525 518  
526 519  
527 520  
528 521  
529 522  
530 523  
531 524  
... ... @@ -524,50 +517,45 @@
524 517 block = create_block(@block.hash, false, [->(t) {
525 518 create_tx(t, @block.tx[0], 0, [[50e8, @key2]]) }], @key)
526 519 @addr = @block.tx[0].out[0].parsed_script.get_address
527   - c = { name: "tx", conf: 1, addresses: [ @key2.addr ] }
528   - r = send "monitor", channels: [ c ]
  520 + r = send "monitor", channel: "tx", conf: 1, addresses: [ @key2.addr ]
529 521 store_block @block
530 522 store_block block
531   - should_receive_tx(r, c, block.tx[1], 1)
  523 + should_receive_tx(r, block.tx[1], 1)
532 524 end
533 525  
534   -
535 526 end
536 527  
537 528 describe :output do
538 529  
539 530 before do
540   - @channel = { name: "output" }
541 531 @tx = @block.tx[0]; @out = @tx.out[0]
542 532 end
543 533  
544 534 it "should monitor unconfirmed outputs" do
545   - r1 = send "monitor", channels: [ @channel ]
  535 + r1 = send "monitor", channel: "output"
546 536 tx = @block.tx[0]
547 537 r2 = send "store_tx", hex: tx.to_payload.hth
548 538 should_receive r2, { "queued" => tx.hash }
549   - should_receive_output(r1, @channel, tx, 0, 0)
  539 + should_receive_output(r1, tx, 0, 0)
550 540 end
551 541  
552 542 it "should monitor confirmed output" do
553   - c = { name: "output", conf: 1 }
554   - r = send "monitor", channels: [ c ]
  543 + r = send "monitor", channel: "output", conf: 1
555 544 store_block @block
556   - should_receive_output(r, c, @tx, 0, 1)
  545 + should_receive_output(r, @tx, 0, 1)
557 546 end
558 547  
559 548 it "should monitor output for given confirmation level" do
560   - c = { name: "output", conf: 3 }
561   - r = send "monitor", channels: [ c ]
  549 + r = send "monitor", channel: "output", conf: 3
562 550 store_block @block
563 551 @block = create_block @block.hash, false
564 552 store_block @block
565 553 tx = @genesis.tx[0]; out = tx.out[0]
566   - should_receive_output(r, c, tx, 0, 3)
  554 + should_receive_output(r, tx, 0, 3)
567 555  
568 556 @block = create_block @block.hash, false
569 557 store_block @block
570   - should_receive_output(r, c, @tx, 0, 3)
  558 + should_receive_output(r, @tx, 0, 3)
571 559 end
572 560  
573 561 it "should receive missed outputs when last txhash:idx is given" do
574 562  
575 563  
576 564  
577 565  
... ... @@ -579,25 +567,23 @@
579 567 store_block blocks.last
580 568 end
581 569 sleep 0.1
582   - channel = { name: "output", conf: 1, last: "#{blocks[0].tx[0].hash}:0" }
583 570  
584   - r = send "monitor", channels: [ channel ]
  571 + r = send "monitor", channel: "output", conf: 1, last: "#{blocks[0].tx[0].hash}:0"
585 572  
586   - should_receive_output(r, channel, blocks[1].tx[0], 0, 3)
587   - should_receive_output(r, channel, blocks[2].tx[0], 0, 2)
588   - should_receive_output(r, channel, blocks[3].tx[0], 0, 1)
  573 + should_receive_output(r, blocks[1].tx[0], 0, 3)
  574 + should_receive_output(r, blocks[2].tx[0], 0, 2)
  575 + should_receive_output(r, blocks[3].tx[0], 0, 1)
589 576 end
590 577  
591 578 it "should filter outputs for given addresses" do
592 579 @key2 = Bitcoin::Key.generate
593 580 block = create_block(@block.hash, false, [->(t) {
594 581 create_tx(t, @block.tx[0], 0, [[50e8, @key2]]) }], @key)
595   - @addr = @block.tx[0].out[0].parsed_script.get_address
596   - c = { name: "output", conf: 1, addresses: [ @key2.addr ] }
597   - r = send "monitor", channels: [ c ]
  582 +
  583 + r = send "monitor", channel: "output", conf: 1, addresses: [ @key2.addr ]
598 584 store_block @block
599 585 store_block block
600   - should_receive_output(r, c, block.tx[1], 0, 1)
  586 + should_receive_output(r, block.tx[1], 0, 1)
601 587 end
602 588  
603 589 end