service

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EngineExecutionTimeoutPanic is the panic message when the engine execution time is exceeded.
	EngineExecutionTimeoutPanic = "engine-execution-panic-timeout"
)
View Source
const (
	FuelTankLockName = "fuel_tank_mux"
)

Variables

View Source
var ErrFuelTankNotEnough = apperr.Errorf(apperr.EMGVM_LOWFUEL, "fuel tank is not enough")

ErrFuelTankCapacity is returned when the initial capacity is greater than the max capacity.

Functions

This section is empty.

Types

type AuthFilter

type AuthFilter struct {
	ID       *int64  `json:"id"`
	UserID   *int64  `json:"user_id"`
	Source   *string `json:"source"`
	SourceID *string `json:"source_id"`

	Offset int `json:"offset"`
	Limit  int `json:"limit"`
}

AuthFilter represents a filter for auths.

type AuthManagmentService

type AuthManagmentService interface {
	// Auhenticate authenticates the user with the given auth source and options.
	// Returns the user if the authentication was successful, otherwise returns an error.
	// Return ENOTIMPLEMENTED if the service not support authentications
	Auhenticate(ctx context.Context, opts *entity.AuthUserOptions) (*entity.Auth, error)

	// CreateAuth creates a new auth.
	// If is attached to a user, links the auth to the user, otherwise creates a new user.
	// On success, the auth.ID is set.
	CreateAuth(ctx context.Context, auth *entity.Auth) error

	// DeleteAuth deletes an auth.
	// Do not delete underlying user.
	DeleteAuth(ctx context.Context, id int64) error
}

AuthManagmentService is the interface for managing auths.

type AuthSearchService

type AuthSearchService interface {
	// FindAuthByID returns a single auth by its id.
	FindAuthByID(ctx context.Context, id int64) (*entity.Auth, error)

	// FindAuths returns a list of auths.
	// Predicate can be used to filter the results.
	// Also returns the total count of auths.
	FindAuths(ctx context.Context, filter AuthFilter) (entity.Auths, int, error)
}

AuthSearchService is the interface for searching auths.

type AuthService

type AuthService interface {
	AuthSearchService
	AuthManagmentService
}

AuthService is an interface for user authentication

type CPUsPoolService added in v0.0.7

type CPUsPoolService interface {
	// AcquireCore acquires a core from the CPUsPool.
	// Returns a function that must be called when the core is not needed anymore.
	// CORE LEAK IF NOT CALLED.
	AcquireCore(ctx context.Context, call VmCallable) (release func(), err error)
}

CPUsPoolService is the interface for the CPUsPool service. It is used from MusicGang VM to acquire and release cores in order to execute operations inside the VM.

type ContractCallOpt added in v0.0.7

type ContractCallOpt struct {
	ContractRef *entity.Contract
	RevisionRef *entity.Revision
	StateRef    *entity.State
}

ContractCallOpt defines the options for calling a contract.

func (ContractCallOpt) Contract added in v0.0.7

func (opt ContractCallOpt) Contract() (*entity.Contract, error)

Contract returns the contract attached to the contract call options.

func (ContractCallOpt) Revision added in v0.0.7

func (opt ContractCallOpt) Revision() (*entity.Revision, error)

Revision returns the revision attached to the contract call options.

type ContractExecutorService

type ContractExecutorService interface {
	// ExecContract executes a contract.
	ExecContract(ctx context.Context, opt ContractCallOpt) (res interface{}, err error)
}

ContractExecutorService rapresents the contract executor service.

type ContractFilter

