2020-09-05 16:07:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-09-06 15:33:05 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
2020-10-12 13:58:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
2020-09-05 16:07:27 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
|
2020-10-12 13:58:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-09-05 16:07:27 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/node"
|
|
|
|
|
|
|
|
turbocli "github.com/ledgerwatch/turbo-geth/turbo/cli"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// defining a custom command-line flag, a string
|
2020-09-05 16:07:27 +00:00
|
|
|
var flag = cli.StringFlag{
|
|
|
|
Name: "custom-stage-greeting",
|
|
|
|
Value: "default-value",
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// defining a custom bucket name
|
2020-09-06 15:33:05 +00:00
|
|
|
const (
|
2020-09-21 14:10:25 +00:00
|
|
|
customBucketName = "ch.torquem.demo.tgcustom.CUSTOM_BUCKET"
|
2020-09-06 15:33:05 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// the regular main function
|
2020-09-05 16:07:27 +00:00
|
|
|
func main() {
|
2020-09-21 14:10:25 +00:00
|
|
|
// initializing turbo-geth application here and providing our custom flag
|
|
|
|
app := turbocli.MakeApp(runTurboGeth,
|
|
|
|
append(turbocli.DefaultFlags, flag), // always use DefaultFlags, but add a new one in the end.
|
|
|
|
)
|
2020-09-05 16:07:27 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncStages(ctx *cli.Context) stagedsync.StageBuilders {
|
|
|
|
return append(
|
2020-09-21 14:10:25 +00:00
|
|
|
stagedsync.DefaultStages(), // adding all default stages
|
|
|
|
stagedsync.StageBuilder{ // adding our custom stage
|
|
|
|
ID: stages.SyncStage("ch.torquem.demo.tgcustom.CUSTOM_STAGE"),
|
2020-09-05 16:07:27 +00:00
|
|
|
Build: func(world stagedsync.StageParameters) *stagedsync.Stage {
|
|
|
|
return &stagedsync.Stage{
|
2020-09-21 14:10:25 +00:00
|
|
|
ID: stages.SyncStage("ch.torquem.demo.tgcustom.CUSTOM_STAGE"),
|
2020-09-05 16:07:27 +00:00
|
|
|
Description: "Custom Stage",
|
|
|
|
ExecFunc: func(s *stagedsync.StageState, _ stagedsync.Unwinder) error {
|
|
|
|
fmt.Println("hello from the custom stage", ctx.String(flag.Name))
|
2020-09-15 14:36:35 +00:00
|
|
|
val, err := world.TX.Get(customBucketName, []byte("test"))
|
2020-09-06 15:33:05 +00:00
|
|
|
fmt.Println("val", string(val), "err", err)
|
2020-09-15 14:36:35 +00:00
|
|
|
world.TX.Put(customBucketName, []byte("test"), []byte(ctx.String(flag.Name))) //nolint:errcheck
|
2020-09-05 16:07:27 +00:00
|
|
|
s.Done()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
UnwindFunc: func(u *stagedsync.UnwindState, s *stagedsync.StageState) error {
|
|
|
|
fmt.Println("hello from the custom stage unwind", ctx.String(flag.Name))
|
2020-09-15 14:36:35 +00:00
|
|
|
world.TX.Delete(customBucketName, []byte("test")) //nolint:errcheck
|
|
|
|
return u.Done(world.TX)
|
2020-09-05 16:07:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// turbo-geth main function
|
2020-09-05 16:07:27 +00:00
|
|
|
func runTurboGeth(ctx *cli.Context) {
|
2020-09-21 14:10:25 +00:00
|
|
|
// creating a staged sync with our new stage
|
2020-09-05 16:07:27 +00:00
|
|
|
sync := stagedsync.New(
|
|
|
|
syncStages(ctx),
|
|
|
|
stagedsync.DefaultUnwindOrder(),
|
2020-10-12 13:58:45 +00:00
|
|
|
stagedsync.OptionalParameters{
|
|
|
|
StateReaderBuilder: func(getter ethdb.Getter) state.StateReader {
|
|
|
|
// put your custom caching code here
|
|
|
|
return state.NewPlainStateReader(getter)
|
|
|
|
},
|
2020-10-13 08:33:02 +00:00
|
|
|
StateWriterBuilder: func(db ethdb.Database, changeSetsDB ethdb.Database, blockNumber uint64) state.WriterWithChangeSets {
|
|
|
|
// put your custom cache update code here
|
|
|
|
return state.NewPlainStateWriter(db, changeSetsDB, blockNumber)
|
|
|
|
},
|
2020-10-12 13:58:45 +00:00
|
|
|
},
|
2020-09-05 16:07:27 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// running a node and initializing a custom bucket with all default settings
|
2020-09-06 15:33:05 +00:00
|
|
|
tg := node.New(ctx, sync, node.Params{
|
|
|
|
CustomBuckets: map[string]dbutils.BucketConfigItem{
|
|
|
|
customBucketName: {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-09-05 16:07:27 +00:00
|
|
|
err := tg.Serve()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error while serving a turbo-geth node", "err", err)
|
|
|
|
}
|
|
|
|
}
|