0% found this document useful (0 votes)
24 views26 pages

Question Ans

blockchain model questions

Uploaded by

mzmindykkyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

Question Ans

blockchain model questions

Uploaded by

mzmindykkyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

CSIT 6000Q- Blockchain and Smart Contracts

Assigment #3
(Due: 11:59pm on Sun Day, Dec. 6, 2023)
November 25, 2023

1 Question 1
What is an Ethereum smart contract?

An Ethereum smart contract is a small program that runs on the Ethereum blockchain.

2 Question 2
What makes an Ethereum smart contract so special compared to other programs?

Once an Ethereum smart contract is deployed to the blockchain, it cannot:

• be stopped

• be hacked (as long as the code of the contract is correct)

• be modified (the code is immutable, however the data is)

3 Question 3
Can a smart contract interact with other smart contracts?

Yes.

4 Question 4
Can a Solidity smart contract call an API on the web?

1
No, it can only execute its own code and interact with other smart contracts on the blockchain.

5 Question 5
Can a Solidity smart contract store a lot of data?

No, storage is very limited on a smart contract. Storing data cost gas, and gas consumption is
capped in each Ethereum block. So indirectly, storage is limited.

6 Question 6
Can a smart contract be written in another language than Solidity?

Yes. There are other smart contract languages like Vyper or LLL. But Solidity is the most
popular.

7 Question 7
Is Solidity a dynamically or statically typed language? (i.e need to define variable types)

It’s a statically typed language, i.e variable types need to be defined, unlike in dynamic lan-
guages like Javascript.

8 Question 8
Is Solidity a dynamically or statically typed language? (i.e need to define variable types)

It’s a statically typed language, i.e variable types need to be defined, unlike in dynamic lan-
guages like Javascript.

9 Question 9
Is Solidity compiled or interpreted?

It’s compiled, meaning it means to be first compiled before we can run the code.

2
10 Question 10
What is the file extension of Solidity files?

.sol

11 Question 11
What is the typical layout of a Solidity smart contract?

/ / pragma s t a t e m e n t ( r e q u i r e d )
pragma s o l i d i t y ˆ 0 . 8 . 2 0 ;

/ / contract declaration ( required )


contract A {
// state variables
uint a ;

// functions
f u n c t i o n foo ( ) { . . . }
}

12 Question 12
What is the typical layout of a Solidity smart contract?

contract A {
// state variable
uint a ;

// functions
f u n c t i o n foo ( ) {
uint b; // local variable
}
}
State variables are persisted on the blockchain after a smart contract finish to execute, whereas
local variables live only during the execution of a function.

13 Question 13

3
What is the problem with the following code

contract Storage {
uint data ;
/ / Should u p d a t e t h e ‘ data ‘ s t o r a g e v a r i a b l e above
function set ( uint data ) external {
address data = data ;
}
}
The set() function redefine the data variable inside its body. This will create a local variable
that will shadow the state variable defined above. The assignment address data = data will
assign to this local variable instead of the state variable. To fix the code, remove the address
keyword in front of data in the set() function: data = data;

14 Question 14
What is the problem with the following code?

contract Storage {
uint data ;
/ / Should u p d a t e t h e ‘ data ‘ s t o r a g e v a r i a b l e above
function set ( uint data ) external {
/ / data = data ?
}
}
This is similar to the problem we have just before. The data argument of the set() function
shadows the data state variable. Because of this, we can’t access the data state variable inside
the set() function. To solve this problem, we need to rename the argument from data to data.

15 Question 15
What are the 2 variable visibilities for state variables in Solidity?

• private

• public

16 Question 16

4
Who can read private and public variables?

• private variables can be read only by functions inside the smart contract

• public variables can be read by anyone

17 Question 17
What is the default visibility of state variables?

Private.

18 Question 18
Are private variables really private?

