diff --git a/api/rpc.go b/api/rpc.go index 33d9cea..e7848ad 100644 --- a/api/rpc.go +++ b/api/rpc.go @@ -3,6 +3,7 @@ package api import ( "encoding/hex" "errors" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -12,14 +13,13 @@ import ( "github.com/btcsuite/btcutil/hdkeychain" "github.com/cpacia/BitcoinCash-Wallet" "github.com/cpacia/BitcoinCash-Wallet/api/pb" + "github.com/cpacia/bchutil" "github.com/golang/protobuf/ptypes" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "net" "sync" - "github.com/OpenBazaar/wallet-interface" - "github.com/cpacia/bchutil" ) const Addr = "127.0.0.1:8234" diff --git a/blockchain.go b/blockchain.go index b5552c9..de1240c 100644 --- a/blockchain.go +++ b/blockchain.go @@ -19,8 +19,8 @@ import ( // chaincfg.Params so they'll go here. If you're into the [ANN]altcoin scene, // you may want to paramaterize these constants. const ( - targetSpacing = 600 - medianTimeBlocks = 11 + targetSpacing = 600 + medianTimeBlocks = 11 ) type ChainState int @@ -138,15 +138,14 @@ func (b *Blockchain) CheckHeader(header wire.BlockHeader, prevHeader StoredHeade // Due to the rolling difficulty period our checkpoint block consists of a block and a hash of a block 146 blocks later // During this period we can skip the validity checks as long as block checkpoint + 146 matches the hardcoded hash. - if height + 1 <= b.checkpoint.Height + 147 { + if height+1 <= b.checkpoint.Height+147 { h := header.BlockHash() - if b.checkpoint.Check2 != nil && height + 1 == b.checkpoint.Height + 147 && !b.checkpoint.Check2.IsEqual(&h){ + if b.checkpoint.Check2 != nil && height+1 == b.checkpoint.Height+147 && !b.checkpoint.Check2.IsEqual(&h) { return false } return true } - // Get hash of n-1 header prevHash := prevHeader.header.BlockHash() @@ -158,18 +157,18 @@ func (b *Blockchain) CheckHeader(header wire.BlockHeader, prevHeader StoredHeade // Check the header meets the difficulty requirement if b.params.Name != chaincfg.RegressionNetParams.Name { // Don't need to check difficulty on regtest - diffTarget, err := b.calcRequiredWork(header, int32(height + 1), prevHeader) + diffTarget, err := b.calcRequiredWork(header, int32(height+1), prevHeader) if err != nil { log.Errorf("Error calclating difficulty", err) return false } if header.Bits != diffTarget && b.params.Name == chaincfg.MainNetParams.Name { log.Warningf("Block %d %s incorrect difficulty. Read %d, expect %d\n", - height + 1, header.BlockHash().String(), header.Bits, diffTarget) + height+1, header.BlockHash().String(), header.Bits, diffTarget) return false } else if diffTarget == b.params.PowLimitBits && header.Bits > diffTarget && b.params.Name == chaincfg.TestNet3Params.Name { log.Warningf("Block %d %s incorrect difficulty. Read %d, expect %d\n", - height + 1, header.BlockHash().String(), header.Bits, diffTarget) + height+1, header.BlockHash().String(), header.Bits, diffTarget) return false } } @@ -187,7 +186,7 @@ func (b *Blockchain) CheckHeader(header wire.BlockHeader, prevHeader StoredHeade // or testnet difficulty rules. func (b *Blockchain) calcRequiredWork(header wire.BlockHeader, height int32, prevHeader StoredHeader) (uint32, error) { // Special difficulty rule for testnet - if b.params.ReduceMinDifficulty && header.Timestamp.After(prevHeader.header.Timestamp.Add(targetSpacing * 2)) { + if b.params.ReduceMinDifficulty && header.Timestamp.After(prevHeader.header.Timestamp.Add(targetSpacing*2)) { return b.params.PowLimitBits, nil } @@ -226,7 +225,7 @@ func (b *Blockchain) CalcMedianTimePast(header wire.BlockHeader) (time.Time, err // Rollsback and grabs block n-144, n-145, and n-146, sorts them by timestamps and returns the middle header. func (b *Blockchain) GetEpoch(hdr wire.BlockHeader) (StoredHeader, error) { - sh := StoredHeader{header:hdr} + sh := StoredHeader{header: hdr} var err error for i := 0; i < 144; i++ { sh, err = b.db.GetPreviousHeader(sh.header) @@ -462,9 +461,9 @@ func calcDiffAdjust(start, end StoredHeader, p *chaincfg.Params) uint32 { // In order to avoid difficulty cliffs, we bound the amplitude of the // adjustement we are going to do. duration := end.header.Timestamp.Unix() - start.header.Timestamp.Unix() - if (duration > 288 * int64(targetSpacing)) { + if duration > 288*int64(targetSpacing) { duration = 288 * int64(targetSpacing) - } else if (duration < 72 * int64(targetSpacing)) { + } else if duration < 72*int64(targetSpacing) { duration = 72 * int64(targetSpacing) } diff --git a/config.go b/config.go index d4384f4..dffb836 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package bitcoincash import ( + "github.com/OpenBazaar/openbazaar-go/bitcoin" "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/chaincfg" "github.com/mitchellh/go-homedir" @@ -12,7 +13,6 @@ import ( "path/filepath" "runtime" "time" - "github.com/OpenBazaar/openbazaar-go/bitcoin" ) type Config struct { @@ -60,6 +60,9 @@ type Config struct { // An exchange rate provider implementation for Bitcoin Cash ExchangeRateProvider bitcoin.ExchangeRates + + // A slice of additional items to add to the bloom filter + AdditionalFilters [][]byte } func NewDefaultConfig() *Config { diff --git a/db/database.go b/db/database.go index cc9098a..8f7aded 100644 --- a/db/database.go +++ b/db/database.go @@ -2,11 +2,11 @@ package db import ( "database/sql" + "github.com/OpenBazaar/wallet-interface" _ "github.com/mattn/go-sqlite3" "path" "sync" "time" - "github.com/OpenBazaar/wallet-interface" ) // This database is mostly just an example implementation used for testing. diff --git a/db/keys.go b/db/keys.go index a23bab1..9acba1a 100644 --- a/db/keys.go +++ b/db/keys.go @@ -5,11 +5,11 @@ import ( "encoding/hex" "errors" "fmt" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/btcec" "math/rand" "strconv" "sync" - "github.com/OpenBazaar/wallet-interface" ) type KeysDB struct { diff --git a/db/keys_test.go b/db/keys_test.go index cc8e54f..03a5067 100644 --- a/db/keys_test.go +++ b/db/keys_test.go @@ -5,10 +5,10 @@ import ( "crypto/rand" "database/sql" "encoding/hex" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/btcec" "sync" "testing" - "github.com/OpenBazaar/wallet-interface" ) var kdb KeysDB diff --git a/db/stxo.go b/db/stxo.go index cd3731b..43adc50 100644 --- a/db/stxo.go +++ b/db/stxo.go @@ -3,12 +3,12 @@ package db import ( "database/sql" "encoding/hex" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "strconv" "strings" "sync" - "github.com/OpenBazaar/wallet-interface" ) type StxoDB struct { diff --git a/db/stxo_test.go b/db/stxo_test.go index 89e9e89..9075cf9 100644 --- a/db/stxo_test.go +++ b/db/stxo_test.go @@ -4,12 +4,12 @@ import ( "bytes" "database/sql" "encoding/hex" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "strconv" "sync" "testing" - "github.com/OpenBazaar/wallet-interface" ) var sxdb StxoDB diff --git a/db/utxo.go b/db/utxo.go index 6c30835..ac1b24a 100644 --- a/db/utxo.go +++ b/db/utxo.go @@ -3,12 +3,12 @@ package db import ( "database/sql" "encoding/hex" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "strconv" "strings" "sync" - "github.com/OpenBazaar/wallet-interface" ) type UtxoDB struct { diff --git a/db/utxo_test.go b/db/utxo_test.go index 27324bb..d784ecf 100644 --- a/db/utxo_test.go +++ b/db/utxo_test.go @@ -4,12 +4,12 @@ import ( "bytes" "database/sql" "encoding/hex" + "github.com/OpenBazaar/wallet-interface" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "strconv" "sync" "testing" - "github.com/OpenBazaar/wallet-interface" ) var uxdb UtxoDB diff --git a/eight333.go b/eight333.go index 652ba57..2ce8f5b 100644 --- a/eight333.go +++ b/eight333.go @@ -1,11 +1,11 @@ package bitcoincash import ( + "bytes" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/peer" "github.com/btcsuite/btcd/wire" "time" - "bytes" ) var ( diff --git a/examples/client.go b/examples/client.go index 80e0c76..d7bbe3f 100644 --- a/examples/client.go +++ b/examples/client.go @@ -4,9 +4,9 @@ import ( "fmt" "os" + "github.com/btcsuite/btcd/chaincfg" "github.com/cpacia/BitcoinCash-Wallet" "github.com/cpacia/BitcoinCash-Wallet/db" - "github.com/btcsuite/btcd/chaincfg" "github.com/op/go-logging" ) diff --git a/exchangerates/exchangerates.go b/exchangerates/exchangerates.go index eaff683..2291499 100644 --- a/exchangerates/exchangerates.go +++ b/exchangerates/exchangerates.go @@ -8,10 +8,10 @@ import ( "golang.org/x/net/proxy" "net" "net/http" + "reflect" "strconv" "sync" "time" - "reflect" ) type ExchangeRateProvider struct { @@ -162,7 +162,7 @@ func (b OpenBazaarDecoder) decode(dat interface{}, cache map[string]float64, bp if !ok { return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed, missing 'last' (float) field") } - cache[k] = price*(1/bchRate) + cache[k] = price * (1 / bchRate) } } return nil @@ -318,4 +318,4 @@ func (b PoloniexDecoder) decode(dat interface{}, cache map[string]float64, bp *e cache[k] = v * rate } return nil -} \ No newline at end of file +} diff --git a/fees.go b/fees.go index ea9bdc0..e2b81cf 100644 --- a/fees.go +++ b/fees.go @@ -1,10 +1,10 @@ package bitcoincash import ( + "github.com/OpenBazaar/openbazaar-go/bitcoin" + "github.com/OpenBazaar/wallet-interface" "net/http" "time" - "github.com/OpenBazaar/wallet-interface" - "github.com/OpenBazaar/openbazaar-go/bitcoin" ) type httpClient interface { @@ -40,18 +40,18 @@ type FeeTarget int const ( EconomicTarget FeeTarget = 1 - NormalTarget FeeTarget = 5 + NormalTarget FeeTarget = 5 PriorityTarget FeeTarget = 10 ) func NewFeeProvider(maxFee, priorityFee, normalFee, economicFee uint64, exchangeRates bitcoin.ExchangeRates) *FeeProvider { return &FeeProvider{ - maxFee: maxFee, - priorityFee: priorityFee, - normalFee: normalFee, - economicFee: economicFee, + maxFee: maxFee, + priorityFee: priorityFee, + normalFee: normalFee, + economicFee: economicFee, exchangeRates: exchangeRates, - cache: new(feeCache), + cache: new(feeCache), } } @@ -94,12 +94,11 @@ func (fp *FeeProvider) GetFeePerByte(feeLevel wallet.FeeLevel) uint64 { target = NormalTarget } - feePerByte := (((float64(target)/100) / rate) * 100000000) / 226 + feePerByte := (((float64(target) / 100) / rate) * 100000000) / 226 if uint64(feePerByte) > fp.maxFee { return fp.maxFee } - return uint64(feePerByte) } diff --git a/fees_test.go b/fees_test.go index 3f41210..eb1fbc9 100644 --- a/fees_test.go +++ b/fees_test.go @@ -2,8 +2,8 @@ package bitcoincash import ( "bytes" - "testing" "github.com/OpenBazaar/wallet-interface" + "testing" ) type ClosingBuffer struct { diff --git a/gui/resources.go b/gui/resources.go index b475cc4..0c0e834 100644 --- a/gui/resources.go +++ b/gui/resources.go @@ -413,20 +413,20 @@ func AssetNames() []string { // _bindata is a table, holding each asset generator, mapped to its name. var _bindata = map[string]func() (*asset, error){ - "resources/app/block.png": resourcesAppBlockPng, - "resources/app/caution.png": resourcesAppCautionPng, - "resources/app/icon.png": resourcesAppIconPng, - "resources/app/index.html": resourcesAppIndexHtml, - "resources/app/js/qrcode.min.js": resourcesAppJsQrcodeMinJs, - "resources/app/list.png": resourcesAppListPng, - "resources/app/minimize.jpg": resourcesAppMinimizeJpg, - "resources/app/minimize.png": resourcesAppMinimizePng, - "resources/app/settings.png": resourcesAppSettingsPng, - "resources/app/static/css/base.css": resourcesAppStaticCssBaseCss, - "resources/app/static/fonts/NotoSans-Bold-webfont.ttf": resourcesAppStaticFontsNotosansBoldWebfontTtf, + "resources/app/block.png": resourcesAppBlockPng, + "resources/app/caution.png": resourcesAppCautionPng, + "resources/app/icon.png": resourcesAppIconPng, + "resources/app/index.html": resourcesAppIndexHtml, + "resources/app/js/qrcode.min.js": resourcesAppJsQrcodeMinJs, + "resources/app/list.png": resourcesAppListPng, + "resources/app/minimize.jpg": resourcesAppMinimizeJpg, + "resources/app/minimize.png": resourcesAppMinimizePng, + "resources/app/settings.png": resourcesAppSettingsPng, + "resources/app/static/css/base.css": resourcesAppStaticCssBaseCss, + "resources/app/static/fonts/NotoSans-Bold-webfont.ttf": resourcesAppStaticFontsNotosansBoldWebfontTtf, "resources/app/static/fonts/NotoSans-BoldItalic-webfont.ttf": resourcesAppStaticFontsNotosansBolditalicWebfontTtf, - "resources/app/static/fonts/NotoSans-Italic-webfont.ttf": resourcesAppStaticFontsNotosansItalicWebfontTtf, - "resources/app/static/fonts/NotoSans-Regular-webfont.ttf": resourcesAppStaticFontsNotosansRegularWebfontTtf, + "resources/app/static/fonts/NotoSans-Italic-webfont.ttf": resourcesAppStaticFontsNotosansItalicWebfontTtf, + "resources/app/static/fonts/NotoSans-Regular-webfont.ttf": resourcesAppStaticFontsNotosansRegularWebfontTtf, } // AssetDir returns the file names below a certain @@ -468,17 +468,18 @@ type bintree struct { Func func() (*asset, error) Children map[string]*bintree } + var _bintree = &bintree{nil, map[string]*bintree{ "resources": &bintree{nil, map[string]*bintree{ "app": &bintree{nil, map[string]*bintree{ - "block.png": &bintree{resourcesAppBlockPng, map[string]*bintree{}}, + "block.png": &bintree{resourcesAppBlockPng, map[string]*bintree{}}, "caution.png": &bintree{resourcesAppCautionPng, map[string]*bintree{}}, - "icon.png": &bintree{resourcesAppIconPng, map[string]*bintree{}}, - "index.html": &bintree{resourcesAppIndexHtml, map[string]*bintree{}}, + "icon.png": &bintree{resourcesAppIconPng, map[string]*bintree{}}, + "index.html": &bintree{resourcesAppIndexHtml, map[string]*bintree{}}, "js": &bintree{nil, map[string]*bintree{ "qrcode.min.js": &bintree{resourcesAppJsQrcodeMinJs, map[string]*bintree{}}, }}, - "list.png": &bintree{resourcesAppListPng, map[string]*bintree{}}, + "list.png": &bintree{resourcesAppListPng, map[string]*bintree{}}, "minimize.jpg": &bintree{resourcesAppMinimizeJpg, map[string]*bintree{}}, "minimize.png": &bintree{resourcesAppMinimizePng, map[string]*bintree{}}, "settings.png": &bintree{resourcesAppSettingsPng, map[string]*bintree{}}, @@ -487,10 +488,10 @@ var _bintree = &bintree{nil, map[string]*bintree{ "base.css": &bintree{resourcesAppStaticCssBaseCss, map[string]*bintree{}}, }}, "fonts": &bintree{nil, map[string]*bintree{ - "NotoSans-Bold-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansBoldWebfontTtf, map[string]*bintree{}}, + "NotoSans-Bold-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansBoldWebfontTtf, map[string]*bintree{}}, "NotoSans-BoldItalic-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansBolditalicWebfontTtf, map[string]*bintree{}}, - "NotoSans-Italic-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansItalicWebfontTtf, map[string]*bintree{}}, - "NotoSans-Regular-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansRegularWebfontTtf, map[string]*bintree{}}, + "NotoSans-Italic-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansItalicWebfontTtf, map[string]*bintree{}}, + "NotoSans-Regular-webfont.ttf": &bintree{resourcesAppStaticFontsNotosansRegularWebfontTtf, map[string]*bintree{}}, }}, }}, }}, @@ -543,4 +544,3 @@ func _filePath(dir, name string) string { cannonicalName := strings.Replace(name, "\\", "/", -1) return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) } - diff --git a/peers.go b/peers.go index 6ea9b7c..f4f9b28 100644 --- a/peers.go +++ b/peers.go @@ -14,8 +14,8 @@ import ( "github.com/btcsuite/btcd/peer" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil/bloom" - "golang.org/x/net/proxy" "github.com/cpacia/bchutil" + "golang.org/x/net/proxy" ) var ( diff --git a/sortsignsend.go b/sortsignsend.go index b8cd630..813895b 100644 --- a/sortsignsend.go +++ b/sortsignsend.go @@ -20,8 +20,8 @@ import ( "github.com/btcsuite/btcutil/txsort" "github.com/btcsuite/btcwallet/wallet/txauthor" "github.com/btcsuite/btcwallet/wallet/txrules" - "time" "github.com/cpacia/bchutil" + "time" ) func (s *SPVWallet) Broadcast(tx *wire.MsgTx) error { diff --git a/timesorter.go b/timesorter.go index 01e77cf..f3139ec 100644 --- a/timesorter.go +++ b/timesorter.go @@ -45,8 +45,8 @@ func (s blockSorter) Swap(i, j int) { // Less returns whether the timstamp with index i should sort before the // timestamp with index j. It is part of the sort.Interface implementation. func (s blockSorter) Less(i, j int) bool { - if s[i].header.Timestamp.Before(s[j].header.Timestamp) || s[i].header.Timestamp.Equal(s[j].header.Timestamp){ + if s[i].header.Timestamp.Before(s[j].header.Timestamp) || s[i].header.Timestamp.Equal(s[j].header.Timestamp) { return true } return false -} \ No newline at end of file +} diff --git a/txstore.go b/txstore.go index 88f32e2..672f6d5 100644 --- a/txstore.go +++ b/txstore.go @@ -31,17 +31,20 @@ type TxStore struct { listeners []func(wallet.TransactionCallback) + additionalFilters [][]byte + wallet.Datastore } -func NewTxStore(p *chaincfg.Params, db wallet.Datastore, keyManager *KeyManager) (*TxStore, error) { +func NewTxStore(p *chaincfg.Params, db wallet.Datastore, keyManager *KeyManager, additionalFilters ...[]byte) (*TxStore, error) { txs := &TxStore{ - params: p, - keyManager: keyManager, - addrMutex: new(sync.Mutex), - cbMutex: new(sync.Mutex), - txids: make(map[string]int32), - Datastore: db, + params: p, + keyManager: keyManager, + addrMutex: new(sync.Mutex), + cbMutex: new(sync.Mutex), + txids: make(map[string]int32), + Datastore: db, + additionalFilters: additionalFilters, } err := txs.PopulateAdrs() if err != nil { @@ -88,7 +91,9 @@ func (ts *TxStore) GimmeFilter() (*bloom.Filter, error) { } f.Add(addrs[0].ScriptAddress()) } - + for _, toAdd := range ts.additionalFilters { + f.Add(toAdd) + } return f, nil } diff --git a/wallet.go b/wallet.go index 93d98a4..4b3de42 100644 --- a/wallet.go +++ b/wallet.go @@ -10,16 +10,16 @@ import ( btc "github.com/btcsuite/btcutil" hd "github.com/btcsuite/btcutil/hdkeychain" "github.com/btcsuite/btcwallet/wallet/txrules" + "github.com/cpacia/bchutil" "github.com/op/go-logging" b39 "github.com/tyler-smith/go-bip39" "io" "sync" "time" - "github.com/cpacia/bchutil" ) func setupNetworkParams(params *chaincfg.Params) { - switch(params.Name){ + switch params.Name { case chaincfg.MainNetParams.Name: params.Net = bchutil.MainnetMagic case chaincfg.TestNet3Params.Name: @@ -29,7 +29,6 @@ func setupNetworkParams(params *chaincfg.Params) { } } - type SPVWallet struct { params *chaincfg.Params @@ -112,7 +111,7 @@ func NewSPVWallet(config *Config) (*SPVWallet, error) { w.keyManager, err = NewKeyManager(config.DB.Keys(), w.params, w.masterPrivateKey) - w.txstore, err = NewTxStore(w.params, config.DB, w.keyManager) + w.txstore, err = NewTxStore(w.params, config.DB, w.keyManager, config.AdditionalFilters...) if err != nil { return nil, err }