Skip to content

Latest commit

 

History

History
32 lines (20 loc) · 1.08 KB

File metadata and controls

32 lines (20 loc) · 1.08 KB

Re-entrancy

Re-entrancy occurs when a contract makes a call back into the contract that called it, e.g. Contract A calls Contract B but then Contract B makes a call back into Contract A.

To mitigate security concerns there are two approaches that are commonly used:

Re-entrancy Guard

Sway provides a stateless re-entrancy guard, which reverts at run-time when re-entrancy is detected.

To use the guard we must import it.

{{#include ../../code/operations/re_entrency/src/main.sw:import}}

Then call it in a contract function.

{{#include ../../code/operations/re_entrency/src/main.sw:guard}}

Checks-Effects-Interactions Pattern

The pattern states that all state (storage) changes should be made before a call is made.

{{#include ../../code/operations/re_entrency/src/main.sw:check}}