Merge branch 'params-registration' into tests

This commit is contained in:
Jakub Matys 2018-10-02 22:11:07 +02:00
commit e102c81577
27 changed files with 342 additions and 255 deletions

28
Gopkg.lock generated
View File

@ -61,12 +61,6 @@
revision = "1d4478f51bed434f1dadf96dcd9b43aabac66795" revision = "1d4478f51bed434f1dadf96dcd9b43aabac66795"
version = "v1.7" version = "v1.7"
[[projects]]
branch = "master"
name = "github.com/erikdubbelboer/gspt"
packages = ["."]
revision = "e39e726e09cc23d1ccf13b36ce10dbdb4a4510e0"
[[projects]] [[projects]]
name = "github.com/ethereum/go-ethereum" name = "github.com/ethereum/go-ethereum"
packages = [".","common","common/hexutil","common/math","core/types","crypto","crypto/secp256k1","crypto/sha3","ethclient","ethdb","log","metrics","p2p/netutil","params","rlp","rpc","trie"] packages = [".","common","common/hexutil","common/math","core/types","crypto","crypto/secp256k1","crypto/sha3","ethclient","ethdb","log","metrics","p2p/netutil","params","rlp","rpc","trie"]
@ -103,18 +97,6 @@
packages = ["."] packages = ["."]
revision = "553a641470496b2327abcac10b36396bd98e45c9" revision = "553a641470496b2327abcac10b36396bd98e45c9"
[[projects]]
name = "github.com/gorilla/context"
packages = ["."]
revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a"
version = "v1.1"
[[projects]]
name = "github.com/gorilla/mux"
packages = ["."]
revision = "53c1911da2b537f792e7cafcb446b05ffe33b996"
version = "v1.6.1"
[[projects]] [[projects]]
name = "github.com/gorilla/websocket" name = "github.com/gorilla/websocket"
packages = ["."] packages = ["."]
@ -131,7 +113,7 @@
branch = "master" branch = "master"
name = "github.com/jakm/btcutil" name = "github.com/jakm/btcutil"
packages = [".","base58","bech32","chaincfg","txscript"] packages = [".","base58","bech32","chaincfg","txscript"]
revision = "a45c5a6a9cb32f0caecb14a4e5a8f82640be1f39" revision = "c50a69d8979c23daad1728b3574c6f20691174f7"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -235,12 +217,6 @@
packages = ["websocket"] packages = ["websocket"]
revision = "61147c48b25b599e5b561d2e9c4f3e1ef489ca41" revision = "61147c48b25b599e5b561d2e9c4f3e1ef489ca41"
[[projects]]
name = "gopkg.in/fatih/set.v0"
packages = ["."]
revision = "57907de300222151a123d29255ed17f5ed43fad3"
version = "v0.1.0"
[[projects]] [[projects]]
branch = "v2" branch = "v2"
name = "gopkg.in/karalabe/cookiejar.v2" name = "gopkg.in/karalabe/cookiejar.v2"
@ -256,6 +232,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "98fb9a6252acd86d1c0a4b09a42e34673c2ccd6327caf49ed85bcee573601d95" inputs-digest = "7cb7ad84fe0b30276bb7775d85c4f3281acddb12cb2e10ea4d80198981c9f80b"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -25,6 +25,23 @@ const (
RegTestPrefix = "bchreg:" RegTestPrefix = "bchreg:"
) )
var (
MainNetParams chaincfg.Params
TestNetParams chaincfg.Params
RegtestParams chaincfg.Params
)
func init() {
MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = bchutil.MainnetMagic
TestNetParams = chaincfg.TestNet3Params
TestNetParams.Net = bchutil.TestnetMagic
RegtestParams = chaincfg.RegressionNetParams
RegtestParams.Net = bchutil.Regtestmagic
}
// BCashParser handle // BCashParser handle
type BCashParser struct { type BCashParser struct {
*btc.BitcoinParser *btc.BitcoinParser
@ -62,17 +79,26 @@ func NewBCashParser(params *chaincfg.Params, c *btc.Configuration) (*BCashParser
// the regression test Bitcoin Cash network, the test Bitcoin Cash network and // the regression test Bitcoin Cash network, the test Bitcoin Cash network and
// the simulation test Bitcoin Cash network, in this order // the simulation test Bitcoin Cash network, in this order
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if !chaincfg.IsRegistered(&MainNetParams) {
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
}
var params *chaincfg.Params var params *chaincfg.Params
switch chain { switch chain {
case "test": case "test":
params = &chaincfg.TestNet3Params return &TestNetParams
params.Net = bchutil.TestnetMagic
case "regtest": case "regtest":
params = &chaincfg.RegressionNetParams return &RegtestParams
params.Net = bchutil.Regtestmagic
default: default:
params = &chaincfg.MainNetParams return &MainNetParams
params.Net = bchutil.MainnetMagic
} }
return params return params

View File

@ -7,10 +7,19 @@ import (
"blockbook/bchain/coins/btc" "blockbook/bchain/coins/btc"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress(t *testing.T) { func Test_GetAddrDescFromAddress(t *testing.T) {
mainParserCashAddr, mainParserLegacy, testParserCashAddr, _ := setupParsers(t) mainParserCashAddr, mainParserLegacy, testParserCashAddr, _ := setupParsers(t)
tests := []struct { tests := []struct {

View File

@ -42,6 +42,9 @@ func NewBitcoinParser(params *chaincfg.Params, c *Configuration) *BitcoinParser
// the regression test Bitcoin network, the test Bitcoin network and // the regression test Bitcoin network, the test Bitcoin network and
// the simulation test Bitcoin network, in this order // the simulation test Bitcoin network, in this order
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if !chaincfg.IsRegistered(&chaincfg.MainNetParams) {
chaincfg.RegisterBitcoinParams()
}
switch chain { switch chain {
case "test": case "test":
return &chaincfg.TestNet3Params return &chaincfg.TestNet3Params

View File

@ -6,10 +6,19 @@ import (
"blockbook/bchain" "blockbook/bchain"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress(t *testing.T) { func Test_GetAddrDescFromAddress(t *testing.T) {
type args struct { type args struct {
address string address string

View File

@ -23,7 +23,7 @@ var (
TestNetParams chaincfg.Params TestNetParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
@ -39,14 +39,6 @@ func initParams() {
// see https://github.com/satoshilabs/slips/blob/master/slip-0173.md // see https://github.com/satoshilabs/slips/blob/master/slip-0173.md
MainNetParams.Bech32HRPSegwit = "btg" MainNetParams.Bech32HRPSegwit = "btg"
TestNetParams.Bech32HRPSegwit = "tbtg" TestNetParams.Bech32HRPSegwit = "tbtg"
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
// BGoldParser handle // BGoldParser handle
@ -63,8 +55,14 @@ func NewBGoldParser(params *chaincfg.Params, c *btc.Configuration) *BGoldParser
// the regression test Bitcoin Cash network, the test Bitcoin Cash network and // the regression test Bitcoin Cash network, the test Bitcoin Cash network and
// the simulation test Bitcoin Cash network, in this order // the simulation test Bitcoin Cash network, in this order
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
case "test": case "test":

View File

@ -8,10 +8,19 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
type testBlock struct { type testBlock struct {
size int size int
time int64 time int64

View File

@ -19,7 +19,7 @@ var (
RegtestParams chaincfg.Params RegtestParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
@ -40,17 +40,6 @@ func initParams() {
// Address encoding magics // Address encoding magics
RegtestParams.PubKeyHashAddrID = []byte{140} // base58 prefix: y RegtestParams.PubKeyHashAddrID = []byte{140} // base58 prefix: y
RegtestParams.ScriptHashAddrID = []byte{19} // base58 prefix: 8 or 9 RegtestParams.ScriptHashAddrID = []byte{19} // base58 prefix: 8 or 9
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
} }
// DashParser handle // DashParser handle
@ -67,8 +56,17 @@ func NewDashParser(params *chaincfg.Params, c *btc.Configuration) *DashParser {
// the regression test Dash network, the test Dash network and // the regression test Dash network, the test Dash network and
// the simulation test Dash network, in this order // the simulation test Dash network, in this order
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
case "test": case "test":

View File

@ -18,16 +18,11 @@ var (
MainNetParams chaincfg.Params MainNetParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{30} MainNetParams.PubKeyHashAddrID = []byte{30}
MainNetParams.ScriptHashAddrID = []byte{22} MainNetParams.ScriptHashAddrID = []byte{22}
err := chaincfg.Register(&MainNetParams)
if err != nil {
panic(err)
}
} }
// DogecoinParser handle // DogecoinParser handle
@ -43,8 +38,11 @@ func NewDogecoinParser(params *chaincfg.Params, c *btc.Configuration) *DogecoinP
// GetChainParams contains network parameters for the main Dogecoin network, // GetChainParams contains network parameters for the main Dogecoin network,
// and the test Dogecoin network // and the test Dogecoin network
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
default: default:

View File

@ -10,11 +10,20 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
type args struct { type args struct {
address string address string

View File

@ -48,6 +48,7 @@ type EthereumRPC struct {
Mempool *bchain.NonUTXOMempool Mempool *bchain.NonUTXOMempool
bestHeaderMu sync.Mutex bestHeaderMu sync.Mutex
bestHeader *ethtypes.Header bestHeader *ethtypes.Header
bestHeaderTime time.Time
chanNewBlock chan *ethtypes.Header chanNewBlock chan *ethtypes.Header
newBlockSubscription *rpc.ClientSubscription newBlockSubscription *rpc.ClientSubscription
chanNewTx chan ethcommon.Hash chanNewTx chan ethcommon.Hash
@ -96,6 +97,7 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
// update best header to the new header // update best header to the new header
s.bestHeaderMu.Lock() s.bestHeaderMu.Lock()
s.bestHeader = h s.bestHeader = h
s.bestHeaderTime = time.Now()
s.bestHeaderMu.Unlock() s.bestHeaderMu.Unlock()
// notify blockbook // notify blockbook
pushHandler(bchain.NotificationNewBlock) pushHandler(bchain.NotificationNewBlock)
@ -188,7 +190,7 @@ func (b *EthereumRPC) Initialize() error {
return nil return nil
} }
// subscribeNewBlocks subscribes to new blocks notification // subscribe subscribes notification and tries to resubscribe in case of error
func (b *EthereumRPC) subscribe(f func() (*rpc.ClientSubscription, error)) error { func (b *EthereumRPC) subscribe(f func() (*rpc.ClientSubscription, error)) error {
s, err := f() s, err := f()
if err != nil { if err != nil {
@ -281,6 +283,12 @@ func (b *EthereumRPC) GetChainInfo() (*bchain.ChainInfo, error) {
func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) { func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
b.bestHeaderMu.Lock() b.bestHeaderMu.Lock()
defer b.bestHeaderMu.Unlock() defer b.bestHeaderMu.Unlock()
// ETC does not have newBlocks subscription, bestHeader must be updated very often (each 1 second)
if b.isETC {
if b.bestHeaderTime.Add(1 * time.Second).Before(time.Now()) {
b.bestHeader = nil
}
}
if b.bestHeader == nil { if b.bestHeader == nil {
var err error var err error
ctx, cancel := context.WithTimeout(context.Background(), b.timeout) ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
@ -289,6 +297,7 @@ func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.bestHeaderTime = time.Now()
} }
return b.bestHeader, nil return b.bestHeader, nil
} }

View File

@ -18,7 +18,7 @@ var (
TestNetParams chaincfg.Params TestNetParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{48} MainNetParams.PubKeyHashAddrID = []byte{48}
@ -30,14 +30,6 @@ func initParams() {
TestNetParams.PubKeyHashAddrID = []byte{111} TestNetParams.PubKeyHashAddrID = []byte{111}
TestNetParams.ScriptHashAddrID = []byte{58} TestNetParams.ScriptHashAddrID = []byte{58}
TestNetParams.Bech32HRPSegwit = "tltc" TestNetParams.Bech32HRPSegwit = "tltc"
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
// LitecoinParser handle // LitecoinParser handle
@ -53,8 +45,17 @@ func NewLitecoinParser(params *chaincfg.Params, c *btc.Configuration) *LitecoinP
// GetChainParams contains network parameters for the main Litecoin network, // GetChainParams contains network parameters for the main Litecoin network,
// and the test Litecoin network // and the test Litecoin network
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&chaincfg.MainNetParams) {
initParams() chaincfg.RegisterBitcoinParams()
}
if !chaincfg.IsRegistered(&MainNetParams) {
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
case "test": case "test":

View File

@ -7,10 +7,19 @@ import (
"blockbook/bchain/coins/btc" "blockbook/bchain/coins/btc"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress_Testnet(t *testing.T) { func Test_GetAddrDescFromAddress_Testnet(t *testing.T) {
type args struct { type args struct {
address string address string
@ -28,17 +37,29 @@ func Test_GetAddrDescFromAddress_Testnet(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "P2SH1", name: "P2SH1-legacy",
args: args{address: "2MvGVySztevmycxrSmMRjJaVj2iJin7qpap"}, args: args{address: "2MvGVySztevmycxrSmMRjJaVj2iJin7qpap"},
want: "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87", want: "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87",
wantErr: false, wantErr: false,
}, },
{ {
name: "P2SH2", name: "P2SH2-legacy",
args: args{address: "2N9a2TNzWz1FEKGFxUdMEh62V83URdZ5QAZ"}, args: args{address: "2N9a2TNzWz1FEKGFxUdMEh62V83URdZ5QAZ"},
want: "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87", want: "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87",
wantErr: false, wantErr: false,
}, },
{
name: "P2SH1",
args: args{address: "QPdG6Ts8g2q4m9cVPTTkPGwAB6kYgXB7Hc"},
want: "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87",
wantErr: false,
},
{
name: "P2SH2",
args: args{address: "QcvnaPrm17JKTT216jPFmnTvGRvFX2fWzN"},
want: "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87",
wantErr: false,
},
} }
parser := NewLitecoinParser(GetChainParams("test"), &btc.Configuration{}) parser := NewLitecoinParser(GetChainParams("test"), &btc.Configuration{})

View File

@ -26,7 +26,7 @@ var (
MonaTestParams monacoinCfg.Params MonaTestParams monacoinCfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{50} MainNetParams.PubKeyHashAddrID = []byte{50}
@ -48,14 +48,6 @@ func initParams() {
MonaTestParams.PubKeyHashAddrID = 111 MonaTestParams.PubKeyHashAddrID = 111
MonaTestParams.ScriptHashAddrID = 117 MonaTestParams.ScriptHashAddrID = 117
MonaTestParams.Bech32HRPSegwit = "tmona" MonaTestParams.Bech32HRPSegwit = "tmona"
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
// MonacoinParser handle // MonacoinParser handle
@ -65,14 +57,22 @@ type MonacoinParser struct {
// NewMonacoinParser returns new MonacoinParser instance // NewMonacoinParser returns new MonacoinParser instance
func NewMonacoinParser(params *chaincfg.Params, c *btc.Configuration) *MonacoinParser { func NewMonacoinParser(params *chaincfg.Params, c *btc.Configuration) *MonacoinParser {
return &MonacoinParser{BitcoinParser: btc.NewBitcoinParser(params, c)} p := &MonacoinParser{BitcoinParser: btc.NewBitcoinParser(params, c)}
p.OutputScriptToAddressesFunc = p.outputScriptToAddresses
return p
} }
// GetChainParams contains network parameters for the main Monacoin network, // GetChainParams contains network parameters for the main Monacoin network,
// and the test Monacoin network // and the test Monacoin network
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
case "test": case "test":
@ -82,17 +82,6 @@ func GetChainParams(chain string) *chaincfg.Params {
} }
} }
// GetMonaChainParams contains network parameters for the main Monacoin network,
// and the test Monacoin network
func GetMonaChainParams(chain string) *monacoinCfg.Params {
switch chain {
case "test":
return &MonaTestParams
default:
return &MonaMainParams
}
}
// GetAddrDescFromAddress returns internal address representation (descriptor) of given address // GetAddrDescFromAddress returns internal address representation (descriptor) of given address
func (p *MonacoinParser) GetAddrDescFromAddress(address string) (bchain.AddressDescriptor, error) { func (p *MonacoinParser) GetAddrDescFromAddress(address string) (bchain.AddressDescriptor, error) {
return p.addressToOutputScript(address) return p.addressToOutputScript(address)
@ -123,3 +112,52 @@ func (p *MonacoinParser) addressToOutputScript(address string) ([]byte, error) {
return script, nil return script, nil
} }
} }
// GetAddressesFromAddrDesc returns addresses for given address descriptor with flag if the addresses are searchable
func (p *MonacoinParser) GetAddressesFromAddrDesc(addrDesc bchain.AddressDescriptor) ([]string, bool, error) {
return p.OutputScriptToAddressesFunc(addrDesc)
}
// outputScriptToAddresses converts ScriptPubKey to bitcoin addresses
func (p *MonacoinParser) outputScriptToAddresses(script []byte) ([]string, bool, error) {
switch p.Params.Net {
case MainnetMagic:
sc, addresses, _, err := txscript.ExtractPkScriptAddrs(script, &MonaMainParams)
if err != nil {
return nil, false, err
}
rv := make([]string, len(addresses))
for i, a := range addresses {
rv[i] = a.EncodeAddress()
}
var s bool
if sc != txscript.NonStandardTy && sc != txscript.NullDataTy {
s = true
} else if len(rv) == 0 {
or := btc.TryParseOPReturn(script)
if or != "" {
rv = []string{or}
}
}
return rv, s, nil
default:
sc, addresses, _, err := txscript.ExtractPkScriptAddrs(script, &MonaTestParams)
if err != nil {
return nil, false, err
}
rv := make([]string, len(addresses))
for i, a := range addresses {
rv[i] = a.EncodeAddress()
}
var s bool
if sc != txscript.NonStandardTy && sc != txscript.NullDataTy {
s = true
} else if len(rv) == 0 {
or := btc.TryParseOPReturn(script)
if or != "" {
rv = []string{or}
}
}
return rv, s, nil
}
}

View File

@ -7,10 +7,19 @@ import (
"blockbook/bchain/coins/btc" "blockbook/bchain/coins/btc"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress_Testnet(t *testing.T) { func Test_GetAddrDescFromAddress_Testnet(t *testing.T) {
type args struct { type args struct {
address string address string

View File

@ -18,16 +18,11 @@ var (
MainNetParams chaincfg.Params MainNetParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{52} MainNetParams.PubKeyHashAddrID = []byte{52}
MainNetParams.ScriptHashAddrID = []byte{13} MainNetParams.ScriptHashAddrID = []byte{13}
err := chaincfg.Register(&MainNetParams)
if err != nil {
panic(err)
}
} }
// NamecoinParser handle // NamecoinParser handle
@ -43,8 +38,11 @@ func NewNamecoinParser(params *chaincfg.Params, c *btc.Configuration) *NamecoinP
// GetChainParams contains network parameters for the main Namecoin network, // GetChainParams contains network parameters for the main Namecoin network,
// and the test Namecoin network // and the test Namecoin network
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
default: default:

View File

@ -8,11 +8,20 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
type args struct { type args struct {
address string address string

View File

@ -18,7 +18,7 @@ var (
TestNetParams chaincfg.Params TestNetParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{71} MainNetParams.PubKeyHashAddrID = []byte{71}
@ -30,14 +30,6 @@ func initParams() {
TestNetParams.PubKeyHashAddrID = []byte{74} TestNetParams.PubKeyHashAddrID = []byte{74}
TestNetParams.ScriptHashAddrID = []byte{196} TestNetParams.ScriptHashAddrID = []byte{196}
TestNetParams.Bech32HRPSegwit = "tvtc" TestNetParams.Bech32HRPSegwit = "tvtc"
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
// VertcoinParser handle // VertcoinParser handle
@ -53,8 +45,14 @@ func NewVertcoinParser(params *chaincfg.Params, c *btc.Configuration) *VertcoinP
// GetChainParams contains network parameters for the main Vertcoin network, // GetChainParams contains network parameters for the main Vertcoin network,
// and the test Vertcoin network // and the test Vertcoin network
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
switch chain { switch chain {
case "test": case "test":

View File

@ -7,10 +7,19 @@ import (
"blockbook/bchain/coins/btc" "blockbook/bchain/coins/btc"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
type args struct { type args struct {
address string address string

View File

@ -17,9 +17,10 @@ const (
var ( var (
MainNetParams chaincfg.Params MainNetParams chaincfg.Params
TestNetParams chaincfg.Params TestNetParams chaincfg.Params
RegtestParams chaincfg.Params
) )
func initParams() { func init() {
MainNetParams = chaincfg.MainNetParams MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic MainNetParams.Net = MainnetMagic
@ -36,13 +37,8 @@ func initParams() {
TestNetParams.PubKeyHashAddrID = []byte{0x1D, 0x25} // base58 prefix: tm TestNetParams.PubKeyHashAddrID = []byte{0x1D, 0x25} // base58 prefix: tm
TestNetParams.ScriptHashAddrID = []byte{0x1C, 0xBA} // base58 prefix: t2 TestNetParams.ScriptHashAddrID = []byte{0x1C, 0xBA} // base58 prefix: t2
err := chaincfg.Register(&MainNetParams) RegtestParams = chaincfg.RegressionNetParams
if err == nil { RegtestParams.Net = RegtestMagic
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
} }
// ZCashParser handle // ZCashParser handle
@ -63,16 +59,24 @@ func NewZCashParser(params *chaincfg.Params, c *btc.Configuration) *ZCashParser
// the regression test ZCash network, the test ZCash network and // the regression test ZCash network, the test ZCash network and
// the simulation test ZCash network, in this order // the simulation test ZCash network, in this order
func GetChainParams(chain string) *chaincfg.Params { func GetChainParams(chain string) *chaincfg.Params {
if MainNetParams.Name == "" { if !chaincfg.IsRegistered(&MainNetParams) {
initParams() err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
} }
var params *chaincfg.Params var params *chaincfg.Params
switch chain { switch chain {
case "test": case "test":
return &TestNetParams return &TestNetParams
case "regtest": case "regtest":
params = &chaincfg.RegressionNetParams return &RegtestParams
params.Net = RegtestMagic
default: default:
return &MainNetParams return &MainNetParams
} }

View File

@ -8,8 +8,11 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"os"
"reflect" "reflect"
"testing" "testing"
"github.com/jakm/btcutil/chaincfg"
) )
var ( var (
@ -91,6 +94,12 @@ func init() {
} }
} }
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func TestGetAddrDesc(t *testing.T) { func TestGetAddrDesc(t *testing.T) {
type args struct { type args struct {
tx bchain.Tx tx bchain.Tx

View File

@ -1,7 +1,7 @@
SHELL = /bin/bash SHELL = /bin/bash
UPDATE_VENDOR ?= 1 UPDATE_VENDOR ?= 1
VERSION ?= devel VERSION ?= devel
GITCOMMIT = $(shell cd /src && git describe --tags --always --dirty) GITCOMMIT = $(shell cd /src && git describe --always --dirty)
BUILDTIME = $(shell date --iso-8601=seconds) BUILDTIME = $(shell date --iso-8601=seconds)
LDFLAGS := -X blockbook/common.version=$(VERSION) -X blockbook/common.gitcommit=$(GITCOMMIT) -X blockbook/common.buildtime=$(BUILDTIME) LDFLAGS := -X blockbook/common.version=$(VERSION) -X blockbook/common.gitcommit=$(GITCOMMIT) -X blockbook/common.buildtime=$(BUILDTIME)
BLOCKBOOK_SRC := $(GOPATH)/src/blockbook BLOCKBOOK_SRC := $(GOPATH)/src/blockbook

View File

@ -11,9 +11,10 @@ package=$1
coin=$2 coin=$2
shift 2 shift 2
mkdir build mkdir -p build
cp -r /src/build/templates build cp -r /src/build/templates build
cp -r /src/configs . cp -r /src/configs .
ln -s /src/ /go/src/blockbook
go run build/templates/generate.go $coin go run build/templates/generate.go $coin
# backend # backend

View File

@ -42,20 +42,17 @@ type connectBlockStats struct {
// RocksDB handle // RocksDB handle
type RocksDB struct { type RocksDB struct {
path string path string
db *gorocksdb.DB db *gorocksdb.DB
wo *gorocksdb.WriteOptions wo *gorocksdb.WriteOptions
ro *gorocksdb.ReadOptions ro *gorocksdb.ReadOptions
cfh []*gorocksdb.ColumnFamilyHandle cfh []*gorocksdb.ColumnFamilyHandle
chainParser bchain.BlockChainParser chainParser bchain.BlockChainParser
is *common.InternalState is *common.InternalState
metrics *common.Metrics metrics *common.Metrics
cache *gorocksdb.Cache cache *gorocksdb.Cache
maxOpenFiles int maxOpenFiles int
cbs connectBlockStats cbs connectBlockStats
chanUpdateBalance chan updateBalanceData
chanUpdateBalanceResult chan error
updateBalancesMap map[string]*AddrBalance
} }
const ( const (
@ -93,20 +90,7 @@ func NewRocksDB(path string, cacheSize, maxOpenFiles int, parser bchain.BlockCha
db, cfh, err := openDB(path, c, maxOpenFiles) db, cfh, err := openDB(path, c, maxOpenFiles)
wo := gorocksdb.NewDefaultWriteOptions() wo := gorocksdb.NewDefaultWriteOptions()
ro := gorocksdb.NewDefaultReadOptions() ro := gorocksdb.NewDefaultReadOptions()
rdb := &RocksDB{ return &RocksDB{path, db, wo, ro, cfh, parser, nil, metrics, c, maxOpenFiles, connectBlockStats{}}, nil
path: path,
db: db,
wo: wo,
ro: ro,
cfh: cfh,
chainParser: parser,
metrics: metrics,
cache: c,
maxOpenFiles: maxOpenFiles,
cbs: connectBlockStats{},
}
rdb.initUpdateBalancesWorker()
return rdb, nil
} }
func (d *RocksDB) closeDB() error { func (d *RocksDB) closeDB() error {
@ -290,8 +274,6 @@ func (d *RocksDB) writeBlock(block *bchain.Block, op int) error {
txAddressesMap := make(map[string]*TxAddresses) txAddressesMap := make(map[string]*TxAddresses)
balances := make(map[string]*AddrBalance) balances := make(map[string]*AddrBalance)
if err := d.processAddressesUTXO(block, addresses, txAddressesMap, balances); err != nil { if err := d.processAddressesUTXO(block, addresses, txAddressesMap, balances); err != nil {
// reinitialize balanceWorker so that there are no left balances in the queue
d.initUpdateBalancesWorker()
return err return err
} }
if err := d.storeAddresses(wb, block.Height, addresses); err != nil { if err := d.storeAddresses(wb, block.Height, addresses); err != nil {
@ -380,80 +362,7 @@ func (d *RocksDB) GetAndResetConnectBlockStats() string {
return s return s
} }
type updateBalanceData struct {
valueSat big.Int
strAddrDesc string
addrDesc bchain.AddressDescriptor
processed, output bool
}
func (d *RocksDB) initUpdateBalancesWorker() {
if d.chanUpdateBalance != nil {
close(d.chanUpdateBalance)
}
d.chanUpdateBalance = make(chan updateBalanceData, 16)
d.chanUpdateBalanceResult = make(chan error, 16)
go d.updateBalancesWorker()
}
// updateBalancesWorker is a single worker used to update balances in parallel to processAddressesUTXO
func (d *RocksDB) updateBalancesWorker() {
var err error
for bd := range d.chanUpdateBalance {
ab, e := d.updateBalancesMap[bd.strAddrDesc]
if !e {
ab, err = d.GetAddrDescBalance(bd.addrDesc)
if err != nil {
d.chanUpdateBalanceResult <- err
continue
}
if ab == nil {
ab = &AddrBalance{}
}
d.updateBalancesMap[bd.strAddrDesc] = ab
d.cbs.balancesMiss++
} else {
d.cbs.balancesHit++
}
// add number of trx in balance only once, address can be multiple times in tx
if !bd.processed {
ab.Txs++
}
if bd.output {
ab.BalanceSat.Add(&ab.BalanceSat, &bd.valueSat)
} else {
ab.BalanceSat.Sub(&ab.BalanceSat, &bd.valueSat)
if ab.BalanceSat.Sign() < 0 {
d.resetValueSatToZero(&ab.BalanceSat, bd.addrDesc, "balance")
}
ab.SentSat.Add(&ab.SentSat, &bd.valueSat)
}
d.chanUpdateBalanceResult <- nil
}
}
func (d *RocksDB) dispatchUpdateBalance(dispatchedBalances int, valueSat *big.Int, strAddrDesc string, addrDesc bchain.AddressDescriptor, processed, output bool) (int, error) {
loop:
for {
select {
// process as many results as possible
case err := <-d.chanUpdateBalanceResult:
if err != nil {
return 0, err
}
dispatchedBalances--
// send input to be processed
case d.chanUpdateBalance <- updateBalanceData{*valueSat, strAddrDesc, addrDesc, processed, output}:
dispatchedBalances++
break loop
}
}
return dispatchedBalances, nil
}
func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string][]outpoint, txAddressesMap map[string]*TxAddresses, balances map[string]*AddrBalance) error { func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string][]outpoint, txAddressesMap map[string]*TxAddresses, balances map[string]*AddrBalance) error {
d.updateBalancesMap = balances
dispatchedBalances := 0
blockTxIDs := make([][]byte, len(block.Txs)) blockTxIDs := make([][]byte, len(block.Txs))
blockTxAddresses := make([]*TxAddresses, len(block.Txs)) blockTxAddresses := make([]*TxAddresses, len(block.Txs))
// first process all outputs so that inputs can point to txs in this block // first process all outputs so that inputs can point to txs in this block
@ -495,10 +404,25 @@ func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string
btxID: btxID, btxID: btxID,
index: int32(i), index: int32(i),
}) })
dispatchedBalances, err = d.dispatchUpdateBalance(dispatchedBalances, &output.ValueSat, strAddrDesc, addrDesc, processed, true) ab, e := balances[strAddrDesc]
if err != nil { if !e {
return err ab, err = d.GetAddrDescBalance(addrDesc)
if err != nil {
return err
}
if ab == nil {
ab = &AddrBalance{}
}
balances[strAddrDesc] = ab
d.cbs.balancesMiss++
} else {
d.cbs.balancesHit++
} }
// add number of trx in balance only once, address can be multiple times in tx
if !processed {
ab.Txs++
}
ab.BalanceSat.Add(&ab.BalanceSat, &output.ValueSat)
} }
} }
// process inputs // process inputs
@ -564,16 +488,29 @@ func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string
btxID: spendingTxid, btxID: spendingTxid,
index: ^int32(i), index: ^int32(i),
}) })
dispatchedBalances, err = d.dispatchUpdateBalance(dispatchedBalances, &ot.ValueSat, strAddrDesc, ot.AddrDesc, processed, false) ab, e := balances[strAddrDesc]
if err != nil { if !e {
return err ab, err = d.GetAddrDescBalance(ot.AddrDesc)
if err != nil {
return err
}
if ab == nil {
ab = &AddrBalance{}
}
balances[strAddrDesc] = ab
d.cbs.balancesMiss++
} else {
d.cbs.balancesHit++
} }
} // add number of trx in balance only once, address can be multiple times in tx
} if !processed {
for i := 0; i < dispatchedBalances; i++ { ab.Txs++
err := <-d.chanUpdateBalanceResult }
if err != nil { ab.BalanceSat.Sub(&ab.BalanceSat, &ot.ValueSat)
return err if ab.BalanceSat.Sign() < 0 {
d.resetValueSatToZero(&ab.BalanceSat, ot.AddrDesc, "balance")
}
ab.SentSat.Add(&ab.SentSat, &ot.ValueSat)
} }
} }
return nil return nil

View File

@ -18,6 +18,7 @@ import (
"testing" "testing"
vlq "github.com/bsm/go-vlq" vlq "github.com/bsm/go-vlq"
"github.com/jakm/btcutil/chaincfg"
"github.com/juju/errors" "github.com/juju/errors"
) )
@ -25,6 +26,12 @@ import (
// for number n, the packing is: 2*n if n>=0 else 2*(-n)-1 // for number n, the packing is: 2*n if n>=0 else 2*(-n)-1
// takes only 1 byte if abs(n)<127 // takes only 1 byte if abs(n)<127
func TestMain(m *testing.M) {
c := m.Run()
chaincfg.ResetParams()
os.Exit(c)
}
func bitcoinTestnetParser() *btc.BitcoinParser { func bitcoinTestnetParser() *btc.BitcoinParser {
return btc.NewBitcoinParser( return btc.NewBitcoinParser(
btc.GetChainParams("test"), btc.GetChainParams("test"),

View File

@ -8,16 +8,18 @@
<table class="table table-striped data-table table-hover"> <table class="table table-striped data-table table-hover">
<thead> <thead>
<tr> <tr>
<th style="width: 20%;">Height</th> <th style="width: 10%;">Height</th>
<th style="width: 48%;">Hash</th>
<th>Timestamp</span></th> <th>Timestamp</span></th>
<th class="text-right" style="width: 20%;">Transactions</th> <th class="text-right" style="width: 10%;">Transactions</th>
<th class="text-right" style="width: 20%;">Size</th> <th class="text-right" style="width: 10%;">Size</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{- range $b := $blocks.Blocks -}} {{- range $b := $blocks.Blocks -}}
<tr> <tr>
<td><a href="/block/{{$b.Height}}">{{$b.Height}}</a></td> <td><a href="/block/{{$b.Height}}">{{$b.Height}}</a></td>
<td class="ellipsis">{{$b.Hash}}</td>
<td>{{formatUnixTime $b.Time}}</td> <td>{{formatUnixTime $b.Time}}</td>
<td class="text-right">{{$b.Txs}}</td> <td class="text-right">{{$b.Txs}}</td>
<td class="text-right">{{$b.Size}}</td> <td class="text-right">{{$b.Size}}</td>

View File

@ -1,11 +1,11 @@
{ {
"bcash": { "bcash": {
"rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync", "rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync",
"EstimateSmartFee", "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"] "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"]
}, },
"bcash_testnet": { "bcash_testnet": {
"rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync", "rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync",
"EstimateSmartFee", "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"] "EstimateFee", "GetBestBlockHash", "GetBestBlockHeight", "GetBlockHeader"]
}, },
"bitcoin": { "bitcoin": {
"rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync", "rpc": ["GetBlock", "GetBlockHash", "GetTransaction", "GetTransactionForMempool", "MempoolSync",