core, eth, miner, xeth: receipt storage fix
* Added GetReceiptsFromBlock, GetReceipt, PutReceipts * Added ContractAddress to receipt. See #1042
This commit is contained in:
@ -8,6 +8,9 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
var receiptsPre = []byte("receipts-")
|
||||
|
||||
// PutTransactions stores the transactions in the given database
|
||||
func PutTransactions(db common.Database, block *types.Block, txs types.Transactions) {
|
||||
for i, tx := range block.Transactions() {
|
||||
rlpEnc, err := rlp.EncodeToBytes(tx)
|
||||
@ -34,18 +37,49 @@ func PutTransactions(db common.Database, block *types.Block, txs types.Transacti
|
||||
}
|
||||
}
|
||||
|
||||
func PutReceipts(db common.Database, hash common.Hash, receipts types.Receipts) error {
|
||||
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
|
||||
for i, receipt := range receipts {
|
||||
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
|
||||
// PutReceipts stores the receipts in the current database
|
||||
func PutReceipts(db common.Database, receipts types.Receipts) error {
|
||||
for _, receipt := range receipts {
|
||||
storageReceipt := (*types.ReceiptForStorage)(receipt)
|
||||
bytes, err := rlp.EncodeToBytes(storageReceipt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
bytes, err := rlp.EncodeToBytes(storageReceipts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db.Put(append(receiptsPre, hash[:]...), bytes)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReceipt returns a receipt by hash
|
||||
func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt {
|
||||
data, _ := db.Get(append(receiptsPre, txHash[:]...))
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var receipt types.Receipt
|
||||
err := rlp.DecodeBytes(data, &receipt)
|
||||
if err != nil {
|
||||
glog.V(logger.Error).Infoln("GetReceipt err:", err)
|
||||
}
|
||||
return &receipt
|
||||
}
|
||||
|
||||
// GetReceiptFromBlock returns all receipts with the given block
|
||||
func GetReceiptsFromBlock(db common.Database, block *types.Block) types.Receipts {
|
||||
// at some point we want:
|
||||
//receipts := make(types.Receipts, len(block.Transactions()))
|
||||
// but since we need to support legacy, we can't (yet)
|
||||
var receipts types.Receipts
|
||||
for _, tx := range block.Transactions() {
|
||||
if receipt := GetReceipt(db, tx.Hash()); receipt != nil {
|
||||
receipts = append(receipts, receipt)
|
||||
}
|
||||
}
|
||||
|
||||
return receipts
|
||||
}
|
||||
|
Reference in New Issue
Block a user