Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event size params #613

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type BaseConfig struct {
// AppDBBackend defines the type of Database to use for the application and snapshots databases.
// An empty string indicates that the Tendermint config's DBBackend value should be used.
AppDBBackend string `mapstructure:"app-db-backend"`

MaxEventSize int `mapstructure:"max-event-size" json:"max-event-size"`
}

// APIConfig defines the API listener configuration.
Expand Down Expand Up @@ -316,6 +318,7 @@ func DefaultConfig() *Config {
IAVLFastNodeModuleWhitelist: []string{"lockup"},
IAVLLazyLoading: false,
AppDBBackend: "",
MaxEventSize: 0,
},
Telemetry: telemetry.Config{
Enabled: false,
Expand Down Expand Up @@ -381,6 +384,7 @@ func GetConfig(v *viper.Viper) (Config, error) {
if err := v.Unmarshal(conf); err != nil {
return Config{}, fmt.Errorf("error extracting app config: %w", err)
}
sdk.MaxEventSize = conf.BaseConfig.MaxEventSize
return *conf, nil
}

Expand Down
5 changes: 4 additions & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ index-events = [{{ range .BaseConfig.IndexEvents }}{{ printf "%q, " . }}{{end}}]
# IavlCacheSize set the size of the iavl tree cache (in number of nodes).
iavl-cache-size = {{ .BaseConfig.IAVLCacheSize }}

# IAVLDisableFastNode enables or disables the fast node feature of IAVL.
# IAVLDisableFastNode enables or disables the fast node feature of IAVL.
# Default is false.
iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastNode }}

Expand All @@ -96,6 +96,9 @@ iavl-lazy-loading = {{ .BaseConfig.IAVLLazyLoading }}
# The fallback is the db_backend value set in Tendermint's config.toml.
app-db-backend = "{{ .BaseConfig.AppDBBackend }}"

# Maximum event size in bytes. 0 means no limit
max-event-size = {{ .BaseConfig.MaxEventSize }}

###############################################################################
### Telemetry Configuration ###
###############################################################################
Expand Down
69 changes: 62 additions & 7 deletions types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,63 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
)

var MaxEventSize = int(0)

// ----------------------------------------------------------------------------
// Event Manager
// ----------------------------------------------------------------------------

// EventManager implements a simple wrapper around a slice of Event objects that
// can be emitted from.
type EventManager struct {
events Events
events Events
maxEventSize int // max size of events in bytes. 0 means no limit
}

func NewEventManager() *EventManager {
return &EventManager{EmptyEvents()}
return &EventManager{events: EmptyEvents(), maxEventSize: MaxEventSize}
}

func (em *EventManager) Events() Events { return em.events }

// EmitEvent stores a single Event object.
// Deprecated: Use EmitTypedEvent
func (em *EventManager) EmitEvent(event Event) {
fmt.Println("EmitEvent, maxEventSize", em.maxEventSize)
if em.maxEventSize > 0 {
totalEventSize := 0
fmt.Println("EmitEvent event.Attributes", event.Attributes)
for _, attr := range event.Attributes {
totalEventSize += len([]byte(attr.Key)) + len([]byte(attr.Value))
fmt.Println("EmitEvent totalEventSize", totalEventSize)
if totalEventSize > em.maxEventSize {
return // Omit the event if it exceeds the max size
}
}
}
em.events = em.events.AppendEvent(event)
}

// EmitEvents stores a series of Event objects.
// Deprecated: Use EmitTypedEvents
func (em *EventManager) EmitEvents(events Events) {
em.events = em.events.AppendEvents(events)
fmt.Println("EmitEvents, maxEventSize", em.maxEventSize)
if em.maxEventSize > 0 {
for _, event := range events {
totalEventSize := 0
fmt.Println("EmitEvents event.Attributes", event.Attributes)
for _, attr := range event.Attributes {
totalEventSize += len([]byte(attr.Key)) + len([]byte(attr.Value))
fmt.Println("EmitEvents totalEventSize", totalEventSize)
if totalEventSize > em.maxEventSize {
continue // Omit the event if it exceeds the max size
}
}
em.events = em.events.AppendEvent(event)
}
} else {
em.events = em.events.AppendEvents(events)
}
}

// ABCIEvents returns all stored Event objects as abci.Event objects.
Expand All @@ -56,19 +87,43 @@ func (em *EventManager) EmitTypedEvent(tev proto.Message) error {
return err
}

fmt.Println("EmitTypedEvent, maxEventSize", em.maxEventSize)
if em.maxEventSize > 0 {
totalEventSize := 0
for _, attr := range event.Attributes {
totalEventSize += len([]byte(attr.Key)) + len([]byte(attr.Value))
fmt.Println("EmitTypedEvent totalEventSize", totalEventSize)
if totalEventSize > em.maxEventSize {
return nil // Omit the event if it exceeds the max size
}
}
}

em.EmitEvent(event)
return nil
}

// EmitTypedEvents takes series of typed events and emit
func (em *EventManager) EmitTypedEvents(tevs ...proto.Message) error {
events := make(Events, len(tevs))
for i, tev := range tevs {
res, err := TypedEventToEvent(tev)
events := make(Events, 0, len(tevs))
for _, tev := range tevs {
event, err := TypedEventToEvent(tev)
if err != nil {
return err
}
events[i] = res

fmt.Println("EmitTypedEvents, maxEventSize", em.maxEventSize)
if em.maxEventSize > 0 {
totalEventSize := 0
for _, attr := range event.Attributes {
totalEventSize += len([]byte(attr.Key)) + len([]byte(attr.Value))
fmt.Println("EmitTypedEvents totalEventSize", totalEventSize)
if totalEventSize > em.maxEventSize {
continue // Omit the event if it exceeds the max size
}
}
}
events = append(events, event)
}

em.EmitEvents(events)
Expand Down
Loading