2015-07-06 17:54:22 -07:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 09:48:40 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 17:54:22 -07:00
|
|
|
//
|
2015-07-23 09:35:11 -07:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 17:54:22 -07:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 09:48:40 -07:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 17:54:22 -07:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 09:48:40 -07:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 17:54:22 -07:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 09:48:40 -07:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 17:54:22 -07:00
|
|
|
|
2014-11-18 07:58:22 -08:00
|
|
|
package types
|
2014-11-11 03:16:36 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-16 15:10:26 -07:00
|
|
|
"io"
|
2014-11-11 03:16:36 -08:00
|
|
|
"math/big"
|
|
|
|
|
2015-03-16 03:27:38 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-08-30 01:19:10 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-04-08 08:14:58 -07:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-11-11 03:16:36 -08:00
|
|
|
)
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// Receipt represents the results of a transaction.
|
2014-11-11 03:16:36 -08:00
|
|
|
type Receipt struct {
|
2015-09-29 09:36:16 -07:00
|
|
|
// Consensus fields
|
2014-11-11 03:16:36 -08:00
|
|
|
PostState []byte
|
|
|
|
CumulativeGasUsed *big.Int
|
2015-03-16 15:48:18 -07:00
|
|
|
Bloom Bloom
|
2015-09-29 09:36:16 -07:00
|
|
|
Logs vm.Logs
|
|
|
|
|
|
|
|
// Implementation fields
|
|
|
|
TxHash common.Hash
|
|
|
|
ContractAddress common.Address
|
|
|
|
GasUsed *big.Int
|
2014-11-11 03:16:36 -08:00
|
|
|
}
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// NewReceipt creates a barebone transaction receipt, copying the init fields.
|
2015-09-30 09:23:31 -07:00
|
|
|
func NewReceipt(root []byte, cumulativeGasUsed *big.Int) *Receipt {
|
|
|
|
return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
|
2014-11-18 07:58:22 -08:00
|
|
|
}
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
|
|
|
|
// into an RLP stream.
|
|
|
|
func (r *Receipt) EncodeRLP(w io.Writer) error {
|
|
|
|
return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs})
|
2014-11-11 03:16:36 -08:00
|
|
|
}
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
|
|
|
|
// from an RLP stream.
|
|
|
|
func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
|
|
|
|
var receipt struct {
|
2015-05-22 13:44:51 -07:00
|
|
|
PostState []byte
|
|
|
|
CumulativeGasUsed *big.Int
|
|
|
|
Bloom Bloom
|
2015-08-30 01:19:10 -07:00
|
|
|
Logs vm.Logs
|
2015-05-22 13:44:51 -07:00
|
|
|
}
|
2015-09-29 09:36:16 -07:00
|
|
|
if err := s.Decode(&receipt); err != nil {
|
2015-05-22 13:44:51 -07:00
|
|
|
return err
|
|
|
|
}
|
2015-09-29 09:36:16 -07:00
|
|
|
r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom, receipt.Logs
|
2015-05-22 13:44:51 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-13 02:04:25 -07:00
|
|
|
// RlpEncode implements common.RlpEncode required for SHA3 derivation.
|
2015-09-29 09:36:16 -07:00
|
|
|
func (r *Receipt) RlpEncode() []byte {
|
|
|
|
bytes, err := rlp.EncodeToBytes(r)
|
2015-03-16 15:10:26 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:36:16 -07:00
|
|
|
panic(err)
|
2015-03-16 15:10:26 -07:00
|
|
|
}
|
|
|
|
return bytes
|
2014-11-11 03:16:36 -08:00
|
|
|
}
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// String implements the Stringer interface.
|
|
|
|
func (r *Receipt) String() string {
|
|
|
|
return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs)
|
|
|
|
}
|
2014-11-11 03:16:36 -08:00
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// ReceiptForStorage is a wrapper around a Receipt that flattens and parses the
|
2015-10-13 02:04:25 -07:00
|
|
|
// entire content of a receipt, as opposed to only the consensus fields originally.
|
2015-09-29 09:36:16 -07:00
|
|
|
type ReceiptForStorage Receipt
|
|
|
|
|
|
|
|
// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
|
|
|
|
// into an RLP stream.
|
|
|
|
func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
|
|
|
|
logs := make([]*vm.LogForStorage, len(r.Logs))
|
|
|
|
for i, log := range r.Logs {
|
|
|
|
logs[i] = (*vm.LogForStorage)(log)
|
|
|
|
}
|
|
|
|
return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, logs, r.GasUsed})
|
2014-11-11 03:16:36 -08:00
|
|
|
}
|
|
|
|
|
2015-10-13 02:04:25 -07:00
|
|
|
// DecodeRLP implements rlp.Decoder, and loads both consensus and implementation
|
|
|
|
// fields of a receipt from an RLP stream.
|
2015-09-29 09:36:16 -07:00
|
|
|
func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
|
|
|
|
var receipt struct {
|
|
|
|
PostState []byte
|
|
|
|
CumulativeGasUsed *big.Int
|
|
|
|
Bloom Bloom
|
|
|
|
TxHash common.Hash
|
|
|
|
ContractAddress common.Address
|
|
|
|
Logs []*vm.LogForStorage
|
|
|
|
GasUsed *big.Int
|
|
|
|
}
|
|
|
|
if err := s.Decode(&receipt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Assign the consensus fields
|
|
|
|
r.PostState, r.CumulativeGasUsed, r.Bloom = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom
|
|
|
|
r.Logs = make(vm.Logs, len(receipt.Logs))
|
|
|
|
for i, log := range receipt.Logs {
|
|
|
|
r.Logs[i] = (*vm.Log)(log)
|
|
|
|
}
|
|
|
|
// Assign the implementation fields
|
|
|
|
r.TxHash, r.ContractAddress, r.GasUsed = receipt.TxHash, receipt.ContractAddress, receipt.GasUsed
|
|
|
|
|
|
|
|
return nil
|
2014-11-11 03:16:36 -08:00
|
|
|
}
|
|
|
|
|
2015-09-29 09:36:16 -07:00
|
|
|
// Receipts is a wrapper around a Receipt array to implement types.DerivableList.
|
2014-11-11 03:16:36 -08:00
|
|
|
type Receipts []*Receipt
|
|
|
|
|
2015-12-10 16:29:41 -08:00
|
|
|
// Len returns the number of receipts in this list.
|
|
|
|
func (r Receipts) Len() int { return len(r) }
|
|
|
|
|
|
|
|
// GetRlp returns the RLP encoding of one receipt from the list.
|
|
|
|
func (r Receipts) GetRlp(i int) []byte {
|
|
|
|
bytes, err := rlp.EncodeToBytes(r[i])
|
2015-03-16 15:10:26 -07:00
|
|
|
if err != nil {
|
2015-09-29 09:36:16 -07:00
|
|
|
panic(err)
|
2015-03-16 15:10:26 -07:00
|
|
|
}
|
|
|
|
return bytes
|
2014-12-05 03:32:47 -08:00
|
|
|
}
|