-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup min activeset weight with data from latest epoch (#5171)
closes: #5137 - added a tool that can be used to read db and compute total weight of atxs in the latest epoch - activeset weight configured as a sorted array of tuples (epoch, weight). components select latest tuple that match epoch based on timing of usage
- Loading branch information
Showing
16 changed files
with
170 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
"github.com/spacemeshos/go-spacemesh/sql/atxs" | ||
) | ||
|
||
func main() { | ||
flag.Usage = func() { | ||
fmt.Println(`Usage: | ||
> activeset <publish epoch> <db path> | ||
Example: | ||
query atxs that are published in epoch 3 and stored in state.sql file. | ||
> activeset 3 state.sql`) | ||
flag.PrintDefaults() | ||
} | ||
flag.Parse() | ||
|
||
publish, err := strconv.Atoi(flag.Arg(0)) | ||
must(err, "publish epoch %v is not a valid integer: %s", flag.Arg(0), err) | ||
dbpath := flag.Arg(1) | ||
if len(dbpath) == 0 { | ||
must(errors.New("dbpath is empty"), "dbpath is empty\n") | ||
} | ||
db, err := sql.Open("file:" + dbpath) | ||
must(err, "can't open db at dbpath=%v. err=%s\n", dbpath, err) | ||
|
||
ids, err := atxs.GetIDsByEpoch(db, types.EpochID(publish)) | ||
must(err, "get ids by epoch %d. dbpath=%v. err=%s\n", publish, dbpath, err) | ||
var weight uint64 | ||
for _, id := range ids { | ||
atx, err := atxs.Get(db, id) | ||
must(err, "get id %v: %s\n", id, err) | ||
weight += atx.GetWeight() | ||
} | ||
fmt.Printf("count = %d\nweight = %d\n", len(ids), weight) | ||
} | ||
|
||
func must(err error, msg string, vars ...any) { | ||
if err != nil { | ||
fmt.Printf(msg, vars...) | ||
fmt.Println("") | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package types | ||
|
||
type EpochMinimalActiveWeight struct { | ||
Epoch EpochID | ||
Weight uint64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package minweight | ||
|
||
import "github.com/spacemeshos/go-spacemesh/common/types" | ||
|
||
func Select(epoch types.EpochID, weights []types.EpochMinimalActiveWeight) uint64 { | ||
var ( | ||
rst uint64 | ||
prev types.EpochID | ||
) | ||
for _, weight := range weights { | ||
if weight.Epoch < prev { | ||
panic("weights are not sorted by epoch") | ||
} | ||
if epoch >= weight.Epoch { | ||
rst = weight.Weight | ||
} | ||
prev = weight.Epoch | ||
} | ||
return rst | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package minweight | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
) | ||
|
||
func TestSelect(t *testing.T) { | ||
t.Run("empty", func(t *testing.T) { | ||
require.EqualValues(t, 0, Select(0, nil)) | ||
}) | ||
t.Run("sorted", func(t *testing.T) { | ||
require.EqualValues(t, 10, Select(5, []types.EpochMinimalActiveWeight{ | ||
{Epoch: 0, Weight: 1}, | ||
{Epoch: 4, Weight: 5}, | ||
{Epoch: 5, Weight: 10}, | ||
{Epoch: 7, Weight: 11}, | ||
})) | ||
}) | ||
t.Run("in-between epochs", func(t *testing.T) { | ||
require.EqualValues(t, 10, Select(6, []types.EpochMinimalActiveWeight{ | ||
{Epoch: 0, Weight: 1}, | ||
{Epoch: 4, Weight: 5}, | ||
{Epoch: 5, Weight: 10}, | ||
{Epoch: 7, Weight: 11}, | ||
})) | ||
}) | ||
t.Run("after all", func(t *testing.T) { | ||
require.EqualValues(t, 11, Select(10, []types.EpochMinimalActiveWeight{ | ||
{Epoch: 0, Weight: 1}, | ||
{Epoch: 4, Weight: 5}, | ||
{Epoch: 5, Weight: 10}, | ||
{Epoch: 7, Weight: 11}, | ||
})) | ||
}) | ||
t.Run("not sorted panic", func(t *testing.T) { | ||
require.Panics(t, func() { | ||
Select(5, []types.EpochMinimalActiveWeight{ | ||
{Epoch: 0, Weight: 1}, | ||
{Epoch: 5, Weight: 10}, | ||
{Epoch: 4, Weight: 5}, | ||
{Epoch: 7, Weight: 11}, | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters