cpacia / BitcoinCash-Wallet

Bitcoin Cash P2P SPV Wallet
MIT License
73 stars 27 forks source link

TxnsDB / wallet.Txns assignment errors #13

Open chinitadelrey opened 6 years ago

chinitadelrey commented 6 years ago

Error while compiling. I can't figure out what's wrong. 1) txns.Get returns three values, but why does TxnDB want to force it into 2 only, when the other files clearly use 3 variables?! 2) same for the Put function. defined with 5 arguments and then the error says there are not enough... when it looks alright.

I'm new to Go. Help me understand what's wrong here?

error message:

make install
cd cmd/bitcoincash && go install
# github.com/cpacia/BitcoinCash-Wallet/db
../../db/database.go:45:7: cannot use TxnsDB literal (type *TxnsDB) as type wallet.Txns in field value:
    *TxnsDB does not implement wallet.Txns (wrong type for Get method)
        have Get(chainhash.Hash) (*wire.MsgTx, wallet.Txn, error)
        want Get(chainhash.Hash) (wallet.Txn, error)
# github.com/cpacia/BitcoinCash-Wallet
../../eight333.go:190:15: assignment mismatch: 3 variables but 2 values
../../sortsignsend.go:124:14: assignment mismatch: 3 variables but 2 values
../../txstore.go:344:15: assignment mismatch: 3 variables but 2 values
../../txstore.go:350:17: not enough arguments in call to ts.Datastore.Txns().Put
../../wallet.go:348:14: assignment mismatch: 3 variables but 2 values
../../wallet.go:353:14: assignment mismatch: 3 variables but 2 values
make: *** [install] Error 2

Here's the relevant code. :


db/database.go:45:7 == > txns: &TxnsDB{db:conn,lock: l,}
eight333.go:190:15 ==> tx, _, err := w.txstore.Txns().Get(thing.Hash)
sortsignsend.go:124:14 ==> _, txn, err := w.txstore.Txns().Get(txid)
txstore.go:344:15 ==> _, txn, err := ts.Txns().Get(tx.TxHash())
wallet.go:348:14 ==> _, txn, err := w.txstore.Txns().Get(txid)
wallet.go:353:14 ==> _, txn, err := w.txstore.Txns().Get(txid)```

txstore.go:350:17 ==> ts.Txns().Put(tx, int(value), int(height), txn.Timestamp, hits == 0)
db/txns.go:18 ==> `func (t *TxnsDB) Put(txn *wire.MsgTx, value, height int, timestamp time.Time, watchOnly bool) error {`

db/txns.go:46-79 ==>
```func (t *TxnsDB) Get(txid chainhash.Hash) (*wire.MsgTx, wallet.Txn, error) {
    t.lock.RLock()
    defer t.lock.RUnlock()
    var txn wallet.Txn
    stmt, err := t.db.Prepare("select tx, value, height, timestamp, watchOnly from txns where txid=?")
    if err != nil {
        return nil, txn, err
    }
    defer stmt.Close()
    var ret []byte
    var value int
    var height int
    var timestamp int
    var watchOnlyInt int
    err = stmt.QueryRow(txid.String()).Scan(&ret, &value, &height, &timestamp, &watchOnlyInt)
    if err != nil {
        return nil, txn, err
    }
    r := bytes.NewReader(ret)
    msgTx := wire.NewMsgTx(1)
    msgTx.BtcDecode(r, 1, wire.BaseEncoding)
    watchOnly := false
    if watchOnlyInt > 0 {
        watchOnly = true
    }
    txn = wallet.Txn{
        Txid:      msgTx.TxHash().String(),
        Value:     int64(value),
        Height:    int32(height),
        Timestamp: time.Unix(int64(timestamp), 0),
        WatchOnly: watchOnly,
    }
    return msgTx, txn, nil
}```

db/txns.go:13-16 ==>
```type TxnsDB struct {
    db   *sql.DB
    lock *sync.RWMutex}```

db/txns.go:69-71 ==>
```func (db *SQLiteDatastore) Txns() wallet.Txns {
    return db.txns }```
chinitadelrey commented 6 years ago

I did a little more digging and think the problem could be in *MsgTx:

btcsuite/btcd/wire/msgtx.go:892:898 ==>

func NewMsgTx(version int32) *MsgTx {
    return &MsgTx{
        Version: version,
        TxIn:    make([]*TxIn, 0, defaultTxInOutAlloc),
        TxOut:   make([]*TxOut, 0, defaultTxInOutAlloc),
    }}

btcsuite/btcd/wire/msgtx.go:289:294 ==>

type MsgTx struct {
    Version  int32
    TxIn     []*TxIn
    TxOut    []*TxOut
    LockTime uint32 // not referenced in NewMsgTx above 
}

Does that look right? I think it just implies that LockTime is allocated to zero under NewMsgTx?

I've posted it up in the original dependency as well -- https://github.com/btcsuite/btcd/issues/1173