0f7b7099d8
Handle conflicts (#244) * Handle conflicts * Update go mod file versions * Make lint changes Disassociate block number from the indexer object Update ipld-eth-db ref Refactor builder code to make it reusable Use prefix comparison for account selective statediffing Update builder unit tests Add mode to write to CSV files in statediff file writer (#249) * Change file writing mode to csv files * Implement writer interface for file indexer * Implement option for csv or sql in file mode * Close files in CSV writer * Add tests for CSV file mode * Implement CSV file for watched addresses * Separate test configs for CSV and SQL * Refactor common code for file indexer tests Update indexer to include block hash in receipts and logs (#256) * Update indexer to include block hash in receipts and logs * Upgrade ipld-eth-db image in docker-compose to run tests Use watched addresses from direct indexing params by default while serving statediff APIs (#262) * Use watched addresses from direct indexing params in statediff APIs by default * Avoid using indexer object when direct indexing is off * Add nil check before accessing watched addresses from direct indexing params
206 lines
5.2 KiB
Go
206 lines
5.2 KiB
Go
// VulcanizeDB
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package ipld
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
node "github.com/ipfs/go-ipld-format"
|
|
mh "github.com/multiformats/go-multihash"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
)
|
|
|
|
type EthReceipt struct {
|
|
*types.Receipt
|
|
|
|
rawdata []byte
|
|
cid cid.Cid
|
|
}
|
|
|
|
// Static (compile time) check that EthReceipt satisfies the node.Node interface.
|
|
var _ node.Node = (*EthReceipt)(nil)
|
|
|
|
/*
|
|
INPUT
|
|
*/
|
|
|
|
// NewReceipt converts a types.ReceiptForStorage to an EthReceipt IPLD node
|
|
func NewReceipt(receipt *types.Receipt) (*EthReceipt, error) {
|
|
rctRaw, err := receipt.MarshalBinary()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c, err := RawdataToCid(MEthTxReceipt, rctRaw, mh.KECCAK_256)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &EthReceipt{
|
|
Receipt: receipt,
|
|
cid: c,
|
|
rawdata: rctRaw,
|
|
}, nil
|
|
}
|
|
|
|
/*
|
|
OUTPUT
|
|
*/
|
|
|
|
// DecodeEthReceipt takes a cid and its raw binary data
|
|
// from IPFS and returns an EthTx object for further processing.
|
|
func DecodeEthReceipt(c cid.Cid, b []byte) (*EthReceipt, error) {
|
|
r := new(types.Receipt)
|
|
if err := r.UnmarshalBinary(b); err != nil {
|
|
return nil, err
|
|
}
|
|
return &EthReceipt{
|
|
Receipt: r,
|
|
cid: c,
|
|
rawdata: b,
|
|
}, nil
|
|
}
|
|
|
|
/*
|
|
Block INTERFACE
|
|
*/
|
|
|
|
// RawData returns the binary of the RLP encode of the receipt.
|
|
func (r *EthReceipt) RawData() []byte {
|
|
return r.rawdata
|
|
}
|
|
|
|
// Cid returns the cid of the receipt.
|
|
func (r *EthReceipt) Cid() cid.Cid {
|
|
return r.cid
|
|
}
|
|
|
|
// String is a helper for output
|
|
func (r *EthReceipt) String() string {
|
|
return fmt.Sprintf("<EthereumReceipt %s>", r.cid)
|
|
}
|
|
|
|
// Loggable returns in a map the type of IPLD Link.
|
|
func (r *EthReceipt) Loggable() map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"type": "eth-receipt",
|
|
}
|
|
}
|
|
|
|
// Resolve resolves a path through this node, stopping at any link boundary
|
|
// and returning the object found as well as the remaining path to traverse
|
|
func (r *EthReceipt) Resolve(p []string) (interface{}, []string, error) {
|
|
if len(p) == 0 {
|
|
return r, nil, nil
|
|
}
|
|
|
|
first, rest := p[0], p[1:]
|
|
if first != "logs" && len(p) != 1 {
|
|
return nil, nil, fmt.Errorf("unexpected path elements past %s", first)
|
|
}
|
|
|
|
switch first {
|
|
case "logs":
|
|
return &node.Link{Cid: commonHashToCid(MEthLog, r.LogRoot)}, rest, nil
|
|
case "root":
|
|
return r.PostState, nil, nil
|
|
case "status":
|
|
return r.Status, nil, nil
|
|
case "cumulativeGasUsed":
|
|
return r.CumulativeGasUsed, nil, nil
|
|
case "logsBloom":
|
|
return r.Bloom, nil, nil
|
|
case "transactionHash":
|
|
return r.TxHash, nil, nil
|
|
case "contractAddress":
|
|
return r.ContractAddress, nil, nil
|
|
case "gasUsed":
|
|
return r.GasUsed, nil, nil
|
|
case "type":
|
|
return r.Type, nil, nil
|
|
default:
|
|
return nil, nil, ErrInvalidLink
|
|
}
|
|
}
|
|
|
|
// Tree lists all paths within the object under 'path', and up to the given depth.
|
|
// To list the entire object (similar to `find .`) pass "" and -1
|
|
func (r *EthReceipt) Tree(p string, depth int) []string {
|
|
if p != "" || depth == 0 {
|
|
return nil
|
|
}
|
|
return []string{"type", "root", "status", "cumulativeGasUsed", "logsBloom", "logs", "transactionHash", "contractAddress", "gasUsed"}
|
|
}
|
|
|
|
// ResolveLink is a helper function that calls resolve and asserts the
|
|
// output is a link
|
|
func (r *EthReceipt) ResolveLink(p []string) (*node.Link, []string, error) {
|
|
obj, rest, err := r.Resolve(p)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
if lnk, ok := obj.(*node.Link); ok {
|
|
return lnk, rest, nil
|
|
}
|
|
|
|
return nil, nil, fmt.Errorf("resolved item was not a link")
|
|
}
|
|
|
|
// Copy will go away. It is here to comply with the Node interface.
|
|
func (r *EthReceipt) Copy() node.Node {
|
|
panic("implement me")
|
|
}
|
|
|
|
// Links is a helper function that returns all links within this object
|
|
func (r *EthReceipt) Links() []*node.Link {
|
|
return []*node.Link{
|
|
{Cid: commonHashToCid(MEthLog, r.LogRoot)},
|
|
}
|
|
}
|
|
|
|
// Stat will go away. It is here to comply with the interface.
|
|
func (r *EthReceipt) Stat() (*node.NodeStat, error) {
|
|
return &node.NodeStat{}, nil
|
|
}
|
|
|
|
// Size will go away. It is here to comply with the interface.
|
|
func (r *EthReceipt) Size() (uint64, error) {
|
|
return strconv.ParseUint(r.Receipt.Size().String(), 10, 64)
|
|
}
|
|
|
|
/*
|
|
EthReceipt functions
|
|
*/
|
|
|
|
// MarshalJSON processes the receipt into readable JSON format.
|
|
func (r *EthReceipt) MarshalJSON() ([]byte, error) {
|
|
out := map[string]interface{}{
|
|
"root": r.PostState,
|
|
"status": r.Status,
|
|
"cumulativeGasUsed": r.CumulativeGasUsed,
|
|
"logsBloom": r.Bloom,
|
|
"logs": r.Logs,
|
|
"transactionHash": r.TxHash,
|
|
"contractAddress": r.ContractAddress,
|
|
"gasUsed": r.GasUsed,
|
|
}
|
|
return json.Marshal(out)
|
|
}
|