Ethernaut Lvl 17 Locked Walkthrough: How to properly use (and abuse) structs in Solidity

This is a in-depth series around Zeppelin team’s smart contract security puzzles. I’ll give you the direct resources and key concepts you’ll need to solve the puzzles 100% on your own.

Nicole Zhu
Sep 17, 2018 · 4 min read

Best practices when using Structs

How to initialize a struct

struct Funder {
address addr;
uint amount;
}

struct StructOfStructs {
...
mapping (uint => Funder) funders;
}
... = Funder(msg.sender, msg.value);
... = Funder({addr: msg.sender, amount: msg.value})

Common usage patterns (memory vs storage)

An array of Funders:

Funders[] public funders;function ... {
Funder memory f;
f.address = ...;
f.amount = ...;
funders.push(f);
}

A mapping of Funders:

mapping (uint => Funder) funders; function ... {
funders[index] = Funder(...);
}

Important to know: when you directly save a memory struct into a state variable, the memory struct is automatically forced into storage.


Bad example 1

// Do NOT do this
function badFunction{
Funder f; //this defaults to storage
f.address = ...;
f.amount = ...;
funders.push(f); //this will fail
}

Bad example 2

// Do NOT do this
function badFunction{
Funder storage f = Funder(...);
}
// Do NOT do this
function badFunction(Funder _funder){
Funder storage f = _funder;
}

Image for post
Image for post

Detailed Walkthrough

0x0000000000000000000000000000000000000000000000000000000000000000
function register(_name...){
NameRecord newRecord; //storage declaration
newRecord.name = _name;
newRecord.mappedAddress = _mappedAddress;
...
0x0000000000000000000000000000000000000000000000000000000000000001
await contract.unlocked();

Key Security Takeaways

Learn more about structs


Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Crypto News

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

Create a free Medium account to get Crypto News in your inbox.

Nicole Zhu

Written by

Engineer @ParityTech | I write about cryptography and code | Tweet @nczhu

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Nicole Zhu

Written by

Engineer @ParityTech | I write about cryptography and code | Tweet @nczhu

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store