No. Private is only for the EVM (Ethereum Virtual Machine, the part of Ethereum that execute
smart contracts). But the data of smart contracts is put on the Ethereum blockchain, and all
data on this blockchain is public. Using a special tool to analyse blockchain data, anyone to
read private variables of smart contracts.

19 Question 19
How to deal with private data then?

You either don’t put private data on the blockchain, or you put hashes.

20 Question 20
Mention 3 data types that you use often, and explain why?

• uint, for ether / token transfers, and identifying data

• address, for identifying humans and smart contracts

• strings, for naming things

21 Question 21

5
What are the 2 container types in Solidity?

• mapping

• arrays

22 Question 22
How to declare an array of integer in Solidity?

uint[] a;

23 Question 23
How to declare a mapping of address to booleans in Solidity?

mapping(address => bool) a;

24 Question 24
How to declare a mapping of address to mapping of address to booleans (nested mapping)?

mapping(address => mapping(address => bool)) a;

25 Question 25
How to declare a mapping of address to mapping of address to booleans (nested mapping)?

mapping(address => mapping(address => bool)) a;

26 Question 26
How to add data to an array declared as a state variable?

6
uint [] a ;

f u n c t i o n add ( u i n t n e w E n t r y ) e x t e r n a l {
add . p u s h ( a ) ;
}

27 Question 27
How to add data to a mapping declared as a state variable?

mapping ( a d d r e s s => b o o l ) a ;

f u n c t i o n add ( a d d r e s s a d d r ) e x t e r n a l {
a [ addr ] = t r u e ;
}

28 Question 28
How to loop through an array?

uint [] a ;
f o r ( u i n t i = 0 ; i < a r r . l e n g t h ; i ++) {
/ / do s o m e t h i n g w i t h a r r [ i ]
/ / reading : uint a = arr [ i ]
/ / writing : arr [ i ] = a
}

29 Question 29
What is the difference between a uint8 and a uint16?

uint8 can store number of up to 28 − 1 (it has 8 bits), whereas uint16 can store numbers of up
to 21 6 − 1.

30 Question 30

7
What are the 4 function visibilities in Solidity, by increasing permissiveness?

• private

• internal

• external

• public

31 Question 31
How to conditionally throw an error, with an error message?

require(a !== b, ’My error message’)

32 Question 32
What are the 2 artifacts produced by the Solidity compiler when compiling a smart contract?

• The ABI (application binary interface)

• The bytecode

33 Question 33
What is the ABI of a smart contract?

The ABI defines the interface of a smart contract, i.e the set of functions that can be called
from outside the smart contract. The ABI only defines the function signatures (function names,
argument types and return types) but not their implementation. The ABI also defines the events
of the contract. The ABI is used outside the smart contract by Ethereum client libraries like
web3 to interact with the smart contract.

34 Question 34
In the following contract, which function will be part of the ABI?

8
contract A {
function foo ( ) e x t e r n a l { . . . }
function bar ( ) public { . . . }
function baz ( ) i n t e r n a l { . . . }
}
foo() and bar() will be part of the ABI.

35 Question 35
Does the EVM understands Solidity?

No. The EVM only understand bytecode, which must first be produced by Solidity, outside of
the blockchain.

36 Question 36
What is the EVM bytecode?

The EVM bytecode is a series of EVM elementary instructions called opcodes. These op-
codes define very simple operations like adding 2 numbers (ADD), loading data from memory
(mload), etc. . . There are more than 100 of these opcodes defined in the Ethereum yellow
paper. Coding directly with opcodes would be very tedious, so we need higher languages like
Solidity to help us reason at a higher level of abstraction.

37 Question 37
What are the 2 APIs used to interact with a smart contract?

eth sendTransaction (transaction) and eth call (call). Transactions cost money (gas) and can
modify the blockchain. Calls do not cost money, cannot modify the blockchain, but can return
a value contrary to transactions.

38 Question 38
What is gas?

Gas is an abstract unit used to pay miners when sending a transaction on the Ethereum network.

