Write state diff to CSV #2
@ -180,7 +180,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctx.GlobalBool(utils.StateDiffFlag.Name) {
|
if ctx.GlobalBool(utils.StateDiffFlag.Name) {
|
||||||
utils.RegisterStateDiffService(stack)
|
utils.RegisterStateDiffService(stack, ctx)
|
||||||
}
|
}
|
||||||
return stack
|
return stack
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,8 @@ var (
|
|||||||
utils.EWASMInterpreterFlag,
|
utils.EWASMInterpreterFlag,
|
||||||
utils.EVMInterpreterFlag,
|
utils.EVMInterpreterFlag,
|
||||||
utils.StateDiffFlag,
|
utils.StateDiffFlag,
|
||||||
|
utils.StateDiffModeFlag,
|
||||||
|
utils.StateDiffPathFlag,
|
||||||
configFileFlag,
|
configFileFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,6 +249,8 @@ var AppHelpFlagGroups = []flagGroup{
|
|||||||
Name: "STATE DIFF",
|
Name: "STATE DIFF",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
utils.StateDiffFlag,
|
utils.StateDiffFlag,
|
||||||
|
utils.StateDiffModeFlag,
|
||||||
|
utils.StateDiffPathFlag,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -59,6 +59,7 @@ import (
|
|||||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
"github.com/ethereum/go-ethereum/statediff/service"
|
"github.com/ethereum/go-ethereum/statediff/service"
|
||||||
|
"github.com/ethereum/go-ethereum/statediff"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -628,10 +629,21 @@ var (
|
|||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statediff flags
|
|
||||||
StateDiffFlag = cli.BoolFlag{
|
StateDiffFlag = cli.BoolFlag{
|
||||||
Name: "statediff",
|
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) {
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
||||||
var ethServ *eth.Ethereum
|
var ethServ *eth.Ethereum
|
||||||
ctx.Service(ðServ)
|
ctx.Service(ðServ)
|
||||||
chainDb := ethServ.ChainDb()
|
chainDb := ethServ.ChainDb()
|
||||||
blockChain := ethServ.BlockChain()
|
blockChain := ethServ.BlockChain()
|
||||||
return service.NewStateDiffService(chainDb, blockChain)
|
return service.NewStateDiffService(chainDb, blockChain, config)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
Fatalf("Failed to register State Diff Service", err)
|
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) {
|
func (mode StateDiffMode) MarshalText() ([]byte, error) {
|
||||||
switch mode {
|
switch mode {
|
||||||
case CSV:
|
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.")
|
||||||
|
}
|
||||||
|
}
|
@ -22,7 +22,6 @@ var (
|
|||||||
publisher p.Publisher
|
publisher p.Publisher
|
||||||
dir string
|
dir string
|
||||||
err error
|
err error
|
||||||
testFailedFormatString = "Test failed: %s, %+v"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var expectedCreatedAccountRow = []string{
|
var expectedCreatedAccountRow = []string{
|
||||||
@ -89,12 +88,12 @@ func TestPublisher(t *testing.T) {
|
|||||||
test(t)
|
test(t)
|
||||||
err := removeFilesFromDir(dir)
|
err := removeFilesFromDir(dir)
|
||||||
if err != nil {
|
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, "*"))
|
files, err := filepath.Glob(filepath.Join(dir, "*"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -112,20 +111,20 @@ func removeFilesFromDir(dir string,) error {
|
|||||||
func testColumnHeaders(t *testing.T) {
|
func testColumnHeaders(t *testing.T) {
|
||||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := getTestDiffFile(dir)
|
file, err := getTestDiffFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines, err := csv.NewReader(file).ReadAll()
|
lines, err := csv.NewReader(file).ReadAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if len(lines) < 1 {
|
if len(lines) < 1 {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[0], p.Headers) {
|
if !equals(lines[0], p.Headers) {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -136,29 +135,29 @@ func testAccountDiffs(t *testing.T) {
|
|||||||
// it persists the created, updated and deleted account diffs to a CSV file
|
// it persists the created, updated and deleted account diffs to a CSV file
|
||||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := getTestDiffFile(dir)
|
file, err := getTestDiffFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines, err := csv.NewReader(file).ReadAll()
|
lines, err := csv.NewReader(file).ReadAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if len(lines) <= 3 {
|
if len(lines) <= 3 {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[1], expectedCreatedAccountRow) {
|
if !equals(lines[1], expectedCreatedAccountRow) {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[2], expectedUpdatedAccountRow) {
|
if !equals(lines[2], expectedUpdatedAccountRow) {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[3], expectedDeletedAccountRow) {
|
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{}
|
emptyDiff := builder.StateDiff{}
|
||||||
_, err = publisher.PublishStateDiff(&emptyDiff)
|
_, err = publisher.PublishStateDiff(&emptyDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := getTestDiffFile(dir)
|
file, err := getTestDiffFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines, err := csv.NewReader(file).ReadAll()
|
lines, err := csv.NewReader(file).ReadAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !equals(len(lines), 1) {
|
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}
|
config := statediff.Config{Path: dir}
|
||||||
publisher, err = p.NewPublisher(config)
|
publisher, err = p.NewPublisher(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := getTestDiffFile(dir)
|
file, err := getTestDiffFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines, err := csv.NewReader(file).ReadAll()
|
lines, err := csv.NewReader(file).ReadAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(len(lines), 4) {
|
if !equals(len(lines), 4) {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[0], p.Headers) {
|
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{}
|
config := statediff.Config{}
|
||||||
publisher, err = p.NewPublisher(config)
|
publisher, err = p.NewPublisher(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := os.Chdir(dir)
|
err := os.Chdir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := getTestDiffFile(dir)
|
file, err := getTestDiffFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lines, err := csv.NewReader(file).ReadAll()
|
lines, err := csv.NewReader(file).ReadAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(len(lines), 4) {
|
if !equals(len(lines), 4) {
|
||||||
t.Errorf(testFailedFormatString, t.Name(), err)
|
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !equals(lines[0], p.Headers) {
|
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
|
BlockChain BlockChain
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStateDiffService(db ethdb.Database, blockChain *core.BlockChain) (*StateDiffService, error) {
|
func NewStateDiffService(db ethdb.Database, blockChain *core.BlockChain, config statediff.Config) (*StateDiffService, error) {
|
||||||
config := statediff.Config{}
|
|
||||||
builder := b.NewBuilder(db)
|
builder := b.NewBuilder(db)
|
||||||
publisher, err := p.NewPublisher(config)
|
publisher, err := p.NewPublisher(config)
|
||||||
if err != nil {
|
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