From 72f9fc5943a6a8d145245bd4fae68f1e5cbde8f2 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Sat, 2 Jul 2022 16:41:38 +0100 Subject: [PATCH] Add Feathercoin support --- bchain/coins/blockchain.go | 2 + bchain/coins/feathercoin/feathercoinparser.go | 65 +++++++++++ .../feathercoin/feathercoinparser_test.go | 71 ++++++++++++ bchain/coins/feathercoin/feathercoinrpc.go | 54 +++++++++ build/docker/bin/Dockerfile | 9 +- build/templates/blockbook/debian/rules | 2 +- configs/coins/feathercoin.json | 70 ++++++++++++ docs/ports.md | 1 + tests/rpc/testdata/feathercoin.json | 32 ++++++ tests/sync/testdata/feathercoin.json | 103 ++++++++++++++++++ tests/tests.json | 5 + 11 files changed, 411 insertions(+), 3 deletions(-) create mode 100644 bchain/coins/feathercoin/feathercoinparser.go create mode 100644 bchain/coins/feathercoin/feathercoinparser_test.go create mode 100644 bchain/coins/feathercoin/feathercoinrpc.go create mode 100644 configs/coins/feathercoin.json create mode 100644 tests/rpc/testdata/feathercoin.json create mode 100644 tests/sync/testdata/feathercoin.json diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index 1fe7baae17..7d71041382 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -26,6 +26,7 @@ import ( "github.com/trezor/blockbook/bchain/coins/dogecoin" "github.com/trezor/blockbook/bchain/coins/ecash" "github.com/trezor/blockbook/bchain/coins/eth" + "github.com/trezor/blockbook/bchain/coins/feathercoin" "github.com/trezor/blockbook/bchain/coins/firo" "github.com/trezor/blockbook/bchain/coins/flo" "github.com/trezor/blockbook/bchain/coins/fujicoin" @@ -126,6 +127,7 @@ func init() { BlockChainFactories["BitZeny"] = bitzeny.NewBitZenyRPC BlockChainFactories["Trezarcoin"] = trezarcoin.NewTrezarcoinRPC BlockChainFactories["ECash"] = ecash.NewECashRPC + BlockChainFactories["Feathercoin"] = feathercoin.NewFeathercoinRPC } // GetCoinNameFromConfig gets coin name and coin shortcut from config file diff --git a/bchain/coins/feathercoin/feathercoinparser.go b/bchain/coins/feathercoin/feathercoinparser.go new file mode 100644 index 0000000000..39740fefa1 --- /dev/null +++ b/bchain/coins/feathercoin/feathercoinparser.go @@ -0,0 +1,65 @@ +package feathercoin + +import ( + "github.com/martinboehm/btcd/wire" + "github.com/martinboehm/btcutil/chaincfg" + "github.com/trezor/blockbook/bchain" + "github.com/trezor/blockbook/bchain/coins/btc" +) + +// magic numbers +const ( + MainnetMagic wire.BitcoinNet = 0x211a1541 +) + +// chain parameters +var ( + MainNetParams chaincfg.Params +) + +func init() { + MainNetParams = chaincfg.MainNetParams + MainNetParams.Net = MainnetMagic + MainNetParams.PubKeyHashAddrID = []byte{14} + MainNetParams.ScriptHashAddrID = []byte{5} + MainNetParams.Bech32HRPSegwit = "fc" +} + +// FeathercoinParser handle +type FeathercoinParser struct { + *btc.BitcoinLikeParser + baseparser *bchain.BaseParser +} + +// NewFeathercoinParser returns new FeathercoinParser instance +func NewFeathercoinParser(params *chaincfg.Params, c *btc.Configuration) *FeathercoinParser { + return &FeathercoinParser{ + BitcoinLikeParser: btc.NewBitcoinLikeParser(params, c), + baseparser: &bchain.BaseParser{}, + } +} + +// GetChainParams contains network parameters for the main Feathercoin network, +// and the test Feathercoin network +func GetChainParams(chain string) *chaincfg.Params { + if !chaincfg.IsRegistered(&chaincfg.MainNetParams) { + chaincfg.RegisterBitcoinParams() + } + if !chaincfg.IsRegistered(&MainNetParams) { + err := chaincfg.Register(&MainNetParams) + if err != nil { + panic(err) + } + } + return &MainNetParams +} + +// PackTx packs transaction to byte array using protobuf +func (p *FeathercoinParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) { + return p.baseparser.PackTx(tx, height, blockTime) +} + +// UnpackTx unpacks transaction from protobuf byte array +func (p *FeathercoinParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) { + return p.baseparser.UnpackTx(buf) +} diff --git a/bchain/coins/feathercoin/feathercoinparser_test.go b/bchain/coins/feathercoin/feathercoinparser_test.go new file mode 100644 index 0000000000..a2398a8cc7 --- /dev/null +++ b/bchain/coins/feathercoin/feathercoinparser_test.go @@ -0,0 +1,71 @@ +//go:build unittest + +package feathercoin + +import ( + "encoding/hex" + "os" + "reflect" + "testing" + + "github.com/martinboehm/btcutil/chaincfg" + "github.com/trezor/blockbook/bchain/coins/btc" +) + +func TestMain(m *testing.M) { + c := m.Run() + chaincfg.ResetParams() + os.Exit(c) +} + +func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { + type args struct { + address string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "pubkeyhash1", + args: args{address: "6z2aHdD9qpg5J2MW8dsbKTDTekVFG37y2S"}, + want: "76a914de9fd965bc63fc198fd7c37de4328ce0bdec88a488ac", + wantErr: false, + }, + { + name: "pubkeyhash2", + args: args{address: "729vtmzauQdywB94HAJbtH3zLmUPrmfQws"}, + want: "76a914f5f436ad39d4e2b4ecf8c45cafa068b4f7a5a05e88ac", + wantErr: false, + }, + { + name: "scripthash1", + args: args{address: "3FfGSMgjEdR7UK8t7WyqSZJy1LEUVTgrFx"}, + want: "a914993d0b74ca784f2817a82b8991bb373e2e6c04eb87", + wantErr: false, + }, + { + name: "scripthash2", + args: args{address: "3MCEGQuc8gfCavDBDuUS4Lz1jyanSs3ijG"}, + want: "a914d5f0c17f0f028ea834ad8fda350f8f5b745ee10387", + wantErr: false, + }, + } + parser := NewFeathercoinParser(GetChainParams("main"), &btc.Configuration{}) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parser.GetAddrDescFromAddress(tt.args.address) + if (err != nil) != tt.wantErr { + t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) + return + } + h := hex.EncodeToString(got) + if !reflect.DeepEqual(h, tt.want) { + t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) + } + }) + } +} diff --git a/bchain/coins/feathercoin/feathercoinrpc.go b/bchain/coins/feathercoin/feathercoinrpc.go new file mode 100644 index 0000000000..5629060d75 --- /dev/null +++ b/bchain/coins/feathercoin/feathercoinrpc.go @@ -0,0 +1,54 @@ +package feathercoin + +import ( + "encoding/json" + + "github.com/golang/glog" + "github.com/trezor/blockbook/bchain" + "github.com/trezor/blockbook/bchain/coins/btc" +) + +// FeathercoinRPC is an interface to JSON-RPC bitcoind service. +type FeathercoinRPC struct { + *btc.BitcoinRPC +} + +// NewFeathercoinRPC returns new FeathercoinRPC instance. +func NewFeathercoinRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { + b, err := btc.NewBitcoinRPC(config, pushHandler) + if err != nil { + return nil, err + } + + s := &FeathercoinRPC{ + b.(*btc.BitcoinRPC), + } + s.RPCMarshaler = btc.JSONMarshalerV2{} + s.ChainConfig.SupportsEstimateFee = false + + return s, nil +} + +// Initialize initializes FeathercoinRPC instance. +func (b *FeathercoinRPC) Initialize() error { + ci, err := b.GetChainInfo() + if err != nil { + return err + } + chainName := ci.Chain + + glog.Info("Chain name ", chainName) + params := GetChainParams(chainName) + + // always create parser + b.Parser = NewFeathercoinParser(params, b.ChainConfig) + + // parameters for getInfo request + b.Testnet = false + b.Network = "livenet" + + glog.Info("rpc: block chain ", params.Name) + + return nil +} + diff --git a/build/docker/bin/Dockerfile b/build/docker/bin/Dockerfile index d11c783896..47faf0eae7 100644 --- a/build/docker/bin/Dockerfile +++ b/build/docker/bin/Dockerfile @@ -1,6 +1,5 @@ # initialize from the image defined by BASE_IMAGE -ARG BASE_IMAGE -FROM $BASE_IMAGE +FROM ubuntu:18.04 ARG DEBIAN_FRONTEND=noninteractive ARG PORTABLE_ROCKSDB @@ -11,6 +10,12 @@ RUN apt-get update && \ liblz4-dev graphviz && \ apt-get clean +RUN cd /opt && wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz && \ + tar zxvf libsodium-1.0.18-stable.tar.gz && cd libsodium-stable && \ + ./configure && make check && make install && ldconfig +RUN cd /opt && wget https://github.com/zeromq/libzmq/releases/download/v4.2.2/zeromq-4.2.2.tar.gz && \ + tar -xvf zeromq-4.2.2.tar.gz && cd zeromq-4.2.2 && ./configure && make && make install + ENV GOLANG_VERSION=go1.17.1.linux-amd64 ENV ROCKSDB_VERSION=v6.22.1 ENV GOPATH=/go diff --git a/build/templates/blockbook/debian/rules b/build/templates/blockbook/debian/rules index bc0f8a1a6f..af943413ed 100755 --- a/build/templates/blockbook/debian/rules +++ b/build/templates/blockbook/debian/rules @@ -14,5 +14,5 @@ override_dh_systemd_start: override_dh_installinit: override_dh_shlibdeps: - dh_shlibdeps -- --warnings=0 + dh_shlibdeps -- --warnings=0 --ignore-missing-info {{end}} diff --git a/configs/coins/feathercoin.json b/configs/coins/feathercoin.json new file mode 100644 index 0000000000..acf98131fe --- /dev/null +++ b/configs/coins/feathercoin.json @@ -0,0 +1,70 @@ +{ + "coin": { + "name": "Feathercoin", + "shortcut": "FTC", + "label": "Feathercoin", + "alias": "feathercoin" + }, + "ports": { + "backend_rpc": 8098, + "backend_message_queue": 38398, + "blockbook_internal": 9098, + "blockbook_public": 9198 + }, + "ipc": { + "rpc_url_template": "http://127.0.0.1:{{.Ports.BackendRPC}}", + "rpc_user": "rpc", + "rpc_pass": "rpc", + "rpc_timeout": 25, + "message_queue_binding_template": "tcp://127.0.0.1:{{.Ports.BackendMessageQueue}}" + }, + "backend": { + "package_name": "backend-feathercoin", + "package_revision": "satoshilabs-1", + "system_user": "feathercoin", + "version": "0.19.1", + "binary_url": "https://github.com/FeatherCoin/Feathercoin/releases/download/v0.19.1/feathercoin-0.19.1-x86_64-linux-gnu.tar.gz", + "verification_type": "sha256", + "verification_source": "56b0ec38ed5643731ba514623785e95f38a25373640c73d6724843b2afb6fa25", + "extract_command": "tar -C backend --strip 1 -xf", + "exclude_files": [ + "bin/feathercoin-qt" + ], + "exec_command_template": "{{.Env.BackendInstallPath}}/{{.Coin.Alias}}/bin/feathercoind -datadir={{.Env.BackendDataPath}}/{{.Coin.Alias}}/backend -conf={{.Env.BackendInstallPath}}/{{.Coin.Alias}}/{{.Coin.Alias}}.conf -pid=/run/{{.Coin.Alias}}/{{.Coin.Alias}}.pid", + "logrotate_files_template": "{{.Env.BackendDataPath}}/{{.Coin.Alias}}/backend/*.log", + "postinst_script_template": "", + "service_type": "forking", + "service_additional_params_template": "", + "protect_memory": true, + "mainnet": true, + "server_config_file": "bitcoin_like.conf", + "client_config_file": "bitcoin_like_client.conf", + "additional_params": { + "whitelist": "127.0.0.1" + } + }, + "blockbook": { + "package_name": "blockbook-feathercoin", + "system_user": "blockbook-feathercoin", + "internal_binding_template": ":{{.Ports.BlockbookInternal}}", + "public_binding_template": ":{{.Ports.BlockbookPublic}}", + "explorer_url": "", + "additional_params": "", + "block_chain": { + "parse": true, + "mempool_workers": 8, + "mempool_sub_workers": 2, + "block_addresses_to_keep": 300, + "xpub_magic": 76069926, + "slip44": 8, + "additional_params": { + "fiat_rates": "coingecko", + "fiat_rates_params": "{\"url\": \"https://api.coingecko.com/api/v3\", \"coin\": \"feathercoin\", \"periodSeconds\": 60}" + } + } + }, + "meta": { + "package_maintainer": "IT", + "package_maintainer_email": "it@satoshilabs.com" + } +} diff --git a/docs/ports.md b/docs/ports.md index 7d7a6b11a4..1080ce283f 100644 --- a/docs/ports.md +++ b/docs/ports.md @@ -46,6 +46,7 @@ | BitZeny | 9095 | 9195 | 8095 | 38395 | | Trezarcoin | 9096 | 9196 | 8096 | 38396 | | eCash | 9097 | 9197 | 8097 | 38397 | +| Feathercoin | 9098 | 9198 | 8098 | 38398 | | Bitcoin Signet | 19020 | 19120 | 18020 | 48320 | | Bitcoin Regtest | 19021 | 19121 | 18021 | 48321 | | Ethereum Goerli | 19026 | 19126 | 18026 | 48326 p2p | diff --git a/tests/rpc/testdata/feathercoin.json b/tests/rpc/testdata/feathercoin.json new file mode 100644 index 0000000000..82cfd77a50 --- /dev/null +++ b/tests/rpc/testdata/feathercoin.json @@ -0,0 +1,32 @@ +{ + "blockHash": "02a020f2615c46baab621129cc48ba224cb603e18d54f786d247eb41d98e9635", + "blockHeight": 4232070, + "blockTime": 1656489414, + "blockTxs": [ + "e79b5f89d8bc061ec1be0c4e1981d300c28a71308f0fd6982db8f328e80d3b73" + ], + "txDetails": { + "e79b5f89d8bc061ec1be0c4e1981d300c28a71308f0fd6982db8f328e80d3b73": { + "txid": "e79b5f89d8bc061ec1be0c4e1981d300c28a71308f0fd6982db8f328e80d3b73", + "version": 2, + "vin": [ + { + "coinbase": "0386934004c605bc62088100036e2b0000007969696d7000", + "sequence": 0 + } + ], + "vout": [ + { + "value": 20.00000000, + "n": 0, + "scriptPubKey": { + "hex": "a914eb95c76c2190549975875f40819b5b5d8a97da8a87" + } + } + ], + "hex": "02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff180386934004c605bc62088100036e2b0000007969696d70000000000001009435770000000017a914eb95c76c2190549975875f40819b5b5d8a97da8a8700000000", + "time": 1656489414, + "blocktime": 1656489414 + } + } +} diff --git a/tests/sync/testdata/feathercoin.json b/tests/sync/testdata/feathercoin.json new file mode 100644 index 0000000000..0cd6bc3605 --- /dev/null +++ b/tests/sync/testdata/feathercoin.json @@ -0,0 +1,103 @@ +{ + "connectBlocks": { + "syncRanges": [ + { "lower": 4232089, "upper": 4232109 } + ], + "blocks": { + "4232109": { + "hash": "3983fa05b34a88b878a9289e4ae304bc7accc45ab0d276de1833dee78ac8eec4", + "height": 4232109, + "noTxs": 1, + "txDetails": [ + { + "txid": "c9e855ada5210cf8247269796b08c37eee06ddd440a3732096e4979b5ed44ef2", + "version": 2, + "vin": [ + { + "coinbase": "03ad9340047813bc620881000355540000007969696d7000", + "sequence": 0 + } + ], + "vout": [ + { + "value": 20.00000000, + "n": 0, + "scriptPubKey": { + "hex": "a914ed73c1c101d588223b545ab0bf99a010d1da828787" + } + } + ], + "hex": "02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1803ad9340047813bc620881000355540000007969696d70000000000001009435770000000017a914ed73c1c101d588223b545ab0bf99a010d1da82878700000000", + "time": 1656492920, + "blocktime": 1656492920 + } + ] + }, + "4232092": { + "noTxs": 1, + "height": 4232092, + "hash": "c5e9267bbb9c8014642af90dd5820b8787eca77001e9f0faf301ed84ca774742", + "txDetails": [ + { + "txid": "5dbc5b80f2dbdecf0a6bdf1c7eef935198ea0ddaa289e9dba2a710f2bd14e0ae", + "version": 2, + "vin": [ + { + "coinbase": "03a6934004e411bc62088100035d210000007969696d7000", + "sequence": 0 + } + ], + "vout": [ + { + "value": 20.00000000, + "n": 0, + "scriptPubKey": { + "hex": "a914eb95c76c2190549975875f40819b5b5d8a97da8a87" + } + } + ], + "hex": "02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff18039c934004910fbc62088100036e0d0000007969696d70000000000001009435770000000017a914eb95c76c2190549975875f40819b5b5d8a97da8a8700000000", + "time": 1656491921, + "blocktime": 1656491921 + } + ] + } + } + }, + "handleFork": { + "syncRanges": [ + { + "lower": 1303790, + "upper": 1303796 + } + ], + "fakeBlocks": { + "1303794": { + "height": 1303794, + "hash": "c669716c44eb8ec04ba57210197ec55796f6ca2d84a39aaac37e5609e73a4da1" + }, + "1303795": { + "height": 1303795, + "hash": "24be474670ce1db3c34c5c4d077aafba14ba6d406023037eb445b3996cbe8bb1" + }, + "1303796": { + "height": 1303796, + "hash": "8919bab7d23acb214e841ebe24bd7c52b06774abc1ac9d4685c4ef220aed7dde" + } + }, + "realBlocks": { + "1303794": { + "height": 1303794, + "hash": "186daaeb62fa64179e95f7d8941102b1f96f71aee8fa83175ddbdb276871a710" + }, + "1303795": { + "height": 1303795, + "hash": "2e26261bbd964f5f44946a44fb0269802860ab65163a4edb6dd6da54ab9719e0" + }, + "1303796": { + "height": 1303796, + "hash": "9c22576a9defad9cc5cf27ed479c2175d4eb396d1b7706bfd5066add9b0366d1" + } + } + } +} diff --git a/tests/tests.json b/tests/tests.json index 82da03c0e8..3679470404 100644 --- a/tests/tests.json +++ b/tests/tests.json @@ -239,5 +239,10 @@ "rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync", "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"], "sync": ["ConnectBlocksParallel", "ConnectBlocks", "HandleFork"] + }, + "feathercoin": { + "rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync", + "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"], + "sync": ["ConnectBlocksParallel", "ConnectBlocks", "HandleFork"] } }