-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask-3
More file actions
153 lines (101 loc) · 6.51 KB
/
Copy pathTask-3
File metadata and controls
153 lines (101 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Part A)
dependencies installed are:
npm install --save-dev "hardhat@^3.0.8" "@nomicfoundation/hardhat-toolbox-mocha-ethers@^3.0.0" "@nomicfoundation/hardhat-ignition@^3.0.0" "@types/chai@^4.2.0" "@types/chai-as-promised@^8.0.1" "@types/mocha@>=10.0.10" "@types/node@^22.8.5" "chai@^5.1.2" "ethers@^6.14.0" "forge-std@foundry-rs/forge-std#v1.9.4" "mocha@^11.0.0" "typescript@~5.8.0"
Part B)
1) HelloVPN code :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract HelloVPN {
// --- State Variables ---
// These variables store data permanently on the blockchain.
// Stores the greeting message.
string private _greeting;
// Stores the node count.
uint256 private _nodeCount;
// Stores the deployer's address.
// 'immutable' means it can be set only once (in the constructor)
// and never changed again, which saves gas.
address private immutable _deployer;
// --- Constructor ---
// This function runs ONLY ONCE when you deploy the contract.
constructor(string memory initialGreeting) {
// 'msg.sender' is a global variable that always means
// "the wallet/account that is making this transaction."
_deployer = msg.sender;
// We set the initial greeting from the deployment script.
_greeting = initialGreeting;
}
// --- Greeting Functions ---
/**
* @notice Stores a new greeting message
* @param _newGreeting The new greeting message to set
*/
function setGreeting(string memory _newGreeting) public {
_greeting = _newGreeting;
}
/**
* @notice Returns the current greeting message
* @return The currently stored greeting
*/
function getGreeting() public view returns (string memory) {
return _greeting;
}
// --- Node Count Functions ---
/**
* @notice Stores a number representing VPN nodes
* @param _count The new node count to set
*/
function setNodeCount(uint256 _count) public {
_nodeCount = _count;
}
/**
* @notice Returns the current node count
* @return The currently stored node count
*/
function getNodeCount() public view returns (uint256) {
return _nodeCount;
}
// --- Deployer Address Function ---
/**
* @notice Returns the address of the account that deployed the contract
* @return The deployer's address
*/
function getDeployerAddress() public view returns (address) {
return _deployer;
}
}
2) Constructor is a function which is used to initialize the variables used in the contract when the contract is first created. Constructor runs only once when the contract is deployed in the blockchain. It cannot be run once the contract is deployed.
Public: It is a keyword which means everyone can access it.
When we create a state variable public, solidity creates a getter function because of which anyone can call public and read its value.
When we create a public function then anyone can call it and run the code inside it.
Private: It is a keyword which means that only the contract can access it.
When we create a state variable private then only the contract can assign and read its value.
When we create a private function then it can be used only by the functions within that contract.
Key: This is not a standalone keyword. It is used in a data type called mopping.
Key is used to find its specific value.
Memory: Memory is a data location keyword. It is a temporary variable and need to be erased after the function is done.
msg.sender is a global variable in Solidity that gives the address of the account or smart contract that called current function.
It is basically used for authentication and authorization.
It can be run only once as it permanently saves the address of the deployer as _deployer.
We declare string and unit256 mainly because of three reasons: Cost, Security and Clarity.
The most important reason is to prevent errors. By declaring a type, you tell the computer exactly what kind of data to expect.
We declare uint256 nodeCount;. This variable can only ever hold an unsigned integer (a whole number that can't be negative).
We declare string greeting;. This variable can only ever hold text.
This prevents us from making costly mistakes. The compiler will stop us if we accidentally try to do something nonsensical.
On the blockchain, storage costs real money (gas).
Declaring a type tells the Ethereum Virtual Machine (EVM) exactly how much storage space to reserve for that variable.
uint256: This means "reserve 256 bits (32 bytes) of space for this number." The EVM knows the exact cost.
uint8: This means "reserve 8 bits (1 byte) of space." This is much cheaper than a uint256 if we only need to store a small number.
string: This tells the EVM, "I'm storing text, which has a dynamic (changing) size." This is much more complex and expensive to store than a number.
If we didn't declare types, the EVM would have no idea how to calculate the gas cost for our transactions, and it couldn't operate efficiently.
Declaring types makes the code far easier for us and other developers to read and understand.
When we see a variable named uint256 nodeCount, we immediately know two things:
It's a number.
It's related to a node count.
We don't have to guess what kind of data is inside it. This makes our code more maintainable and less bug-prone.
3)
1) A state variable in a smart contract is a variable whose value is permanently stored on the blockchain within the contract. It represents the persistent "state" or data held by the contract, and its value remains recorded even after functions finish running.
2) State variables are stored directly on the blockchain. They are part of the contract's permanent storage, meaning their values are recorded in the distributed ledger and persist across transactions and function calls.
3) State variables are stored on the blockchain, not just on our computer.
If the contract is deployed on a local Hardhat node, restarting our computer will erase the local blockchain, including the contract and its state variables. The local node is temporary and only exists in our computer's memory while it's running.
If the contract is deployed on a public blockchain (like Ethereum Mainnet or a testnet like Sepolia), restarting our computer has no effect on the state variables. They remain permanently stored on the global, decentralized network.