Documentation ¶
Index ¶
- Constants
- Variables
- func AddComponentTo[T types.Component](wCtx engine.Context, id types.EntityID) (err error)
- func AuthorizePersonaAddressSystem(wCtx engine.Context) error
- func Create(wCtx engine.Context, components ...types.Component) (_ types.EntityID, err error)
- func CreateMany(wCtx engine.Context, num int, components ...types.Component) (entityIDs []types.EntityID, err error)
- func CreatePersonaSystem(wCtx engine.Context) error
- func EachMessage[In any, Out any](wCtx engine.Context, fn func(message.TxData[In]) (Out, error)) error
- func GetComponent[T types.Component](wCtx engine.Context, id types.EntityID) (comp *T, err error)
- func MustRegisterComponent[T types.Component](w *World)
- func NewReadOnlyWorldContext(world *World) engine.Context
- func NewWorldContext(world *World) engine.Context
- func RegisterComponent[T types.Component](w *World) error
- func RegisterInitSystems(w *World, sys ...system.System) error
- func RegisterMessage[In any, Out any](world *World, name string, opts ...message.MessageOption[In, Out]) error
- func RegisterQuery[Request any, Reply any](w *World, name string, ...) (err error)
- func RegisterSystems(w *World, sys ...system.System) error
- func Remove(wCtx engine.Context, id types.EntityID) (err error)
- func RemoveComponentFrom[T types.Component](wCtx engine.Context, id types.EntityID) (err error)
- func SetComponent[T types.Component](wCtx engine.Context, id types.EntityID, component *T) (err error)
- func UpdateComponent[T types.Component](wCtx engine.Context, id types.EntityID, fn func(*T) *T) (err error)
- type EVMTxReceipt
- type Namespace
- type Option
- type Plugin
- type RunMode
- type System
- type TickResults
- type World
- func (w *World) AddEVMTransaction(id types.MessageID, v any, sig *sign.Transaction, evmTxHash string) (tick uint64, txHash types.TxHash)
- func (w *World) AddTransaction(id types.MessageID, v any, sig *sign.Transaction) (tick uint64, txHash types.TxHash)
- func (w *World) ConsumeEVMMsgResult(evmTxHash string) ([]byte, []error, string, bool)
- func (w *World) CurrentTick() uint64
- func (w *World) GameStateManager() gamestate.Manager
- func (w *World) GetComponentByName(name string) (types.ComponentMetadata, error)
- func (w *World) GetEVMMsgReceipt(evmTxHash string) (EVMTxReceipt, bool)
- func (w *World) GetMessageByFullName(name string) (types.Message, bool)
- func (w *World) GetMessageByID(id types.MessageID) (types.Message, bool)
- func (w *World) GetQueryByName(name string) (engine.Query, error)
- func (w *World) GetRegisteredComponents() []types.ComponentMetadata
- func (w *World) GetRegisteredMessages() []types.Message
- func (w *World) GetRegisteredQueries() []engine.Query
- func (w *World) GetRegisteredSystemNames() []string
- func (w *World) GetSignerComponentForPersona(personaTag string) (*component.SignerComponent, error)
- func (w *World) GetSignerForPersonaTag(personaTag string, tick uint64) (addr string, err error)
- func (w *World) GetTransactionReceiptsForTick(tick uint64) ([]receipt.Receipt, error)
- func (w *World) HandleEVMQuery(name string, abiRequest []byte) ([]byte, error)
- func (w *World) IsGameRunning() bool
- func (w *World) Namespace() string
- func (w *World) RecoverFromChain(ctx context.Context) error
- func (w *World) RegisterPlugin(plugin Plugin)
- func (w *World) Search(filter filter.ComponentFilter) *search.Search
- func (w *World) Shutdown() error
- func (w *World) StartGame() error
- func (w *World) StoreReader() gamestate.Reader
- func (w *World) UseNonce(signerAddress string, nonce uint64) error
- func (w *World) WaitForNextTick() (success bool)
- type WorldConfig
- type WorldContext
- type WorldOption
- func WithCustomLogger(logger zerolog.Logger) WorldOption
- func WithCustomRouter(rtr router.Router) WorldOption
- func WithDisableSignatureVerification() WorldOption
- func WithMockRedis() WorldOption
- func WithPort(port string) WorldOption
- func WithPrettyLog() WorldOption
- func WithReceiptHistorySize(size int) WorldOption
- func WithStoreManager(s gamestate.Manager) WorldOption
- func WithTickChannel(ch <-chan time.Time) WorldOption
- func WithTickDoneChannel(ch chan<- uint64) WorldOption
Constants ¶
const ( RunModeProd RunMode = "production" RunModeDev RunMode = "development" DefaultRunMode = RunModeDev DefaultNamespace = "world-1" DefaultRedisAddress = "localhost:6379" DefaultLogLevel = "info" DefaultStatsdAddress = "localhost:8125" )
const ( DefaultHistoricalTicksToStore = 10 RedisDialTimeOut = 15 )
Variables ¶
var ( ErrEntityMutationOnReadOnly = errors.New("cannot modify state with read only context") ErrEntitiesCreatedBeforeReady = errors.New("entities should not be created before world is ready") ErrEntityDoesNotExist = iterators.ErrEntityDoesNotExist ErrEntityMustHaveAtLeastOneComponent = iterators.ErrEntityMustHaveAtLeastOneComponent ErrComponentNotOnEntity = iterators.ErrComponentNotOnEntity ErrComponentAlreadyOnEntity = iterators.ErrComponentAlreadyOnEntity )
var NewSearch = search.NewSearch
var NonFatalError = []error{ ErrEntityDoesNotExist, ErrComponentNotOnEntity, ErrComponentAlreadyOnEntity, ErrEntityMustHaveAtLeastOneComponent, }
Functions ¶
func AddComponentTo ¶
func AuthorizePersonaAddressSystem ¶
AuthorizePersonaAddressSystem enables users to authorize an address to a persona tag. This is mostly used so that users who want to interact with the game via smart contract can link their EVM address to their persona tag, enabling them to mutate their owned state from the context of the EVM.
func Create ¶
Create creates a single entity in the world, and returns the id of the newly created entity. At least 1 component must be provided.
func CreateMany ¶
func CreateMany(wCtx engine.Context, num int, components ...types.Component) (entityIDs []types.EntityID, err error)
CreateMany creates multiple entities in the world, and returns the slice of ids for the newly created entities. At least 1 component must be provided.
func CreatePersonaSystem ¶
CreatePersonaSystem is an system that will associate persona tags with signature addresses. Each persona tag may have at most 1 signer, so additional attempts to register a signer with a persona tag will be ignored.
func EachMessage ¶
func GetComponent ¶
GetComponent returns component data from the entity.
func MustRegisterComponent ¶
func NewReadOnlyWorldContext ¶
func NewWorldContext ¶
func RegisterMessage ¶
func RegisterMessage[In any, Out any](world *World, name string, opts ...message.MessageOption[In, Out]) error
RegisterMessage registers a message to the world. Cardinal will automatically set up HTTP routes that map to each registered message. Message URLs are take the form of "group.name". A default group, "game", is used unless the WithCustomMessageGroup option is used. Example: game.throw-rock
func RegisterQuery ¶
func RemoveComponentFrom ¶
RemoveComponentFrom removes a component from an entity.
Types ¶
type EVMTxReceipt ¶
type Namespace ¶
type Namespace string
Namespace is a unique identifier for a world used for posting to the data availability layer and to prevent signature replay attacks across multiple worlds.
type TickResults ¶
func NewTickResults ¶
func NewTickResults(initialTick uint64) *TickResults
func (*TickResults) AddEvent ¶
func (tr *TickResults) AddEvent(event any) error
func (*TickResults) Clear ¶
func (tr *TickResults) Clear()
func (*TickResults) SetReceipts ¶
func (tr *TickResults) SetReceipts(newReceipts []receipt.Receipt)
func (*TickResults) SetTick ¶
func (tr *TickResults) SetTick(tick uint64)
type World ¶
type World struct {
// contains filtered or unexported fields
}
func NewWorld ¶
func NewWorld(opts ...WorldOption) (*World, error)
NewWorld creates a new World object using Redis as the storage layer
func (*World) AddEVMTransaction ¶
func (*World) AddTransaction ¶
func (w *World) AddTransaction(id types.MessageID, v any, sig *sign.Transaction) ( tick uint64, txHash types.TxHash, )
AddTransaction adds a transaction to the transaction pool. This should not be used directly. Instead, use a MessageType.AddTransaction to ensure type consistency. Returns the tick this transaction will be executed in.
func (*World) ConsumeEVMMsgResult ¶
ConsumeEVMMsgResult consumes a tx result from an EVM originated Cardinal message. It will fetch the receipt from the map, and then delete ('consume') it from the map.
func (*World) CurrentTick ¶
func (*World) GameStateManager ¶
func (*World) GetComponentByName ¶
func (w *World) GetComponentByName(name string) (types.ComponentMetadata, error)
func (*World) GetEVMMsgReceipt ¶
func (w *World) GetEVMMsgReceipt(evmTxHash string) (EVMTxReceipt, bool)
func (*World) GetMessageByFullName ¶
func (*World) GetMessageByID ¶
func (*World) GetRegisteredComponents ¶
func (w *World) GetRegisteredComponents() []types.ComponentMetadata
func (*World) GetRegisteredMessages ¶
func (*World) GetRegisteredQueries ¶
func (*World) GetRegisteredSystemNames ¶
func (*World) GetSignerComponentForPersona ¶
func (w *World) GetSignerComponentForPersona(personaTag string) (*component.SignerComponent, error)
func (*World) GetSignerForPersonaTag ¶
GetSignerForPersonaTag returns the signer address that has been registered for the given persona tag after the given tick. If the engine's tick is less than or equal to the given tick, ErrorCreatePersonaTXsNotProcessed is returned. If the given personaTag has no signer address, ErrPersonaTagHasNoSigner is returned.
func (*World) GetTransactionReceiptsForTick ¶
func (*World) HandleEVMQuery ¶
func (*World) IsGameRunning ¶
func (*World) RecoverFromChain ¶
RecoverFromChain will attempt to recover the state of the engine based on historical transaction data. The function puts the World in a recovery state, and will then query all transaction batches under the World's namespace. The function will continuously ask the EVM base shard for batches, and run ticks for each batch returned.
func (*World) RegisterPlugin ¶
func (*World) StartGame ¶
StartGame starts running the world game loop. Each time a message arrives on the tickChannel, a world tick is attempted. In addition, an HTTP server (listening on the given port) is created so that game messages can be sent to this world. After StartGame is called, RegisterComponent, registerMessagesByName, RegisterQueries, and RegisterSystems may not be called. If StartGame doesn't encounter any errors, it will block forever, running the server and ticking the game in the background.
func (*World) StoreReader ¶
func (*World) WaitForNextTick ¶
WaitForNextTick blocks until at least one game tick has completed. It returns true if it successfully waited for a tick. False may be returned if the engine was shut down while waiting for the next tick to complete.
type WorldConfig ¶
type WorldConfig struct { // Cardinal CardinalMode RunMode `config:"CARDINAL_MODE"` CardinalNamespace string `config:"CARDINAL_NAMESPACE"` CardinalLogLevel string `config:"CARDINAL_LOG_LEVEL"` // Redis RedisAddress string `config:"REDIS_ADDRESS"` RedisPassword string `config:"REDIS_PASSWORD"` // Shard networking BaseShardSequencerAddress string `config:"BASE_SHARD_SEQUENCER_ADDRESS"` // Telemetry StatsdAddress string `config:"STATSD_ADDRESS"` TraceAddress string `config:"TRACE_ADDRESS"` // RouterKey is a token used to secure communications between the game shard and the base shard. RouterKey string `config:"ROUTER_KEY"` }
func (*WorldConfig) Validate ¶
func (w *WorldConfig) Validate() error
Validate validates the config values and ensures that when the RunMode is production, the required values are set.
type WorldContext ¶
type WorldOption ¶
type WorldOption struct {
// contains filtered or unexported fields
}
WorldOption represents an option that can be used to augment how the cardinal.World will be run.
func WithCustomLogger ¶
func WithCustomLogger(logger zerolog.Logger) WorldOption
func WithCustomRouter ¶
func WithCustomRouter(rtr router.Router) WorldOption
func WithDisableSignatureVerification ¶
func WithDisableSignatureVerification() WorldOption
WithDisableSignatureVerification disables signature verification for the HTTP server. This should only be used for local development.
func WithMockRedis ¶
func WithMockRedis() WorldOption
WithMockRedis runs the World with an embedded miniredis instance on port 6379.
func WithPort ¶
func WithPort(port string) WorldOption
WithPort sets the port that the HTTP server will run on.
func WithPrettyLog ¶
func WithPrettyLog() WorldOption
func WithReceiptHistorySize ¶
func WithReceiptHistorySize(size int) WorldOption
WithReceiptHistorySize specifies how many ticks worth of transaction receipts should be kept in memory. The default is 10. A smaller number uses less memory, but limits the amount of historical receipts available.
func WithStoreManager ¶
func WithStoreManager(s gamestate.Manager) WorldOption
func WithTickChannel ¶
func WithTickChannel(ch <-chan time.Time) WorldOption
WithTickChannel sets the channel that will be used to decide when world.doTick is executed. If unset, a loop interval of 1 second will be set. To set some other time, use: WithTickChannel(time.Tick(<some-duration>)). Tests can pass in a channel controlled by the test for fine-grained control over when ticks are executed.
func WithTickDoneChannel ¶
func WithTickDoneChannel(ch chan<- uint64) WorldOption
WithTickDoneChannel sets a channel that will be notified each time a tick completes. The completed tick will be pushed to the channel. This option is useful in tests when assertions need to be performed at the end of a tick.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package ecb allows for buffering of state changes to the ECS dbStorage layer, and either committing those changes in an atomic Redis transaction, or discarding the changes.
|
Package ecb allows for buffering of state changes to the ECS dbStorage layer, and either committing those changes in an atomic Redis transaction, or discarding the changes. |
Package receipt keeps track of transaction receipts for a number of ticks.
|
Package receipt keeps track of transaction receipts for a number of ticks. |
iterator/mocks
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
mocks
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
docs
Package docs Code generated by swaggo/swag.
|
Package docs Code generated by swaggo/swag. |
Package statsd is a helper package that wraps some common statsd methods.
|
Package statsd is a helper package that wraps some common statsd methods. |
engine/mocks
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |