From 110bd6f6a6ee923c7946b94c6d0ba7bdfd28c6a1 Mon Sep 17 00:00:00 2001 From: Jakub Matys Date: Tue, 22 May 2018 12:33:38 +0200 Subject: [PATCH] Added testnet for ZCash --- bchain/coins/blockchain.go | 1 + bchain/coins/zec/zcashparser.go | 29 +++++++++++ bchain/coins/zec/zcashrpc.go | 20 +++++--- .../debian/blockbook-zec-testnet.conffiles | 1 + .../debian/blockbook-zec-testnet.cron.daily | 2 + build/deb/debian/blockbook-zec-testnet.dirs | 2 + .../deb/debian/blockbook-zec-testnet.install | 6 +++ build/deb/debian/blockbook-zec-testnet.links | 2 + .../deb/debian/blockbook-zec-testnet.postinst | 23 +++++++++ .../deb/debian/blockbook-zec-testnet.service | 43 +++++++++++++++++ build/deb/debian/control | 5 ++ configs/zec-testnet.json | 8 ++++ contrib/backends/zcash/debian/control | 5 ++ .../zcash/debian/zcash-testnet.conffiles | 1 + .../backends/zcash/debian/zcash-testnet.dirs | 1 + .../zcash/debian/zcash-testnet.install | 2 + .../zcash/debian/zcash-testnet.logrotate | 10 ++++ .../zcash/debian/zcash-testnet.postinst | 22 +++++++++ .../zcash/debian/zcash-testnet.service | 48 +++++++++++++++++++ contrib/backends/zcash/zec-testnet.conf | 13 +++++ 20 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 build/deb/debian/blockbook-zec-testnet.conffiles create mode 100644 build/deb/debian/blockbook-zec-testnet.cron.daily create mode 100644 build/deb/debian/blockbook-zec-testnet.dirs create mode 100755 build/deb/debian/blockbook-zec-testnet.install create mode 100644 build/deb/debian/blockbook-zec-testnet.links create mode 100644 build/deb/debian/blockbook-zec-testnet.postinst create mode 100644 build/deb/debian/blockbook-zec-testnet.service create mode 100644 configs/zec-testnet.json create mode 100644 contrib/backends/zcash/debian/zcash-testnet.conffiles create mode 100644 contrib/backends/zcash/debian/zcash-testnet.dirs create mode 100644 contrib/backends/zcash/debian/zcash-testnet.install create mode 100644 contrib/backends/zcash/debian/zcash-testnet.logrotate create mode 100644 contrib/backends/zcash/debian/zcash-testnet.postinst create mode 100644 contrib/backends/zcash/debian/zcash-testnet.service create mode 100644 contrib/backends/zcash/zec-testnet.conf 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/zec/zcashparser.go b/bchain/coins/zec/zcashparser.go index 039542ad..39f26e12 100644 --- a/bchain/coins/zec/zcashparser.go +++ b/bchain/coins/zec/zcashparser.go @@ -2,6 +2,15 @@ package zec import ( "blockbook/bchain" + + "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 @@ -14,6 +23,26 @@ func NewZCashParser() *ZCashParser { return &ZCashParser{&bchain.BaseParser{AddressFactory: bchain.NewBaseAddress}} } +// 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 a5575a70..f347ef69 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.Testnet = false - z.Network = "livenet" + params := GetChainParams(chainName) - glog.Info("rpc: block chain mainnet") + z.Parser = NewZCashParser() + + // 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 } @@ -89,7 +98,6 @@ func (z *ZCashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { return nil, err } } - glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) res := resGetBlockThin{} 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..1521b6dd --- /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=:18235 -socketio=:18236 -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..747de52b --- /dev/null +++ b/configs/zec-testnet.json @@ -0,0 +1,8 @@ +{ + "rpcURL": "http://127.0.0.1:18232", + "rpcUser": "rpc", + "rpcPass": "rpc", + "rpcTimeout": 25, + "parse": true, + "zeroMQBinding": "tcp://127.0.0.1:18234" +} 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..114a3dc4 --- /dev/null +++ b/contrib/backends/zcash/zec-testnet.conf @@ -0,0 +1,13 @@ +daemon=1 +server=1 +rpcuser=rpc +rpcpassword=rpc +rpcport=18232 +txindex=1 +testnet=1 +addnode=testnet.z.cash +rpcworkqueue=32 +zmqpubhashtx=tcp://127.0.0.1:18234 +zmqpubhashblock=tcp://127.0.0.1:18234 +zmqpubrawblock=tcp://127.0.0.1:18234 +zmqpubrawtx=tcp://127.0.0.1:18234