9
39 Question 39
How is gas paid?

Gas is paid in ether using the formula: ether cost = gasPrice * gas, where gas represents the
gas cost of the execution of a transaction. gasPrice is in wei / gas, generally express is Gwei. A
transaction also specifies a gasLimit parameter, which specify a maximum number of gas that
can be paid by a transaction. Without this, a transaction could potentially drain an account of
all its Ether.

40 Question 40
What happen if there is not enough gas in a transaction?

The transaction is stopped, and all state changes are reverted.

41 Question 41
Who pays for gas in a transaction?

The sender of the transaction.

42 Question 42
What do you need to deploy a smart contract to the Ethereum network?

• bytecode of smart contract

• an Ethereum address with enough Ether

• A wallet to sign the transaction

• A tool to create the transaction and coordinate the signing process with the wallet

43 Question 43
List 4 famous Ethereum wallets

10
• Metamask

• ..

• ..

• ..

44 Question 44
List 3 networks where you can deploy a Solidity smart contract

• Mainnet

• Ropsten

• Goerli

45 Question 45
List 3 networks where you can deploy a Solidity smart contract

• Mainnet

• Ropsten

• Goerli

46 Question 46
List 3 networks where you can deploy a Solidity smart contract

• Mainnet

• Ropsten

• Goerli

47 Question 47
How to manage dates in Solidity?

11
You need to use uint variables.

48 Question 48
How to have the current timestamp in seconds?

You need to use the now keyword.

49 Question 49
What are the 2 ways to define custom data structure in Solidity?

• Struct

• Enum

50 Question 50
When would you use a struct vs an enum?

Struct are for representing complex data structures with different fields. Enum are for creating
variant for a single data. Ex: a color can be red, blue, yellow. You can combine both by defining
an enum, and a struct that uses this enum in a field;
enum F a v o r i t e C o l o r { Blue , Red } ;
s t r u c t User {
address id ;
s t r i n g name ;
Color f a v o r i t e C o l o r ;
}

51 Question 51
What are the 2 ways to instantiate a struct?

s t r u c t User {
address id ;
s t r i n g name ;
}

/ / Method 1 ( a r g u m e n t o r d e r m a t t e r s )

12
U s e r ( ” 0 xAio90 . . . . ” , ” Mike ” ) ;

/ / Method 2 ( a r g u m e n t o r d e r d o e s n o t m a t t e r )
U s e r ( { name : ” Mike ” , a d d r e s s : ”0 xAio90 . . . . ” } ) ;

52 Question 52
How to instantiate a struct that has an inner mapping?

s t r u c t User {
address id ;
s t r i n g name ;
mapping ( a d d r e s s => b o o l ) f r i e n d s ;
}
/ / l e t assume t h e U s e r s t r u c t i s s t o r e d i n s i d e a mapping
mapping ( a d d r e s s => U s e r ) u s e r s ;

/ / I n s i d e a f u n c t i o n , you would i n s t a n t i a t e y o u r s t r u c t l i k e t h i s
u s e r s [ ” 0 xAio90 . . . ” ] . i d = ”0 xAio90 . . . ” ;
u s e r s [ ” 0 xAio90 . . . ” ] . name = ” Mike ” ;
u s e r s [ ” 0 xAio90 . . . ” ] . f r i e n d s [ ” 0 x I o p o p . . . ” ] = t r u e ;
u s e r s [ ” 0 xAio90 . . . ” ] . f r i e n d s [ ” 0 x j k 8 9 I . . . ” ] = t r u e ;

53 Question 53
When would you use an array vs a mapping?

I would use an array if I need to iterate through a collection of data. And I would use a mapping
if I need to rapidly lookup a specific value

54 Question 54
How to combine array and mapping to allow both iteration and rapid lookup of a struct?

/ / Let ’ s c o n s i d e r t h i s s t r u c t
s t r u c t User {
uint id ;
s t r i n g name ;
}

/ / F i r s t , l e t ’ s u s e an a r r a y t o s t o r e a l l i t s ids

13
uint [] userIds ;

/ / Then , l e t ’ s u s e a mapping f o r r a p i d l o o k u p
mapping ( u i n t => U s e r ) u s e r s ;

/ / I f we n e e d t o r a p i d l y l o o k u p a u s e r , we u s e t h e mapping
/ / And i f we n e e d t o i t e r a t e t h r o u g h u s e r s ,
/ / we i t e r a t e t h r o u g h t h e u s e r I d s a r r a y ,
/ / and f o r e a c h u s e r I f we c a n
/ / l o o k u p t h e c o r r e c t u s e r i n t h e mapping

55 Question 55
How to define an in-memory array of 3 integers?

u i n t [ ] memory a r r = new u i n t [ ] ( 3 ) ;

56 Question 56
How to add a value to an in-memory array?

u i n t [ ] memory a r r = new u i n t [ ] ( 3 ) ;

uint [0] = 1;
uint [1] = 2;
uint [2] = 3;
uint [3] = 1 ; / / o u t − of − b o u n d s e r r o r

57 Question 57
How to create an in-memory mapping?

You can’t. Solidity has only storage mappings.

58 Question 58
What happen if you try to access the key of a mapping that does not exist?

14
Contrary to arrays, there is no error, Solidity will give you a value, which is the default value
of the type.
Ex:
mapping ( u i n t => b o o l ) myMap ;
If you access myMap[10] but nothing exist there, Solidity will produce false.

59 Question 59
What are the 3 mechanisms for code re-use in Solidity?

• Group common codes in functions

• Contract inheritance

• Libraries

60 Question 61
How to make a contract A inherit from a contract B in Solidity?

/ / F i r s t import the c o n t r a c t
i m p o r t B from ’ p a t h / t o / B . s o l ’ ;

/ / Then make y o u r c o n t r a c t i n h e r i t from i t


contract A is B {

/ / Then c a l l t h e c o n s t r u c t o r o f t h e B c o n t r a c t
c o n s t r u c t o r ( ) B ( ) {}
}

61 Question 62
If A inherit from B, and both define the same function foo, which one will be resolved?

//Case 1
contract B {
f u n c t i o n foo ( ) e x t e r n a l { . . . }
}
contract A is B {
f u n c t i o n foo ( ) e x t e r n a l { . . . }
}

15
If I call foo() on A, the function A.foo() will be resolved
//Case 2
contract B {
f u n c t i o n foo ( u i n t data ) e x t e r n a l { . . . }
}
contract A is B {
f u n c t i o n foo ( ) e x t e r n a l { . . . }
}
If I call foo(1) on A, the function B.foo() will be resolved, because only B defines foo(uint)

62 Question 63
What are the 4 memory locations of Solidity?

63 Question 62
If A inherit from B, and both define the same function foo, which one will be resolved?

Storage, Memory, Stack and Calldata

64 Question 63
What is the default visibility of state variables?

Private

65 Question 64
What is the difference between address and address payable?

Only address payable can receive money

66 Question 66
Is it necessary to make an address address payable to transfer ERC20 tokens?

16
No. The payable requirement is only required for native Ether. Ethereum has no knowledge of
ERC20 tokens. For Ethereum, this is just a variable in a smart contract, like any other variables.

67 Question 67
Give 3 ways to save gas

• Put less data on-chain

• Use events instead of storage

• Optimal order for variable declaration.

68 Question 68
How would optimally order uint128, bytes32 and another uint128 to save gas?

• uint128

• uint128

• bytes32

69 Question 69
How would optimally order uint128, bytes32 and another uint128 to save gas?

• uint128

• uint128

• bytes32
The EVM stores variable in 32-bytes slot. However Solidity is smart enough to pack into
a single slot several variables if they can fit together. For this optimization to work, packed
variables have to be defined next to each other. In the above example, the 2 uint128 will be
placed in the same 256 bit slots (128 + 128 = 256).

70 Question 70
How to concatenate 2 strings a, b?

17
Use the abi.encodePacked() function: string(abi.encodePacked(a, b));

71 Question 71
How to get the length of a string in solidity?

bytes memory byteStr = bytes(a); //a is a string


bytesStr.length;

72 Question 72
How to to create a smart contract from a smart contract?

contract A {
constructor ( uint a) {...}
f u n c t i o n foo ( ) e x t e r n a l { . . . }
}

contract B {
function createA ( uint a ) external {
A A I n s t a n c e = new A( a ) ; / / p a s s c o n s t r u c t o r a r g u m e n t ( s ) i f any
}
}

73 Question 73
How to to call another smart contract from a smart contract?

contract A {
f u n c t i o n f o o ( ) view e x t e r n a l r e t u r n s ( u i n t ) { . . . }
}

contract B {
f u n c t i o n c a l l F o o ( a d d r e s s addrA ) e x t e r n a l {
u i n t r e s u l t = A( addrA ) . f o o ( ) ;
}
}

18
74 Question 74
How to get the address of a smart contract that was deployed from a smart contract?

Using the address() operator to cast the contract type into an address:
a d d r e s s c h i l d A d d r e s s = a d d r e s s ( new C h i l d ( ) ) ;

75 Question 75
What will be the value of msg.sender if a contract calls another one?

Using the address() operator to cast the contract type into an address:
/ / This i s the inner c o n t r a c t
contract A {
f u n c t i o n b a r ( ) view e x t e r n a l r e t u r n s ( a d d r e s s ) {
/ / What w i l l be t h e v a l u e o f ‘ msg . s e n d e r ‘ h e r e ?
}
}

/ / This i s the outer c o n t r a c t


contract B {
f u n c t i o n foo ( ) e x t e r n a l {
A a I n s t a n c e = new A ( ) ;
aInstance . bar ( ) ;
}
}
This is the address of the calling contract, i.e B in our example.

76 Question 76
How to transfer ERC20 tokens?

Using the address() operator to cast the contract type into an address:
c o n t r a c t ERC20Interface {
f u n c t i o n t o t a l S u p p l y ( ) p u b l i c view r e t u r n s ( u i n t ) ;
f u n c t i o n b a l a n c e O f ( a d d r e s s tokenOwner )
p u b l i c view r e t u r n s ( u i n t b a l a n c e ) ;
f u n c t i o n a l l o w a n c e ( a d d r e s s tokenOwner , a d d r e s s s p e n d e r )
p u b l i c view r e t u r n s ( u i n t r e m a i n i n g ) ;
f u n c t i o n t r a n s f e r ( a d d r e s s to , u i n t t o k e n s )
public r e t u r n s ( bool success ) ;
f u n c t i o n approve ( address spender , u i n t tokens )

19
public r e t u r n s ( bool success ) ;
f u n c t i o n t r a n s f e r F r o m ( a d d r e s s from , a d d r e s s t o , u i n t t o k e n s )
public r e t u r n s ( bool success ) ;

e v e n t T r a n s f e r ( a d d r e s s i n d e x e d from ,
a d d r e s s indexed to , u i n t t o k e n s ) ;
e v e n t A p p r o v a l ( a d d r e s s i n d e x e d tokenOwner ,
address indexed spender , u i n t tokens ) ;
}

contract DecentralizedExchange {
function transferToken
( a d d r e s s ERC20Address , a d d r e s s t o , u i n t amount ) {
E R C 2 0 I n t e r f a c e ( ERC20Address ) . t r a n s f e r ( t o , amount ) ;
}
}
This is the address of the calling contract, i.e B in our example.

77 Question 77
How to declare and emit an event?

