-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move event staging logic into a generic EventStager
- Loading branch information
Showing
7 changed files
with
108 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package finalizeblock | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"cosmossdk.io/store/prefix" | ||
storetypes "cosmossdk.io/store/types" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/gogoproto/proto" | ||
ante_types "github.com/dydxprotocol/v4-chain/protocol/app/ante/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/lib" | ||
) | ||
|
||
// EventStager supports staging and retrieval of events (of type T) from FinalizeBlock. | ||
type EventStager[T proto.Message] struct { | ||
transientStoreKey storetypes.StoreKey | ||
cdc codec.BinaryCodec | ||
stagedEventCountKey string | ||
stagedEventKeyPrefix string | ||
} | ||
|
||
// NewEventStager creates a new EventStager. | ||
func NewEventStager[T proto.Message]( | ||
transientStoreKey storetypes.StoreKey, | ||
cdc codec.BinaryCodec, | ||
stagedEventCountKey string, | ||
stagedEventKeyPrefix string, | ||
) EventStager[T] { | ||
return EventStager[T]{ | ||
transientStoreKey: transientStoreKey, | ||
cdc: cdc, | ||
stagedEventCountKey: stagedEventCountKey, | ||
stagedEventKeyPrefix: stagedEventKeyPrefix, | ||
} | ||
} | ||
|
||
// GetStagedFinalizeBlockEventsFromStore retrieves all staged events from the store. | ||
func (s EventStager[T]) GetStagedFinalizeBlockEventsFromStore( | ||
store storetypes.KVStore, | ||
newStagedEvent func() T, | ||
) []T { | ||
count := s.getStagedEventsCount(store) | ||
events := make([]T, count) | ||
store = prefix.NewStore(store, []byte(s.stagedEventKeyPrefix)) | ||
for i := uint32(0); i < count; i++ { | ||
event := newStagedEvent() | ||
bytes := store.Get(lib.Uint32ToKey(i)) | ||
s.cdc.MustUnmarshal(bytes, event) | ||
events[i] = event | ||
} | ||
return events | ||
} | ||
|
||
func (s EventStager[T]) getStagedEventsCount( | ||
store storetypes.KVStore, | ||
) uint32 { | ||
countsBytes := store.Get([]byte(s.stagedEventCountKey)) | ||
if countsBytes == nil { | ||
return 0 | ||
} | ||
return binary.BigEndian.Uint32(countsBytes) | ||
} | ||
|
||
// StageFinalizeBlockEvent stages an event in the transient store. | ||
func (s EventStager[T]) StageFinalizeBlockEvent( | ||
ctx sdk.Context, | ||
eventBytes []byte, | ||
) { | ||
noGasCtx := ctx.WithGasMeter(ante_types.NewFreeInfiniteGasMeter()) | ||
|
||
store := noGasCtx.TransientStore(s.transientStoreKey) | ||
|
||
// Increment events count. | ||
count := s.getStagedEventsCount(store) | ||
store.Set([]byte(s.stagedEventCountKey), lib.Uint32ToKey(count+1)) | ||
|
||
// Store events keyed by index. | ||
store = prefix.NewStore(store, []byte(s.stagedEventKeyPrefix)) | ||
store.Set(lib.Uint32ToKey(count), eventBytes) | ||
} |
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