Merge branch 'zcash'

This commit is contained in:
Jakub Matys 2018-05-29 09:11:21 +02:00
commit 78d7ff3a96
22 changed files with 248 additions and 87 deletions

View File

@ -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")

View File

@ -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

View File

@ -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)

View File

@ -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 {

View File

@ -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.

View File

@ -0,0 +1 @@
/opt/coins/blockbook/zec-testnet/config/blockchaincfg.json

View File

@ -0,0 +1,2 @@
#!/bin/sh
/opt/coins/blockbook/zec-testnet/bin/logrotate.sh

View File

@ -0,0 +1,2 @@
/opt/coins/data/zec-testnet/blockbook
/opt/coins/blockbook/zec-testnet/logs

View File

@ -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

View File

@ -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

View File

@ -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#

View File

@ -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

View File

@ -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

8
configs/zec-testnet.json Normal file
View File

@ -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"
}

View File

@ -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

View File

@ -0,0 +1 @@
/opt/coins/nodes/zcash/zec-testnet/zec-testnet.conf

View File

@ -0,0 +1 @@
/opt/coins/data/zec-testnet/zcash

View File

@ -0,0 +1,2 @@
zcash/* /opt/coins/nodes/zcash/zec-testnet
zec-testnet.conf /opt/coins/nodes/zcash/zec-testnet

View File

@ -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
}

View File

@ -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#

View File

@ -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

View File

@ -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