2019-05-21 19:27:24 +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-05-21 19:27:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-01-31 18:03:37 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
|
|
|
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ipfs/go-block-format"
|
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// IPLDResolver satisfies the IPLDResolver interface for ethereum
|
|
|
|
type IPLDResolver struct{}
|
2019-05-21 19:27:24 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// NewIPLDResolver returns a pointer to an IPLDResolver which satisfies the IPLDResolver interface
|
|
|
|
func NewIPLDResolver() *IPLDResolver {
|
|
|
|
return &IPLDResolver{}
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Resolve is the exported method for resolving all of the ETH IPLDs packaged in an IpfsBlockWrapper
|
2020-01-31 18:03:37 +00:00
|
|
|
func (eir *IPLDResolver) Resolve(iplds shared.FetchedIPLDs) (shared.ServerResponse, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
ipfsBlocks, ok := iplds.(*IPLDWrapper)
|
|
|
|
if !ok {
|
2020-01-31 18:03:37 +00:00
|
|
|
return StreamResponse{}, fmt.Errorf("eth resolver expected iplds type %T got %T", &IPLDWrapper{}, iplds)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
return StreamResponse{
|
2019-08-28 19:43:27 +00:00
|
|
|
BlockNumber: ipfsBlocks.BlockNumber,
|
2020-01-16 20:48:38 +00:00
|
|
|
HeadersRlp: eir.ResolveHeaders(ipfsBlocks.Headers),
|
|
|
|
UnclesRlp: eir.ResolveUncles(ipfsBlocks.Uncles),
|
|
|
|
TransactionsRlp: eir.ResolveTransactions(ipfsBlocks.Transactions),
|
|
|
|
ReceiptsRlp: eir.ResolveReceipts(ipfsBlocks.Receipts),
|
|
|
|
StateNodesRlp: eir.ResolveState(ipfsBlocks.StateNodes),
|
|
|
|
StorageNodesRlp: eir.ResolveStorage(ipfsBlocks.StorageNodes),
|
2020-01-17 23:16:01 +00:00
|
|
|
}, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveHeaders(iplds []blocks.Block) [][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
headerRlps := make([][]byte, 0, len(iplds))
|
|
|
|
for _, ipld := range iplds {
|
|
|
|
headerRlps = append(headerRlps, ipld.RawData())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return headerRlps
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveUncles(iplds []blocks.Block) [][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
uncleRlps := make([][]byte, 0, len(iplds))
|
|
|
|
for _, ipld := range iplds {
|
|
|
|
uncleRlps = append(uncleRlps, ipld.RawData())
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return uncleRlps
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveTransactions(iplds []blocks.Block) [][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
trxs := make([][]byte, 0, len(iplds))
|
|
|
|
for _, ipld := range iplds {
|
|
|
|
trxs = append(trxs, ipld.RawData())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return trxs
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveReceipts(iplds []blocks.Block) [][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
rcts := make([][]byte, 0, len(iplds))
|
|
|
|
for _, ipld := range iplds {
|
|
|
|
rcts = append(rcts, ipld.RawData())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return rcts
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveState(iplds map[common.Hash]blocks.Block) map[common.Hash][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
stateNodes := make(map[common.Hash][]byte, len(iplds))
|
|
|
|
for key, ipld := range iplds {
|
|
|
|
stateNodes[key] = ipld.RawData()
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return stateNodes
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (eir *IPLDResolver) ResolveStorage(iplds map[common.Hash]map[common.Hash]blocks.Block) map[common.Hash]map[common.Hash][]byte {
|
2020-01-16 20:48:38 +00:00
|
|
|
storageNodes := make(map[common.Hash]map[common.Hash][]byte)
|
|
|
|
for stateKey, storageIPLDs := range iplds {
|
|
|
|
storageNodes[stateKey] = make(map[common.Hash][]byte)
|
|
|
|
for storageKey, storageVal := range storageIPLDs {
|
|
|
|
storageNodes[stateKey][storageKey] = storageVal.RawData()
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return storageNodes
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|