Pass statediff mode and path in through cli
This commit is contained in:
parent
67a86b1f43
commit
dc19bd4e3b
@ -180,7 +180,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
|
||||
}
|
||||
|
||||
if ctx.GlobalBool(utils.StateDiffFlag.Name) {
|
||||
utils.RegisterStateDiffService(stack)
|
||||
utils.RegisterStateDiffService(stack, ctx)
|
||||
}
|
||||
return stack
|
||||
}
|
||||
|
@ -133,6 +133,8 @@ var (
|
||||
utils.EWASMInterpreterFlag,
|
||||
utils.EVMInterpreterFlag,
|
||||
utils.StateDiffFlag,
|
||||
utils.StateDiffModeFlag,
|
||||
utils.StateDiffPathFlag,
|
||||
configFileFlag,
|
||||
}
|
||||
|
||||
|
@ -249,6 +249,8 @@ var AppHelpFlagGroups = []flagGroup{
|
||||
Name: "STATE DIFF",
|
||||
Flags: []cli.Flag{
|
||||
utils.StateDiffFlag,
|
||||
utils.StateDiffModeFlag,
|
||||
utils.StateDiffPathFlag,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -59,6 +59,7 @@ import (
|
||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
||||
"gopkg.in/urfave/cli.v1"
|
||||
"github.com/ethereum/go-ethereum/statediff/service"
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -628,10 +629,21 @@ var (
|
||||
Value: "",
|
||||
}
|
||||
|
||||
// Statediff flags
|
||||
StateDiffFlag = cli.BoolFlag{
|
||||
Name: "statediff",
|
||||
Usage: "Enables the calculation of state diffs between each block, persists these state diffs in ipfs",
|
||||
Usage: "Enables the calculation of state diffs between each block, persists these state diffs the configured persistance mode.",
|
||||
}
|
||||
|
||||
StateDiffModeFlag = cli.StringFlag{
|
||||
Name: "statediff.mode",
|
||||
Usage: "Enables the user to determine which persistence mode they'd like to store the state diffs in.",
|
||||
Value: "csv",
|
||||
}
|
||||
|
||||
StateDiffPathFlag = cli.StringFlag{
|
||||
Name: "statediff.path",
|
||||
Usage: "Enables the user to determine where to persist the state diffs.",
|
||||
Value: ".",
|
||||
}
|
||||
)
|
||||
|
||||
@ -1328,13 +1340,30 @@ func RegisterEthStatsService(stack *node.Node, url string) {
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterStateDiffService(stack *node.Node) {
|
||||
func RegisterStateDiffService(stack *node.Node, ctx *cli.Context) {
|
||||
//based on the context, if path and mode are set, update the config here
|
||||
//otherwise pass in an empty config
|
||||
|
||||
modeFlag := ctx.GlobalString(StateDiffModeFlag.Name)
|
||||
mode, err := statediff.NewMode(modeFlag)
|
||||
if err != nil {
|
||||
Fatalf("Failed to register State Diff Service", err)
|
||||
}
|
||||
|
||||
path := ctx.GlobalString(StateDiffPathFlag.Name)
|
||||
|
||||
config := statediff.Config{
|
||||
On: false,
|
||||
Mode: mode,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
||||
var ethServ *eth.Ethereum
|
||||
ctx.Service(ðServ)
|
||||
chainDb := ethServ.ChainDb()
|
||||
blockChain := ethServ.BlockChain()
|
||||
return service.NewStateDiffService(chainDb, blockChain)
|
||||
return service.NewStateDiffService(chainDb, blockChain, config)
|
||||
}); err != nil {
|
||||
Fatalf("Failed to register State Diff Service", err)
|
||||
}
|
||||
|
@ -56,6 +56,12 @@ func (mode StateDiffMode) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
func NewMode(mode string) (StateDiffMode, error) {
|
||||
stateDiffMode := StateDiffMode(0)
|
||||
err := stateDiffMode.UnmarshalText([]byte(mode))
|
||||
return stateDiffMode, err
|
||||
}
|
||||
|
||||
func (mode StateDiffMode) MarshalText() ([]byte, error) {
|
||||
switch mode {
|
||||
case CSV:
|
||||
|
23
statediff/config_test.go
Normal file
23
statediff/config_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package statediff_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
"testing"
|
||||
"github.com/ethereum/go-ethereum/statediff/testhelpers"
|
||||
)
|
||||
|
||||
func TestNewMode(t *testing.T) {
|
||||
mode, err := statediff.NewMode("csv")
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
if mode != statediff.CSV {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
_, err = statediff.NewMode("not a real mode")
|
||||
if err == nil {
|
||||
t.Error("Expected an error, and got nil.")
|
||||
}
|
||||
}
|
@ -17,12 +17,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
tempDir = os.TempDir()
|
||||
testFilePrefix = "test-statediff"
|
||||
publisher p.Publisher
|
||||
dir string
|
||||
err error
|
||||
testFailedFormatString = "Test failed: %s, %+v"
|
||||
tempDir = os.TempDir()
|
||||
testFilePrefix = "test-statediff"
|
||||
publisher p.Publisher
|
||||
dir string
|
||||
err error
|
||||
)
|
||||
|
||||
var expectedCreatedAccountRow = []string{
|
||||
@ -89,12 +88,12 @@ func TestPublisher(t *testing.T) {
|
||||
test(t)
|
||||
err := removeFilesFromDir(dir)
|
||||
if err != nil {
|
||||
t.Error("Error removing files from temp dir: %s", dir)
|
||||
t.Errorf("Error removing files from temp dir: %s", dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeFilesFromDir(dir string,) error {
|
||||
func removeFilesFromDir(dir string) error {
|
||||
files, err := filepath.Glob(filepath.Join(dir, "*"))
|
||||
if err != nil {
|
||||
return err
|
||||
@ -112,20 +111,20 @@ func removeFilesFromDir(dir string,) error {
|
||||
func testColumnHeaders(t *testing.T) {
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if len(lines) < 1 {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Error()
|
||||
@ -136,29 +135,29 @@ func testAccountDiffs(t *testing.T) {
|
||||
// it persists the created, updated and deleted account diffs to a CSV file
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if len(lines) <= 3 {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[1], expectedCreatedAccountRow) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[2], expectedUpdatedAccountRow) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[3], expectedDeletedAccountRow) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,21 +166,21 @@ func testWhenNoDiff(t *testing.T) {
|
||||
emptyDiff := builder.StateDiff{}
|
||||
_, err = publisher.PublishStateDiff(&emptyDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
if !equals(len(lines), 1) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -190,28 +189,28 @@ func testDefaultPublisher(t *testing.T) {
|
||||
config := statediff.Config{Path: dir}
|
||||
publisher, err = p.NewPublisher(config)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(len(lines), 4) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -220,33 +219,33 @@ func testDefaultDirectory(t *testing.T) {
|
||||
config := statediff.Config{}
|
||||
publisher, err = p.NewPublisher(config)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(len(lines), 4) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,7 @@ type StateDiffService struct {
|
||||
BlockChain BlockChain
|
||||
}
|
||||
|
||||
func NewStateDiffService(db ethdb.Database, blockChain *core.BlockChain) (*StateDiffService, error) {
|
||||
config := statediff.Config{}
|
||||
func NewStateDiffService(db ethdb.Database, blockChain *core.BlockChain, config statediff.Config) (*StateDiffService, error) {
|
||||
builder := b.NewBuilder(db)
|
||||
publisher, err := p.NewPublisher(config)
|
||||
if err != nil {
|
||||
|
3
statediff/testhelpers/helpers.go
Normal file
3
statediff/testhelpers/helpers.go
Normal file
@ -0,0 +1,3 @@
|
||||
package testhelpers
|
||||
|
||||
var TestFailedFormatString = "Test failed: %s, %+v"
|
Loading…
Reference in New Issue
Block a user