Skip to content

Commit

Permalink
feat: Add consensus-independent vat transcript span retention configu…
Browse files Browse the repository at this point in the history
…ration to AG_COSMOS_INIT

Ref #9174
Ref #9386
  • Loading branch information
gibson042 committed Sep 6, 2024
1 parent 531c110 commit 23db0f5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
9 changes: 9 additions & 0 deletions golang/cosmos/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import (
"github.com/spf13/viper"
)

func IndexOf[T comparable](a []T, x T) int {
for i, s := range a {
if s == x {
return i
}
}
return -1
}

func NewFileOnlyViper(v1 *viper.Viper) (*viper.Viper, error) {
v2 := viper.New()
v2.SetConfigFile(v1.ConfigFileUsed())
Expand Down
67 changes: 59 additions & 8 deletions golang/cosmos/x/swingset/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/flags"
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"

"github.com/Agoric/agoric-sdk/golang/cosmos/util"
Expand All @@ -15,8 +17,16 @@ import (
const (
ConfigPrefix = "swingset"
FlagSlogfile = ConfigPrefix + ".slogfile"

TranscriptRetentionOptionArchival = "archival"
TranscriptRetentionOptionOperational = "operational"
)

var transcriptRetentionValues []string = []string{
TranscriptRetentionOptionArchival,
TranscriptRetentionOptionOperational,
}

// DefaultConfigTemplate defines a default TOML configuration section for the SwingSet VM.
// Values are pulled from a "Swingset" property, in accord with CustomAppConfig from
// ../../daemon/cmd/root.go.
Expand All @@ -38,6 +48,16 @@ slogfile = "{{ .Swingset.SlogFile }}"
# requires less memory but may have a negative performance impact if vats need to
# be frequently paged out to remain under this limit.
max-vats-online = {{ .Swingset.MaxVatsOnline }}
# Retention of vat transcript spans, with values analogous to those of export
# ` + "`artifactMode`" + ` (cf.
# https://github.com/Agoric/agoric-sdk/blob/master/packages/swing-store/docs/data-export.md#optional--historical-data
# * "archival": keep all transcript spans
# * "operational": keep only necessary transcript spans (i.e., since the
# last snapshot of their vat)
# * "default": determined by ` + "`pruning`" + ` ("archival" if ` + "`pruning`" + ` is
# "nothing", otherwise "operational")
vat-transcript-retention = "{{ .Swingset.VatTranscriptRetention }}"
`

// SwingsetConfig defines configuration for the SwingSet VM.
Expand All @@ -53,11 +73,21 @@ type SwingsetConfig struct {
// MaxVatsOnline is the maximum number of vats that the SwingSet kernel will have online
// at any given time.
MaxVatsOnline int `mapstructure:"max-vats-online" json:"maxVatsOnline,omitempty"`
// VatTranscriptRetention controls retention of vat transcript spans,
// and has values analogous to those of export `artifactMode` (cf.
// ../../../../packages/swing-store/docs/data-export.md#optional--historical-data ).
// * "archival": keep all transcript spans
// * "operational": keep only necessary transcript spans (i.e., since the
// last snapshot of their vat)
// * "default": determined by `pruning` ("archival" if `pruning` is
// "nothing", otherwise "operational")
VatTranscriptRetention string `mapstructure:"vat-transcript-retention" json:"vatTranscriptRetention,omitempty"`
}

var DefaultSwingsetConfig = SwingsetConfig{
SlogFile: "",
MaxVatsOnline: 50,
SlogFile: "",
MaxVatsOnline: 50,
VatTranscriptRetention: "default",
}

func SwingsetConfigFromViper(resolvedConfig servertypes.AppOptions) (*SwingsetConfig, error) {
Expand All @@ -73,11 +103,32 @@ func SwingsetConfigFromViper(resolvedConfig servertypes.AppOptions) (*SwingsetCo
return nil, nil
}
v.MustBindEnv(FlagSlogfile, "SLOGFILE")
wrapper := struct{ Swingset SwingsetConfig }{}
if err := v.Unmarshal(&wrapper); err != nil {
// See CustomAppConfig in ../../daemon/cmd/root.go.
type ExtendedConfig struct {
serverconfig.Config `mapstructure:",squash"`
Swingset SwingsetConfig `mapstructure:"swingset"`
}
extendedConfig := ExtendedConfig{}
if err := v.Unmarshal(&extendedConfig); err != nil {
return nil, err
}
ssConfig := &extendedConfig.Swingset

// Default/validate transcript retention.
if ssConfig.VatTranscriptRetention == "" || ssConfig.VatTranscriptRetention == "default" {
if extendedConfig.Pruning == pruningtypes.PruningOptionNothing {
ssConfig.VatTranscriptRetention = TranscriptRetentionOptionArchival
} else {
ssConfig.VatTranscriptRetention = TranscriptRetentionOptionOperational
}
}
if util.IndexOf(transcriptRetentionValues, ssConfig.VatTranscriptRetention) == -1 {
err := fmt.Errorf(
"value for vat-transcript-retention must be in %q",
transcriptRetentionValues,
)
return nil, err
}
config := &wrapper.Swingset

// Interpret relative paths from config files against the application home
// directory and from other sources (e.g. env vars) against the current
Expand Down Expand Up @@ -108,11 +159,11 @@ func SwingsetConfigFromViper(resolvedConfig servertypes.AppOptions) (*SwingsetCo
return filepath.Abs(path)
}

resolvedSlogFile, err := resolvePath(config.SlogFile, FlagSlogfile)
resolvedSlogFile, err := resolvePath(ssConfig.SlogFile, FlagSlogfile)
if err != nil {
return nil, err
}
config.SlogFile = resolvedSlogFile
ssConfig.SlogFile = resolvedSlogFile

return config, nil
return ssConfig, nil
}
7 changes: 6 additions & 1 deletion packages/cosmic-swingset/src/chain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ const toNumber = specimen => {
* @typedef {object} CosmosSwingsetConfig
* @property {string} [slogfile]
* @property {number} [maxVatsOnline]
* @property {'archival' | 'operational'} [vatTranscriptRetention]
*/
const SwingsetConfigShape = M.splitRecord(
{},
{
slogfile: M.string(),
maxVatsOnline: M.number(),
vatTranscriptRetention: M.or('archival', 'operational'),
},
M.any(),
);
Expand Down Expand Up @@ -308,7 +310,8 @@ export default async function main(progname, args, { env, homedir, agcc }) {
/** @type {CosmosSwingsetConfig} */
const swingsetConfig = harden(initAction.resolvedConfig || {});
validateSwingsetConfig(swingsetConfig);
const { slogfile } = swingsetConfig;
const { slogfile, vatTranscriptRetention } = swingsetConfig;
const keepTranscripts = vatTranscriptRetention === 'archival';

// As a kludge, back-propagate selected configuration into environment variables.
// eslint-disable-next-line dot-notation
Expand Down Expand Up @@ -461,6 +464,7 @@ export default async function main(progname, args, { env, homedir, agcc }) {
trueValue: pathResolve(stateDBDir, 'store-trace.log'),
});

// TODO: Add to SwingsetConfig (#9386)
const keepSnapshots =
XSNAP_KEEP_SNAPSHOTS === '1' || XSNAP_KEEP_SNAPSHOTS === 'true';

Expand Down Expand Up @@ -545,6 +549,7 @@ export default async function main(progname, args, { env, homedir, agcc }) {
swingStoreExportCallback,
swingStoreTraceFile,
keepSnapshots,
keepTranscripts,
afterCommitCallback,
swingsetConfig,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/cosmic-swingset/src/launch-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export async function launch({
swingStoreTraceFile,
swingStoreExportCallback,
keepSnapshots,
keepTranscripts,
afterCommitCallback = async () => ({}),
swingsetConfig,
}) {
Expand Down Expand Up @@ -373,6 +374,7 @@ export async function launch({
traceFile: swingStoreTraceFile,
exportCallback: swingStoreExportSyncCallback,
keepSnapshots,
keepTranscripts,
});
const { kvStore, commit } = hostStorage;

Expand Down

0 comments on commit 23db0f5

Please sign in to comment.