Skip to content
This repository has been archived by the owner on Mar 2, 2023. It is now read-only.

Commit

Permalink
Merge fd20d53 into 292747f
Browse files Browse the repository at this point in the history
  • Loading branch information
alokmenghrajani committed Oct 27, 2018
2 parents 292747f + fd20d53 commit b789a11
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 87 deletions.
4 changes: 2 additions & 2 deletions accounter/accounter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/square/beancounter/backend"
"github.com/square/beancounter/deriver"
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestProcessTransactions(t *testing.T) {

func TestComputeBalanceTestnet(t *testing.T) {
pubs := []string{"tpubDBrCAXucLxvjC9n9nZGGcYS8pk4X1N97YJmUgdDSwG2p36gbSqeRuytHYCHe2dHxLsV2EchX9ePaFdRwp7cNLrSpnr3PsoPLUQqbvLBDWvh"}
deriver := deriver.NewAddressDeriver(utils.Testnet, pubs, 1, "")
deriver := deriver.NewAddressDeriver(Testnet, pubs, 1, "")
b, err := backend.NewFixtureBackend("testdata/tpub_data.json")
assert.NoError(t, err)
a := New(b, deriver, 100, 1435169)
Expand Down
10 changes: 5 additions & 5 deletions backend/btcd_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/square/beancounter/deriver"
"github.com/square/beancounter/reporter"
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
)

// BtcdBackend wraps Btcd node and its API to provide a simple
Expand All @@ -21,7 +21,7 @@ type BtcdBackend struct {
chainHeight uint32

client *rpcclient.Client
network utils.Network
network Network
blockHeightMu sync.Mutex // mutex to guard read/writes to blockHeightLookup map
blockHeightLookup map[string]int64

Expand Down Expand Up @@ -58,7 +58,7 @@ const (
// BtcdBackend is meants to connect to a personal Btcd node (because public nodes don't expose the
// API we need). There's no TLS support. If your node is not co-located with Beancounter, we
// recommend wrapping your connection in a ssh or other secure tunnel.
func NewBtcdBackend(host, port, user, pass string, network utils.Network) (*BtcdBackend, error) {
func NewBtcdBackend(host, port, user, pass string, network Network) (*BtcdBackend, error) {
connCfg := &rpcclient.ConnConfig{
Host: fmt.Sprintf("%s:%s", host, port),
User: user,
Expand All @@ -76,8 +76,8 @@ func NewBtcdBackend(host, port, user, pass string, network utils.Network) (*Btcd
if err != nil {
return nil, errors.Wrap(err, "GetBlockHash(0) failed")
}
if genesis.String() != utils.GenesisBlock(network) {
return nil, errors.Errorf("Unexpected genesis block %s != %s", genesis.String(), utils.GenesisBlock(network))
if genesis.String() != GenesisBlock(network) {
return nil, errors.Errorf("Unexpected genesis block %s != %s", genesis.String(), GenesisBlock(network))
}

height, err := client.GetBlockCount()
Expand Down
14 changes: 7 additions & 7 deletions backend/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backend

import (
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
"time"
)

Expand All @@ -20,12 +20,12 @@ type metadata struct {
}

type address struct {
Address string `json:"address"`
Path string `json:"path"`
Network utils.Network `json:"network"`
Change uint32 `json:"change"`
AddressIndex uint32 `json:"addr_index"`
TxHashes []string `json:"tx_hashes"`
Address string `json:"address"`
Path string `json:"path"`
Network Network `json:"network"`
Change uint32 `json:"change"`
AddressIndex uint32 `json:"addr_index"`
TxHashes []string `json:"tx_hashes"`
}

type byAddress []address
Expand Down
12 changes: 6 additions & 6 deletions backend/electrum/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/bcext/cashutil"
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
)

const (
Expand All @@ -19,7 +19,7 @@ const (
type Node struct {
// Ident is a an identifier of the form 127.0.0.1|s1234 or ::1|t5432.
Ident string
Network utils.Network
Network Network

transport Transport

Expand Down Expand Up @@ -125,7 +125,7 @@ type Block struct {
Max uint `json:"max"`
}

func NewNode(addr, port string, network utils.Network) (*Node, error) {
func NewNode(addr, port string, network Network) (*Node, error) {
n := &Node{}
var a string
var t Transport
Expand Down Expand Up @@ -300,11 +300,11 @@ func (n *Node) request(method string, params []interface{}, result interface{})
return nil
}

func defaultPorts(network utils.Network) (string, string) {
func defaultPorts(network Network) (string, string) {
switch network {
case utils.Mainnet:
case Mainnet:
return "50001", "50002"
case utils.Testnet:
case Testnet:
return "50101", "50102"
default:
panic("unreachable")
Expand Down
18 changes: 9 additions & 9 deletions backend/electrum_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/square/beancounter/backend/electrum"
"github.com/square/beancounter/deriver"
"github.com/square/beancounter/reporter"
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
)

// Fetches transaction information from Electrum servers.
Expand All @@ -41,7 +41,7 @@ type ElectrumBackend struct {
// todo: blacklistedNodes should be a timestamp and we should re-try after a certain amount of
// time has elapsed.
blacklistedNodes map[string]struct{}
network utils.Network
network Network

// channels used to communicate with the Accounter
addrRequests chan *deriver.Address
Expand Down Expand Up @@ -77,7 +77,7 @@ var (
// NewElectrumBackend returns a new ElectrumBackend structs or errors.
// Initially connects to 1 node. A background job handles connecting to
// additional peers. The background job fails if there are no peers left.
func NewElectrumBackend(addr, port string, network utils.Network) (*ElectrumBackend, error) {
func NewElectrumBackend(addr, port string, network Network) (*ElectrumBackend, error) {

// TODO: should the channels have k * maxPeers buffers? Each node needs to enqueue a
// potentially large number of transactions. If all nodes are doing that at the same time,
Expand Down Expand Up @@ -178,7 +178,7 @@ func (eb *ElectrumBackend) ChainHeight() uint32 {
}

// Connect to a node and add it to the map of nodes
func (eb *ElectrumBackend) addNode(addr, port string, network utils.Network) error {
func (eb *ElectrumBackend) addNode(addr, port string, network Network) error {
ident := electrum.NodeIdent(addr, port)

eb.nodeMu.RLock()
Expand Down Expand Up @@ -211,7 +211,7 @@ func (eb *ElectrumBackend) addNode(addr, port string, network utils.Network) err
return err
}
// Check genesis block
if feature.Genesis != utils.GenesisBlock(network) {
if feature.Genesis != GenesisBlock(network) {
eb.nodeMu.Lock()
eb.blacklistedNodes[ident] = struct{}{}
eb.nodeMu.Unlock()
Expand Down Expand Up @@ -251,7 +251,7 @@ func (eb *ElectrumBackend) addNode(addr, port string, network utils.Network) err
}

// Connect to a node without registering it, fetch height and disconnect.
func (eb *ElectrumBackend) getHeight(addr, port string, network utils.Network) (uint32, error) {
func (eb *ElectrumBackend) getHeight(addr, port string, network Network) (uint32, error) {
log.Printf("connecting to %s", addr)
node, err := electrum.NewNode(addr, port, network)
if err != nil {
Expand All @@ -265,7 +265,7 @@ func (eb *ElectrumBackend) getHeight(addr, port string, network utils.Network) (
return 0, err
}
// Check genesis block
if feature.Genesis != utils.GenesisBlock(network) {
if feature.Genesis != GenesisBlock(network) {
return 0, ErrIncorrectGenesisBlock
}
// TODO: check pruning. Currently, servers currently don't prune, so it's fine to skip for now.
Expand Down Expand Up @@ -505,7 +505,7 @@ func (eb *ElectrumBackend) addPeer(peer electrum.Peer) {
}
for _, feature := range peer.Features {
if strings.HasPrefix(feature, "t") {
go func(addr, feature string, network utils.Network) {
go func(addr, feature string, network Network) {
if err := eb.addNode(addr, feature, network); err != nil {
log.Printf("error on addNode: %+v\n", err)
}
Expand All @@ -515,7 +515,7 @@ func (eb *ElectrumBackend) addPeer(peer electrum.Peer) {
}
for _, feature := range peer.Features {
if strings.HasPrefix(feature, "s") {
go func(addr, feature string, network utils.Network) {
go func(addr, feature string, network Network) {
if err := eb.addNode(addr, feature, network); err != nil {
log.Printf("error on addNode: %+v\n", err)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/electrum_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package backend
import (
"github.com/square/beancounter/backend/electrum"
"github.com/square/beancounter/deriver"
"github.com/square/beancounter/utils"
. "github.com/square/beancounter/utils"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -14,7 +14,7 @@ func TestTransactionCache(t *testing.T) {
eb := &ElectrumBackend{
nodes: make(map[string]*electrum.Node),
blacklistedNodes: make(map[string]struct{}),
network: utils.Testnet,
network: Testnet,
addrRequests: make(chan *deriver.Address, 2*maxPeers),
addrResponses: make(chan *AddrResponse, 2*maxPeers),
txRequests: make(chan string, 2*maxPeers),
Expand Down
92 changes: 46 additions & 46 deletions backend/fixture_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,114 +75,114 @@ func NewFixtureBackend(filepath string) (*FixtureBackend, error) {

// AddrRequest schedules a request to the backend to lookup information related
// to the given address.
func (b *FixtureBackend) AddrRequest(addr *deriver.Address) {
func (fb *FixtureBackend) AddrRequest(addr *deriver.Address) {
reporter.GetInstance().IncAddressesScheduled()
reporter.GetInstance().Logf("[fixture] scheduling address: %s", addr)
b.addrRequests <- addr
fb.addrRequests <- addr
}

// TxRequest schedules a request to the backend to lookup information related
// to the given transaction hash.
func (b *FixtureBackend) TxRequest(txHash string) {
func (fb *FixtureBackend) TxRequest(txHash string) {
reporter.GetInstance().IncTxScheduled()
reporter.GetInstance().Logf("[fixture] scheduling tx: %s", txHash)
b.txRequests <- txHash
fb.txRequests <- txHash
}

func (b *FixtureBackend) BlockRequest(height uint32) {
b.blockRequests <- height
func (fb *FixtureBackend) BlockRequest(height uint32) {
fb.blockRequests <- height
}

// AddrResponses exposes a channel that allows to consume backend's responses to
// address requests created with AddrRequest()
func (b *FixtureBackend) AddrResponses() <-chan *AddrResponse {
return b.addrResponses
func (fb *FixtureBackend) AddrResponses() <-chan *AddrResponse {
return fb.addrResponses
}

// TxResponses exposes a channel that allows to consume backend's responses to
// address requests created with addrrequest().
// if an address has any transactions then they will be sent to this channel by the
// backend.
func (b *FixtureBackend) TxResponses() <-chan *TxResponse {
return b.txResponses
func (fb *FixtureBackend) TxResponses() <-chan *TxResponse {
return fb.txResponses
}

func (b *FixtureBackend) BlockResponses() <-chan *BlockResponse {
return b.blockResponses
func (fb *FixtureBackend) BlockResponses() <-chan *BlockResponse {
return fb.blockResponses
}

// Finish informs the backend to stop doing its work.
func (b *FixtureBackend) Finish() {
close(b.doneCh)
func (fb *FixtureBackend) Finish() {
close(fb.doneCh)
}

func (b *FixtureBackend) ChainHeight() uint32 {
return b.height
func (fb *FixtureBackend) ChainHeight() uint32 {
return fb.height
}

func (b *FixtureBackend) processRequests() {
func (fb *FixtureBackend) processRequests() {
for {
select {
case addr := <-b.addrRequests:
b.processAddrRequest(addr)
case tx := <-b.txRequests:
b.processTxRequest(tx)
case addrResp, ok := <-b.addrResponses:
case addr := <-fb.addrRequests:
fb.processAddrRequest(addr)
case tx := <-fb.txRequests:
fb.processTxRequest(tx)
case addrResp, ok := <-fb.addrResponses:
if !ok {
b.addrResponses = nil
fb.addrResponses = nil
continue
}
b.addrResponses <- addrResp
case txResp, ok := <-b.txResponses:
fb.addrResponses <- addrResp
case txResp, ok := <-fb.txResponses:
if !ok {
b.txResponses = nil
fb.txResponses = nil
continue
}
b.txResponses <- txResp
case block := <-b.blockRequests:
b.processBlockRequest(block)
case <-b.doneCh:
fb.txResponses <- txResp
case block := <-fb.blockRequests:
fb.processBlockRequest(block)
case <-fb.doneCh:
return
}
}
}

func (b *FixtureBackend) processAddrRequest(addr *deriver.Address) {
b.addrIndexMu.Lock()
resp, exists := b.addrIndex[addr.String()]
b.addrIndexMu.Unlock()
func (fb *FixtureBackend) processAddrRequest(addr *deriver.Address) {
fb.addrIndexMu.Lock()
resp, exists := fb.addrIndex[addr.String()]
fb.addrIndexMu.Unlock()

if exists {
b.addrResponses <- &resp
fb.addrResponses <- &resp
return
}

// assuming that address has not been used
b.addrResponses <- &AddrResponse{
fb.addrResponses <- &AddrResponse{
Address: addr,
}
}

func (b *FixtureBackend) processTxRequest(txHash string) {
b.txIndexMu.Lock()
resp, exists := b.txIndex[txHash]
b.txIndexMu.Unlock()
func (fb *FixtureBackend) processTxRequest(txHash string) {
fb.txIndexMu.Lock()
resp, exists := fb.txIndex[txHash]
fb.txIndexMu.Unlock()

if exists {
b.txResponses <- &resp
fb.txResponses <- &resp
return
}

// assuming that transaction does not exist in the fixture file
}

func (b *FixtureBackend) processBlockRequest(height uint32) {
b.blockIndexMu.Lock()
resp, exists := b.blockIndex[height]
b.blockIndexMu.Unlock()
func (fb *FixtureBackend) processBlockRequest(height uint32) {
fb.blockIndexMu.Lock()
resp, exists := fb.blockIndex[height]
fb.blockIndexMu.Unlock()

if exists {
b.blockResponses <- &resp
fb.blockResponses <- &resp
return
}
log.Panicf("fixture doesn't contain block %d", height)
Expand Down

0 comments on commit b789a11

Please sign in to comment.