entity

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: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthSourceLocal  = "local"
	AuthSourceGitHub = "github"
)

Authentication providers.

View Source
const (
	VisibilityPrivate = "private"
	VisibilityPublic  = "public"
)

Visibility consts for the visibility of the contract.

View Source
const (
	FuelInstantActionAmount  = Fuel(50)
	FuelQuickActionAmount    = Fuel(200)
	FuelFastestActionAmount  = Fuel(400)
	FuelFastActionAmount     = Fuel(600)
	FuelMidActionAmount      = Fuel(800)
	FuelSlowActionAmount     = Fuel(1200)
	FuelLongActionAmount     = Fuel(2500)
	FuelExtremeActionAmount  = Fuel(5000)
	FuelAbsoluteActionAmount = Fuel(10000)
)

Fuel*ActionCost rappresents the cost of an action. Greater is the execution time, greater is the cost.

View Source
const (
	AccessTokenExpiration  = 60 * time.Minute           // 1 hour
	RefreshTokenExpiration = 60 * 24 * 15 * time.Minute // 15 days
)
View Source
const (
	// NotDefinedOperationCost is the default fuel used if not specified by the VmCall when
	NotDefinedOperationCost = Fuel(25)
	// StateFulOperationCost is the extra fuel used when the operation is executed in the full state.
	StateFulOperationCost = Fuel(500)
)
View Source
const (
	// EmptyState is the empty state.
	EmptyState = `{}`
)
View Source
const (
	// UserNameInvalidCharacters defines all invalid characters for a user name.
	UserNameInvalidCharacters = "!@#$%^&*()+=[]{}|\\;:'\"<>,/?`~"
)

Variables

View Source
var FuelRefillAmount = Fuel(FuelTankCapacity * 5 / 100)

FuelRefillAmount retruns how much fuel is refilled at a time. It is equivalent to 5% of the capacity of the fuel tank. This is the default value that may be overwritten by the init function.

View Source
var FuelRefillRate = 400 * time.Millisecond

FuelRefillRate returns the rate of fuel refill. This is the default value that may be overwritten by the init function.

View Source
var FuelTankCapacity = 100 * vKFuel

FuelTankCapacity is the maximum capacity of the fuel tank. This is the default value that may be overwritten by the init function.

View Source
var MaxExecutionTime = 10 * time.Second

MaxExecutionTime returns the maximum execution time of an action. This is the default value that may be overwritten by the init function.

Functions

func CanAuthBeDeleted

func CanAuthBeDeleted(auth *Auth) bool

CanAuthBeDeleted returns if the passed authentication source can be deleted.

func IsSourceIDRequired

func IsSourceIDRequired(source string) bool

IsSourceIDRequired returns if the authentication source requires a source ID.

func IsValidOperation

func IsValidOperation(op VmOperation) bool

IsValidOperation returns true if the operation is valid.

func MaxExecutionTimeFromFuel

func MaxExecutionTimeFromFuel(fuel Fuel) time.Duration

MaxExecutionTime returns the maximum execution time of the contract. MaxExecutionTime is based on max fuel compared with fuelAmountTable.

Types

type AppClaims

type AppClaims struct {
	jwt.StandardClaims
	Auth *Auth `json:"auth"`
}

AppClaims is a custom claims type for JWT It contains the information about the user and the standard claims

func NewAppClaims

func NewAppClaims(auth *Auth, expiresAfterMinutes time.Duration) *AppClaims

NewAppClaims creates a new AppClaims

type Auth

