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/>.
|
|
|
|
|
|
|
|
package ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ipfs/go-block-format"
|
2019-08-05 17:56:15 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/streamer"
|
2019-05-21 19:27:24 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// IPLDResolver is the interface to resolving IPLDs
|
2019-05-21 19:27:24 +00:00
|
|
|
type IPLDResolver interface {
|
2019-10-08 19:51:38 +00:00
|
|
|
ResolveIPLDs(ipfsBlocks IPLDWrapper) streamer.SuperNodePayload
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// EthIPLDResolver is the underlying struct to support the IPLDResolver interface
|
2019-05-21 19:27:24 +00:00
|
|
|
type EthIPLDResolver struct{}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// NewIPLDResolver returns a pointer to an EthIPLDResolver which satisfies the IPLDResolver interface
|
2019-05-21 19:27:24 +00:00
|
|
|
func NewIPLDResolver() *EthIPLDResolver {
|
|
|
|
return &EthIPLDResolver{}
|
|
|
|
}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// ResolveIPLDs is the exported method for resolving all of the ETH IPLDs packaged in an IpfsBlockWrapper
|
2019-10-08 19:51:38 +00:00
|
|
|
func (eir *EthIPLDResolver) ResolveIPLDs(ipfsBlocks IPLDWrapper) streamer.SuperNodePayload {
|
2019-10-02 14:10:37 +00:00
|
|
|
response := &streamer.SuperNodePayload{
|
2019-08-28 19:43:27 +00:00
|
|
|
BlockNumber: ipfsBlocks.BlockNumber,
|
|
|
|
StateNodesRlp: make(map[common.Hash][]byte),
|
|
|
|
StorageNodesRlp: make(map[common.Hash]map[common.Hash][]byte),
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
eir.resolveHeaders(ipfsBlocks.Headers, response)
|
2019-06-18 17:28:57 +00:00
|
|
|
eir.resolveUncles(ipfsBlocks.Uncles, response)
|
2019-05-21 19:27:24 +00:00
|
|
|
eir.resolveTransactions(ipfsBlocks.Transactions, response)
|
|
|
|
eir.resolveReceipts(ipfsBlocks.Receipts, response)
|
|
|
|
eir.resolveState(ipfsBlocks.StateNodes, response)
|
|
|
|
eir.resolveStorage(ipfsBlocks.StorageNodes, response)
|
2019-10-08 19:51:38 +00:00
|
|
|
return *response
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveHeaders(blocks []blocks.Block, response *streamer.SuperNodePayload) {
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, block := range blocks {
|
|
|
|
raw := block.RawData()
|
|
|
|
response.HeadersRlp = append(response.HeadersRlp, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveUncles(blocks []blocks.Block, response *streamer.SuperNodePayload) {
|
2019-06-18 17:28:57 +00:00
|
|
|
for _, block := range blocks {
|
|
|
|
raw := block.RawData()
|
|
|
|
response.UnclesRlp = append(response.UnclesRlp, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveTransactions(blocks []blocks.Block, response *streamer.SuperNodePayload) {
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, block := range blocks {
|
|
|
|
raw := block.RawData()
|
|
|
|
response.TransactionsRlp = append(response.TransactionsRlp, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveReceipts(blocks []blocks.Block, response *streamer.SuperNodePayload) {
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, block := range blocks {
|
|
|
|
raw := block.RawData()
|
|
|
|
response.ReceiptsRlp = append(response.ReceiptsRlp, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveState(blocks map[common.Hash]blocks.Block, response *streamer.SuperNodePayload) {
|
2019-05-21 19:27:24 +00:00
|
|
|
for key, block := range blocks {
|
|
|
|
raw := block.RawData()
|
|
|
|
response.StateNodesRlp[key] = raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
func (eir *EthIPLDResolver) resolveStorage(blocks map[common.Hash]map[common.Hash]blocks.Block, response *streamer.SuperNodePayload) {
|
2019-05-21 19:27:24 +00:00
|
|
|
for stateKey, storageBlocks := range blocks {
|
2019-08-28 19:43:27 +00:00
|
|
|
response.StorageNodesRlp[stateKey] = make(map[common.Hash][]byte)
|
2019-05-21 19:27:24 +00:00
|
|
|
for storageKey, storageVal := range storageBlocks {
|
|
|
|
raw := storageVal.RawData()
|
|
|
|
response.StorageNodesRlp[stateKey][storageKey] = raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|