Where are smart contracts stored?


#1

Hey guys,

I was reading the whitepaper and I came across a question.

I am confused that the place where the smart contracts stay.

Is it like the ethereum and stored in the block?

(Then my question would be, in the whitepaper, ‘the block contents’ part, I didt see anything about it. Why?)

or

Is it stored in the state channel?

So, currently my thought is that the smart contracts are stored in the blockchain, and any body can call them.

It is like “drag” the smart contract into the state channel and execute it by the 2 nodes that needed.

so, overall, the smart contracts are stored globally, executed locally, and then the results are uploaded and stored globally. Am I right?


#2

Hey @waynee,

Sorry for the late response.

Your question has been forwarded to our dev team and they’ll get back to you as soon as possible.

Best regards,
The AE Team


#3

Hi, @waynee

That’s a great question, indeed. It might not be well described in the whitepaper so let me clarify: we have two different types of smart contracts.

  1. Public smart contracts (we call them just smart contracts) - those are part of the block chain. They are created and called with transactions, once those are included in blocks. Those smart channels are persisted on-chain and their execution and content are visible to all.

  2. Smart contracts in state channels - those are part of the state of the state channel. Once both parties agree upon adding a new smart contract to a channel - it becomes part of its state. They could refer to a contract that already exists on-chain or rather use their own private smart contract. Situation is similar to contract calls - if both parties agree for a call - the smart contract’s call is executed by both participants locally and updates the state channel’s internal state. If one party decides not to cooperate - the other could unilaterally force a smart contract’s progress on-chain. This would require the contract to be posted on-chain and the call being executed by a miner (in a similar way of 1.). This would expose some of the smart contract’s internal state on-chain.

One big difference between those two would be that there is no gas cost for state channel’s smart contracts (unless one has to force progress on-chain…)

Regards,
Dimitar Ivanov


#4

Thanks for the explanation @Dimitar.Ivanov. I have a somewhat related question: is there an API function to retrieve the smart contract code from the chain or from a state channel (that the node is part of)? Or otherwise, how are clients supposed to verify that a deployed contract is really the same as the one whose source code one might have been shown?


#5

Hi @databu,
The logic is the other way around: provided the code, one can inspect it and decide if one likes it or not. Then a participant in a channel can compile it locally to ensure that what one had been offered is indeed the same contract - if the bytecode of the offered contract is the same as the expected one - it is the same contract.
Contract compilation is deterministic so it is really easy to prove if someone is being honest or not.


#6

Yes I’m aware of this, I should have been more precise: how do I get the byte code of a contract from the chain/channel?

Also how is one supposed to handle the case in this model where the contract was compiled with an earlier compiler version?


#7

Getting the code from the chain is pretty straighforward: there is an HTTP endpoint for this - /contracts/{pubkey}/code. This will get you the code for a contract that is deployed on-chain.
Off-chain it is not that pipelined as this is not part of the protocol itself. Users are expected to have a wallet that is keeping the off-chain state for them and provides means of inspecting it (hint: we have a team working on this :wink: )

The version is inside the contract itself so it is known which compiler is to be used to get the correct bytecode.


#8

I wanted to test this but currently have problems deploying a smart contract, see also my last post here: Æpps update: Smart Contract Development


#9

I will ping the guys developing https://contracts.aepps.com to check your problem in the other thread