Skip to content

Commit

Permalink
delete beacon from store CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkolasg committed Apr 30, 2020
1 parent 170d05b commit 70d67b6
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 31 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ Although we **do not recommend** it, you can always disable TLS in drand via:
drand start --tls-disable
```

#### Test the connection to a node

In order to test if your node is reachable from the internet and your setup is
correct, you can run the following command:
```
drand util check <address>
```
where address is the address as listed in the public key. If you disabled TLS,
you need to add the `--tls-disable` flag.

#### Run the setup phase

To setup a new network, drand uses the notion the of a coordinator that collects
Expand Down
7 changes: 3 additions & 4 deletions beacon/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ type Store interface {
Last() (*Beacon, error)
Get(round uint64) (*Beacon, error)
Cursor(func(Cursor))
// XXX Misses a delete function
Close()
del(rount uint64)
Del(round uint64) error
}

// Iterate over items in sorted key order. This starts from the
Expand Down Expand Up @@ -163,8 +162,8 @@ func (b *boltStore) Get(round uint64) (*Beacon, error) {
return beacon, err
}

func (b *boltStore) del(round uint64) {
b.db.Update(func(tx *bolt.Tx) error {
func (b *boltStore) Del(round uint64) error {
return b.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(beaconBucket)
return bucket.Delete(roundToBytes(round))
})
Expand Down
5 changes: 5 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func WithBoltOptions(opts *bolt.Options) ConfigOption {
}
}

// BoltOptions returns the options given to the bolt db
func (c *Config) BoltOptions() *bolt.Options {
return c.boltOpts
}

// WithDbFolder sets the path folder for the db file. This path is NOT relative
// to the DrandFolder path if set.
func WithDbFolder(folder string) ConfigOption {
Expand Down
102 changes: 77 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
gonet "net"

"github.com/BurntSushi/toml"
"github.com/drand/drand/beacon"
"github.com/drand/drand/core"
"github.com/drand/drand/fs"
"github.com/drand/drand/key"
Expand Down Expand Up @@ -258,18 +259,6 @@ func main() {
},
},

&cli.Command{
Name: "check",
Usage: "Check node at the given `ADDRESS` (you can put multiple ones)" +
" in the group for accessibility over the gRPC communication. If the node " +
" is not running behind TLS, you need to pass the tls-disable flag. You can " +
"also check a whole group's connectivity with the group flag.",
Flags: toArray(groupFlag, certsDirFlag, insecureFlag),
Action: func(c *cli.Context) error {
banner()
return checkConnection(c)
},
},
{
Name: "get",
Usage: "get allows for public information retrieval from a remote " +
Expand Down Expand Up @@ -318,19 +307,45 @@ func main() {
},
},
{
Name: "ping",
Usage: "pings the daemon checking its state\n",
Flags: toArray(controlFlag),
Action: func(c *cli.Context) error {
return pingpongCmd(c)
},
},
{
Name: "reset",
Usage: "Resets the local distributed information (share, group file and random beacons). It KEEPS the private/public key pair.",
Flags: toArray(folderFlag, controlFlag),
Action: func(c *cli.Context) error {
return resetCmd(c)
Name: "util",
Usage: "Multiple commands of utility functions, such as reseting a state, checking the connection of a peer...",
Subcommands: []*cli.Command{
&cli.Command{
Name: "check",
Usage: "Check node at the given `ADDRESS` (you can put multiple ones)" +
" in the group for accessibility over the gRPC communication. If the node " +
" is not running behind TLS, you need to pass the tls-disable flag. You can " +
"also check a whole group's connectivity with the group flag.",
Flags: toArray(groupFlag, certsDirFlag, insecureFlag),
Action: func(c *cli.Context) error {
banner()
return checkConnection(c)
},
},
{
Name: "ping",
Usage: "pings the daemon checking its state\n",
Flags: toArray(controlFlag),
Action: func(c *cli.Context) error {
return pingpongCmd(c)
},
},
{
Name: "reset",
Usage: "Resets the local distributed information (share, group file and random beacons). It KEEPS the private/public key pair.",
Flags: toArray(folderFlag, controlFlag),
Action: func(c *cli.Context) error {
return resetCmd(c)
},
},
{
Name: "del-beacon",
Usage: "Delete all beacons from the given `ROUND` number until the head of the chain. You MUST restart the daemon after that command.",
Flags: toArray(folderFlag),
Action: func(c *cli.Context) error {
return deleteBeaconCmd(c)
},
},
},
},
{
Expand Down Expand Up @@ -617,6 +632,43 @@ func checkConnection(c *cli.Context) error {
return nil
}

// deleteBeaconCmd deletes all beacon in the database from the given round until
// the head of the chain
func deleteBeaconCmd(c *cli.Context) error {
conf := contextToConfig(c)
startRoundStr := c.Args().First()
sr, err := strconv.Atoi(startRoundStr)
if err != nil {
return fmt.Errorf("given round not valid: %d", sr)
}
startRound := uint64(sr)
store, err := beacon.NewBoltStore(conf.DBFolder(), conf.BoltOptions())
if err != nil {
return fmt.Errorf("invalid bolt store creation: %s", err)
}
defer store.Close()
lastBeacon, err := store.Last()
if err != nil {
return fmt.Errorf("can't fetch last beacon: %s", err)
}
if startRound > lastBeacon.Round {
return fmt.Errorf("given round is ahead of the chain: %d", lastBeacon.Round)
}
if c.IsSet(verboseFlag.Name) {
fmt.Println("Planning to delete ", lastBeacon.Round-startRound, " beacons")
}
for round := startRound; round <= lastBeacon.Round; round++ {
err := store.Del(round)
if err != nil {
return fmt.Errorf("Error deleting round %d: %s\n", round, err)
}
if c.IsSet(verboseFlag.Name) {
fmt.Println("- Deleted beacon round ", round)
}
}
return nil
}

func toArray(flags ...cli.Flag) []cli.Flag {
return flags
}
Expand Down
57 changes: 55 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/drand/drand/beacon"
"github.com/drand/drand/core"
"github.com/drand/drand/fs"
"github.com/drand/drand/key"
"github.com/drand/drand/test"
"github.com/drand/kyber"
Expand All @@ -35,6 +37,57 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func TestDeleteBeacon(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)

var opt = core.WithConfigFolder(tmp)
conf := core.NewConfig(opt)
fs.CreateSecureFolder(conf.DBFolder())
store, err := beacon.NewBoltStore(conf.DBFolder(), conf.BoltOptions())
require.NoError(t, err)
store.Put(&beacon.Beacon{
Round: 1,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 2,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 3,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 4,
Signature: []byte("hello"),
})
// try to fetch round 3 and 4
b, err := store.Get(3)
require.NoError(t, err)
require.NotNil(t, b)
b, err = store.Get(4)
require.NoError(t, err)
require.NotNil(t, b)

store.Close()
// that commmand should delete round 3 and 4
cmd := exec.Command("drand", "util", "del-beacon", "--folder", tmp, "3")
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
store, err = beacon.NewBoltStore(conf.DBFolder(), conf.BoltOptions())
require.NoError(t, err)

// try to fetch round 3 and 4 - it should now fail
b, err = store.Get(3)
require.Error(t, err)
require.Nil(t, b)
b, err = store.Get(4)
require.Error(t, err)
require.Nil(t, b)

}

func TestKeyGen(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)
Expand Down Expand Up @@ -194,7 +247,7 @@ func TestStartWithoutGroup(t *testing.T) {
time.Sleep(500 * time.Millisecond)

fmt.Println(" + running PING command with ", ctrlPort2)
ping := exec.Command("drand", "ping", "--control", ctrlPort2)
ping := exec.Command("drand", "util", "ping", "--control", ctrlPort2)
out, err = ping.CombinedOutput()
require.NoError(t, err, string(out))

Expand Down Expand Up @@ -226,7 +279,7 @@ func TestStartWithoutGroup(t *testing.T) {
require.NoError(t, err)

// reset state
resetCmd := exec.Command("drand", "reset", "--folder", tmpPath)
resetCmd := exec.Command("drand", "util", "reset", "--folder", tmpPath)
var in bytes.Buffer
in.WriteString("y\n")
resetCmd.Stdin = &in
Expand Down

0 comments on commit 70d67b6

Please sign in to comment.