ratelang / ratel

Experimental / research project
3 stars 0 forks source link

Notes & Updates #1

Open sbellem opened 4 years ago

sbellem commented 4 years ago

Issue to post notes and updates.

To post something, write it as a comment.

sbellem commented 4 years ago

NOTES - ratelang

class RatelCompiler:
    # ...
    def compile(
        self,
        contract_source,
        *,
        vyper_output_formats=None,
        vyper_interface_codes=None,
        evm_version=DEFAULT_EVM_VERSION,
        mpc_output_formats=None
    ):
        """Compiles the given contract source code."""
        if mpc_output_formats:
            raise NotImplementedError
        else:
            mpc_output_formats = ("src_code",)
        vyper_code, mpc_code = self._extract_codes(contract_source)
        vyper_output = vyper_compiler.compile_code(
            vyper_code,
            output_formats=vyper_output_formats,
            interface_codes=vyper_interface_codes,
            evm_version=evm_version,
        )
        mpc_output = {"src_code": self._mpc_code}
        return {"vyper": vyper_output, "mpc": mpc_output}
    ##########################################################################
    #
    # MPC program
    #
    # IDEA: will come from hbmpc aka ratel contract.
    async def _prog(ctx, *, field_element):
        logging.info(f"[{ctx.myid}] Running MPC network")
        msg_share = ctx.Share(field_element)
        opened_value = await msg_share.open()
        opened_value_bytes = opened_value.value.to_bytes(32, "big")
        logging.info(f"opened_value in bytes: {opened_value_bytes}")
        msg = opened_value_bytes.decode().strip("\x00")
        return msg
    ##########################################################################

    asyncio.run(
        main(
            "sid",
            myid,
            host=host,
            mpc_port=mpc_port,
            peers=peers,
            # node_communicator=node_communicator,
            w3=w3,
            contract_context=contract_context,
            db=db,
            http_context={"host": host, "port": http_port},
            preprocessor_class=PreProcessor,
            httpserver_class=HTTPServer,
            mpcprogrunner_class=MPCProgRunner,
            prog=_prog,
        )
    )

could become

    ##########################################################################
    #
    # MPC program
    #
    # IDEA: will come from hbmpc aka ratel contract.
    ratel_compiler = RatelCompiler()
    out = ratel_compiler.compile(
        mpc_contract_code, vyper_output_formats=["abi", "bytecode"],
    )
    vyper_source = ratel_compiler._vyper_code
    # get vyper contract context ...
    # ...
    mpc_output = out["mpc"]
    mpc_src_code = mpc_output["src_code"]
    exec(mpc_src_code, globals())
    ##########################################################################

    asyncio.run(
        main(
            "sid",
            myid,
            host=host,
            mpc_port=mpc_port,
            peers=peers,
            # node_communicator=node_communicator,
            w3=w3,
            contract_context=contract_context,
            db=db,
            http_context={"host": host, "port": http_port},
            preprocessor_class=PreProcessor,
            httpserver_class=HTTPServer,
            mpcprogrunner_class=MPCProgRunner,
            prog=_prog,
        )
    )