Skip to content

Commit

Permalink
Lower memory consumption for testnet and integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Dec 2, 2024
1 parent 0b774c3 commit a484991
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 23 deletions.
1 change: 1 addition & 0 deletions cmd/sonicd/app/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func initFlags() {
flags.CacheFlag,
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
flags.StateDbCacheCapacityFlag,
}
networkingFlags = []cli.Flag{
flags.BootnodesFlag,
Expand Down
7 changes: 6 additions & 1 deletion cmd/sonicd/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func tmpdir(t *testing.T) string {
}

func initFakenetDatadir(dataDir string, validatorsNum idx.Validator) {
genesisStore := makefakegenesis.FakeGenesisStore(validatorsNum, futils.ToFtm(1000000000), futils.ToFtm(5000000))
genesisStore := makefakegenesis.FakeGenesisStore(
validatorsNum,
futils.ToFtm(1000000000),
futils.ToFtm(5000000),
1024, // 1024 bytes is both small and safe value used by tests
)
defer genesisStore.Close()

if err := genesis.ImportGenesisStore(genesis.ImportParams{
Expand Down
11 changes: 9 additions & 2 deletions cmd/sonictool/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func jsonGenesisImport(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to load JSON genesis: %w", err)
}
genesisStore, err := makefakegenesis.ApplyGenesisJson(genesisJson)

stateDbCacheCapacity := ctx.Int(flags.StateDbCacheCapacityFlag.Name)
genesisStore, err := makefakegenesis.ApplyGenesisJson(genesisJson, stateDbCacheCapacity)
if err != nil {
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
}
Expand Down Expand Up @@ -139,7 +141,12 @@ func fakeGenesisImport(ctx *cli.Context) error {
return err
}

genesisStore := makefakegenesis.FakeGenesisStore(idx.Validator(validatorsNumber), futils.ToFtm(1000000000), futils.ToFtm(5000000))
genesisStore := makefakegenesis.FakeGenesisStore(
idx.Validator(validatorsNumber),
futils.ToFtm(1000000000),
futils.ToFtm(5000000),
ctx.Int(flags.StateDbCacheCapacityFlag.Name),
)
defer genesisStore.Close()
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
Expand Down
3 changes: 3 additions & 0 deletions cmd/sonictool/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Initialize the database using data from the experimental genesis file.
ArgsUsage: "<validators>",
Action: fakeGenesisImport,
Flags: []cli.Flag{
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
flags.StateDbCacheCapacityFlag,
ModeFlag,
},
Description: `
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func MakeAllConfigsFromFile(ctx *cli.Context, configFile string) (*Config, error
cfg.Lachesis.SuppressFramePanic = true
}

if ctx.IsSet(flags.StateDbCacheCapacityFlag.Name) {
cfg.OperaStore.EVM.Cache.StateDbCapacity = ctx.Int(flags.StateDbCacheCapacityFlag.Name)
}

return &cfg, nil
}

Expand Down
6 changes: 6 additions & 0 deletions config/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,10 @@ var (
"Setting this value to <=2000 will result in pre-confugired cache capacity of 2KB", CacheFlag.Name),
Value: 0,
}
StateDbCacheCapacityFlag = cli.IntFlag{
Name: "statedb.cache",
Usage: "Size of StateDb instances cache in bytes. Leaving this blank (which is generally recommended), or setting" +
"this to <1 will automatically set the cache capacity a hard-coded constant.",
Value: 0,
}
)
10 changes: 9 additions & 1 deletion gossip/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@ func newTestEnv(firstEpoch idx.Epoch, validatorsNum idx.Validator, tb testing.TB
rules.Blocks.MaxEmptyBlockSkipPeriod = 0
rules.Emitter.Interval = 0

genStore := makefakegenesis.FakeGenesisStoreWithRulesAndStart(validatorsNum, utils.ToFtm(genesisBalance), utils.ToFtm(genesisStake), rules, firstEpoch, 2)
genStore := makefakegenesis.FakeGenesisStoreWithRulesAndStart(
validatorsNum,
utils.ToFtm(genesisBalance),
utils.ToFtm(genesisStake),
rules,
firstEpoch,
2,
1024,
)
genesis := genStore.Genesis()

store, err := NewMemStore(tb)
Expand Down
12 changes: 7 additions & 5 deletions gossip/evmstore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type (
EvmBlocksNum int
// Cache size for EvmBlock (size in bytes).
EvmBlocksSize uint
// Cache size for StateDb instances (0 for DB-selected default)
StateDbCapacity int
}
// StoreConfig is a config for store db.
StoreConfig struct {
Expand All @@ -38,11 +40,11 @@ type (
func DefaultStoreConfig(scale cachescale.Func) StoreConfig {
return StoreConfig{
Cache: StoreCacheConfig{
ReceiptsSize: scale.U(4 * opt.MiB),
ReceiptsBlocks: scale.I(4000),
TxPositions: scale.I(20000),
EvmBlocksNum: scale.I(5000),
EvmBlocksSize: scale.U(6 * opt.MiB),
ReceiptsSize: scale.U(4 * opt.MiB),
ReceiptsBlocks: scale.I(4000),
TxPositions: scale.I(20000),
EvmBlocksNum: scale.I(5000),
EvmBlocksSize: scale.U(6 * opt.MiB),
},
StateDb: carmen.Parameters{
Variant: "go-file",
Expand Down
2 changes: 1 addition & 1 deletion gossip/evmstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Store) Open() error {
if err != nil {
return fmt.Errorf("failed to create carmen state; %s", err)
}
s.liveStateDb = carmen.CreateStateDBUsing(s.carmenState)
s.liveStateDb = carmen.CreateCustomStateDBUsing(s.carmenState, s.cfg.Cache.StateDbCapacity)
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion gossip/handler_fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ func makeFuzzedHandler() (h *handler, err error) {
genesisStake = 2 * 4e6
)

genStore := makefakegenesis.FakeGenesisStore(genesisStakers, utils.ToFtm(genesisBalance), utils.ToFtm(genesisStake))
genStore := makefakegenesis.FakeGenesisStore(
genesisStakers,
utils.ToFtm(genesisBalance),
utils.ToFtm(genesisStake),
0, // 0 sets default value
)
genesis := genStore.Genesis()

config := DefaultConfig(cachescale.Identity)
Expand Down
19 changes: 13 additions & 6 deletions integration/makefakegenesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ func FakeKey(n idx.ValidatorID) *ecdsa.PrivateKey {
return evmcore.FakeKey(uint32(n))
}

func FakeGenesisStore(num idx.Validator, balance, stake *big.Int) *genesisstore.Store {
return FakeGenesisStoreWithRules(num, balance, stake, opera.FakeNetRules())
func FakeGenesisStore(num idx.Validator, balance, stake *big.Int, stateDbCacheCapacity int) *genesisstore.Store {
return FakeGenesisStoreWithRules(num, balance, stake, opera.FakeNetRules(), stateDbCacheCapacity)
}

func FakeGenesisStoreWithRules(num idx.Validator, balance, stake *big.Int, rules opera.Rules) *genesisstore.Store {
return FakeGenesisStoreWithRulesAndStart(num, balance, stake, rules, 2, 1)
func FakeGenesisStoreWithRules(num idx.Validator, balance, stake *big.Int, rules opera.Rules, stateDbCacheCapacity int) *genesisstore.Store {
return FakeGenesisStoreWithRulesAndStart(num, balance, stake, rules, 2, 1, stateDbCacheCapacity)
}

func FakeGenesisStoreWithRulesAndStart(num idx.Validator, balance, stake *big.Int, rules opera.Rules, epoch idx.Epoch, block idx.Block) *genesisstore.Store {
builder := makegenesis.NewGenesisBuilder()
func FakeGenesisStoreWithRulesAndStart(
num idx.Validator,
balance, stake *big.Int,
rules opera.Rules,
epoch idx.Epoch,
block idx.Block,
stateDbCacheCapacity int,
) *genesisstore.Store {
builder := makegenesis.NewGenesisBuilder(stateDbCacheCapacity)

validators := GetFakeValidators(num)

Expand Down
4 changes: 2 additions & 2 deletions integration/makefakegenesis/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func LoadGenesisJson(filename string) (*GenesisJson, error) {
return &decoded, nil
}

func ApplyGenesisJson(json *GenesisJson) (*genesisstore.Store, error) {
func ApplyGenesisJson(json *GenesisJson, stateDbCacheCapacity int) (*genesisstore.Store, error) {
if json.BlockZeroTime.IsZero() {
return nil, fmt.Errorf("block zero time must be set")
}

builder := makegenesis.NewGenesisBuilder()
builder := makegenesis.NewGenesisBuilder(stateDbCacheCapacity)

fmt.Printf("Building genesis file - rules: %+v\n", json.Rules)

Expand Down
4 changes: 2 additions & 2 deletions integration/makegenesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (b *GenesisBuilder) CurrentHash() hash.Hash {
return er.Hash()
}

func NewGenesisBuilder() *GenesisBuilder {
func NewGenesisBuilder(stateDbCacheCapacity int) *GenesisBuilder {
carmenDir, err := os.MkdirTemp("", "opera-tmp-genesis")
if err != nil {
panic(fmt.Errorf("failed to create temporary dir for GenesisBuilder: %v", err))
Expand All @@ -128,7 +128,7 @@ func NewGenesisBuilder() *GenesisBuilder {
if err != nil {
panic(fmt.Errorf("failed to create carmen state; %s", err))
}
carmenStateDb := carmen.CreateStateDBUsing(carmenState)
carmenStateDb := carmen.CreateCustomStateDBUsing(carmenState, stateDbCacheCapacity)
tmpStateDB := evmstore.CreateCarmenStateDb(carmenStateDb)
return &GenesisBuilder{
tmpStateDB: tmpStateDB,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_test_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,14 @@ func startIntegrationTestNet(directory string, args []string) (*IntegrationTestN
"--datadir", result.stateDir(),
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
"--statedb.cache", "1024",
}, args...)
if err := sonictool.Run(); err != nil {
os.Args = originalArgs
return nil, fmt.Errorf("failed to initialize the test network: %w", err)
}
os.Args = originalArgs

os.Args = originalArgs

if err := result.start(); err != nil {
return nil, fmt.Errorf("failed to start the test network: %w", err)
}
Expand Down Expand Up @@ -273,6 +272,7 @@ func (n *IntegrationTestNet) start() error {
// database memory usage options
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
"--statedb.cache", "1024",
}

err := sonicd.Run()
Expand Down

0 comments on commit a484991

Please sign in to comment.