Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into addBitZeny
Browse files Browse the repository at this point in the history
  • Loading branch information
zinntikumugai committed Mar 6, 2019
2 parents 0b452ca + efca04a commit a13a791
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 39 deletions.
4 changes: 3 additions & 1 deletion api/worker.go
Expand Up @@ -657,6 +657,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
uBalSat big.Int
totalReceived, totalSent *big.Int
nonce string
unconfirmedTxs int
nonTokenTxs int
totalResults int
)
Expand Down Expand Up @@ -705,6 +706,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
} else {
// skip already confirmed txs, mempool may be out of sync
if tx.Confirmations == 0 {
unconfirmedTxs++
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(addrDesc))
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(addrDesc))
if page == 0 {
Expand Down Expand Up @@ -763,7 +765,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
Txs: int(ba.Txs),
NonTokenTxs: nonTokenTxs,
UnconfirmedBalanceSat: (*Amount)(&uBalSat),
UnconfirmedTxs: len(txm),
UnconfirmedTxs: unconfirmedTxs,
Transactions: txs,
Txids: txids,
Tokens: tokens,
Expand Down
38 changes: 25 additions & 13 deletions api/xpub.go
Expand Up @@ -34,9 +34,17 @@ type xpubTxid struct {

type xpubTxids []xpubTxid

func (a xpubTxids) Len() int { return len(a) }
func (a xpubTxids) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a xpubTxids) Less(i, j int) bool { return a[i].height >= a[j].height }
func (a xpubTxids) Len() int { return len(a) }
func (a xpubTxids) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a xpubTxids) Less(i, j int) bool {
// if the heights are equal, make inputs less than outputs
hi := a[i].height
hj := a[j].height
if hi == hj {
return (a[i].inputOutput & txInput) >= (a[j].inputOutput & txInput)
}
return hi > hj
}

type xpubAddress struct {
addrDesc bchain.AddressDescriptor
Expand Down Expand Up @@ -359,15 +367,16 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc
inputOutput byte
}
var (
txc xpubTxids
txmMap map[string]*Tx
txCount int
txs []*Tx
txids []string
pg Paging
filtered bool
err error
uBalSat big.Int
txc xpubTxids
txmMap map[string]*Tx
txCount int
txs []*Tx
txids []string
pg Paging
filtered bool
err error
uBalSat big.Int
unconfirmedTxs int
)
data, bestheight, err := w.getXpubData(xpub, page, txsOnPage, option, filter, gap)
if err != nil {
Expand Down Expand Up @@ -418,6 +427,9 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc
}
// skip already confirmed txs, mempool may be out of sync
if tx.Confirmations == 0 {
if !foundTx {
unconfirmedTxs++
}
uBalSat.Add(&uBalSat, tx.getAddrVoutValue(ad.addrDesc))
uBalSat.Sub(&uBalSat, tx.getAddrVinValue(ad.addrDesc))
if page == 0 && !foundTx && (useTxids == nil || useTxids(&txid, ad)) {
Expand Down Expand Up @@ -521,7 +533,7 @@ func (w *Worker) GetXpubAddress(xpub string, page int, txsOnPage int, option Acc
TotalSentSat: (*Amount)(&data.sentSat),
Txs: txCount,
UnconfirmedBalanceSat: (*Amount)(&uBalSat),
UnconfirmedTxs: len(txmMap),
UnconfirmedTxs: unconfirmedTxs,
Transactions: txs,
Txids: txids,
TotalTokens: totalTokens,
Expand Down
31 changes: 19 additions & 12 deletions bchain/coins/bch/bcashparser.go
Expand Up @@ -12,22 +12,31 @@ import (
"github.com/schancel/cashaddr-converter/address"
)

// AddressFormat type is used to specify different formats of address
type AddressFormat = uint8

const (
// Legacy AddressFormat is the same as Bitcoin
Legacy AddressFormat = iota
// CashAddr AddressFormat is new Bitcoin Cash standard
CashAddr
)

const (
// MainNetPrefix is CashAddr prefix for mainnet
MainNetPrefix = "bitcoincash:"
// TestNetPrefix is CashAddr prefix for testnet
TestNetPrefix = "bchtest:"
// RegTestPrefix is CashAddr prefix for regtest
RegTestPrefix = "bchreg:"
)

var (
// MainNetParams are parser parameters for mainnet
MainNetParams chaincfg.Params
// TestNetParams are parser parameters for testnet
TestNetParams chaincfg.Params
// RegtestParams are parser parameters for regtest
RegtestParams chaincfg.Params
)

Expand Down Expand Up @@ -118,17 +127,16 @@ func (p *BCashParser) addressToOutputScript(address string) ([]byte, error) {
return nil, err
}
return script, nil
} else {
da, err := btcutil.DecodeAddress(address, p.Params)
if err != nil {
return nil, err
}
script, err := txscript.PayToAddrScript(da)
if err != nil {
return nil, err
}
return script, nil
}
da, err := btcutil.DecodeAddress(address, p.Params)
if err != nil {
return nil, err
}
script, err := txscript.PayToAddrScript(da)
if err != nil {
return nil, err
}
return script, nil
}

func isCashAddr(addr string) bool {
Expand Down Expand Up @@ -163,9 +171,8 @@ func (p *BCashParser) outputScriptToAddresses(script []byte) ([]string, bool, er
return []string{or}, false, nil
}
return []string{}, false, nil
} else {
return nil, false, err
}
return nil, false, err
}
// EncodeAddress returns CashAddr address
addr := a.EncodeAddress()
Expand Down
4 changes: 2 additions & 2 deletions bchain/coins/bellcoin/bellcoinparser_test.go
Expand Up @@ -262,7 +262,7 @@ func Test_PackTx(t *testing.T) {
name: "Bellcoin-1",
args: args{
tx: testTx1,
height: 205708,
height: 205708,
blockTime: 1549095010,
parser: NewBellcoinParser(GetChainParams("main"), &btc.Configuration{}),
},
Expand Down Expand Up @@ -304,7 +304,7 @@ func Test_UnpackTx(t *testing.T) {
parser: NewBellcoinParser(GetChainParams("main"), &btc.Configuration{}),
},
want: &testTx1,
want1: 205708,
want1: 205708,
wantErr: false,
},
}
Expand Down
6 changes: 6 additions & 0 deletions bchain/coins/dash/dashparser.go
Expand Up @@ -9,14 +9,20 @@ import (
)

const (
// MainnetMagic is mainnet network constant
MainnetMagic wire.BitcoinNet = 0xbd6b0cbf
// TestnetMagic is testnet network constant
TestnetMagic wire.BitcoinNet = 0xffcae2ce
// RegtestMagic is regtest network constant
RegtestMagic wire.BitcoinNet = 0xdcb7c1fc
)

var (
// MainNetParams are parser parameters for mainnet
MainNetParams chaincfg.Params
// TestNetParams are parser parameters for testnet
TestNetParams chaincfg.Params
// RegtestParams are parser parameters for regtest
RegtestParams chaincfg.Params
)

Expand Down
10 changes: 5 additions & 5 deletions bchain/coins/dash/dashrpc.go
Expand Up @@ -11,12 +11,12 @@ import (

const firstBlockWithSpecialTransactions = 1028160

// DashRPC is an interface to JSON-RPC bitcoind service.
// DashRPC is an interface to JSON-RPC bitcoind service
type DashRPC struct {
*btc.BitcoinRPC
}

// NewDashRPC returns new DashRPC instance.
// NewDashRPC returns new DashRPC instance
func NewDashRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
b, err := btc.NewBitcoinRPC(config, pushHandler)
if err != nil {
Expand Down Expand Up @@ -58,7 +58,7 @@ func (b *DashRPC) Initialize() error {
return nil
}

// GetBlock returns block with given hash.
// GetBlock returns block with given hash
func (b *DashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
if hash == "" && height < firstBlockWithSpecialTransactions {
return b.BitcoinRPC.GetBlock(hash, height)
Expand Down Expand Up @@ -108,6 +108,6 @@ func (b *DashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {

// GetTransactionForMempool returns a transaction by the transaction ID.
// It could be optimized for mempool, i.e. without block time and confirmations
func (z *DashRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
return z.GetTransaction(txid)
func (b *DashRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
return b.GetTransaction(txid)
}
6 changes: 6 additions & 0 deletions bchain/coins/zec/zcashparser.go
Expand Up @@ -9,14 +9,20 @@ import (
)

const (
// MainnetMagic is mainnet network constant
MainnetMagic wire.BitcoinNet = 0x6427e924
// TestnetMagic is testnet network constant
TestnetMagic wire.BitcoinNet = 0xbff91afa
// RegtestMagic is regtest network constant
RegtestMagic wire.BitcoinNet = 0x5f3fe8aa
)

var (
// MainNetParams are parser parameters for mainnet
MainNetParams chaincfg.Params
// TestNetParams are parser parameters for testnet
TestNetParams chaincfg.Params
// RegtestParams are parser parameters for regtest
RegtestParams chaincfg.Params
)

Expand Down
4 changes: 3 additions & 1 deletion bchain/coins/zec/zcashrpc.go
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/juju/errors"
)

// ZCashRPC is an interface to JSON-RPC bitcoind service
type ZCashRPC struct {
*btc.BitcoinRPC
}

// NewZCashRPC returns new ZCashRPC instance
func NewZCashRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
b, err := btc.NewBitcoinRPC(config, pushHandler)
if err != nil {
Expand All @@ -26,7 +28,7 @@ func NewZCashRPC(config json.RawMessage, pushHandler func(bchain.NotificationTyp
return z, nil
}

// Initialize initializes ZCashRPC instance.
// Initialize initializes ZCashRPC instance
func (z *ZCashRPC) Initialize() error {
chainName, err := z.GetChainInfoAndInitializeMempool(z)
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions db/rocksdb.go
Expand Up @@ -353,37 +353,44 @@ type outpoint struct {
index int32
}

// TxInput holds input data of the transaction in TxAddresses
type TxInput struct {
AddrDesc bchain.AddressDescriptor
ValueSat big.Int
}

// Addresses converts AddressDescriptor of the input to array of strings
func (ti *TxInput) Addresses(p bchain.BlockChainParser) ([]string, bool, error) {
return p.GetAddressesFromAddrDesc(ti.AddrDesc)
}

// TxOutput holds output data of the transaction in TxAddresses
type TxOutput struct {
AddrDesc bchain.AddressDescriptor
Spent bool
ValueSat big.Int
}

// Addresses converts AddressDescriptor of the output to array of strings
func (to *TxOutput) Addresses(p bchain.BlockChainParser) ([]string, bool, error) {
return p.GetAddressesFromAddrDesc(to.AddrDesc)
}

// TxAddresses stores transaction inputs and outputs with amounts
type TxAddresses struct {
Height uint32
Inputs []TxInput
Outputs []TxOutput
}

// AddrBalance stores number of transactions and balances of an address
type AddrBalance struct {
Txs uint32
SentSat big.Int
BalanceSat big.Int
}

// ReceivedSat computes received amount from total balance and sent amount
func (ab *AddrBalance) ReceivedSat() *big.Int {
var r big.Int
r.Add(&ab.BalanceSat, &ab.SentSat)
Expand Down Expand Up @@ -547,15 +554,18 @@ func (d *RocksDB) processAddressesBitcoinType(block *bchain.Block, addresses add
return nil
}

// addToAddressesMap maintains mapping between addresses and transactions in one block
// the method assumes that outpus in the block are processed before the inputs
// the return value is true if the tx was processed before, to not to count the tx multiple times
func addToAddressesMap(addresses addressesMap, strAddrDesc string, btxID []byte, index int32) bool {
// check that the address was used already in this block
// check that the address was already processed in this block
// if not found, it has certainly not been counted
at, found := addresses[strAddrDesc]
if found {
// check that the address was already used in this tx
// if the tx is already in the slice, append the index to the array of indexes
for i, t := range at {
if bytes.Equal(btxID, t.btxID) {
at[i].indexes = append(at[i].indexes, index)
at[i].indexes = append(t.indexes, index)
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions db/rocksdb_ethereumtype.go
Expand Up @@ -219,15 +219,15 @@ func (d *RocksDB) processAddressesEthereumType(block *bchain.Block, addresses ad
glog.Warningf("rocksdb: GetErc20FromTx %v - height %d, tx %v, transfer %v", err, block.Height, tx.Txid, t)
continue
}
if err = d.addToAddressesAndContractsEthereumType(from, btxID, ^int32(i), contract, addresses, addressContracts, true); err != nil {
if err = d.addToAddressesAndContractsEthereumType(to, btxID, int32(i), contract, addresses, addressContracts, true); err != nil {
return nil, err
}
eq := bytes.Equal(from, to)
bc := &blockTx.contracts[j]
j++
bc.addr = from
bc.contract = contract
if err = d.addToAddressesAndContractsEthereumType(to, btxID, int32(i), contract, addresses, addressContracts, !eq); err != nil {
if err = d.addToAddressesAndContractsEthereumType(from, btxID, ^int32(i), contract, addresses, addressContracts, !eq); err != nil {
return nil, err
}
// add to address to blockTx.contracts only if it is different from from address
Expand Down
4 changes: 4 additions & 0 deletions db/rocksdb_test.go
Expand Up @@ -484,6 +484,10 @@ func TestRocksDB_Index_BitcoinType(t *testing.T) {
verifyGetTransactions(t, d, dbtestdata.Addr8, 0, 1000000, []txidIndex{
{dbtestdata.TxidB2T2, 0},
}, nil)
verifyGetTransactions(t, d, dbtestdata.Addr6, 0, 1000000, []txidIndex{
{dbtestdata.TxidB2T2, ^0},
{dbtestdata.TxidB2T1, 0},
}, nil)
verifyGetTransactions(t, d, "mtGXQvBowMkBpnhLckhxhbwYK44Gs9eBad", 500000, 1000000, []txidIndex{}, errors.New("checksum mismatch"))

// GetBestBlock
Expand Down

0 comments on commit a13a791

Please sign in to comment.