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

Commit 67a8bfb

Browse files
committed
2 parents a9122dc + eb70609 commit 67a8bfb

File tree

10 files changed

+159
-31
lines changed

10 files changed

+159
-31
lines changed

Godeps/Godeps.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bitcoin/bitcoind/wallet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (w *BitcoindWallet) Transactions() ([]spvwallet.Txn, error) {
182182
t := spvwallet.Txn{
183183
Txid: r.TxID,
184184
Value: int64(amt.ToUnit(btc.AmountSatoshi)),
185-
Height: uint32(*r.BlockIndex),
185+
Height: int32(*r.BlockIndex),
186186
}
187187
ret = append(ret, t)
188188
}

core/listings.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"crypto/sha256"
18+
1819
"github.com/OpenBazaar/jsonpb"
1920
"github.com/OpenBazaar/openbazaar-go/ipfs"
2021
"github.com/OpenBazaar/openbazaar-go/pb"
@@ -32,8 +33,10 @@ const (
3233
MaxCategories = 10
3334
MaxListItems = 30
3435
FilenameMaxCharacters = 255
36+
CodeMaxCharacters = 20
3537
WordMaxCharacters = 40
3638
SentenceMaxCharacters = 70
39+
CouponTitleMaxCharacters = 70
3740
PolicyMaxCharacters = 10000
3841
MaxCountryCodes = 255
3942
)
@@ -886,9 +889,12 @@ func validateListing(listing *pb.Listing) (err error) {
886889
return fmt.Errorf("Number of coupons is greater than the max of %d", MaxListItems)
887890
}
888891
for _, coupon := range listing.Coupons {
889-
if len(coupon.Title) > SentenceMaxCharacters {
892+
if len(coupon.Title) > CouponTitleMaxCharacters {
890893
return fmt.Errorf("Coupon title length must be less than the max of %d", SentenceMaxCharacters)
891894
}
895+
if len(coupon.GetDiscountCode()) > CodeMaxCharacters {
896+
return fmt.Errorf("Coupon code length must be less than the max of %d", CodeMaxCharacters)
897+
}
892898
if coupon.GetPercentDiscount() > 100 {
893899
return errors.New("Percent discount cannot be over 100 percent")
894900
}

net/service/handlers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ func (service *OpenBazaarService) handleChat(p peer.ID, pmes *pb.Message, option
925925
if !offline {
926926
t = time.Now()
927927
} else {
928-
t = time.Unix(chat.Timestamp.Seconds, int64(chat.Timestamp.Nanos))
928+
if chat.Timestamp == nil {
929+
return nil, errors.New("Invalid timestamp")
930+
}
931+
t = time.Unix(chat.Timestamp.Seconds, 0)
929932
}
930933

931934
// Put to database

repo/db/db.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func initDatabaseTables(db *sql.DB, password string) error {
262262
create table keys (scriptPubKey text primary key not null, purpose integer, keyIndex integer, used integer, key text);
263263
create table utxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, freeze int);
264264
create table stxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, spendHeight integer, spendTxid text);
265-
create table txns (txid text primary key not null, value integer, height integer, tx blob);
265+
create table txns (txid text primary key not null, value integer, height integer, timestamp integer, tx blob);
266266
create table inventory (slug text primary key not null, count integer);
267267
create table purchases (orderID text primary key not null, contract blob, state integer, read integer, date integer, total integer, thumbnail text, vendorID text, vendorBlockchainID text, title text, shippingName text, shippingAddress text, paymentAddr text, funded integer, transactions blob);
268268
create index index_purchases on purchases (paymentAddr);
@@ -316,7 +316,6 @@ func (c *ConfigDB) Init(mnemonic string, identityKey []byte, password string) er
316316
}
317317
tx.Commit()
318318
return nil
319-
320319
}
321320

