Sword-Smith / Sword

Sword — A financial derivative language for the blockchain
MIT License
29 stars 2 forks source link

Do we need to check if contract is activated? #6

Closed Sword-Smith closed 3 years ago

Sword-Smith commented 3 years ago

Do we need this check in pay, in mint, or any other place? To check if the contract is activated, we should check if the timestamp word in storage is zero? If it is not zero, the contract is considered activated.

Sword-Smith commented 3 years ago

A similar check should disallow activate from being called more than once.

Ulrik-dk commented 3 years ago

I have added the following functions to EvmCompiler.hs.

throwIfNotActivated :: [EvmOpcode]
throwIfNotActivated = [push $ storageAddress CreationTimestamp,
                      SLOAD,
                      ISZERO,
                      JUMPITO "global_throw"]

throwIfActivated :: [EvmOpcode]
throwIfActivated = [push $ storageAddress CreationTimestamp,
                   SLOAD,
                   JUMPITO "global_throw"]

One example of a usecase is pay:

pay :: Compiler [EvmOpcode]
pay = do
    memExpCode <- concatMap executeMemExp <$> reader getMemExps
    transferCallCode <- executeTransferCalls
    return $
        [ JUMPDESTFROM "pay_method" ]
        ++ throwIfNotActivated
        ++ memExpCode
        ++ transferCallCode
        ++ emitEvent "Paid"
        ++ [ STOP ]

I have done similarly for activate and mint.

The commit: https://github.com/Sword-Smith/Daggerc2.0/commit/9da4173ba9d87556c69372f87fe84e7a90bcf226

Sword-Smith commented 3 years ago

Great. Did you also add throwIfActivated to the activate method?

Sword-Smith commented 3 years ago

We would also like tests of this.

Ulrik-dk commented 3 years ago

Great. Did you also add throwIfActivated to the activate method?

Yes

activateABI :: Compiler [EvmOpcode]
activateABI = do
  m <- mint
  return $
    [JUMPDESTFROM "activate_method"]
    ++ throwIfActivated
    -- start any timers
    ++ saveTimestampToStorage
    -- transferFrom and mint
    ++ emitEvent "Activated"
    -- mintABI methods jumps in here.
    ++ [JUMPDESTFROM "mint_method"]
    ++ throwIfNotActivated
    ++ m
    -- finalize
    ++ [ STOP ]

We would also like tests of this.

Alright, coming up.

Ulrik-dk commented 3 years ago

We would like tests of this

Conceivably incomplete: https://github.com/einar-io/geth_tools/commit/e8351666d1056438d1b0eaf1dd93a3251f8f9576

Sword-Smith commented 3 years ago

Seems complete