2022-02-09 15:19:10 +00:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
|
|
|
|
2022-05-23 11:26:48 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-state-snapshot/pkg/prom"
|
2022-03-30 23:57:30 +00:00
|
|
|
file "github.com/vulcanize/ipld-eth-state-snapshot/pkg/snapshot/file"
|
|
|
|
pg "github.com/vulcanize/ipld-eth-state-snapshot/pkg/snapshot/pg"
|
|
|
|
snapt "github.com/vulcanize/ipld-eth-state-snapshot/pkg/types"
|
2022-02-09 15:19:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewPublisher(mode SnapshotMode, config *Config) (snapt.Publisher, error) {
|
|
|
|
switch mode {
|
|
|
|
case PgSnapshot:
|
|
|
|
driver, err := postgres.NewPGXDriver(context.Background(), config.DB.ConnConfig, config.Eth.NodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-23 11:26:48 +00:00
|
|
|
|
|
|
|
prom.RegisterDBCollector(config.DB.ConnConfig.DatabaseName, driver)
|
|
|
|
|
2022-02-09 15:19:10 +00:00
|
|
|
return pg.NewPublisher(postgres.NewPostgresDB(driver)), nil
|
|
|
|
case FileSnapshot:
|
|
|
|
return file.NewPublisher(config.File.OutputDir, config.Eth.NodeInfo)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid snapshot mode: %s", mode)
|
|
|
|
}
|
2022-06-06 12:08:38 +00:00
|
|
|
|
|
|
|
// Subtracts 1 from the last byte in a path slice, carrying if needed.
|
|
|
|
// Does nothing, returning false, for all-zero inputs.
|
|
|
|
func decrementPath(path []byte) bool {
|
|
|
|
// check for all zeros
|
|
|
|
allzero := true
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
allzero = allzero && path[i] == 0
|
|
|
|
}
|
|
|
|
if allzero {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := len(path) - 1; i >= 0; i-- {
|
|
|
|
val := path[i]
|
|
|
|
path[i]--
|
|
|
|
if val == 0 {
|
|
|
|
path[i] = 0xf
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|