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/>.
|
|
|
|
|
|
|
|
package ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
)
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// PayloadConverter interface is used to convert a geth statediff.Payload to our IPLDPayload type
|
|
|
|
type PayloadConverter interface {
|
2019-04-15 16:28:55 +00:00
|
|
|
Convert(payload statediff.Payload) (*IPLDPayload, error)
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Converter is the underlying struct for the PayloadConverter interface
|
|
|
|
type Converter struct {
|
2019-04-15 16:28:55 +00:00
|
|
|
client core.EthClient
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// NewPayloadConverter creates a pointer to a new Converter which satisfies the PayloadConverter interface
|
|
|
|
func NewPayloadConverter(client core.EthClient) *Converter {
|
|
|
|
return &Converter{
|
2019-04-15 16:28:55 +00:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Convert method is used to convert a geth statediff.Payload to a IPLDPayload
|
2019-04-30 17:48:31 +00:00
|
|
|
func (pc *Converter) Convert(payload statediff.Payload) (*IPLDPayload, error) {
|
2019-04-15 16:28:55 +00:00
|
|
|
// Unpack block rlp to access fields
|
|
|
|
block := new(types.Block)
|
|
|
|
err := rlp.DecodeBytes(payload.BlockRlp, block)
|
|
|
|
header := block.Header()
|
|
|
|
headerRlp, err := rlp.EncodeToBytes(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
trxLen := len(block.Transactions())
|
2019-04-15 16:28:55 +00:00
|
|
|
convertedPayload := &IPLDPayload{
|
2019-04-30 17:48:31 +00:00
|
|
|
BlockHash: block.Hash(),
|
|
|
|
BlockNumber: block.Number(),
|
|
|
|
HeaderRLP: headerRlp,
|
|
|
|
BlockBody: block.Body(),
|
|
|
|
TrxMetaData: make([]*TrxMetaData, 0, trxLen),
|
|
|
|
Receipts: make(types.Receipts, 0, trxLen),
|
|
|
|
ReceiptMetaData: make([]*ReceiptMetaData, 0, trxLen),
|
2019-05-17 04:08:53 +00:00
|
|
|
StateNodes: make(map[common.Hash]StateNode),
|
|
|
|
StorageNodes: make(map[common.Hash][]StorageNode),
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
for gethTransactionIndex, trx := range block.Transactions() {
|
|
|
|
// Extract to and from data from the the transactions for indexing
|
|
|
|
from, err := pc.client.TransactionSender(context.Background(), trx, block.Hash(), uint(gethTransactionIndex))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txMeta := &TrxMetaData{
|
2019-05-21 19:27:24 +00:00
|
|
|
Dst: handleNullAddr(trx.To()),
|
|
|
|
Src: from.Hex(),
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
// txMeta will have same index as its corresponding trx in the convertedPayload.BlockBody
|
|
|
|
convertedPayload.TrxMetaData = append(convertedPayload.TrxMetaData, txMeta)
|
|
|
|
|
|
|
|
// Retrieve receipt for this transaction
|
2019-04-15 16:28:55 +00:00
|
|
|
gethReceipt, err := pc.client.TransactionReceipt(context.Background(), trx.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
// Extract topic0 data from the receipt's logs for indexing
|
|
|
|
rctMeta := &ReceiptMetaData{
|
2019-06-20 15:59:10 +00:00
|
|
|
Topic0s: make([]string, 0, len(gethReceipt.Logs)),
|
|
|
|
ContractAddress: gethReceipt.ContractAddress.Hex(),
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
for _, log := range gethReceipt.Logs {
|
|
|
|
if len(log.Topics[0]) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rctMeta.Topic0s = append(rctMeta.Topic0s, log.Topics[0].Hex())
|
|
|
|
}
|
|
|
|
// receipt and rctMeta will have same indexes
|
2019-04-15 16:28:55 +00:00
|
|
|
convertedPayload.Receipts = append(convertedPayload.Receipts, gethReceipt)
|
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
|
|
|
|
stateDiff := new(statediff.StateDiff)
|
|
|
|
err = rlp.DecodeBytes(payload.StateDiffRlp, stateDiff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, createdAccount := range stateDiff.CreatedAccounts {
|
|
|
|
hashKey := common.BytesToHash(createdAccount.Key)
|
|
|
|
convertedPayload.StateNodes[hashKey] = StateNode{
|
|
|
|
Value: createdAccount.Value,
|
|
|
|
Leaf: createdAccount.Leaf,
|
|
|
|
}
|
|
|
|
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
2019-04-15 16:28:55 +00:00
|
|
|
for _, storageDiff := range createdAccount.Storage {
|
2019-05-17 04:08:53 +00:00
|
|
|
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
|
|
|
Key: common.BytesToHash(storageDiff.Key),
|
|
|
|
Value: storageDiff.Value,
|
|
|
|
Leaf: storageDiff.Leaf,
|
|
|
|
})
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, deletedAccount := range stateDiff.DeletedAccounts {
|
|
|
|
hashKey := common.BytesToHash(deletedAccount.Key)
|
|
|
|
convertedPayload.StateNodes[hashKey] = StateNode{
|
|
|
|
Value: deletedAccount.Value,
|
|
|
|
Leaf: deletedAccount.Leaf,
|
|
|
|
}
|
|
|
|
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
2019-04-15 16:28:55 +00:00
|
|
|
for _, storageDiff := range deletedAccount.Storage {
|
2019-05-17 04:08:53 +00:00
|
|
|
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
|
|
|
Key: common.BytesToHash(storageDiff.Key),
|
|
|
|
Value: storageDiff.Value,
|
|
|
|
Leaf: storageDiff.Leaf,
|
|
|
|
})
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, updatedAccount := range stateDiff.UpdatedAccounts {
|
|
|
|
hashKey := common.BytesToHash(updatedAccount.Key)
|
|
|
|
convertedPayload.StateNodes[hashKey] = StateNode{
|
|
|
|
Value: updatedAccount.Value,
|
|
|
|
Leaf: updatedAccount.Leaf,
|
|
|
|
}
|
|
|
|
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
2019-04-15 16:28:55 +00:00
|
|
|
for _, storageDiff := range updatedAccount.Storage {
|
2019-05-17 04:08:53 +00:00
|
|
|
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
|
|
|
Key: common.BytesToHash(storageDiff.Key),
|
|
|
|
Value: storageDiff.Value,
|
|
|
|
Leaf: storageDiff.Leaf,
|
|
|
|
})
|
2019-04-15 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return convertedPayload, nil
|
|
|
|
}
|
2019-05-17 15:23:39 +00:00
|
|
|
|
|
|
|
func handleNullAddr(to *common.Address) string {
|
|
|
|
if to == nil {
|
|
|
|
return "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
}
|
|
|
|
return to.Hex()
|
|
|
|
}
|