Example #1
0
    def Storage_Delete(self, engine):

        context = engine.EvaluationStack.Pop().GetInterface()

        if not self.CheckStorageContext(context):
            return False

        key = engine.EvaluationStack.Pop().GetByteArray()

        storage_key = StorageKey(script_hash=context.ScriptHash, key=key)

        keystr = key

        if len(key) == 20:
            keystr = Crypto.ToAddress(UInt160(data=key))

        dispatch_smart_contract_event(SmartContractEvent.STORAGE_DELETE,
                                      keystr,
                                      context.ScriptHash,
                                      Blockchain.Default().Height,
                                      engine.ScriptContainer.Hash,
                                      test_mode=engine.testMode)

        self._storages.Remove(storage_key.GetHashCodeBytes())

        return True
Example #2
0
    def Runtime_Log(self, engine):
        message = engine.EvaluationStack.Pop().GetByteArray()

        hash = UInt160(data=engine.CurrentContext.ScriptHash())

        # Build and emit smart contract event
        dispatch_smart_contract_event(SmartContractEvent.RUNTIME_LOG,
                                      message,
                                      hash,
                                      Blockchain.Default().Height,
                                      engine.ScriptContainer.Hash,
                                      test_mode=engine.testMode)

        return True
Example #3
0
    def Storage_Get(self, engine):

        context = None
        try:
            item = engine.EvaluationStack.Pop()
            context = item.GetInterface()
            shash = context.ScriptHash
        except Exception as e:
            logger.error("could not get storage context %s " % e)
            return False

        if not self.CheckStorageContext(context):
            return False

        key = engine.EvaluationStack.Pop().GetByteArray()
        storage_key = StorageKey(script_hash=context.ScriptHash, key=key)
        item = self._storages.TryGet(storage_key.GetHashCodeBytes())

        keystr = key

        valStr = bytearray(0)

        if item is not None:
            valStr = bytearray(item.Value)

        if len(key) == 20:
            keystr = Crypto.ToAddress(UInt160(data=key))

            try:
                valStr = int.from_bytes(valStr, 'little')
            except Exception as e:
                pass

        if item is not None:

            engine.EvaluationStack.PushT(bytearray(item.Value))

        else:
            engine.EvaluationStack.PushT(bytearray([0]))

        dispatch_smart_contract_event(SmartContractEvent.STORAGE_GET,
                                      '%s -> %s' % (keystr, valStr),
                                      context.ScriptHash,
                                      Blockchain.Default().Height,
                                      engine.ScriptContainer.Hash,
                                      test_mode=engine.testMode)

        return True
Example #4
0
    def ExecutionCompleted(self, engine, success, error=None):

        height = Blockchain.Default().Height
        tx_hash = engine.ScriptContainer.Hash

        entry_script = None
        try:
            # get the first script that was executed
            # this is usually the script that sets up the script to be executed
            entry_script = UInt160(data=engine.ExecutedScriptHashes[0])

            # ExecutedScriptHashes[1] will usually be the first contract executed
            if len(engine.ExecutedScriptHashes) > 1:
                entry_script = UInt160(data=engine.ExecutedScriptHashes[1])
        except Exception as e:
            logger.error("Could not get entry script: %s " % e)

        payload = []
        for item in engine.EvaluationStack.Items:
            payload_item = stack_item_to_py(item)
            payload.append(payload_item)

        if success:

            # dispatch all notify events, along with the success of the contract execution
            for notify_event_args in self.notifications:
                dispatch_smart_contract_event(SmartContractEvent.RUNTIME_NOTIFY, notify_event_args.State,
                                              notify_event_args.ScriptHash, height, tx_hash,
                                              success, engine.testMode)

            if engine.Trigger == Application:
                dispatch_smart_contract_event(SmartContractEvent.EXECUTION_SUCCESS, payload, entry_script,
                                              height, tx_hash, success, engine.testMode)
            else:
                dispatch_smart_contract_event(SmartContractEvent.VERIFICATION_SUCCESS, payload, entry_script,
                                              height, tx_hash, success, engine.testMode)

        else:
            if engine.Trigger == Application:

                dispatch_smart_contract_event(SmartContractEvent.EXECUTION_FAIL, [payload, error, engine._VMState],
                                              entry_script, height, tx_hash, success, engine.testMode)
            else:
                dispatch_smart_contract_event(SmartContractEvent.VERIFICATION_FAIL, [payload, error, engine._VMState],
                                              entry_script, height, tx_hash, success, engine.testMode)
Example #5
0
    def Storage_Put(self, engine):

        context = None
        try:

            context = engine.EvaluationStack.Pop().GetInterface()
        except Exception as e:
            logger.error("Storage Context Not found on stack")
            return False

        if not self.CheckStorageContext(context):
            return False

        key = engine.EvaluationStack.Pop().GetByteArray()
        if len(key) > 1024:
            return False

        value = engine.EvaluationStack.Pop().GetByteArray()

        new_item = StorageItem(value=value)
        storage_key = StorageKey(script_hash=context.ScriptHash, key=key)
        item = self._storages.GetOrAdd(storage_key.GetHashCodeBytes(),
                                       new_item)

        keystr = key
        valStr = bytearray(item.Value)

        if len(key) == 20:
            keystr = Crypto.ToAddress(UInt160(data=key))

            try:
                valStr = int.from_bytes(valStr, 'little')
            except Exception as e:
                pass

        dispatch_smart_contract_event(SmartContractEvent.STORAGE_PUT,
                                      '%s -> %s' % (keystr, valStr),
                                      context.ScriptHash,
                                      Blockchain.Default().Height,
                                      engine.ScriptContainer.Hash,
                                      test_mode=engine.testMode)

        return True