Skip to content

Commit c8eead9

Browse files
acl cli
1 parent aa4b8a4 commit c8eead9

File tree

8 files changed

+756
-89
lines changed

8 files changed

+756
-89
lines changed

cmd/acl/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# ACL - tool for managing access lists
2+
3+
In the root of `Erigon` project, use this command to build the commands:
4+
5+
```shell
6+
make acl
7+
```
8+
9+
It can then be run using the following command
10+
11+
```shell
12+
./buid/bin/acl sub-command options...
13+
```
14+
15+
Snapshots supports the following sub commands:
16+
17+
## mode - access list mode
18+
19+
This command takes the following form:
20+
21+
```shell
22+
acl mode <data-dir> <mode>
23+
```
24+
25+
## supported ACL Types
26+
-`allowlist` - allow list type
27+
- `blocklist` - block list type
28+
29+
## supported policies
30+
- `sendTx` - enables or disables ability of an account to send transactions (deploy contracts transactions not included).
31+
- `deploy` - enables or disables ability of an account to deploy smart contracts (other transactions not included)
32+
33+
This command updates the `mode` of access list in the `acl` data base. Supported modes are:
34+
- `disabled` - access lists are disabled.
35+
- `allowlist` - allow list is enabled. If address is not in the allow list, it won't be able to send transactions (regular, contract deployment, or both).
36+
- `blocklist` - block list is enabled. If address is in the block list, it won't be able to send transactions (regular, contract deployment, or both).
37+
38+
## update - update access list
39+
40+
This command can be used to update an access list in the `acl` data base.
41+
42+
This command takes the following form:
43+
44+
```shell
45+
acl update <data-dir> <type> <csv>
46+
```
47+
The `update` command will read the `.csv` file provided which should be in format `address,"policy1,policy2"`, and update the defined `acl` in the `db`. Note that the `.csv` file is considered as the final state of policies for given `acl` type for defined addresses, meaning, if an address in the `.csv` file has `sendTx` policy, but in `db` it had `deploy`, after this command, it will have `sendTx` in the `db`, there is no appending. Also, it is worth mentioning that using a `.csv` file user can delete addresses from an `acl` table by leaving policies string as empty `""`. This will tell the command that the user wants to remove an address completely from an `acl`.
48+
49+
## add - add a policy to an account
50+
51+
This command can be used to add a policy to an account in the specified `acl`.
52+
53+
This command takes the following form:
54+
55+
```shell
56+
acl add <data-dir> <type> <address> <policy>
57+
```
58+
59+
The `add` command will add the given policy to an account in given access list table if account is not already added to access list table, or if given account does not have that policy.
60+
61+
## remove - removes a policy from an account
62+
63+
This command can be used to remove a policy from an account in the specified `acl`.
64+
65+
This command takes the following form:
66+
67+
```shell
68+
acl remove <data-dir> <type> <address> <policy>
69+
```
70+
The `add` command will remove the given policy to an account in given access list table ifgiven account has that policy.
71+
72+

