forked from cerc-io/ipld-eth-server
d5c2ab33fc
= Add eth_node_fingerprint to block that can be imitated by both hot and cold imports - Only sync missing blocks (blocks that are missing or don't share a fingerprint) on cold import - Set block is_final status after import
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package cold_import
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
"github.com/vulcanize/vulcanizedb/pkg/crypto"
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
ColdImportClientName = "LevelDbColdImport"
|
|
ColdImportNetworkId float64 = 1
|
|
)
|
|
|
|
var (
|
|
NoChainDataErr = errors.New("Level DB path does not include chaindata extension.")
|
|
NoGethRootErr = errors.New("Level DB path does not include root path to geth.")
|
|
)
|
|
|
|
type ColdImportNodeBuilder struct {
|
|
reader fs.Reader
|
|
parser crypto.PublicKeyParser
|
|
}
|
|
|
|
func NewColdImportNodeBuilder(reader fs.Reader, parser crypto.PublicKeyParser) ColdImportNodeBuilder {
|
|
return ColdImportNodeBuilder{reader: reader, parser: parser}
|
|
}
|
|
|
|
func (cinb ColdImportNodeBuilder) GetNode(genesisBlock []byte, levelPath string) (core.Node, error) {
|
|
var coldNode core.Node
|
|
nodeKeyPath, err := getNodeKeyPath(levelPath)
|
|
if err != nil {
|
|
return coldNode, err
|
|
}
|
|
nodeKey, err := cinb.reader.Read(nodeKeyPath)
|
|
if err != nil {
|
|
return coldNode, err
|
|
}
|
|
nodeId, err := cinb.parser.ParsePublicKey(string(nodeKey))
|
|
if err != nil {
|
|
return coldNode, err
|
|
}
|
|
genesisBlockHash := common.BytesToHash(genesisBlock).String()
|
|
coldNode = core.Node{
|
|
GenesisBlock: genesisBlockHash,
|
|
NetworkID: ColdImportNetworkId,
|
|
ID: nodeId,
|
|
ClientName: ColdImportClientName,
|
|
}
|
|
return coldNode, nil
|
|
}
|
|
|
|
func getNodeKeyPath(levelPath string) (string, error) {
|
|
chaindataExtension := "chaindata"
|
|
if !strings.Contains(levelPath, chaindataExtension) {
|
|
return "", NoChainDataErr
|
|
}
|
|
chaindataExtensionLength := len(chaindataExtension)
|
|
gethRootPathLength := len(levelPath) - chaindataExtensionLength
|
|
if gethRootPathLength <= chaindataExtensionLength {
|
|
return "", NoGethRootErr
|
|
}
|
|
gethRootPath := levelPath[:gethRootPathLength]
|
|
nodeKeyPath := gethRootPath + "nodekey"
|
|
return nodeKeyPath, nil
|
|
}
|