Fix computation of confirmations

indexv1
Martin Boehm 2018-04-06 17:13:38 +02:00
parent 5912e941e3
commit f8eca976ab
4 changed files with 30 additions and 24 deletions

View File

@ -322,7 +322,8 @@ func (b *EthereumRPC) computeConfirmations(n uint64) (uint32, error) {
return 0, err
}
bn := bh.Number.Uint64()
return uint32(bn - n), nil
// transaction in the best block has 1 confirmation
return uint32(bn - n + 1), nil
}
// GetBlock returns block with given hash or height, hash has precedence if both passed

View File

@ -216,7 +216,7 @@ func TestEthRPC_GetBlockHeader(t *testing.T) {
want: &bchain.BlockHeader{
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
Height: 2870000,
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000),
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
},
},
{
@ -276,7 +276,7 @@ func TestEthRPC_GetBlock(t *testing.T) {
BlockHeader: bchain.BlockHeader{
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
Height: 2870000,
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000),
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
},
},
wantTxCount: 12,
@ -293,7 +293,7 @@ func TestEthRPC_GetBlock(t *testing.T) {
BlockHeader: bchain.BlockHeader{
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
Height: 2870000,
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000),
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
},
},
wantTxCount: 12,
@ -367,7 +367,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
},
want: &bchain.Tx{
Blocktime: 1521515026,
Confirmations: uint32(bh.Number.Uint64()) - 2870000,
Confirmations: uint32(bh.Number.Uint64()) - 2870000 + 1,
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
Time: 1521515026,
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
@ -395,7 +395,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
},
want: &bchain.Tx{
Blocktime: 1521533434,
Confirmations: uint32(bh.Number.Uint64()) - 2871048,
Confirmations: uint32(bh.Number.Uint64()) - 2871048 + 1,
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
Time: 1521533434,
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",

View File

@ -25,28 +25,33 @@ func NewTxCache(db *RocksDB, chain bchain.BlockChain, metrics *common.Metrics) (
// GetTransaction returns transaction either from RocksDB or if not present from blockchain
// it the transaction is confirmed, it is stored in the RocksDB
func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, error) {
func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, uint32, error) {
tx, h, err := c.db.GetTx(txid)
if err != nil {
return nil, err
return nil, 0, err
}
if tx != nil {
tx.Confirmations = bestheight - h
// number of confirmations is not stored in cache, they change all the time
tx.Confirmations = bestheight - h + 1
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "hit"}).Inc()
return tx, nil
return tx, h, nil
}
tx, err = c.chain.GetTransaction(txid)
if err != nil {
return nil, err
return nil, 0, err
}
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "miss"}).Inc()
// do not cache mempool transactions
if tx.Confirmations > 0 {
err = c.db.PutTx(tx, bestheight-tx.Confirmations, tx.Blocktime)
// the transaction in the currently best block has 1 confirmation
h = bestheight - tx.Confirmations + 1
err = c.db.PutTx(tx, h, tx.Blocktime)
// do not return caching error, only log it
if err != nil {
glog.Error("PutTx error ", err)
}
} else {
h = 0
}
return tx, nil
return tx, h, nil
}

View File

@ -389,7 +389,7 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
res.Result.Items = make([]addressHistoryItem, 0)
for i, txid := range txids {
if i >= rr.From && i < rr.To {
tx, err := s.txCache.GetTransaction(txid, bestheight)
tx, height, err := s.txCache.GetTransaction(txid, bestheight)
if err != nil {
return res, err
}
@ -422,13 +422,13 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
ahi := addressHistoryItem{}
ahi.Addresses = ads
ahi.Confirmations = int(tx.Confirmations)
var height int
var h int
if tx.Confirmations == 0 {
height = -1
h = -1
} else {
height = int(bestheight) - int(tx.Confirmations) + 1
h = int(height)
}
ahi.Tx = txToResTx(tx, height, hi, ho)
ahi.Tx = txToResTx(tx, h, hi, ho)
res.Result.Items = append(res.Result.Items, ahi)
}
}
@ -612,7 +612,7 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
if err != nil {
return
}
tx, err := s.txCache.GetTransaction(txid, bestheight)
tx, height, err := s.txCache.GetTransaction(txid, bestheight)
if err != nil {
return res, err
}
@ -625,7 +625,7 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
OutputIndex: int(vin.Vout),
}
if vin.Txid != "" {
otx, err := s.txCache.GetTransaction(vin.Txid, bestheight)
otx, _, err := s.txCache.GetTransaction(vin.Txid, bestheight)
if err != nil {
return res, err
}
@ -650,13 +650,13 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
}
ho = append(ho, ao)
}
var height int
var h int
if tx.Confirmations == 0 {
height = -1
h = -1
} else {
height = int(bestheight) - int(tx.Confirmations) + 1
h = int(height)
}
res.Result = txToResTx(tx, height, hi, ho)
res.Result = txToResTx(tx, h, hi, ho)
return
}