diff --git a/accounter/accounter_test.go b/accounter/accounter_test.go index 1c7d837..6d67f88 100644 --- a/accounter/accounter_test.go +++ b/accounter/accounter_test.go @@ -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" ) @@ -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) diff --git a/backend/btcd_backend.go b/backend/btcd_backend.go index dbb1cb2..1b5a398 100644 --- a/backend/btcd_backend.go +++ b/backend/btcd_backend.go @@ -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 @@ -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 @@ -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, @@ -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() diff --git a/backend/common.go b/backend/common.go index 5e639b0..67649bd 100644 --- a/backend/common.go +++ b/backend/common.go @@ -1,7 +1,7 @@ package backend import ( - "github.com/square/beancounter/utils" + . "github.com/square/beancounter/utils" "time" ) @@ -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 diff --git a/backend/electrum/blockchain.go b/backend/electrum/blockchain.go index 3210dfb..cb2dae5 100644 --- a/backend/electrum/blockchain.go +++ b/backend/electrum/blockchain.go @@ -9,7 +9,7 @@ import ( "time" "github.com/bcext/cashutil" - "github.com/square/beancounter/utils" + . "github.com/square/beancounter/utils" ) const ( @@ -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 @@ -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 @@ -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") diff --git a/backend/electrum_backend.go b/backend/electrum_backend.go index f799cb2..de8be67 100644 --- a/backend/electrum_backend.go +++ b/backend/electrum_backend.go @@ -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. @@ -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 @@ -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, @@ -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() @@ -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() @@ -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 { @@ -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. @@ -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) } @@ -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) } diff --git a/backend/electrum_backend_test.go b/backend/electrum_backend_test.go index 96bb83f..8f97a00 100644 --- a/backend/electrum_backend_test.go +++ b/backend/electrum_backend_test.go @@ -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" ) @@ -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), diff --git a/backend/fixture_backend.go b/backend/fixture_backend.go index f6afd9d..b29145b 100644 --- a/backend/fixture_backend.go +++ b/backend/fixture_backend.go @@ -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) diff --git a/backend/fixture_backend_test.go b/backend/fixture_backend_test.go index 6804fbd..7234f27 100644 --- a/backend/fixture_backend_test.go +++ b/backend/fixture_backend_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/square/beancounter/deriver" - "github.com/square/beancounter/utils" + . "github.com/square/beancounter/utils" "github.com/stretchr/testify/assert" ) @@ -47,7 +47,7 @@ func TestNoAddress(t *testing.T) { b, err := NewFixtureBackend("../accounter/testdata/tpub_data.json") assert.NoError(t, err) - b.AddrRequest(deriver.NewAddress("m/1'/1/0/1", "BAD_ADDRESS", utils.Testnet, 0, 1)) + b.AddrRequest(deriver.NewAddress("m/1'/1/0/1", "BAD_ADDRESS", Testnet, 0, 1)) var addrs []*AddrResponse var txs []*TxResponse @@ -66,7 +66,7 @@ func TestAddressNoTransactions(t *testing.T) { b, err := NewFixtureBackend("../accounter/testdata/tpub_data.json") assert.NoError(t, err) - b.AddrRequest(deriver.NewAddress("m/1'/1234/0/61", "mfsNoNz57ANkYrCzHaLZDLoMGujBW8u3zv", utils.Testnet, 0, 61)) + b.AddrRequest(deriver.NewAddress("m/1'/1234/0/61", "mfsNoNz57ANkYrCzHaLZDLoMGujBW8u3zv", Testnet, 0, 61)) var addrs []*AddrResponse var txs []*TxResponse @@ -83,7 +83,7 @@ func TestAddressWithTransactions(t *testing.T) { b, err := NewFixtureBackend("../accounter/testdata/tpub_data.json") assert.NoError(t, err) - b.AddrRequest(deriver.NewAddress("m/1'/1234/0/7", "mi2udMvJHeeJJNp5wWKToa86L2cJUKzrby", utils.Testnet, 0, 7)) + b.AddrRequest(deriver.NewAddress("m/1'/1234/0/7", "mi2udMvJHeeJJNp5wWKToa86L2cJUKzrby", Testnet, 0, 7)) var addrs []*AddrResponse var txs []*TxResponse diff --git a/utils/utils.go b/utils/utils.go index b6a6a6f..5038e86 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -25,13 +25,13 @@ func Max(num uint32, nums ...uint32) uint32 { } type Network string -type Backend string +type BackendName string const ( - Mainnet Network = "mainnet" - Testnet Network = "testnet" - Electrum Backend = "electrum" - Btcd Backend = "btcd" + Mainnet Network = "mainnet" + Testnet Network = "testnet" + Electrum BackendName = "electrum" + Btcd BackendName = "btcd" ) // ChainConfig returns a given chaincfg.Params for a given Network @@ -105,7 +105,7 @@ func VerifyMandN(m int, n int) error { // Picks a default server for electrum or localhost for btcd // Returns a pair of hostname:port (or pseudo-port for electrum) -func GetDefaultServer(network Network, backend Backend, addr string) (string, string) { +func GetDefaultServer(network Network, backend BackendName, addr string) (string, string) { if addr != "" { host, port, err := net.SplitHostPort(addr) PanicOnError(err)