Using the address() operator to cast the contract type into an address:
contract A {
/ / declare event
E v e n t T o k e n S e n t ( u i n t amount , a d d r e s s t o ) ;
f u n c t i o n s e n d T o k e n ( u i n t amount , a d d r e s s t o ) e x t e r n a l {
...
/ / Emit e v e n t
e m i t T o k e n S e n t ( amount , t o ) ;
/ / c a r e f u l , old S o l i d i t y 0 . 4 code d i d n t not r e q u i r e
t h e e m i t keyword , d o n t be c o n f u s e d
}
}
This is the address of the calling contract, i.e B in our example.

78 Question 78
What is the indexed keyword in event definition?

If an event field is declared with the indexed keyword, it means that external entities can filter
only events whose field match a specific value. For example, in the below example, it means
it’s possible to filter events with a to field equal to a specific value.

20
E v e n t T o k e n S e n t ( u i n t amount , a d d r e s s i n d e x e d t o ) ;

79 Question 79
Is it possible for a smart contract to read the events emitted before?

No. Only external entities can queries events.

80 Question 80
Is it possible to delete or modify a past event?

No. Events are immutable.

81 Question 81
How would you implement access control without modifier?

contract A {
a d d r e s s admin ;
constructor () {
admin = msg . s e n d e r ;
}

function protectedFunction () external {


r e q u i r e ( msg . s e n d e r == admin , ’ o n l y admin ’ ) ;
...
}
}

82 Question 82
How would you implement access control WITH modifier?

contract A {
a d d r e s s admin ;
constructor () {
admin = msg . s e n d e r ;
}

21
f u n c t i o n p r o t e c t e d F u n c t i o n ( ) e x t e r n a l onlyAdmin ( ) {
...
}

m o d i f i e r onlyAdmin ( ) {
r e q u i r e ( msg . s e n d e r == admin , ’ o n l y admin ’ ) ;
;
}
}

83 Question 83
How to cancel a transaction?

Once a transaction has been sent, nobody can prevent it from being mined by a miner. But you
can still send another transaction preventing the first one from working IF its mined before the
first one. This second transaction will have the following properties:

• it will have the same nonce (i.e an incrementing integer that is sent in each transaction,
specific to each Ethereum address)

• it will have a higher gasPrice than the first transaction

• it will send a tiny amount of Ether to another address

Let’s review why we need these. The same nonce means that the first transaction to be
mined will prevent the other one from being mined: miners only mine transactions whose
nonce is higher than the previous nonce for the address that has signed the transaction.
The higher gasPrice means a higher reward for miner, so if a miner has the choice to mine
the second or the first transaction he will choose the second one.
And finally, sending a tiny amount of Ether is just because a transaction needs to do some-
thing on the blockchain, so we just do something that is useless but doesn’t cost us much.
Actually, you could even call a read-only function in any smart contract, in a transaction, and
you wouldn’t even need to send this tiny amount of Ether. You would still need to cover the
gas fee in every case.

84 Question 84
Is it possible to send a transaction without requiring users to pay gas?

Yes. You would ask users to first sign a message on the frontend. Then the message and
signature would be sent to a centralized backend (your app, off-chain) that would create a
transaction and embed the payload (message + signature) into it. That means that gas fees will

22
be covered by the wallet of the app, instead of the user wallet. On-chain, a smart contract will
verify the validity of the signature and perform on operation on behalf of the user.

85 Question 85
Which Solidity function would you use to verify a signature?

ecrecover().

86 Question 86
What is a library in Solidity?

A library is a piece of code that be re-used by other smart contracts. There are 2 types of
libraries:

• deployed

• embedded

Deployed libraries have their own address, and they can be used by several other smart con-
tracts. Embedded libraries don’t have their own address and are deployed as part of the code of
the smart contract that use them.

87 Question 87
Give an example of how to use a library in a smart contract

