Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

packet_event.gno

1.64 Kb · 53 lines
 1package core
 2
 3// MaxPacketDataEventSize returns the configured packet_data event budget after
 4// hexAttr encoding.
 5//
 6// The budget is MaxEventAttrPairs * MaxEventAttrValueLen and is enforced only
 7// when SplitEventAttrs is true.
 8func MaxPacketDataEventSize() int {
 9	config := GetEventConfig()
10	return int(config.MaxEventAttrPairs * config.MaxEventAttrValueLen)
11}
12
13// IsValidPacketDataSize reports whether data stays within the configured
14// packet_data event budget after hexAttr encoding.
15//
16// It returns true when SplitEventAttrs is false because that mode intentionally
17// disables packet_data size checks.
18func IsValidPacketDataSize(data []byte) bool {
19	if !GetEventConfig().SplitEventAttrs {
20		return true
21	}
22
23	return hexAttrLen(data) <= MaxPacketDataEventSize()
24}
25
26// eventColumnPairs returns the number of attr pairs encodedEventAttrs would
27// emit for data: one "<key>_size" pair plus one pair per split column.
28func eventColumnPairs(data []byte) int {
29	maxValueLen := int(GetEventConfig().MaxEventAttrValueLen)
30	columns := (hexAttrLen(data) + maxValueLen - 1) / maxValueLen
31	if columns < 1 {
32		columns = 1
33	}
34
35	return 1 + columns
36}
37
38// IsValidEventSize reports whether fields, split together into one host
39// event, stay within chain.Emit's 64-attr-pair ceiling. Fields within
40// IsValidPacketDataSize's solo budget individually can still exceed it
41// combined, so MaxEventAttrPairs also serves as the shared pair budget here.
42func IsValidEventSize(fields ...[]byte) bool {
43	if !GetEventConfig().SplitEventAttrs {
44		return true
45	}
46
47	total := 0
48	for _, data := range fields {
49		total += eventColumnPairs(data)
50	}
51
52	return total <= int(GetEventConfig().MaxEventAttrPairs)
53}