type Auth struct {
	ID       int64       `json:"id"`
	UserID   int64       `json:"user_id"`
	Source   string      `json:"source"`
	SourceID null.String `json:"-"`

	AccessToken  null.String `json:"-"`
	RefreshToken null.String `json:"-"`
	Expiry       null.Time   `json:"-"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	User *User `json:"user"`
}

Auth represents a set of OAuth credentials. These are linked to a User so a single user could authenticate through multiple providers.

The authentication system links users by email address, however, some GitHub users don't provide their email publicly so we may not be able to link them by email address.

func (*Auth) AvatarURL

func (a *Auth) AvatarURL(size int) string

AvatarURL returns a URL to the avatar image hosted by the authentication source. Returns an empty string if the authentication source is invalid.

func (*Auth) Validate

func (a *Auth) Validate() error

Validate validates the Auth object and returns an error if it's invalid. This can be used from any method that accepts a Auth object to do basic checks.

type AuthUserOptions

type AuthUserOptions struct {
	AuthCode   *string
	Source     *string
	UserParams *UserParams
}

AuthUserOptions represents the options for a user during fetching auth service.

type Auths

type Auths []*Auth

Auths represents a list of Auth objects.

type Contract

type Contract struct {
	ID          int64      `json:"id"`
	Name        string     `json:"name"`
	Description string     `json:"description"`
	UserID      int64      `json:"user_id"`
	Visibility  Visibility `json:"visibility"`
	MaxFuel     Fuel       `json:"max_fuel"` // The maximum amount of fuel that can be burned from the contract.
	Stateful    bool       `json:"stateful"` // Enables the contract to persist its state during different executions (of same revision).
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`

	// avoid to access this field directly because it can be nil, use the Revision method UnwrapRevision instead.
	LastRevision *Revision `json:"last_revision"`
	User         *User     `json:"user"`
}

Contract represents a contract. The contract is a cloud function that is executed on a server, deployed by users; The contract can have multiple revisions.

func (*Contract) MaxExecutionTime

func (c *Contract) MaxExecutionTime() time.Duration

MaxExecutionTime returns the maximum execution time of the contract. MaxExecutionTime is based on max fuel compared with fuelAmountTable.

func (*Contract) UnwrapRevision

func (c *Contract) UnwrapRevision() (*Revision, error)

UnwrapRevision returns the last revision of the contract if it exists, otherwise error is returned.

func (*Contract) Validate

func (c *Contract) Validate() error

Validate validates the contract.

type Contracts

type Contracts []*Contract

Contracts represents a list of contracts.

type Fuel

type Fuel uint64

Fuel is a virtual unit of measure for the power consuption of the MusicGang VM. Like the real world, the fuel is limited, so the VM will stop if the fuel usage reaches the max capacity.

func FuelAmount

func FuelAmount(execution time.Duration) Fuel

FuelAmount returns the cost of an action based only on the execution time.

func FuelFromCustomFuel added in v0.0.7

func FuelFromCustomFuel(customFuel Fuel) Fuel

func ParseFuel added in v0.0.7

func ParseFuel(s string) (Fuel, error)

ParseFuel accepts a string and returns a Fuel unit measurement, like the vFuel, vKFuel, vMFuel, vGFuel, vTFuel. for an example, 10 vKFuel is equal to 10 * 1024 = 10240.

func VmOperationCost

func VmOperationCost(op VmOperation) Fuel

VmOperationCost returns the cost of the operation.

func (Fuel) MarshalBinary

func (f Fuel) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

type FuelStat

type FuelStat struct {
	FuelCapacity    Fuel      `json:"fuel_capacity"`
	FuelUsed        Fuel      `json:"fuel_used"`
	LastRefuelAmout Fuel      `json:"last_refuel_amount"`
	LastRefuelAt    time.Time `json:"last_refuel_at"`
}

FuelStats represents the statistics of the fuel tank.

type Revision

type Revision struct {
	ID           int64           `json:"id"`
	CreatedAt    time.Time       `json:"created_at"`
	Rev          RevisionNumber  `json:"rev"`
	Version      RevisionVersion `json:"version"`
	ContractID   int64           `json:"contract_id"`
	Notes        string          `json:"notes"`
	CompiledCode []byte          `json:"-"`
	MaxFuel      Fuel            `json:"max_fuel"`

	Contract *Contract `json:"contract"`
}

Revision represents a revision of a contract. Each revision is a snapshot of the contract state.

func (*Revision) Validate

func (r *Revision) Validate() error

Validate validates the revision.

type RevisionNumber

type RevisionNumber uint

RevisionNumber is the revision number of the entity.

type RevisionVersion

type RevisionVersion string

RevisionVersion indicates the version of revision management system.

const (
	AnchorageVersion RevisionVersion = "Anchorage"

	CurrentRevisionVersion RevisionVersion = AnchorageVersion
)

This is a list of all revisions versions.