cmd/acl/main.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package acl
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
8+
"path/filepath"
9+
"syscall"
10+
11+
"github.com/ledgerwatch/erigon-lib/common/disk"
12+
"github.com/ledgerwatch/erigon-lib/common/mem"
13+
"github.com/ledgerwatch/erigon/cmd/acl/mode"
14+
"github.com/ledgerwatch/erigon/cmd/acl/update"
15+
"github.com/ledgerwatch/erigon/cmd/snapshots/sync"
16+
"github.com/ledgerwatch/erigon/cmd/utils"
17+
"github.com/ledgerwatch/erigon/params"
18+
"github.com/ledgerwatch/erigon/turbo/logging"
19+
"github.com/ledgerwatch/log/v3"
20+
"github.com/urfave/cli/v2"
21+
)
22+
23+
func main() {
24+
logging.LogVerbosityFlag.Value = log.LvlError.String()
25+
logging.LogConsoleVerbosityFlag.Value = log.LvlError.String()
26+
27+
app := cli.NewApp()
28+
app.Name = "acl"
29+
app.Version = params.VersionWithCommit(params.GitCommit)
30+
31+
app.Commands = []*cli.Command{
32+
&mode.Command,
33+
&update.UpdateCommand,
34+
&update.RemoveCommand,
35+
&update.AddCommand,
36+
}
37+
38+
app.Flags = []cli.Flag{}
39+
40+
app.UsageText = app.Name + ` [command] [flags]`
41+
42+
app.Action = func(context *cli.Context) error {
43+
if context.Args().Present() {
44+
var goodNames []string
45+
for _, c := range app.VisibleCommands() {
46+
goodNames = append(goodNames, c.Name)
47+
}
48+
_, _ = fmt.Fprintf(os.Stderr, "Command '%s' not found. Available commands: %s\n", context.Args().First(), goodNames)
49+
cli.ShowAppHelpAndExit(context, 1)
50+
}
51+
52+
return nil
53+
}
54+
55+
for _, command := range app.Commands {
56+
command.Before = func(ctx *cli.Context) error {
57+
logger, err := setupLogger(ctx)
58+
59+
if err != nil {
60+
return err
61+
}
62+
63+
var cancel context.CancelFunc
64+
65+
ctx.Context, cancel = context.WithCancel(sync.WithLogger(ctx.Context, logger))
66+
67+
// setup periodic logging and prometheus updates
68+
go mem.LogMemStats(ctx.Context, logger)
69+
go disk.UpdateDiskStats(ctx.Context, logger)
70+
71+
go handleTerminationSignals(cancel, logger)
72+
73+
return nil
74+
}
75+
}
76+
77+
if err := app.Run(os.Args); err != nil {
78+
_, _ = fmt.Fprintln(os.Stderr, err)
79+
os.Exit(1)
80+
}
81+
}
82+
83+
// setupLogger sets up the logger for the command
84+
func setupLogger(ctx *cli.Context) (log.Logger, error) {
85+
dataDir := ctx.String(utils.DataDirFlag.Name)
86+
87+
if len(dataDir) > 0 {
88+
logsDir := filepath.Join(dataDir, "logs")
89+
90+
if err := os.MkdirAll(logsDir, 0755); err != nil {
91+
return nil, err
92+
}
93+
}
94+
95+
logger := logging.SetupLoggerCtx("acl-"+ctx.Command.Name, ctx, log.LvlError, log.LvlInfo, false)
96+
97+
return logger, nil
98+
}
99+
100+
// handleTerminationSignals handles termination signals
101+
func handleTerminationSignals(stopFunc func(), logger log.Logger) {
102+
signalCh := make(chan os.Signal, 1)
103+
signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
104+
105+
switch s := <-signalCh; s {
106+
case syscall.SIGTERM:
107+
logger.Info("Stopping")
108+
stopFunc()
109+
case syscall.SIGINT:
110+
logger.Info("Terminating")
111+
os.Exit(-int(syscall.SIGINT))
112+
}
113+
}

cmd/acl/mode/mode.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package mode
2+
3+
import (
4+
"errors"
5+
6+
"github.com/ledgerwatch/erigon/cmd/snapshots/sync"
7+
"github.com/ledgerwatch/erigon/cmd/utils"
8+
"github.com/ledgerwatch/erigon/zk/txpool"
9+
"github.com/urfave/cli/v2"
10+
)
11+
12+
var (
13+
mode string // Mode of the ACL
14+
)
15+
16+
var Command = cli.Command{
17+
Action: run,
18+
Name: "mode",
19+
Usage: "Set the mode of the ACL",
20+
Flags: []cli.Flag{
21+
&utils.DataDirFlag,
22+
&cli.StringFlag{
23+
Name: "mode",
24+
Usage: "Mode of the ACL (whitelist, blacklist or disabled)",
25+
Destination: &mode,
26+
},
27+
},
28+
}
29+
30+
func run(cliCtx *cli.Context) error {
31+
logger := sync.Logger(cliCtx.Context)
32+
33+
if !cliCtx.IsSet(utils.DataDirFlag.Name) {
34+
return errors.New("data directory is not set")
35+
}
36+
37+
if mode == "" {
38+
return errors.New("mode is not set")
39+
}
40+
41+
dataDir := cliCtx.String(utils.DataDirFlag.Name)
42+
43+
logger.Info("Setting mode", "mode", mode, "dataDir", dataDir)
44+
45+
aclDB, err := txpool.OpenACLDB(cliCtx.Context, dataDir)
46+
if err != nil {
47+
logger.Error("Failed to open ACL database", "err", err)
48+
return err
49+
}
50+
51+
if err := txpool.SetMode(cliCtx.Context, aclDB, mode); err != nil {
52+
logger.Error("Failed to set acl mode", "err", err)
53+
return err
54+
}
55+
56+
logger.Info("ACL Mode set", "mode", mode)
57+
58+
return nil
59+
}

0 commit comments

Comments
 (0)