2019-04-15 16:28:55 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth
|
2019-04-15 16:28:55 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-04-15 16:28:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-03-11 18:41:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2019-08-09 16:22:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-04-15 16:28:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2020-02-19 04:42:53 +00:00
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
"github.com/vulcanize/ipfs-chain-watcher/pkg/shared"
|
2019-04-15 16:28:55 +00:00
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// PayloadConverter satisfies the PayloadConverter interface for ethereum
|
|
|
|
type PayloadConverter struct {
|
2019-08-09 16:22:29 +00:00
|
|
|
chainConfig *params.ChainConfig
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 20:37:20 +00:00
|
|
|
// NewPayloadConverter creates a pointer to a new PayloadConverter which satisfies the PayloadConverter interface
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewPayloadConverter(chainConfig *params.ChainConfig) *PayloadConverter {
|
|
|
|
return &PayloadConverter{
|
2019-08-09 16:22:29 +00:00
|
|
|
chainConfig: chainConfig,
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:37:20 +00:00
|
|
|
// Convert method is used to convert a eth statediff.Payload to an IPLDPayload
|
|
|
|
// Satisfies the shared.PayloadConverter interface
|
2020-02-20 22:12:52 +00:00
|
|
|
func (pc *PayloadConverter) Convert(payload shared.RawChainData) (shared.ConvertedData, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
stateDiffPayload, ok := payload.(statediff.Payload)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("eth converter: expected payload type %T got %T", statediff.Payload{}, payload)
|
|
|
|
}
|
2019-04-15 16:28:55 +00:00
|
|
|
// Unpack block rlp to access fields
|
|
|
|
block := new(types.Block)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err := rlp.DecodeBytes(stateDiffPayload.BlockRlp, block); err != nil {
|
2020-01-16 23:21:30 +00:00
|
|
|
return nil, err
|
2019-08-05 17:56:15 +00:00
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
trxLen := len(block.Transactions())
|
2020-02-20 22:12:52 +00:00
|
|
|
convertedPayload := ConvertedPayload{
|
2020-01-17 23:16:01 +00:00
|
|
|
TotalDifficulty: stateDiffPayload.TotalDifficulty,
|
|
|
|
Block: block,
|
2020-01-29 20:37:20 +00:00
|
|
|
TxMetaData: make([]TxModel, 0, trxLen),
|
2019-04-30 17:48:31 +00:00
|
|
|
Receipts: make(types.Receipts, 0, trxLen),
|
2020-01-17 23:16:01 +00:00
|
|
|
ReceiptMetaData: make([]ReceiptModel, 0, trxLen),
|
|
|
|
StateNodes: make([]TrieNode, 0),
|
2020-05-19 20:09:30 +00:00
|
|
|
StorageNodes: make(map[string][]TrieNode),
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
2019-08-09 16:22:29 +00:00
|
|
|
signer := types.MakeSigner(pc.chainConfig, block.Number())
|
2019-08-14 18:49:30 +00:00
|
|
|
transactions := block.Transactions()
|
2020-01-26 19:55:26 +00:00
|
|
|
for i, trx := range transactions {
|
2019-04-30 17:48:31 +00:00
|
|
|
// Extract to and from data from the the transactions for indexing
|
2020-01-16 23:21:30 +00:00
|
|
|
from, err := types.Sender(signer, trx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
txMeta := TxModel{
|
2020-04-02 03:34:06 +00:00
|
|
|
Dst: shared.HandleNullAddrPointer(trx.To()),
|
|
|
|
Src: shared.HandleNullAddr(from),
|
2020-01-21 19:12:35 +00:00
|
|
|
TxHash: trx.Hash().String(),
|
2020-01-26 19:55:26 +00:00
|
|
|
Index: int64(i),
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
// txMeta will have same index as its corresponding trx in the convertedPayload.BlockBody
|
2020-01-29 20:37:20 +00:00
|
|
|
convertedPayload.TxMetaData = append(convertedPayload.TxMetaData, txMeta)
|
2019-08-05 15:47:03 +00:00
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
|
2019-08-05 15:47:03 +00:00
|
|
|
// Decode receipts for this block
|
|
|
|
receipts := make(types.Receipts, 0)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err := rlp.DecodeBytes(stateDiffPayload.ReceiptsRlp, &receipts); err != nil {
|
2020-01-16 23:21:30 +00:00
|
|
|
return nil, err
|
2019-08-05 15:47:03 +00:00
|
|
|
}
|
2019-08-14 18:49:30 +00:00
|
|
|
// Derive any missing fields
|
2020-01-16 23:21:30 +00:00
|
|
|
if err := receipts.DeriveFields(pc.chainConfig, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
|
|
|
|
return nil, err
|
2019-08-14 18:49:30 +00:00
|
|
|
}
|
2020-04-02 03:34:06 +00:00
|
|
|
for _, receipt := range receipts {
|
2020-01-21 19:12:35 +00:00
|
|
|
// Extract topic and contract data from the receipt for indexing
|
|
|
|
topicSets := make([][]string, 4)
|
2020-04-02 03:34:06 +00:00
|
|
|
mappedContracts := make(map[string]bool) // use map to avoid duplicate addresses
|
2019-08-05 15:47:03 +00:00
|
|
|
for _, log := range receipt.Logs {
|
2020-04-02 03:34:06 +00:00
|
|
|
for i, topic := range log.Topics {
|
|
|
|
topicSets[i] = append(topicSets[i], topic.Hex())
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
2020-04-02 03:34:06 +00:00
|
|
|
mappedContracts[log.Address.String()] = true
|
|
|
|
}
|
2020-05-19 20:09:30 +00:00
|
|
|
// These are the contracts seen in the logs
|
2020-04-02 03:34:06 +00:00
|
|
|
logContracts := make([]string, 0, len(mappedContracts))
|
|
|
|
for addr := range mappedContracts {
|
|
|
|
logContracts = append(logContracts, addr)
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
2020-05-19 20:09:30 +00:00
|
|
|
// This is the contract address if this receipt is for a contract creation tx
|
2020-04-03 20:15:06 +00:00
|
|
|
contract := shared.HandleNullAddr(receipt.ContractAddress)
|
|
|
|
var contractHash string
|
|
|
|
if contract != "" {
|
|
|
|
contractHash = crypto.Keccak256Hash(common.HexToAddress(contract).Bytes()).String()
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
rctMeta := ReceiptModel{
|
2020-04-02 03:34:06 +00:00
|
|
|
Topic0s: topicSets[0],
|
|
|
|
Topic1s: topicSets[1],
|
|
|
|
Topic2s: topicSets[2],
|
|
|
|
Topic3s: topicSets[3],
|
2020-04-04 19:56:11 +00:00
|
|
|
Contract: contract,
|
2020-04-03 20:15:06 +00:00
|
|
|
ContractHash: contractHash,
|
2020-04-02 03:34:06 +00:00
|
|
|
LogContracts: logContracts,
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
// receipt and rctMeta will have same indexes
|
2019-08-05 15:47:03 +00:00
|
|
|
convertedPayload.Receipts = append(convertedPayload.Receipts, receipt)
|
2019-04-30 17:48:31 +00:00
|
|
|
convertedPayload.ReceiptMetaData = append(convertedPayload.ReceiptMetaData, rctMeta)
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack state diff rlp to access fields
|
2020-05-19 20:09:30 +00:00
|
|
|
stateDiff := new(statediff.StateObject)
|
|
|
|
if err := rlp.DecodeBytes(stateDiffPayload.StateObjectRlp, stateDiff); err != nil {
|
2020-01-16 23:21:30 +00:00
|
|
|
return nil, err
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
2020-05-19 20:09:30 +00:00
|
|
|
for _, stateNode := range stateDiff.Nodes {
|
|
|
|
statePath := common.Bytes2Hex(stateNode.Path)
|
2020-01-17 23:16:01 +00:00
|
|
|
convertedPayload.StateNodes = append(convertedPayload.StateNodes, TrieNode{
|
2020-05-19 20:09:30 +00:00
|
|
|
Path: stateNode.Path,
|
|
|
|
Value: stateNode.NodeValue,
|
|
|
|
Type: stateNode.NodeType,
|
|
|
|
LeafKey: common.BytesToHash(stateNode.LeafKey),
|
2020-01-17 23:16:01 +00:00
|
|
|
})
|
2020-05-19 20:09:30 +00:00
|
|
|
for _, storageNode := range stateNode.StorageNodes {
|
|
|
|
convertedPayload.StorageNodes[statePath] = append(convertedPayload.StorageNodes[statePath], TrieNode{
|
|
|
|
Path: storageNode.Path,
|
|
|
|
Value: storageNode.NodeValue,
|
|
|
|
Type: storageNode.NodeType,
|
|
|
|
LeafKey: common.BytesToHash(storageNode.LeafKey),
|
2019-05-17 04:08:53 +00:00
|
|
|
})
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 20:09:30 +00:00
|
|
|
|
2019-04-15 16:28:55 +00:00
|
|
|
return convertedPayload, nil
|
|
|
|
}
|