Skip to content

Commit

Permalink
Update txstore db
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacia committed May 11, 2018
1 parent aaa54a7 commit 54ba896
Show file tree
Hide file tree
Showing 21 changed files with 80 additions and 75 deletions.
4 changes: 2 additions & 2 deletions api/rpc.go
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
23 changes: 11 additions & 12 deletions blockchain.go
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 4 additions & 1 deletion 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"
Expand All @@ -12,7 +13,6 @@ import (
"path/filepath"
"runtime"
"time"
"github.com/OpenBazaar/openbazaar-go/bitcoin"
)

type Config struct {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion db/database.go
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion db/keys.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion db/keys_test.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion db/stxo.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion db/stxo_test.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion db/utxo.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion db/utxo_test.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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 (
Expand Down
2 changes: 1 addition & 1 deletion examples/client.go
Expand Up @@ -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"
)

Expand Down
6 changes: 3 additions & 3 deletions exchangerates/exchangerates.go
Expand Up @@ -8,10 +8,10 @@ import (
"golang.org/x/net/proxy"
"net"
"net/http"
"reflect"
"strconv"
"sync"
"time"
"reflect"
)

type ExchangeRateProvider struct {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -318,4 +318,4 @@ func (b PoloniexDecoder) decode(dat interface{}, cache map[string]float64, bp *e
cache[k] = v * rate
}
return nil
}
}
19 changes: 9 additions & 10 deletions 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 {
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion fees_test.go
Expand Up @@ -2,8 +2,8 @@ package bitcoincash

import (
"bytes"
"testing"
"github.com/OpenBazaar/wallet-interface"
"testing"
)

type ClosingBuffer struct {
Expand Down

0 comments on commit 54ba896

Please sign in to comment.