bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

allow extending BlockHeader

Open Vasiliy-Bondarenko opened this issue 6 years ago • 2 comments

to allow extending BlockHeader for ZCash

Vasiliy-Bondarenko avatar Apr 12 '18 05:04 Vasiliy-Bondarenko

Sorry, this one made me pause for a while, and I regret not getting back to it sooner!

Any reason this can't be achieved with a complementary class ZcashBlockHeader which implements BlockHeaderInterface? That way you avoid the BlockHeader constructor checks, and we can avoid making private properties protected (part of the API, as people can hook into them..)

afk11 avatar Jul 09 '18 17:07 afk11

there is no way to hook into private properties, so it can not be considered as a part of api. it is completely hidden from the users.

you are right. i used decorator pattern to "extend" BlockHeader. but i still think it would be better to use protected props rather then private to allow direct extension.

<?php namespace ZCash;

use BitWasp\Bitcoin\Block\BlockHeader;
use BitWasp\Bitcoin\Block\BlockHeaderInterface;
use BitWasp\Bitcoin\Serializable;
use BitWasp\Buffertools\BufferInterface;

class ZCashBlockHeader extends Serializable implements BlockHeaderInterface
{
    /** @var string */
    protected $zcashNonce;
    /** @var BlockHeader */
    protected $blockHeader;

    public function __construct(int $version, BufferInterface $prevBlock, BufferInterface $merkleRoot, int $timestamp, int $bits, string $zcashNonce)
    {
        $this->blockHeader = new BlockHeader($version, $prevBlock, $merkleRoot, $timestamp, $bits, 0);
        $this->zcashNonce = $zcashNonce;
    }

    /**
     * {@inheritdoc}
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce()
     */
    public function getNonce(): int
    {
        throw new ZCashGetNonceException("ZCash does not have integer nonce. Call ->getZcashNonce() method instead.");
    }

    public function getZcashNonce(): string
    {
        return $this->zcashNonce;
    }

    public function __call($name, $arguments)
    {
        $this->blockHeader->$name(...$arguments);
    }
}

Vasiliy-Bondarenko avatar Jul 10 '18 04:07 Vasiliy-Bondarenko