type Revisions

type Revisions []*Revision

Revisions is a slice of Revision.

type State

type State struct {
	ID         int64      `json:"id"`
	RevisionID int64      `json:"revision_id"`
	Value      StateValue `json:"value"`
	UserID     int64      `json:"user_id"`
	CreatedAt  time.Time  `json:"created_at"`
	UpdatedAt  time.Time  `json:"updated_at"`

	User     *User     `json:"user"`
	Revision *Revision `json:"revision"`
}

State represents the state of the contract at a given revision for specific user. State enables contract to persist its state during different executions. The state should not be shared between users and different revisions.

func (State) Validate added in v0.0.7

func (s State) Validate() error

Validate validates the state.

type StateValue added in v0.0.7

type StateValue map[string]any

StateValue represents the state value.

func NewStateFromBytes added in v0.0.7

func NewStateFromBytes(b []byte) (StateValue, error)

NewStateFromBytes creates a state from bytes. If b is nil, it returns an empty state.

func (*StateValue) Scan added in v0.0.7

func (s *StateValue) Scan(src any) error

Scan implements sql.Scanner

func (StateValue) Value added in v0.0.7

func (s StateValue) Value() (driver.Value, error)

Value implements driver.Valuer

type TokenPair

type TokenPair struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	TokenType    string `json:"token_type"`
	Expiry       int64  `json:"expires_in"`
}

TokenPair is a struct that contains the tokens and the expiration time

type User

type User struct {
	ID        int64       `json:"id"`
	CreatedAt time.Time   `json:"created_at"`
	UpdatedAt time.Time   `json:"updated_at"`
	Email     null.String `json:"email"`
	Name      string      `json:"name"`
	Password  null.String `json:"-"`

	// List of associated OAuth authentication objects.
	// Currently only GitHub is supported so there should only be a maximum of one.
	Auths []*Auth `json:"auths"`
}

User represents a user in the system. Users are typically created via OAuth using the AuthService but users can also be created directly for testing.

func (*User) AvatarURL

func (u *User) AvatarURL(size int) string

AvatarURL returns a URL to the avatar image for the user. This loops over all auth providers to find the first available avatar. Currently only GitHub is supported. Returns blank string if no avatar URL available.

func (*User) CanCreateContract

func (u *User) CanCreateContract() bool

CanCreateContract returns if user is authorized to created contracts.

func (*User) Validate

func (u *User) Validate() error

Validate returns an error if the user contains invalid fields. This only performs basic validation.

type UserParams

type UserParams struct {
	Email    *string
	Password *string
}

UserParams represents the parameters for a user authentication.

type Users

type Users []*User

Users is a list of users.

type Visibility

type Visibility string

Visibility defines the visibility of a contract.

func (Visibility) Validate

func (v Visibility) Validate() error

Validate validates the visibility.

type VmOperation

type VmOperation string

VmOperation is a type for the operations of the MusicGang VM.

const (
	VmOperationCreateContract  VmOperation = "create-contract"
	VmOperationExecuteContract VmOperation = "execute-contract"
	VmOperationUpdateContract  VmOperation = "update-contract"
	VmOperationDeleteContract  VmOperation = "delete-contract"

	VmOperationMakeContractRevision VmOperation = "make-contract-revision"

	VmOperationCreateUser VmOperation = "create-user"
	VmOperationUpdateUser VmOperation = "update-user"
	VmOperationDeleteUser VmOperation = "delete-user"

	VmOperationAuthenticate VmOperation = "authenticate"
	VmOperationCreateAuth   VmOperation = "create-auth"
	VmOperationDeleteAuth   VmOperation = "delete-auth"

	VmOperationVmStats VmOperation = "vm-stats"

	VmOperationGeneric VmOperation = "generic"
)

Defines the operations of the MusicGang VM.

type VmState added in v0.0.7

type VmState int32

VmState represents the state of the MusicGangVM.

const (
	// StateRunning is the state of the MusicGangVM when it is running.
	StateInitializing VmState = iota
	StateRunning
	StatePaused
	StateStopped
)

func (VmState) String added in v0.0.7

func (s VmState) String() string

String returns a string representation of the VmState.

Jump to

Keyboard shortcuts

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