From ba2804adbd43c369515f89d11070c8197c76952a Mon Sep 17 00:00:00 2001 From: qianbin Date: Sun, 19 Nov 2023 00:11:02 +0800 Subject: [PATCH] temp --- chain/chain.go | 13 ++++++++----- chain/persist.go | 11 ++++++----- chain/repository.go | 8 ++++---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/chain/chain.go b/chain/chain.go index 78853df90..2519a5768 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -37,6 +37,8 @@ type TxMeta struct { // The block id this tx is involved. BlockID thor.Bytes32 + Conflicts uint32 + // Index the position of the tx in block's txs. Index uint64 // rlp require uint64. @@ -139,9 +141,10 @@ func (c *Chain) GetTransactionMeta(id thor.Bytes32) (*TxMeta, error) { return nil, err } return &TxMeta{ - BlockID: bs.Header.ID(), - Index: sMeta.Index, - Reverted: sMeta.Reverted, + BlockID: bs.Header.ID(), + Conflicts: bs.Conflicts, + Index: sMeta.Index, + Reverted: sMeta.Reverted, }, nil } if err := iter.Error(); err != nil { @@ -214,7 +217,7 @@ func (c *Chain) GetTransaction(id thor.Bytes32) (*tx.Transaction, *TxMeta, error return nil, nil, err } - key := makeTxKey(txMeta.BlockID, txInfix) + key := makeTxKey(block.Number(txMeta.BlockID), txMeta.Conflicts, txInfix) key.SetIndex(txMeta.Index) tx, err := c.repo.getTransaction(key) if err != nil { @@ -230,7 +233,7 @@ func (c *Chain) GetTransactionReceipt(txID thor.Bytes32) (*tx.Receipt, error) { return nil, err } - key := makeTxKey(txMeta.BlockID, receiptInfix) + key := makeTxKey(block.Number(txMeta.BlockID), txMeta.Conflicts, receiptInfix) key.SetIndex(txMeta.Index) receipt, err := c.repo.getReceipt(key) if err != nil { diff --git a/chain/persist.go b/chain/persist.go index 5a426c58d..eaf8a790f 100644 --- a/chain/persist.go +++ b/chain/persist.go @@ -31,16 +31,17 @@ type BlockSummary struct { // the key for tx/receipt. // it consists of: ( block id | infix | index ) -type txKey [32 + 1 + 8]byte +type txKey [8 + 1 + 8]byte -func makeTxKey(blockID thor.Bytes32, infix byte) (k txKey) { - copy(k[:], blockID[:]) - k[32] = infix +func makeTxKey(blockNum uint32, conflicts uint32, infix byte) (k txKey) { + binary.BigEndian.PutUint32(k[:], blockNum) + binary.BigEndian.PutUint32(k[4:], conflicts) + k[8] = infix return } func (k *txKey) SetIndex(i uint64) { - binary.BigEndian.PutUint64(k[33:], i) + binary.BigEndian.PutUint64(k[8+1:], i) } func saveRLP(w kv.Putter, key []byte, val interface{}) error { diff --git a/chain/repository.go b/chain/repository.go index 16dfe6b45..ff6534362 100644 --- a/chain/repository.go +++ b/chain/repository.go @@ -215,7 +215,7 @@ func (r *Repository) saveBlock(block *block.Block, receipts tx.Receipts, conflic } // save tx & receipt data - key := makeTxKey(id, txInfix) + key := makeTxKey(header.Number(), conflicts, txInfix) for i, tx := range txs { key.SetIndex(uint64(i)) if err := saveTransaction(dataPutter, key, tx); err != nil { @@ -223,7 +223,7 @@ func (r *Repository) saveBlock(block *block.Block, receipts tx.Receipts, conflic } r.caches.txs.Add(key, tx) } - key = makeTxKey(id, receiptInfix) + key = makeTxKey(header.Number(), conflicts, receiptInfix) for i, receipt := range receipts { key.SetIndex(uint64(i)) if err := saveReceipt(dataPutter, key, receipt); err != nil { @@ -351,7 +351,7 @@ func (r *Repository) GetBlockTransactions(id thor.Bytes32) (tx.Transactions, err if n := len(summary.Txs); n > 0 { txs := make(tx.Transactions, n) - key := makeTxKey(id, txInfix) + key := makeTxKey(block.Number(id), summary.Conflicts, txInfix) for i := range summary.Txs { key.SetIndex(uint64(i)) txs[i], err = r.getTransaction(key) @@ -396,7 +396,7 @@ func (r *Repository) GetBlockReceipts(id thor.Bytes32) (tx.Receipts, error) { if n := len(summary.Txs); n > 0 { receipts := make(tx.Receipts, n) - key := makeTxKey(id, receiptInfix) + key := makeTxKey(block.Number(id), summary.Conflicts, receiptInfix) for i := range summary.Txs { key.SetIndex(uint64(i)) receipts[i], err = r.getReceipt(key)