322321
func (c *ConfigDB) GetMnemonic() (string, error) {

repo/db/txns.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,30 @@ import (
77
"github.com/btcsuite/btcd/chaincfg/chainhash"
88
"github.com/btcsuite/btcd/wire"
99
"sync"
10+
"time"
1011
)
1112

1213
type TxnsDB struct {
1314
db *sql.DB
1415
lock sync.RWMutex
1516
}
1617

17-
func (t *TxnsDB) Put(txn *wire.MsgTx, value, height int) error {
18+
func (t *TxnsDB) Put(txn *wire.MsgTx, value, height int, timestamp time.Time) error {
1819
t.lock.Lock()
1920
defer t.lock.Unlock()
2021
tx, err := t.db.Begin()
2122
if err != nil {
2223
return err
2324
}
24-
stmt, err := tx.Prepare("insert or replace into txns(txid, value, height, tx) values(?,?,?,?)")
25+
stmt, err := tx.Prepare("insert or replace into txns(txid, value, height, timestamp, tx) values(?,?,?,?,?)")
26+
defer stmt.Close()
2527
if err != nil {
2628
tx.Rollback()
2729
return err
2830
}
29-
defer stmt.Close()
3031
var buf bytes.Buffer
3132
txn.Serialize(&buf)
32-
_, err = stmt.Exec(txn.TxHash().String(), value, height, buf.Bytes())
33+
_, err = stmt.Exec(txn.TxHash().String(), value, height, int(timestamp.Unix()), buf.Bytes())
3334
if err != nil {
3435
tx.Rollback()
3536
return err
@@ -38,28 +39,29 @@ func (t *TxnsDB) Put(txn *wire.MsgTx, value, height int) error {
3839
return nil
3940
}
4041

41-
func (t *TxnsDB) Get(txid chainhash.Hash) (*wire.MsgTx, uint32, error) {
42+
func (t *TxnsDB) Get(txid chainhash.Hash) (*wire.MsgTx, int32, time.Time, error) {
4243
t.lock.Lock()
4344
defer t.lock.Unlock()
44-
stmt, err := t.db.Prepare("select tx, height from txns where txid=?")
45+
stmt, err := t.db.Prepare("select tx, height, timestamp from txns where txid=?")
4546
defer stmt.Close()
4647
var ret []byte
4748
var height int
48-
err = stmt.QueryRow(txid.String()).Scan(&ret, &height)
49+
var timestamp int
50+
err = stmt.QueryRow(txid.String()).Scan(&ret, &height, &timestamp)
4951
if err != nil {
50-
return nil, uint32(0), err
52+
return nil, int32(0), time.Now(), err
5153
}
5254
r := bytes.NewReader(ret)
5355
msgTx := wire.NewMsgTx(1)
5456
msgTx.BtcDecode(r, 1)
55-
return msgTx, uint32(height), nil
57+
return msgTx, int32(height), time.Unix(int64(timestamp), 0), nil
5658
}
5759

5860
func (t *TxnsDB) GetAll() ([]spvwallet.Txn, error) {
5961
t.lock.Lock()
6062
defer t.lock.Unlock()
6163
var ret []spvwallet.Txn
62-
stm := "select tx, value, height from txns"
64+
stm := "select tx, value, height, timestamp from txns"
6365
rows, err := t.db.Query(stm)
6466
defer rows.Close()
6567
if err != nil {
@@ -69,14 +71,15 @@ func (t *TxnsDB) GetAll() ([]spvwallet.Txn, error) {
6971
var tx []byte
7072
var value int
7173
var height int
72-
if err := rows.Scan(&tx, &value, &height); err != nil {
74+
var timestamp int
75+
if err := rows.Scan(&tx, &value, &height, &timestamp); err != nil {
7376
continue
7477
}
7578
r := bytes.NewReader(tx)
7679
msgTx := wire.NewMsgTx(1)
7780
msgTx.BtcDecode(r, 1)
7881

79-
txn := spvwallet.Txn{msgTx.TxHash().String(), int64(value), uint32(height)}
82+
txn := spvwallet.Txn{msgTx.TxHash().String(), int64(value), int32(height), time.Unix(int64(timestamp), 0)}
8083
ret = append(ret, txn)
8184
}
8285
return ret, nil
@@ -91,3 +94,24 @@ func (t *TxnsDB) Delete(txid *chainhash.Hash) error {
9194
}
9295
return nil
9396
}
97+
98+
func (t *TxnsDB) MarkAsDead(txid chainhash.Hash) error {
99+
t.lock.Lock()
100+
defer t.lock.Unlock()
101+
tx, err := t.db.Begin()
102+
if err != nil {
103+
return err
104+
}
105+
stmt, err := tx.Prepare("update txns set height=-1 where txid=?")
106+
if err != nil {
107+
return err
108+
}
109+
defer stmt.Close()
110+
_, err = stmt.Exec(txid.String())
111+
if err != nil {
112+
tx.Rollback()
113+
return err
114+
}
115+
tx.Commit()
116+
return nil
117+
}

repo/db/txns_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"github.com/btcsuite/btcd/wire"
88
"testing"
9+
"time"
910
)
1011

1112
var txdb TxnsDB
@@ -25,7 +26,7 @@ func TestTxnsPut(t *testing.T) {
2526
r := bytes.NewReader(raw)
2627
tx.Deserialize(r)
2728

28-
err := txdb.Put(tx, 5, 1)
29+
err := txdb.Put(tx, 5, 1, time.Now())
2930
if err != nil {
3031
t.Error(err)
3132
}
@@ -56,11 +57,12 @@ func TestTxnsGet(t *testing.T) {
5657
r := bytes.NewReader(raw)
5758
tx.Deserialize(r)
5859

59-
err := txdb.Put(tx, 0, 1)
60+
now := time.Now()
61+
err := txdb.Put(tx, 0, 1, now)
6062
if err != nil {
6163
t.Error(err)
6264
}
63-
tx2, h, err := txdb.Get(tx.TxHash())
65+
tx2, h, ts, err := txdb.Get(tx.TxHash())
6466
if err != nil {
6567
t.Error(err)
6668
}
@@ -70,6 +72,9 @@ func TestTxnsGet(t *testing.T) {
7072
if h != 1 {
7173
t.Error("Txn db failed to get height")
7274
}
75+
if now.Unix() != ts.Unix() {
76+
t.Error("Txn db failed to return correct time")
77+
}
7378
}
7479

7580
func TestTxnsGetAll(t *testing.T) {
@@ -79,7 +84,7 @@ func TestTxnsGetAll(t *testing.T) {
7984
r := bytes.NewReader(raw)
8085
tx.Deserialize(r)
8186

82-
err := txdb.Put(tx, 1, 5)
87+
err := txdb.Put(tx, 1, 5, time.Now())
8388
if err != nil {
8489
t.Error(err)
8590
}
@@ -99,7 +104,7 @@ func TestDeleteTxns(t *testing.T) {
99104
r := bytes.NewReader(raw)
100105
tx.Deserialize(r)
101106

102-
err := txdb.Put(tx, 0, 1)
107+
err := txdb.Put(tx, 0, 1, time.Now())
103108
if err != nil {
104109
t.Error(err)
105110
}
@@ -118,3 +123,24 @@ func TestDeleteTxns(t *testing.T) {
118123
}
119124
}
120125
}
126+
127+
func TestTxnsDB_MarkAsDead(t *testing.T) {
128+
tx := wire.NewMsgTx(wire.TxVersion)
129+
txHex := "0100000001cbfe4948ebc9113244b802a96e4940fa063c0455a16ca1f39a1e1db03837d9c701000000da004830450221008994e3dba54cb0ea23ca008d0e361b4339ee7b44b5e9101f6837e6a1a89ce044022051be859c68a547feaf60ffacc43f528cf2963c088bde33424d859274505e3f450147304402206cd4ef92cc7f2862c67810479013330fcafe4d468f1370563d4dff6be5bcbedc02207688a09163e615bc82299a29e987e1d718cb99a91d46a1ab13d18c0f6e616a1601475221024760c9ba5fa6241da6ee8601f0266f0e0592f53735703f0feaae23eda6673ae821038cfa8e97caaafbe21455803043618440c28c501ec32d6ece6865003165a0d4d152aeffffffff029ae2c700000000001976a914f72f20a739ec3c3df1a1fd7eff122d13bd5ca39188acb64784240000000017a9140be09225644b4cfdbb472028d8ccaf6df736025c8700000000"
130+
raw, _ := hex.DecodeString(txHex)
131+
r := bytes.NewReader(raw)
132+
tx.Deserialize(r)
133+
134+
err := txdb.Put(tx, 0, 1, time.Now())
135+
if err != nil {
136+
t.Error(err)
137+
}
138+
err = txdb.MarkAsDead(tx.TxHash())
139+
if err != nil {
140+
t.Error(err)
141+
}
142+
_, height, _, err := txdb.Get(tx.TxHash())
143+
if height != -1 {
144+
t.Error("Txn db failed to mark as dead")
145+
}
146+
}

vendor/github.com/OpenBazaar/spvwallet/datastore.go

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/OpenBazaar/spvwallet/eight333.go

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)