Formal Formal Specif Specification ication and Verific and - - PowerPoint PPT Presentation

formal formal specif specification ication and verific
SMART_READER_LITE
LIVE PREVIEW

Formal Formal Specif Specification ication and Verific and - - PowerPoint PPT Presentation

Presented at FMBC 2020 Formal Formal Specif Specification ication and Verific and Verification ation of Solid of Solidity ity Contracts Contracts with E with Events vents kos Hajdu 1 , Dejan Jovanovi 2 , Gabriela Ciocarlie 2 1


slide-1
SLIDE 1

Formal Formal Specif Specification ication and Verific and Verification ation

  • f Solid
  • f Solidity

ity Contracts Contracts with E with Events vents

Ákos Hajdu1, Dejan Jovanović2, Gabriela Ciocarlie2

1Budapest University of Technology and Economics 2SRI International

Presented at FMBC 2020

slide-2
SLIDE 2

contract Token { } mapping(address=>uint) balances; uint total;

2

Solidity Smart Contracts and Events

event initialized(address from, uint amount); event transferred(address from, address to, uint amount); constructor(uint _total) public { balances[msg.sender] = total = _total; } emit initialized(msg.sender, total); function transfer(address to, uint amount) public { require(balances[msg.sender] >= amount && msg.sender != to); balances[msg.sender] -= amount; balances[to] += amount; } emit transferred(msg.sender, to, amount);

slide-3
SLIDE 3

3

  • Stored in blockchain logs
  • Contract communicates with user

– Important state changes

  • Abstract view of execution

– Relevant aspect to each user

Solidity Events

E1(x) E2(x,y) E1(x) E1(x) E2(x,y) E2(x,y)

slide-4
SLIDE 4

4

Motivation

Can we trust (rely on) the emitted events?

Do we always emit if balances change? Was there a change when emitted? Is the amount correct? Not really…

slide-5
SLIDE 5

5

  • What state variable(s) do events track?

– Emit event iff there was a change

Formal Specification of Events

contract Token { mapping(address=>uint) balances; uint total; event initialized(address from, uint amount); event transferred(address from, address to, uint amount); } /// @notice tracks-changes-in balances /// @notice tracks-changes-in total /// @notice tracks-changes-in balances

slide-6
SLIDE 6

6

  • What events can functions emit?

– Similar to Java throws

Formal Specification of Events

contract Token { constructor(uint _total) public { ... } function transfer(address to, uint amount) public { ... } } /// @notice emits initialized /// @notice emits transferred

slide-7
SLIDE 7

7

  • What are the conditions before and at the emit?

Formal Specification of Events

contract Token { event initialized(address from, uint amount); event transferred(address from, address to, uint amount); } /// @notice precondition balances[from] == 0 /// @notice postcondition balances[from] == amount /// @notice postcondition total == amount /// @notice precondition balances[from] >= amount /// @notice postcondition balances[from] == before(balances[from]) - amount /// @notice postcondition balances[to] == before(balances[to]) + amount

slide-8
SLIDE 8

DEMO

8

slide-9
SLIDE 9

9

  • Where to check if an event has been emitted?

– Cannot check immediately (modification in multiple steps)

  • Where to check preconditions?

– What does “before the change” exactly mean?

Formal Verification

slide-10
SLIDE 10

10

Checkpoints

function transfer(address to, uint amount) public { require(balances[msg.sender] >= amount && msg.sender != to); ... balances[msg.sender] -= amount; balances[to] += amount; ... emit transferred(msg.sender, to, amount); ... } Before checkpoint

  • First time variable changes
  • Save state (for precondition)

After checkpoint

  • Static barrier
  • Latest point to emit
  • E.g., function end

Emit

  • Check pre/post
  • Clear before/after checkpoint
slide-11
SLIDE 11

11

Overview

solc-verify

.bpl

∆vΣ→φ μ□β→λ

.sol

Extended compiler Boogie verifier SMT solvers

Solidity contract with specification Boogie program w/ instrumentation Back-annotation

Verification conditions Proofs

github.com/SRI-CSL/solidity

slide-12
SLIDE 12

12

Instrumentation

mapping(address=>uint) balances; /// @notice emits transferred function transfer(address to, uint amount) public { require(balances[msg.sender] >= amount && msg.sender != to); balances[msg.sender] -= amount; balances[to] += amount; emit transferred(msg.sender, to, amount); } mapping(address=>uint) bal_old; bool bal_modif; require(!bal_modif); if (!bal_modif) { bal_old = balances; bal_modif = true; } assert(!bal_modif); assert(bal_modif); assert(bal_old[msg.sender] >= amount); assert(balances[msg.sender] == bal_old[msg.sender]-amount); assert(balances[to] == bal_old[to] + amount); bal_modif = false; if (!bal_modif) { bal_old = balances; bal_modif = true; }

new vars assume clear check modif check modif emit specs after checkpt

slide-13
SLIDE 13

13

  • We used solc-verify

– Modular verifier based on Boogie and SMT – Can work with other verifiers (supporting assertions)

  • After checkpoints

– Depend on verification approach – Modular verification: loop boundaries as well

Discussion

slide-14
SLIDE 14

14

  • Solidity events provide abstract view
  • Formal specification and verification
  • In-code annotations
  • Checkpoints
  • Instrumentation

Conclusions

E1(x) E2(x,y) E1(x) E1(x) E2(x,y) E2(x,y)

contract Token { mapping(address=>uint) balances; uint total; /// @notice tracks-changes-in balances /// @notice tracks-changes-in total event initialized(address from, uint amount); /// @notice tracks-changes-in balances event transferred(address from, address to, uint amount); }

arxiv.org/abs/2005.10382 github.com/SRI-CSL/solidity