Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Nov 18, 2023
1 parent 2ec082a commit ba2804a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
13 changes: 8 additions & 5 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions chain/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions chain/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ 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 {
return nil, err
}
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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ba2804a

Please sign in to comment.