Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Executor and typechecker of a contract in Morley language.
Synopsis
- originateContract :: ("dbPath" :! FilePath) -> ("tcOpts" :? TypeCheckOptions) -> ("originator" :? ImplicitAddress) -> ("alias" :? ContractAlias) -> ("delegate" :? KeyHash) -> ("csod" :! ContractSimpleOriginationData Contract) -> ("verbose" :? Bool) -> IO ContractAddress
- runContract :: CommonRunOptions -> ContractSimpleOriginationData Contract -> TxData -> IO SomeStorage
- transfer :: CommonRunOptions -> SomeAddressOrAlias -> TxData -> IO ()
- runCode :: RunCodeParameters cp epArg st -> Either (InterpretError Void) ([Operation], Value st)
- runView :: CommonRunOptions -> ContractSpecification (ContractSimpleOriginationData Contract) -> ViewName -> SomeAddressOrAlias -> TxParam -> IO SomeValue
- data RunCodeParameters cp epArg st = RunCodeParameters {
- rcScript :: Contract cp st
- rcStorage :: Value st
- rcInput :: Value epArg
- rcEntryPoint :: EntrypointCallT cp epArg
- rcAmount :: Mutez
- rcBalance :: Mutez
- rcChainId :: ChainId
- rcNow :: Timestamp
- rcLevel :: Natural
- rcMinBlockTime :: Natural
- rcSource :: L1Address
- rcSender :: L1Address
- rcKnownContracts :: Map ContractAddress ContractState
- rcSelf :: Maybe ContractAddress
- rcDelegate :: Maybe KeyHash
- rcVotingPowers :: VotingPowers
- runCodeParameters :: Contract cp st -> Value st -> EntrypointCallT cp epArg -> Value epArg -> RunCodeParameters cp epArg st
- resolveRunCodeBigMaps :: SingI t => BigMapFinder -> Value -> Either TcError (Value t)
- mkBigMapFinder :: ExecutorM BigMapFinder
- data CommonRunOptions = CommonRunOptions {}
- data ContractSpecification a
- data ContractSimpleOriginationData a = ContractSimpleOriginationData {
- csodContract :: a
- csodStorage :: Value
- csodBalance :: Mutez
- parseContract :: MichelsonSource -> Text -> Either ParserException (Contract' ParsedOp)
- parseExpandContract :: MichelsonSource -> Text -> Either ParserException Contract
- readAndParseContract :: Maybe FilePath -> IO (Contract' ParsedOp)
- prepareContract :: Maybe FilePath -> IO Contract
- data ContractState = forall cp st.(ParameterScope cp, StorageScope st) => ContractState {
- csBalance :: Mutez
- csContract :: Contract cp st
- csStorage :: Value st
- csDelegate :: Maybe KeyHash
- data VotingPowers
- mkVotingPowers :: [(KeyHash, Natural)] -> VotingPowers
- mkVotingPowersFromMap :: Map KeyHash Natural -> VotingPowers
- data TxData = TxData {}
- data TxParam where
- TxTypedParam :: forall t. ParameterScope t => Value t -> TxParam
- TxUntypedParam :: Value -> TxParam
- data ExecutorOp
- data ExecutorRes = ExecutorRes {}
- erGState :: Lens' ExecutorRes GState
- erUpdates :: Lens' ExecutorRes [GStateUpdate]
- erInterpretResults :: Lens' ExecutorRes [(Address, SomeInterpretResult)]
- erRemainingSteps :: Lens' ExecutorRes RemainingSteps
- data ExecutorError' a = ExecutorError {
- eeCallStack :: [ExecutorOp]
- eeError :: ExecutorErrorPrim a
- data ExecutorErrorPrim a
- = EEUnknownContract a
- | EEInterpreterFailed a (InterpretError Void)
- | EEViewLookupError a ViewLookupError
- | EEViewArgTcError a TcError
- | EEUnknownAddressAlias SomeAlias
- | EEUnknownL1AddressAlias Text
- | EEAmbiguousAlias Text ImplicitAddress ContractAddress
- | EEUnknownSender a
- | EEUnknownManager a
- | EENotEnoughFunds a Mutez
- | EEEmptyImplicitContract a
- | EEZeroTransaction a
- | EEFailedToApplyUpdates GStateUpdateError
- | EEIllTypedParameter a TcError
- | EEDeprecatedType TcError
- | EEUnexpectedParameterType a (MismatchError T)
- | EEUnknownEntrypoint EpName
- | EETransactionFromContract a Mutez
- | EEWrongParameterType a
- | EEOperationReplay ExecutorOp
- | EEGlobalOperationSourceNotImplicit Address
- | EEGlobalEmitOp
- type ExecutorError = ExecutorError' Address
- type ExecutorM = ReaderT ExecutorEnv (StateT ExecutorState (Except ExecutorError))
- runExecutorM :: Timestamp -> Natural -> Natural -> RemainingSteps -> TypeCheckOptions -> GState -> ExecutorM a -> Either ExecutorError (ExecutorRes, a)
- runExecutorMWithDB :: CommonRunOptions -> ExecutorM a -> IO (ExecutorRes, a)
- executeGlobalOperations :: [ExecutorOp] -> ExecutorM [EmitOperation]
- executeGlobalOrigination :: OriginationOperation -> ExecutorM ContractAddress
- executeOrigination :: ("isGlobalOp" :! Bool) -> OriginationOperation -> ExecutorM ContractAddress
- executeTransfer :: ("isGlobalOp" :! Bool) -> TransferOperation -> ExecutorM [ExecutorOp]
- data ExecutorState = ExecutorState {}
- esGState :: Lens' ExecutorState GState
- esRemainingSteps :: Lens' ExecutorState RemainingSteps
- esSourceAddress :: Lens' ExecutorState (Maybe L1Address)
- esLog :: Lens' ExecutorState ExecutorLog
- esOperationHash :: Lens' ExecutorState OperationHash
- esPrevCounters :: Lens' ExecutorState (HashSet GlobalCounter)
- data ExecutorLog = ExecutorLog {}
- data SomeInterpretResult = forall st.SomeInterpretResult {}
- elInterpreterResults :: Lens' ExecutorLog [(Address, SomeInterpretResult)]
- elUpdates :: Lens' ExecutorLog [GStateUpdate]
High level interface for end user
originateContract :: ("dbPath" :! FilePath) -> ("tcOpts" :? TypeCheckOptions) -> ("originator" :? ImplicitAddress) -> ("alias" :? ContractAlias) -> ("delegate" :? KeyHash) -> ("csod" :! ContractSimpleOriginationData Contract) -> ("verbose" :? Bool) -> IO ContractAddress Source #
Originate a contract. Returns the address of the originated contract.
runContract :: CommonRunOptions -> ContractSimpleOriginationData Contract -> TxData -> IO SomeStorage Source #
Run a contract. The contract is originated first (if it's not already) and then we pretend that we send a transaction to it.
transfer :: CommonRunOptions -> SomeAddressOrAlias -> TxData -> IO () Source #
Send a transaction to given address with given parameters.
runCode :: RunCodeParameters cp epArg st -> Either (InterpretError Void) ([Operation], Value st) Source #
Emulate run_code
RPC endpoint to an extent. Unlike runContract
, runs
the contract through the emulator directly, without doing any operations.
This includes not doing the origination operation, and not applying the
operations produced by the transfer.
runView :: CommonRunOptions -> ContractSpecification (ContractSimpleOriginationData Contract) -> ViewName -> SomeAddressOrAlias -> TxParam -> IO SomeValue Source #
Run a contract view. The contract is originated first (if it's not already) and then we pretend that we send a transaction to it.
data RunCodeParameters cp epArg st Source #
Data required for calling runCode
.
RunCodeParameters | |
|
:: Contract cp st | Contract to run |
-> Value st | Contract storage |
-> EntrypointCallT cp epArg | Entrypoint call specification |
-> Value epArg | Entrypoint argument |
-> RunCodeParameters cp epArg st |
Construct RunCodeParameters
with some reasonable defaults.
Prepare untyped storage and parameter with resolveRunCodeBigMaps
.
Use mkEntrypointCall
or mkDefEntrypointCall
to construct the entrypoint
call specification.
resolveRunCodeBigMaps :: SingI t => BigMapFinder -> Value -> Either TcError (Value t) Source #
Given an untyped value, possibly containing big_map
ids, typecheck it,
resolving ids to the corresponding big_map
s.
BigMapFinder
can be constructed using mkBigMapFinder
.
mkBigMapFinder :: ExecutorM BigMapFinder Source #
Construct BigMapFinder
using the current executor context.
data CommonRunOptions Source #
Instances
Default CommonRunOptions Source # | |
Defined in Morley.Michelson.Runtime def :: CommonRunOptions # |
data ContractSpecification a Source #
Instances
data ContractSimpleOriginationData a Source #
Instances
Other helpers
parseContract :: MichelsonSource -> Text -> Either ParserException (Contract' ParsedOp) Source #
Parse a contract from Text
.
parseExpandContract :: MichelsonSource -> Text -> Either ParserException Contract Source #
Parse a contract from Text
and expand macros.
prepareContract :: Maybe FilePath -> IO Contract Source #
Read a contract using readAndParseContract
, expand and
flatten. The contract is not type checked.
Re-exports
data ContractState Source #
State of a contract with code.
forall cp st.(ParameterScope cp, StorageScope st) => ContractState | |
|
Instances
FromJSON ContractState Source # | |
Defined in Morley.Michelson.Runtime.GState parseJSON :: Value -> Parser ContractState # parseJSONList :: Value -> Parser [ContractState] # | |
ToJSON ContractState Source # | |
Defined in Morley.Michelson.Runtime.GState toJSON :: ContractState -> Value # toEncoding :: ContractState -> Encoding # toJSONList :: [ContractState] -> Value # toEncodingList :: [ContractState] -> Encoding # | |
Show ContractState Source # | |
Defined in Morley.Michelson.Runtime.GState showsPrec :: Int -> ContractState -> ShowS # show :: ContractState -> String # showList :: [ContractState] -> ShowS # | |
Eq ContractState Source # | |
Defined in Morley.Michelson.Runtime.GState (==) :: ContractState -> ContractState -> Bool # (/=) :: ContractState -> ContractState -> Bool # | |
Buildable ContractState Source # | |
Defined in Morley.Michelson.Runtime.GState build :: ContractState -> Doc buildList :: [ContractState] -> Doc |
data VotingPowers Source #
Distribution of voting power among the contracts.
Voting power reflects the ability of bakers to accept, deny or pass new proposals for protocol updates. I.e. each baker has its vote weight.
This datatype definition may change in future, so its internals are not exported.
Instances
FromJSON VotingPowers Source # | |
Defined in Morley.Michelson.Runtime.GState parseJSON :: Value -> Parser VotingPowers # parseJSONList :: Value -> Parser [VotingPowers] # | |
ToJSON VotingPowers Source # | |
Defined in Morley.Michelson.Runtime.GState toJSON :: VotingPowers -> Value # toEncoding :: VotingPowers -> Encoding # toJSONList :: [VotingPowers] -> Value # toEncodingList :: [VotingPowers] -> Encoding # | |
Show VotingPowers Source # | |
Defined in Morley.Michelson.Runtime.GState showsPrec :: Int -> VotingPowers -> ShowS # show :: VotingPowers -> String # showList :: [VotingPowers] -> ShowS # | |
Eq VotingPowers Source # | |
Defined in Morley.Michelson.Runtime.GState (==) :: VotingPowers -> VotingPowers -> Bool # (/=) :: VotingPowers -> VotingPowers -> Bool # |
mkVotingPowers :: [(KeyHash, Natural)] -> VotingPowers Source #
Create voting power distribution.
If some key is encountered multiple times, voting power will be summed up.
mkVotingPowersFromMap :: Map KeyHash Natural -> VotingPowers Source #
Create voting power distribution from map.
Data associated with a particular transaction.
TxData | |
|
A parameter associated with a particular transaction.
TxTypedParam :: forall t. ParameterScope t => Value t -> TxParam | |
TxUntypedParam :: Value -> TxParam |
For testing
data ExecutorOp Source #
Operations executed by interpreter.
In our model one Michelson's operation (operation
type in Michelson)
corresponds to 0 or 1 interpreter operation.
Note: Address
is not part of TxData
, because TxData
is
supposed to be provided by the user, while Address
can be
computed by our code.
OriginateOp OriginationOperation | Originate a contract. |
TransferOp TransferOperation | Transfer tokens to the address. |
SetDelegateOp SetDelegateOperation | Set the delegate of a contract. |
EmitOp EmitOperation | Emit contract event. |
Instances
Show ExecutorOp Source # | |
Defined in Morley.Michelson.Runtime showsPrec :: Int -> ExecutorOp -> ShowS # show :: ExecutorOp -> String # showList :: [ExecutorOp] -> ShowS # | |
Buildable ExecutorOp Source # | |
Defined in Morley.Michelson.Runtime build :: ExecutorOp -> Doc buildList :: [ExecutorOp] -> Doc |
data ExecutorRes Source #
Result of a single execution of interpreter.
ExecutorRes | |
|
Instances
Show ExecutorRes Source # | |
Defined in Morley.Michelson.Runtime showsPrec :: Int -> ExecutorRes -> ShowS # show :: ExecutorRes -> String # showList :: [ExecutorRes] -> ShowS # |
data ExecutorError' a Source #
ExecutorErrorPrim
, enriched by the list of operations that succeeded
before the error.
Instances
data ExecutorErrorPrim a Source #
Errors that can happen during contract interpreting.
Type parameter a
determines how contracts will be represented
in these errors, e.g. Address
.
EEUnknownContract a | The interpreted contract hasn't been originated. |
EEInterpreterFailed a (InterpretError Void) | Interpretation of Michelson contract failed. |
EEViewLookupError a ViewLookupError | Error looking up view while trying to call it. |
EEViewArgTcError a TcError | Error type-checking untyped view argument. |
EEUnknownAddressAlias SomeAlias | The given alias isn't associated with any address OR is associated with an address of an unexpected kind (e.g. we expected an implicit address and found a contract address, or vice-versa). |
EEUnknownL1AddressAlias Text | The given alias is not associated with any address. |
EEAmbiguousAlias Text ImplicitAddress ContractAddress | The given alias is ambiguous, i.e. it is associated with both an implicit address and a contract address. |
EEUnknownSender a | Sender address is unknown. |
EEUnknownManager a | Manager address is unknown. |
EENotEnoughFunds a Mutez | Sender doesn't have enough funds. |
EEEmptyImplicitContract a | Sender is an implicit address with the balance of 0. We mimic
|
EEZeroTransaction a | Sending 0tz towards an address. |
EEFailedToApplyUpdates GStateUpdateError | Failed to apply updates to GState. |
EEIllTypedParameter a TcError | Contract parameter is ill-typed. |
EEDeprecatedType TcError | Found deprecated types. |
EEUnexpectedParameterType a (MismatchError T) | Contract parameter is well-typed, but its type does not match the entrypoint's type. |
EEUnknownEntrypoint EpName | Specified entrypoint to run is not found. |
EETransactionFromContract a Mutez | A transaction from an originated contract was attempted as a global operation. |
EEWrongParameterType a | Type of parameter in transfer to an implicit account is not Unit. |
EEOperationReplay ExecutorOp | An attempt to perform the operation duplicated with |
EEGlobalOperationSourceNotImplicit Address | Attempted to initiate global operation from a non-implicit address. |
EEGlobalEmitOp | Trying to run emit operation as a global operation, which should be impossible. |
Instances
type ExecutorError = ExecutorError' Address Source #
type ExecutorM = ReaderT ExecutorEnv (StateT ExecutorState (Except ExecutorError)) Source #
A monad in which contract executor runs.
runExecutorM :: Timestamp -> Natural -> Natural -> RemainingSteps -> TypeCheckOptions -> GState -> ExecutorM a -> Either ExecutorError (ExecutorRes, a) Source #
Run some executor action, returning its result and final executor state in ExecutorRes
.
The action has access to the hash of currently executed global operation, in order to construct
addresses of originated contracts. It is expected that the action uses #isGlobalOp :! True
to specify this hash. Otherwise it is initialized with error
.
runExecutorMWithDB :: CommonRunOptions -> ExecutorM a -> IO (ExecutorRes, a) Source #
Run some executor action, reading state from the DB on disk.
If croDryRun
is False
, the final state is written back to the disk.
If the executor fails with ExecutorError
it will be thrown as an exception.
executeGlobalOperations :: [ExecutorOp] -> ExecutorM [EmitOperation] Source #
Execute a list of global operations, returning a list of generated events.
executeGlobalOrigination :: OriginationOperation -> ExecutorM ContractAddress Source #
Execute a global origination operation.
executeOrigination :: ("isGlobalOp" :! Bool) -> OriginationOperation -> ExecutorM ContractAddress Source #
Execute an origination operation.
executeTransfer :: ("isGlobalOp" :! Bool) -> TransferOperation -> ExecutorM [ExecutorOp] Source #
Execute a transfer operation.
Note: we're handling both XTZ and ticket transfers here to avoid code
duplication. We assume that if an implicit account sends tickets via
TxTypedParam
, it should be interpreted as transfer_ticket
manager
operation, and not a regular transfer.
Note that this only works for TxTypedParam
, as for ticket transfers between
implicit accounts we can't know the exact type of the ticket to transfer if
the value is untyped.
data ExecutorState Source #
Instances
data ExecutorLog Source #
Instances
data SomeInterpretResult Source #
forall st. SomeInterpretResult | |
Instances
Show SomeInterpretResult Source # | |
Defined in Morley.Michelson.Runtime showsPrec :: Int -> SomeInterpretResult -> ShowS # show :: SomeInterpretResult -> String # showList :: [SomeInterpretResult] -> ShowS # |