look up tx in mempool first for getrawtransaction

Closes #26
This commit is contained in:
Owain G. Ainsworth 2013-11-12 19:03:23 +00:00
parent bb276b53aa
commit 31a97d5c09

View file

@ -681,6 +681,10 @@ func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification c
// TODO: check error code. tx is not checked before // TODO: check error code. tx is not checked before
// this point. // this point.
txSha, _ := btcwire.NewShaHashFromStr(c.Txid) txSha, _ := btcwire.NewShaHashFromStr(c.Txid)
var mtx *btcwire.MsgTx
var blksha *btcwire.ShaHash
tx, err := s.server.txMemPool.FetchTransaction(txSha)
if err != nil {
txList, err := s.server.db.FetchTxBySha(txSha) txList, err := s.server.db.FetchTxBySha(txSha)
if err != nil || len(txList) == 0 { if err != nil || len(txList) == 0 {
log.Errorf("RPCS: Error fetching tx: %v", err) log.Errorf("RPCS: Error fetching tx: %v", err)
@ -688,19 +692,17 @@ func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification c
} }
lastTx := len(txList) - 1 lastTx := len(txList) - 1
txS := txList[lastTx].Tx mtx = txList[lastTx].Tx
blksha := txList[lastTx].BlkSha
blk, err := s.server.db.FetchBlockBySha(blksha)
if err != nil {
log.Errorf("RPCS: Error fetching sha: %v", err)
return nil, btcjson.ErrBlockNotFound
}
idx := blk.Height()
txOutList := txS.TxOut blksha = txList[lastTx].BlkSha
} else {
mtx = tx.MsgTx()
}
txOutList := mtx.TxOut
voutList := make([]btcjson.Vout, len(txOutList)) voutList := make([]btcjson.Vout, len(txOutList))
txInList := txS.TxIn txInList := mtx.TxIn
vinList := make([]btcjson.Vin, len(txInList)) vinList := make([]btcjson.Vin, len(txInList))
for i, v := range txInList { for i, v := range txInList {
@ -730,26 +732,34 @@ func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification c
} }
} }
txReply := btcjson.TxRawResult{
Txid: c.Txid,
Vout: voutList,
Vin: vinList,
Version: mtx.Version,
LockTime: mtx.LockTime,
}
if blksha != nil {
blk, err := s.server.db.FetchBlockBySha(blksha)
if err != nil {
log.Errorf("RPCS: Error fetching sha: %v", err)
return nil, btcjson.ErrBlockNotFound
}
idx := blk.Height()
_, maxidx, err := s.server.db.NewestSha() _, maxidx, err := s.server.db.NewestSha()
if err != nil { if err != nil {
log.Errorf("RPCS: Cannot get newest sha: %v", err) log.Errorf("RPCS: Cannot get newest sha: %v", err)
return nil, btcjson.ErrNoNewestBlockInfo return nil, btcjson.ErrNoNewestBlockInfo
} }
confirmations := uint64(1 + maxidx - idx)
blockHeader := &blk.MsgBlock().Header blockHeader := &blk.MsgBlock().Header
txReply := btcjson.TxRawResult{
Txid: c.Txid,
Vout: voutList,
Vin: vinList,
Version: txS.Version,
LockTime: txS.LockTime,
// This is not a typo, they are identical in // This is not a typo, they are identical in
// bitcoind as well. // bitcoind as well.
Time: blockHeader.Timestamp.Unix(), txReply.Time = blockHeader.Timestamp.Unix()
Blocktime: blockHeader.Timestamp.Unix(), txReply.Blocktime = blockHeader.Timestamp.Unix()
BlockHash: blksha.String(), txReply.BlockHash = blksha.String()
Confirmations: confirmations, txReply.Confirmations = uint64(1 + maxidx - idx)
} }
return txReply, nil return txReply, nil
} else { } else {