type ContractFilter struct {
	ID          *int64  `json:"id"`
	Name        *string `json:"name"`
	Description *string `json:"description"`
	UserID      *int64  `json:"user_id"`

	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

ContractFilter represents the options used to filter the contracts.

type ContractManagmentService

type ContractManagmentService interface {
	// CreateContract creates a new contract.
	// Return EINVALID if the contract is invalid.
	// Return EEXISTS if the contract already exists.
	// Return EFORBIDDEN if the user is not allowed to create a contract.
	// Return EUNAUTHORIZED if the contract owner is not the authenticated user.
	CreateContract(ctx context.Context, contract *entity.Contract) error

	// DeleteContract deletes the contract with the given id.
	// Return EUNAUTHORIZED if the contract is not the same as the authenticated user.
	// Return ENOTFOUND if the contract does not exist.
	// This service also deletes the revisions of the contract.
	DeleteContract(ctx context.Context, id int64) error

	// MakeRevision creates a new revision of the contract.
	// Return ENOTFOUND if the contract does not exist.
	// Return EINVALID if the revision is invalid.
	// It shouldn't return ECONFLICT because the revision is generated by the database and most likely there should be a UNIQUE constraint on the revision number and the Contract ID.
	MakeRevision(ctx context.Context, revision *entity.Revision) error

	// UpdateContract updates the given contract.
	// Return ENOTFOUND if the contract does not exist.
	// Return EUNAUTHORIZED if the contract is not owned by the authenticated user.
	UpdateContract(ctx context.Context, id int64, contract ContractUpdate) (*entity.Contract, error)
}

ContractManagmentService is the interface for managing contracts. It is used to create, update and delete contracts. Also is used to create new contract revisions.

type ContractSearchService

type ContractSearchService interface {
	// FindContractByID returns the contract with the given id.
	// Return ENOTFOUND if the contract does not exist.
	FindContractByID(ctx context.Context, id int64) (*entity.Contract, error)

	// FindContracts returns a list of contracts filtered by the given options.
	// Also returns the total count of contracts.
	FindContracts(ctx context.Context, filter ContractFilter) (entity.Contracts, int, error)

	// FindRevisionByContractAndRev returns the revision searched by the given contract and revision number.
	// Return ENOTFOUND if the revision does not exist.
	FindRevisionByContractAndRev(ctx context.Context, contractID int64, rev entity.RevisionNumber) (*entity.Revision, error)
}

ContractSearchService is the interface for searching contracts and revisions.

type ContractService

type ContractService interface {
	ContractSearchService
	ContractManagmentService
}

ContractService rapresents the contract managment service. Except for search services, it is required that in passed context is injected the user who try to access this repository, otherwise it will return EUNAUTHORIZED.

type ContractUpdate

type ContractUpdate struct {
	Name        *string      `json:"name"`
	Description *string      `json:"description"`
	MaxFuel     *entity.Fuel `json:"max_fuel"`
}

ContractUpdate represents the options used to update the contracts.

type EngineService

type EngineService interface {
	ContractExecutorService
	EngineStateService
	// IsRunning returns true if the engine is running.
	IsRunning() bool
	// Pause pauses the engine.
	Pause() error
	// Resume resumes the engine.
	Resume() error
	// Stop stops the engine.
	Stop() error
}

EngineService is the interface for the engine service.

type EngineStateService added in v0.0.8

type EngineStateService interface {
	// State returns the state of the engine.
	State() entity.VmState
}

type FuelMonitorService added in v0.0.8

type FuelMonitorService interface {
	// StartMonitoring starts the fuel monitor.
	StartMonitoring(ctx context.Context) error
	// StopMonitoring stops the fuel monitor.
	StopMonitoring(ctx context.Context) error
}

FuelMonitorService is the interface for the fuel monitor. This service monitors the fuel consumption of the engine and should send events when reach safe levels or when the fuel usage is too high.

type FuelService added in v0.0.8

type FuelService interface {
	// Fuel returns the current amount of fuel used.
	Fuel(ctx context.Context) (entity.Fuel, error)
}

type FuelStationService

type FuelStationService interface {
	// IsRunning returns true if the FuelStation is running
	IsRunning() bool
	// ResumeRefueling starts the FuelStation.
	// It will start refueling the fuel tank every FuelRefillRate.
	// If the FuelStation is already running, it will return an error.
	ResumeRefueling(ctx context.Context) error
	// StopRefueling stops the FuelStation.
	// If the FuelStation is not running, it will return an error.
	StopRefueling(ctx context.Context) error
}

FuelStationService is the interface for the fuel station.

type FuelStatsService added in v0.0.8

type FuelStatsService interface {
	// Stats returns the current amount of fuel used.
	Stats(ctx context.Context) (*entity.FuelStat, error)
}

FuelStatsService is the interface for the fuel meter. FuelStatsService returns the current amount of fuel used.

type FuelTankService

type FuelTankService interface {
	FuelService

	// FuelTankService can implements the FuelStatsService
	FuelStatsService

	// Burn consumes the specified amount of fuel.
	Burn(ctx context.Context, fuel entity.Fuel) error
	// Refuel refills the fuel tank by the specified amount.
	Refuel(ctx context.Context, fuelToRefill entity.Fuel) error
}

FuelTanker is the interface for the fuel tank.

type JWTBlacklistService

type JWTBlacklistService interface {

	// Invalidate a JWT token.
	// Returns EUNAUTHORIZED if the user is not allowed to invalidate the token.
	Invalidate(ctx context.Context, token string, expiration time.Duration) error

	// IsBlacklisted checks if a token is blacklisted.
	IsBlacklisted(ctx context.Context, token string) (bool, error)
}

JWTBlacklistService is an interface for JWT blacklist service.

type JWTService

type JWTService interface {
	// Exchange a auth entity for a JWT token pair.
	Exchange(ctx context.Context, auth *entity.Auth) (*entity.TokenPair, error)

	// Invalidate a JWT token.
	Invalidate(ctx context.Context, token string, expiration time.Duration) error

	// Parse a JWT token and return the associated claims.
	Parse(ctx context.Context, token string) (*entity.AppClaims, error)

	// Refresh a JWT token and returns the new token pair.
	Refresh(ctx context.Context, refreshToken string) (*entity.TokenPair, error)
}

JWTService is an interface for JWT service. It is used to generate and validate JWT tokens.

type LockService

type LockService interface {
	// LockContext acquires the lock.
	LockContext(ctx context.Context) error
	// Name returns the name of the lock.
	Name() string
	// UnlockContext releases the lock.
	UnlockContext(ctx context.Context) (bool, error)
}

LockService is the interface for the lock service. It is used to implement a distributed lock.

type RevisionFilter

type RevisionFilter struct {
	// ContractID is not a pointer because should be a mandatory field and honestly i prefer check for zero value istead of nil.
	// It is not possible to filter by contract id if it is not present.
	ContractID int64                  `json:"contract_id"`
	Rev        *entity.RevisionNumber `json:"number"`

	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

RevisionFilter represents the options used to filter the revisions of a contract.

type StateCacheService added in v0.0.7

type StateCacheService interface {
	// CacheState caches the state.
	CacheState(ctx context.Context, state *entity.State) error
}

StateCacheService is the interface for caching states.

type StateManagementService added in v0.0.7

type StateManagementService interface {
	// CreateState creates a state.
	// Should returns EUNAUTHORIZED if the user is not authorized to create the state.
	CreateState(ctx context.Context, state *entity.State) error
	// UpdateState updates a state.
	// Should returns ENOTFOUND if the state is not found.
	// Should returns EUNAUTHORIZED if the user is not authorized to access the state.
	UpdateState(ctx context.Context, revisionID int64, value entity.StateValue) (*entity.State, error)
}

StateManagementService is the interface for managing states.

type StateSearchService added in v0.0.7

type StateSearchService interface {
	// FindStateByRevisionID finds the state by revision ID and the authenticated user retrieved from the context.
	// Should returns ENOTFOUND if the state is not found.
	// Should returns EUNAUTHORIZED if the user is not authorized to access the state.
	FindStateByRevisionID(ctx context.Context, revisionID int64) (*entity.State, error)
}

StateSearchService is the interface for searching states.

type StateService added in v0.0.7

type StateService interface {
	StateSearchService
	StateManagementService
}

StateService is the interface for managing and searching states.

type UserFilter

type UserFilter struct {
	ID    *int64  `json:"id"`
	Email *string `json:"email"`
	Name  *string `json:"name"`

	Offset int `json:"offset"`
	Limit  int `json:"limit"`
}

UserFilter represents the options used to filter the users.

type UserManagmentService

type UserManagmentService interface {
	// CreateUser creates a new user.
	CreateUser(ctx context.Context, user *entity.User) error

	// DeleteUser deletes the user with the given id.
	// Return EUNAUTHORIZED if the user is not the same as the authenticated user.
	// Return ENOTFOUND if the user does not exist.
	DeleteUser(ctx context.Context, id int64) error

	// UpdateUser updates the given user.
	// Return EUNAUTHORIZED if the user is not the same as the authenticated user.
	// Return ENOTFOUND if the user does not exist.
	UpdateUser(ctx context.Context, id int64, user UserUpdate) (*entity.User, error)
}

UserManagmentService is the interface for managing users.

type UserSearchService

type UserSearchService interface {
	// FindUserByEmail returns the user with the given email.
	// Return ENOTFOUND if the user does not exist.
	FindUserByEmail(ctx context.Context, email string) (*entity.User, error)

	// FindUserByID returns the user with the given id.
	// Return ENOTFOUND if the user does not exist.
	FindUserByID(ctx context.Context, id int64) (*entity.User, error)

	// FindUsers returns a list of users filtered by the given options.
	// Also returns the total count of auths.
	FindUsers(ctx context.Context, filter UserFilter) (entity.Users, int, error)
}

UserSearchService is the interface for searching users.

type UserService

type UserService interface {
	UserSearchService
	UserManagmentService
}

UserService rapresents the user service.

type UserUpdate

type UserUpdate struct {
	Name *string `json:"name"`
}

type VmCall

type VmCall struct {
	// ContractRef is the contract that is being called.
	ContractRef *entity.Contract `json:"contract"`

	// User is the user that is calling the vm.
	User *entity.User `json:"caller"`

	// RevisionRef is the revision of the contract that is being called.
	// If RevisionRef is nil but ContractRef is not nil, RevisionRef is set to ContractRef.LastRevision.
	RevisionRef *entity.Revision `json:"revision"`

	// CustomMaxFuel is the maximum fuel that can be used to call the vm.
	CustomMaxFuel *entity.Fuel `json:"custom_max_fuel"`

	// VmOperation is the operation that is being called.
	VmOperation entity.VmOperation `json:"operation"`

	// IgnoreRefuel is true if the remaining fuel must not be refueled.
	IgnoreRefuel bool `json:"ignore_refuel"`

	// IgnoreEngineState ignores the engine state and executes the call anyway.
	IgnoreEngineState bool `json:"ignore_engine_state"`
}

VmCall rappresents a request from an user to call a contract.

func NewVmCall

func NewVmCall() *VmCall

NewVmCall creates a new contract call.

func NewVmCallWithConfig

func NewVmCallWithConfig(opt VmCallOpt) *VmCall

NewVmCallWithConfig creates a new contract call with the given config.

func (*VmCall) Caller

func (c *VmCall) Caller() *entity.User

Caller returns the caller of the contract.

func (VmCall) Contract

func (c VmCall) Contract() *entity.Contract

Contract returns the contract that is being called.

func (*VmCall) Fuel added in v0.0.7

func (c *VmCall) Fuel() entity.Fuel

Fuel returns the fuel to perform the choosen operation.

func (*VmCall) MaxFuel

func (c *VmCall) MaxFuel() entity.Fuel

MaxFuel returns the maximum fuel that can be used to call the contract.

func (*VmCall) Operation

func (c *VmCall) Operation() entity.VmOperation

Operation returns the operation type that is being called.

func (*VmCall) Revision

func (c *VmCall) Revision() *entity.Revision

Revision is the revision of the contract that is being called.

func (*VmCall) WithEngineState added in v0.0.7

func (c *VmCall) WithEngineState() bool

WithEngineState returns true if the engine state should not be ignored.

func (*VmCall) WithRefuel

func (c *VmCall) WithRefuel() bool

WithRefuel returns true if is necessary to refuel remaining fuel after Call ends.

type VmCallOpt

type VmCallOpt struct {
	ContractRef       *entity.Contract
	User              *entity.User
	RevisionRef       *entity.Revision
	CustomMaxFuel     *entity.Fuel
	VmOperation       entity.VmOperation
	IgnoreRefuel      bool
	IgnoreEngineState bool
}

VmCallOpt is the options for the contract call constructor

type VmCallable

type VmCallable interface {
	// Caller returns the caller of the contract.
	// Can be nil if the Caller is not defined.
	Caller() *entity.User
	// Contract returns the contract that is being called.
	// Can be nil if the Contract is not defined.
	Contract() *entity.Contract
	// Fuel returns only the fuel to perform the choosen operation.
	Fuel() entity.Fuel
	// MaxFuel returns the maximum fuel that the caller can use.
	MaxFuel() entity.Fuel
	// Operation returns the operation that is being called.
	Operation() entity.VmOperation
	// Revision is the revision of the contract that is being called.
	// Can be nil if the Revision is not defined.
	Revision() *entity.Revision
	// WithEngineState returns true if the engine state should not be ignored.
	WithEngineState() bool
	// WithRefuel returns true if is necessary to refuel remaining fuel after Call ends.
	WithRefuel() bool
}

VmCallable is the interface for the MusicGang VM callable operations. Everyone who wants to call the MusicGang VM must implement this interface.

type VmCallableService

VmCallableService defines all callable services of the MusicGang VM.

type VmService

type VmService interface {
	EngineService
	VmCallableService
}

VmService is a general service for of the MusicGang VM.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL