require 'spec_helper'

describe Tapyrus::Message::CmpctBlock do
  describe '#parse_from_payload' do
    subject do
      Tapyrus::Message::CmpctBlock.parse_from_payload(
        '01000000728d6a3b0bcf01ea66f47935ce0bee26ed45202322f7793e83755a3e0e17e1d9ee5273db93bbf5fc80acf6a806cf85c8c2a1493e0a56569d128f25aa75827013faf623370d962c6d17db2ef30b33796fa18775506533babfe07abdb0f9757ebde286235f00403cf0bd0b83f4760beefd2f5bc84933c2f0032f457d3a4949783c9a967e4222730d5a76a8a18fb00ece75d7af27b4febff0ed8b8e8b9d3598cfa028843b4c2de6000000000000000002c5b863d439170248ee55b0bf0400010000000100000000000000000000000000000000000000000000000000000000000000008000000000ffffffff0100f2052a0100000001510000000001010000000149d471b8493f639fc445333fbb1ed975e6470ab26150b0fe6fb591364be145a800000000000000000001305dcd1d000000001f517551755175517551755175517551755175517551755175517551755175510000000000010000000167fb6d0769ee3bc5a1253fb236c89af03fe55421ede34113c2406a6336f66dd6000000000000000000014859cd1d000000001f5175517551755175517551755175517551755175517551755175517551755100000000000100000001c354c7c1aa53e65828b2974d8322e7688d9537712957609537596cf47ccbee2f000000000000000000016055cd1d000000001f5175517551755175517551755175517551755175517551755175517551755100000000'
          .htb
      )
    end
    it 'should be parsed.' do
      expect(subject.header_and_short_ids.header.block_hash).to eq(
        '962a5d4b1be807e972185b15660f268eb384e9b7e2409f8c1d0af330c6243b17'
      )
      expect(subject.header_and_short_ids.header.to_hex).to eq(
        '01000000728d6a3b0bcf01ea66f47935ce0bee26ed45202322f7793e83755a3e0e17e1d9ee5273db93bbf5fc80acf6a806cf85c8c2a1493e0a56569d128f25aa75827013faf623370d962c6d17db2ef30b33796fa18775506533babfe07abdb0f9757ebde286235f00403cf0bd0b83f4760beefd2f5bc84933c2f0032f457d3a4949783c9a967e4222730d5a76a8a18fb00ece75d7af27b4febff0ed8b8e8b9d3598cfa028843b4c2de6'
      )
      expect(subject.header_and_short_ids.nonce).to eq(0)
      expect(subject.header_and_short_ids.short_ids[0]).to eq(25_537_143_879_877)
      expect(subject.header_and_short_ids.short_ids[1]).to eq(210_764_076_828_674)
      expect(subject.header_and_short_ids.short_ids.size).to eq(2)
      expect(subject.header_and_short_ids.prefilled_txn.size).to eq(4)
      expect(subject.header_and_short_ids.prefilled_txn.first.index).to eq(0)
      expect(subject.header_and_short_ids.siphash_key.bth).to eq('d168e2c95b1e1a5c6edbb73b7d7b3874')
      expect(subject.header_and_short_ids.prefilled_txn.first.tx.txid).to eq(
        'fd6c9bcdc9d1070cb40c29e1764c84e27c18391cb5d9e89305e95514cb1129b4'
      )
      expect(subject.to_hex).to eq(
        '01000000728d6a3b0bcf01ea66f47935ce0bee26ed45202322f7793e83755a3e0e17e1d9ee5273db93bbf5fc80acf6a806cf85c8c2a1493e0a56569d128f25aa75827013faf623370d962c6d17db2ef30b33796fa18775506533babfe07abdb0f9757ebde286235f00403cf0bd0b83f4760beefd2f5bc84933c2f0032f457d3a4949783c9a967e4222730d5a76a8a18fb00ece75d7af27b4febff0ed8b8e8b9d3598cfa028843b4c2de6000000000000000002c5b863d439170248ee55b0bf0400010000000100000000000000000000000000000000000000000000000000000000000000008000000000ffffffff0100f2052a0100000001510000000001010000000149d471b8493f639fc445333fbb1ed975e6470ab26150b0fe6fb591364be145a800000000000000000001305dcd1d000000001f517551755175517551755175517551755175517551755175517551755175510000000000010000000167fb6d0769ee3bc5a1253fb236c89af03fe55421ede34113c2406a6336f66dd6000000000000000000014859cd1d000000001f5175517551755175517551755175517551755175517551755175517551755100000000000100000001c354c7c1aa53e65828b2974d8322e7688d9537712957609537596cf47ccbee2f000000000000000000016055cd1d000000001f5175517551755175517551755175517551755175517551755175517551755100000000'
      )
    end
  end

  describe '#from_block' do
    let(:block) do
      Tapyrus::Message::Block.parse_from_payload(
        load_block('75be7b3b19e07f2c3644523016132db7e7e67063b3f5abbaa420cafc8a44557f').htb
      ).to_block
    end

    context 'version 1' do
      subject { Tapyrus::Message::CmpctBlock.from_block(block, 7_907_921_748_630_648_002) }
      it 'should generate CmpctBlock.' do
        expect(subject.header_and_short_ids.short_ids[0]).to eq(254_701_722_717_646)
        expect(subject.header_and_short_ids.short_ids.size).to eq(1)
        expect(subject.header_and_short_ids.prefilled_txn.size).to eq(1)
        expect(subject.to_hex).to eq(
          '01000000f62d08535b0df780676256ad4f6dbb7ac5c9b8e86150e26425330b853d8796a2abd52ce318e5c5d6c3aeb3aea278ebf7fc8b8fe8cb5535543a76d4fd903589742ecee1ba9a3d25123dada6041c6bafff1423b3dd4f561816adcf245736004698f1f40f5f00403b9b731cb77c87078c0bdc8f1f2dc91406b42f082f9d8f095be16b5b0dd68f549b5ad1dda9e01ff87d022404b1f80dd40d40e0d3accc69fb9180fd8db30b5064c234b7b2ee94be6d01cecdb45da6e7010001000000010000000000000000000000000000000000000000000000000000000000000000df3a00000502df3a0101ffffffff0180889e2a010000001976a9146713b478d99432aac667b7d8e87f9d06edca03bb88ac00000000'
        )
      end
    end
  end
end