l i b r a r y Lib {
f u n c t i o n add ( u i n t a , u i n t b ) p u r e i n t e r n a l r e t u r n s ( u i n t ) {
return a + b;
}
}

contract A {
u s i n g Lib f o r u i n t ;

f u n c t i o n add ( u i n t a , u i n t b ) p u r e e x t e r n a l r e t u r n s ( u i n t ) {
r e t u r n a . add ( b ) ;
}
}

23
88 Question 88
When is a library embedded vs deployed?

/ / Embedded ( f u n c t i o n i s i n t e r n a l )
l i b r a r y Lib {
f u n c t i o n add ( u i n t a , u i n t b ) p u r e i n t e r n a l r e t u r n s ( u i n t ) {
return a + b;
}
}

/ / Deployed ( f u n c t i o n i s p u b l i c )
l i b r a r y Lib {
f u n c t i o n add ( u i n t a , u i n t b ) p u r e p u b l i c r e t u r n s ( u i n t ) {
return a + b;
}
}

89 Question 89
When is a library embedded vs deployed?

/ / Embedded ( f u n c t i o n i s i n t e r n a l )
l i b r a r y Lib {
f u n c t i o n add ( u i n t a , u i n t b ) p u r e i n t e r n a l r e t u r n s ( u i n t ) {
return a + b;
}
}

/ / Deployed ( f u n c t i o n i s p u b l i c )
l i b r a r y Lib {
f u n c t i o n add ( u i n t a , u i n t b ) p u r e p u b l i c r e t u r n s ( u i n t ) {
return a + b;
}
}

90 Question 90
What is a re-entrancy attack?

24
A re-entrancy attack happen when a contract A calls a contract B which call back the calling
function on contract A to perform some malicious effect. Example with a DAO-like attack:
contract A {
//...
f u n c t i o n pay ( a d d r e s s p a y a b l e t o , u i n t amount ) e x t e r n a l {
i f ( amount <= b a l a n c e s [ msg . s e n d e r ] ) {
B ( t o ) . b a d F u n c t i o n ( ) . s e n d ( amount ) ;
b a l a n c e s [ msg . s e n d e r ] −= amount ;
}
}

contract B {
address
function badFunction ( address payable to ) e x t e r n a l {
C o n t r a c t A ( msg . s e n d e r ) . pay ( ) ;
}
}

91 Question 91
How to prevent against a re-entrancy attack?

• Solution 1: Decrease balances / do other state variable update BEFORE calling the other
contract.

• Solution 2: Put in place re-entrancy guard with a variable that knows when a call is the
second in the stack

• Solution 3: Limit the gas available to the called contract. If using transfer(), this is done
automatically:

92 Question 92
How to produce a hash of multiple values in Solidity?

keccak256(abi.encodePacked(a, b, c))

93 Question 93
How to generate a random integer in Solidity?

25
We can leverage the block.timestamp and block.difficulty as a source of randomness, and use
the keccak256() hashing function:
uint(keccak256(abi.encodePacked(block.timestamp,
, block.difficulty)))
Be aware that miners can manipulate block.difficulty and block.timestamp, so this is not
100% secure.

94 Question 94
How to generate a random integer in Solidity?

We can leverage the block.timestamp and block.difficulty as a source of randomness, and use
the keccak256() hashing function:
uint(keccak256(abi.encodePacked(block.timestamp,
, block.difficulty)))
Be aware that miners can manipulate block.difficulty and block.timestamp, so this is not
100% secure.

95 Question 95
How to declare assembly code?

Functional and instructional. Functional uses functions, whereas instructional is a raw series
of opcodes. Most people use the functional style.

96 Question 96
How to declare assembly code?

f u n c t i o n isHuman ( a d d r e s s a d d r ) e x t e r n a l {
uint256 codeLength ;

assembly { codeLength := e x t c o d e s i z e ( addr )}


r e t u r n c o d e L e n g t h == 0 ? t r u e : f a l s e ;
}
}

26

You might also like