diff --git a/bchain/coins/bch/bcashrpc.go b/bchain/coins/bch/bcashrpc.go index e3cd3b49..b148b019 100644 --- a/bchain/coins/bch/bcashrpc.go +++ b/bchain/coins/bch/bcashrpc.go @@ -103,11 +103,6 @@ func (b *BCashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { return nil, err } } - // XXX - // // optimization - // if height > 0 { - // return b.getBlockWithoutHeader(hash, height) - // } header, err := b.GetBlockHeader(hash) if err != nil { return nil, err @@ -146,42 +141,6 @@ func (b *BCashRPC) GetBlockRaw(hash string) ([]byte, error) { return hex.DecodeString(res.Result) } -// GetBlockList returns block with given hash by downloading block -// transactions one by one. -func (b *BCashRPC) GetBlockList(hash string) (*bchain.Block, error) { - glog.V(1).Info("rpc: getblock (verbose=1) ", hash) - - res := resGetBlockThin{} - req := cmdGetBlock{Method: "getblock"} - req.Params.BlockHash = hash - req.Params.Verbose = true - err := b.Call(&req, &res) - - if err != nil { - return nil, errors.Annotatef(err, "hash %v", hash) - } - if res.Error != nil { - if isErrBlockNotFound(res.Error) { - return nil, bchain.ErrBlockNotFound - } - return nil, errors.Annotatef(res.Error, "hash %v", hash) - } - - txs := make([]bchain.Tx, len(res.Result.Txids)) - for i, txid := range res.Result.Txids { - tx, err := b.GetTransaction(txid) - if err != nil { - return nil, err - } - txs[i] = *tx - } - block := &bchain.Block{ - BlockHeader: res.Result.BlockHeader, - Txs: txs, - } - return block, nil -} - // GetBlockFull returns block with given hash. func (b *BCashRPC) GetBlockFull(hash string) (*bchain.Block, error) { return nil, errors.New("Not implemented") diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index 59ff2777..5d3e1e90 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -24,6 +24,7 @@ func init() { blockChainFactories["btc"] = btc.NewBitcoinRPC blockChainFactories["btc-testnet"] = btc.NewBitcoinRPC blockChainFactories["zec"] = zec.NewZCashRPC + blockChainFactories["zec-testnet"] = zec.NewZCashRPC blockChainFactories["eth"] = eth.NewEthereumRPC blockChainFactories["eth-testnet"] = eth.NewEthereumRPC blockChainFactories["bch"] = bch.NewBCashRPC diff --git a/bchain/coins/btc/bitcoinrpc.go b/bchain/coins/btc/bitcoinrpc.go index 0abaad90..86bdae2d 100644 --- a/bchain/coins/btc/bitcoinrpc.go +++ b/bchain/coins/btc/bitcoinrpc.go @@ -505,42 +505,6 @@ func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) { return hex.DecodeString(res.Result) } -// GetBlockList returns block with given hash by downloading block -// transactions one by one. -func (b *BitcoinRPC) GetBlockList(hash string) (*bchain.Block, error) { - glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) - - res := resGetBlockThin{} - req := cmdGetBlock{Method: "getblock"} - req.Params.BlockHash = hash - req.Params.Verbosity = 1 - err := b.Call(&req, &res) - - if err != nil { - return nil, errors.Annotatef(err, "hash %v", hash) - } - if res.Error != nil { - if isErrBlockNotFound(res.Error) { - return nil, bchain.ErrBlockNotFound - } - return nil, errors.Annotatef(res.Error, "hash %v", hash) - } - - txs := make([]bchain.Tx, len(res.Result.Txids)) - for i, txid := range res.Result.Txids { - tx, err := b.GetTransaction(txid) - if err != nil { - return nil, err - } - txs[i] = *tx - } - block := &bchain.Block{ - BlockHeader: res.Result.BlockHeader, - Txs: txs, - } - return block, nil -} - // GetBlockFull returns block with given hash. func (b *BitcoinRPC) GetBlockFull(hash string) (*bchain.Block, error) { glog.V(1).Info("rpc: getblock (verbosity=2) ", hash) diff --git a/bchain/coins/zec/zcashparser.go b/bchain/coins/zec/zcashparser.go index 84ee8d1b..60c23276 100644 --- a/bchain/coins/zec/zcashparser.go +++ b/bchain/coins/zec/zcashparser.go @@ -3,6 +3,15 @@ package zec import ( "blockbook/bchain" "blockbook/bchain/coins/btc" + + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/wire" +) + +const ( + MainnetMagic wire.BitcoinNet = 0x6427e924 + TestnetMagic wire.BitcoinNet = 0xbff91afa + RegtestMagic wire.BitcoinNet = 0x5f3fe8aa ) // ZCashParser handle @@ -20,6 +29,26 @@ func NewZCashParser(c *btc.Configuration) *ZCashParser { } } +// GetChainParams contains network parameters for the main ZCash network, +// the regression test ZCash network, the test ZCash network and +// the simulation test ZCash network, in this order +func GetChainParams(chain string) *chaincfg.Params { + var params *chaincfg.Params + switch chain { + case "test": + params = &chaincfg.TestNet3Params + params.Net = TestnetMagic + case "regtest": + params = &chaincfg.RegressionNetParams + params.Net = RegtestMagic + default: + params = &chaincfg.MainNetParams + params.Net = MainnetMagic + } + + return params +} + // GetAddrIDFromVout returns internal address representation of given transaction output func (p *ZCashParser) GetAddrIDFromVout(output *bchain.Vout) ([]byte, error) { if len(output.ScriptPubKey.Addresses) != 1 { diff --git a/bchain/coins/zec/zcashrpc.go b/bchain/coins/zec/zcashrpc.go index c20aaf4c..ca368f10 100644 --- a/bchain/coins/zec/zcashrpc.go +++ b/bchain/coins/zec/zcashrpc.go @@ -26,16 +26,25 @@ func NewZCashRPC(config json.RawMessage, pushHandler func(bchain.NotificationTyp // Initialize initializes ZCashRPC instance. func (z *ZCashRPC) Initialize() error { - _, err := z.GetChainInfoAndInitializeMempool(z) + chainName, err := z.GetChainInfoAndInitializeMempool(z) if err != nil { return err } - z.Parser = NewZCashParser(z.ChainConfig) - z.Testnet = false - z.Network = "livenet" + params := GetChainParams(chainName) - glog.Info("rpc: block chain mainnet") + z.Parser = NewZCashParser(z.ChainConfig) + + // parameters for getInfo request + if params.Net == MainnetMagic { + z.Testnet = false + z.Network = "livenet" + } else { + z.Testnet = true + z.Network = "testnet" + } + + glog.Info("rpc: block chain ", params.Name) return nil } @@ -63,7 +72,7 @@ type resGetBlockThin struct { type resGetRawTransaction struct { Error *bchain.RPCError `json:"error"` - Result bchain.Tx `json:"result"` + Result json.RawMessage `json:"result"` } // getblockheader @@ -105,8 +114,8 @@ func (z *ZCashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { return nil, errors.Annotatef(res.Error, "hash %v", hash) } - txs := make([]bchain.Tx, len(res.Result.Txids)) - for i, txid := range res.Result.Txids { + txs := make([]bchain.Tx, 0, len(res.Result.Txids)) + for _, txid := range res.Result.Txids { tx, err := z.GetTransaction(txid) if err != nil { if isInvalidTx(err) { @@ -115,7 +124,7 @@ func (z *ZCashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { } return nil, err } - txs[i] = *tx + txs = append(txs, *tx) } block := &bchain.Block{ BlockHeader: res.Result.BlockHeader, @@ -160,7 +169,11 @@ func (z *ZCashRPC) GetTransaction(txid string) (*bchain.Tx, error) { if res.Error != nil { return nil, errors.Annotatef(res.Error, "txid %v", txid) } - return &res.Result, nil + tx, err := z.Parser.ParseTxFromJson(res.Result) + if err != nil { + return nil, errors.Annotatef(err, "txid %v", txid) + } + return tx, nil } // GetBlockHash returns hash of block in best-block-chain at given height. diff --git a/build/deb/debian/blockbook-zec-testnet.conffiles b/build/deb/debian/blockbook-zec-testnet.conffiles new file mode 100644 index 00000000..e5212acb --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.conffiles @@ -0,0 +1 @@ +/opt/coins/blockbook/zec-testnet/config/blockchaincfg.json diff --git a/build/deb/debian/blockbook-zec-testnet.cron.daily b/build/deb/debian/blockbook-zec-testnet.cron.daily new file mode 100644 index 00000000..4302f1a1 --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.cron.daily @@ -0,0 +1,2 @@ +#!/bin/sh +/opt/coins/blockbook/zec-testnet/bin/logrotate.sh diff --git a/build/deb/debian/blockbook-zec-testnet.dirs b/build/deb/debian/blockbook-zec-testnet.dirs new file mode 100644 index 00000000..ae909942 --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.dirs @@ -0,0 +1,2 @@ +/opt/coins/data/zec-testnet/blockbook +/opt/coins/blockbook/zec-testnet/logs diff --git a/build/deb/debian/blockbook-zec-testnet.install b/build/deb/debian/blockbook-zec-testnet.install new file mode 100755 index 00000000..f8640fdc --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.install @@ -0,0 +1,6 @@ +#!/usr/bin/dh-exec --with=install +blockbook /opt/coins/blockbook/zec-testnet/bin +cert /opt/coins/blockbook/zec-testnet +static /opt/coins/blockbook/zec-testnet +configs/zec-testnet.json => /opt/coins/blockbook/zec-testnet/config/blockchaincfg.json +logrotate.sh /opt/coins/blockbook/zec-testnet/bin diff --git a/build/deb/debian/blockbook-zec-testnet.links b/build/deb/debian/blockbook-zec-testnet.links new file mode 100644 index 00000000..40a764a9 --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.links @@ -0,0 +1,2 @@ +/opt/coins/blockbook/zec-testnet/cert/testcert.crt /opt/coins/blockbook/zec-testnet/cert/blockbook.crt +/opt/coins/blockbook/zec-testnet/cert/testcert.key /opt/coins/blockbook/zec-testnet/cert/blockbook.key diff --git a/build/deb/debian/blockbook-zec-testnet.postinst b/build/deb/debian/blockbook-zec-testnet.postinst new file mode 100644 index 00000000..fcbc854c --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.postinst @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u blockbook-zec &> /dev/null + then + useradd --system -M -U blockbook-zec -s /bin/false + fi + + for dir in /opt/coins/data/zec-testnet/blockbook /opt/coins/blockbook/zec-testnet/logs + do + if [ "$(stat -c '%U' $dir)" != "blockbook-zec" ] + then + chown -R blockbook-zec:blockbook-zec $dir + fi + done + ;; + +esac + +#DEBHELPER# diff --git a/build/deb/debian/blockbook-zec-testnet.service b/build/deb/debian/blockbook-zec-testnet.service new file mode 100644 index 00000000..ec7449fa --- /dev/null +++ b/build/deb/debian/blockbook-zec-testnet.service @@ -0,0 +1,43 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit blockbook-zec-testnet.service +# See "man systemd.service" for details. + +[Unit] +Description=Blockbook daemon (ZEC testnet) +After=network.target +Wants=zcash-zec-testnet.service + +[Service] +ExecStart=/opt/coins/blockbook/zec-testnet/bin/blockbook -coin=zec-testnet -blockchaincfg=/opt/coins/blockbook/zec-testnet/config/blockchaincfg.json -datadir=/opt/coins/data/zec-testnet/blockbook/db -sync -httpserver=:19032 -socketio=:19132 -certfile=/opt/coins/blockbook/zec-testnet/cert/blockbook -explorer=https://zec-testnet-bitcore1.trezor.io/ -log_dir=/opt/coins/blockbook/zec-testnet/logs +User=blockbook-zec +Type=simple +Restart=on-failure +WorkingDirectory=/opt/coins/blockbook/zec-testnet + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/build/deb/debian/control b/build/deb/debian/control index 424137cf..d2dc922e 100644 --- a/build/deb/debian/control +++ b/build/deb/debian/control @@ -20,6 +20,11 @@ Architecture: amd64 Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, zcash-zec Description: Satoshilabs blockbook server (ZCash mainnet) +Package: blockbook-zec-testnet +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, zcash-testnet +Description: Satoshilabs blockbook server (ZCash testnet) + Package: blockbook-bch Architecture: amd64 Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, bcash-bch diff --git a/configs/zec-testnet.json b/configs/zec-testnet.json new file mode 100644 index 00000000..663b08c5 --- /dev/null +++ b/configs/zec-testnet.json @@ -0,0 +1,8 @@ +{ + "rpcURL": "http://127.0.0.1:18032", + "rpcUser": "rpc", + "rpcPass": "rpc", + "rpcTimeout": 25, + "parse": true, + "zeroMQBinding": "tcp://127.0.0.1:48332" +} diff --git a/contrib/backends/zcash/debian/control b/contrib/backends/zcash/debian/control index 1f6e861c..aa94da62 100644 --- a/contrib/backends/zcash/debian/control +++ b/contrib/backends/zcash/debian/control @@ -9,3 +9,8 @@ Package: zcash-zec Architecture: amd64 Depends: ${shlibs:Depends}, ${misc:Depends}, logrotate Description: Satoshilabs packaged zcash server + +Package: zcash-testnet +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, logrotate +Description: Satoshilabs packaged zcash server diff --git a/contrib/backends/zcash/debian/zcash-testnet.conffiles b/contrib/backends/zcash/debian/zcash-testnet.conffiles new file mode 100644 index 00000000..0b9e398d --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.conffiles @@ -0,0 +1 @@ +/opt/coins/nodes/zcash/zec-testnet/zec-testnet.conf diff --git a/contrib/backends/zcash/debian/zcash-testnet.dirs b/contrib/backends/zcash/debian/zcash-testnet.dirs new file mode 100644 index 00000000..513c4b77 --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.dirs @@ -0,0 +1 @@ +/opt/coins/data/zec-testnet/zcash diff --git a/contrib/backends/zcash/debian/zcash-testnet.install b/contrib/backends/zcash/debian/zcash-testnet.install new file mode 100644 index 00000000..5f846680 --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.install @@ -0,0 +1,2 @@ +zcash/* /opt/coins/nodes/zcash/zec-testnet +zec-testnet.conf /opt/coins/nodes/zcash/zec-testnet diff --git a/contrib/backends/zcash/debian/zcash-testnet.logrotate b/contrib/backends/zcash/debian/zcash-testnet.logrotate new file mode 100644 index 00000000..8b075f62 --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.logrotate @@ -0,0 +1,10 @@ +/opt/coins/data/zec-testnet/zcash/testnet3/debug.log +/opt/coins/data/zec-testnet/zcash/testnet3/db.log +{ + rotate 7 + daily + compress + missingok + notifempty + copytruncate +} diff --git a/contrib/backends/zcash/debian/zcash-testnet.postinst b/contrib/backends/zcash/debian/zcash-testnet.postinst new file mode 100644 index 00000000..a5d155aa --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.postinst @@ -0,0 +1,22 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u zcash &> /dev/null + then + useradd --system -M -U zcash -s /bin/false + fi + + if [ "$(stat -c '%U' /opt/coins/data/zec-testnet/zcash)" != "zcash" ] + then + chown -R zcash:zcash /opt/coins/data/zec-testnet/zcash + fi + + HOME=/opt/coins/data/zec-testnet/zcash /opt/coins/nodes/zcash/zec-testnet/bin/zcash-fetch-params + ;; + +esac + +#DEBHELPER# diff --git a/contrib/backends/zcash/debian/zcash-testnet.service b/contrib/backends/zcash/debian/zcash-testnet.service new file mode 100644 index 00000000..39b9fc31 --- /dev/null +++ b/contrib/backends/zcash/debian/zcash-testnet.service @@ -0,0 +1,48 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit zcash-testnet.service +# See "man systemd.service" for details. + +# Note that almost all daemon options could be specified in +# /opt/coins/nodes/zcash/zec-testnet/zec-testnet.conf + +[Unit] +Description=ZCash daemon (testnet) +After=network.target + +[Service] +Environment="HOME=/opt/coins/data/zec-testnet/zcash" +ExecStart=/opt/coins/nodes/zcash/zec-testnet/bin/zcashd -datadir=/opt/coins/data/zec-testnet/zcash -conf=/opt/coins/nodes/zcash/zec-testnet/zec-testnet.conf -pid=/run/zcashd/zec-testnet.pid +# Creates /run/zcashd owned by zcash +RuntimeDirectory=zcashd +User=zcash +Type=forking +PIDFile=/run/zcashd/zec-testnet.pid +Restart=on-failure + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +# MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/contrib/backends/zcash/zec-testnet.conf b/contrib/backends/zcash/zec-testnet.conf new file mode 100644 index 00000000..100bd5b1 --- /dev/null +++ b/contrib/backends/zcash/zec-testnet.conf @@ -0,0 +1,14 @@ +daemon=1 +server=1 +nolisten=1 +rpcuser=rpc +rpcpassword=rpc +rpcport=18032 +txindex=1 +testnet=1 +addnode=testnet.z.cash +rpcworkqueue=32 +zmqpubhashtx=tcp://127.0.0.1:48332 +zmqpubhashblock=tcp://127.0.0.1:48332 +zmqpubrawblock=tcp://127.0.0.1:48332 +zmqpubrawtx=tcp://127.0.0.1:48332