Are there null like thing in solidity - ethereum

struct buyer{
uint amount;
Status status;
}
mapping(address=>buyer) public buyers;
mapping(uint=>address) buyerIndex;
uint public buyerNum;
//Order a product.
function(){
uint doubleValue=value*2;
uint amount=msg.value/doubleValue;
if(buyers[msg.sender]==null){ //Error in this line
buyer abuyer=buyer({amount:amount,status:Status.Created}); //Error in this line
buyerNum++;
buyerIndex[buyerNum]=msg.sender;
buyers[msg.sender]=abuyer;
}else{
buyers[msg.sender].amount+=amount;
}
Order(msg.sender,amount*doubleValue,amount);
}
If a buyer is not recorded in the buyer mapping, then buyerNum++;
but I don't know how to tell whether a buyer is in the mapping

In solidity every variable is set to 0 by default.
You should think of mappings as all possible combinations are set to 0 by default.
In your specific case I would use the following:
if (buyers[msg.sender].amount == 0)

For integers:
You could create none variable to use it as a NULL:
uint256 constant NULL = 0;
Example code for check:
function isNULL(uint256 variable) internal returns (bool) {
return variable == NULL;
}
For bytes32:
You can follow different approach for bytes:
bytes32 constant NULL = "";
Example code piece:
pragma solidity ^0.6.0;
mapping(address => bytes32) public Countries;
function isCountriesInitialized(address _user) external view returns (bool)
{
if (Countries[_user] == NULL) // Returns true if `Countries[_user]` is not initialized
return false;
return true;
}
I observe that on solidity >= v0.6.0 it may return 32 for length even though it is not mapped.
Example of its returned value:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

As Viktor said, default value for all possible values in mapping is zero. So if a buyer has not already inserted in mapping, the amount value for that address will be zero. But this approach has a flaw, if a buyer does exists but its balance became zero after some operations, you will treat it as it does not exist.
I think the best approach is to add a exists member to the buyer struct with bool type. Default value for this member is false and when the buyer get created, you initialize it with true value. So you can check exactly if a buyer exist or not via this member.
Buyer struct:
struct buyer{
uint amount;
Status status;
bool exists;
}
Initialize buyer:
buyer memory b = buyer(0, status, true);
Check if buyer exists:
if(buyers[msg.sender].exists) {
//so can buy
}

There is nothing like null in solidity.
Just check for the length of the address:
if(buyers[msg.sender].length == 0){
// do your thing
}
See also this answer on ethereum stack exchange.

Instead of using one of the values or creating an extra boolean, you can check for the byte size of the structure.
if (bytes(buyers[msg.sender]).length > 0) {
// buyer exists.
}

Related

How to return "Null" or an "Empty" object in Solidity?

I am currently writing a Smart Contract in Solidity. The smart contract, amongst other information, stores an array of properties object at the general level. The property object Looks like this:
struct PropertyObj {
string id;
uint weiPrice;
address owner;
}
Now there is a specific function that iterates over the array, finds the property and returns it (code below)
function getPropertyByid(string memory _propertyId)private view returns(PropertyObj memory){
for(uint i = 0; i<PropertyArray.length; i++){
if (keccak256(bytes((PropertyArray[i].id))) == keccak256(bytes((_propertyId)))) {
return PropertyArray[i];
}
return null;
}
}
The "Problem" is that, unlike other programming languages, Solidity does not allow to return null (as far as I am concerned).
In other words, if throughout the iteration we do not find the property, then what we shall return if we specified that we need to return PropertyObj memory in the function signature?
Solidity does not have null value, as you're correctly stating.
Your function can throw an exception using the revert() function.
It also seems that your implementation has a logical error. Your example would "return null" if the hash was not found during the first iteration. Instead, you may want to throw the exception after the loop has ended.
for(uint i = 0; i<PropertyArray.length; i++){
if (keccak256(bytes((PropertyArray[i].id))) == keccak256(bytes((_propertyId)))) {
return PropertyArray[i];
}
}
revert('Not found');
Other option would be to return the empty object (with default values, i.e. zeros), if it fits your use case.
for(uint i = 0; i<PropertyArray.length; i++) {
// ...
}
// not found, return empty `PropertyObj`
PropertyObj memory emptyPropertyObj;
return emptyPropertyObj;

Solidity - Truffle: Error: VM Exception while processing transaction: invalid opcode NOT because of revert

I am working on a smart contract, and I am testing it by deploying it on truffle. While it compiles fine, when I call the train() function, I get the following error:
Error: VM Exception while processing transaction: invalid opcode
After reading a bit on this, I understood it is usually caused after a revert has occurred, so I tried commenting out the 2 require functions I had just to see if it would behave differently, and it did not.
Checking out this question did not help me, or I did not see how it could.
Here is the train() function, as well as the mapping and struct type I am using in it. I should note that upon creation of a Developer, their wallet is set to 300 so I do not see how the first call of the train function by the owner could revert.
struct Developer {
address owner;
string name;
bytes32 namehash;
bytes32[] skills;
uint256[] skill_levels;
uint wallet;
}
mapping (bytes32=>Developer) public developers_all;
function train(string _name, bytes32 _skill) public {
bytes32 h = keccak256(abi.encodePacked(_name));
require(developers_all[h].owner == msg.sender, "Only the owner of the developer can train them");
require(developers_all[h].wallet >= 150, "Insufficient funds");
uint256 i = 0;
do {
if (developers_all[h].skills[i] == _skill) {
developers_all[h].skill_levels[i]++;
} else if ((i == (developers_all[h].skills.length - 1)) || (developers_all[h].skills.length == 0)) {
developers_all[h].skills.push(_skill);
developers_all[h].skill_levels.push(1);
}
i++;
} while (i < developers_all[h].skills.length);
developers_all[h].wallet = developers_all[h].wallet - 150;
}
Thank you for any help.
This is most likely because you are trying to access the first entry of an empty array. You're using a do while loop, and you're trying to access developers_all[h].skills[i] before you're checking developers_all[h].skills.length == 0, so it is possible that the array is empty at the first if statement in the do while.
You could rewrite the code to something like the following, to make sure you're never accessing an unassigned array slot.
bool foundSkill = false;
for (uint i = 0; i < developers_all[h].skills.length; i++) {
if (developers_all[h].skills[i] == _skill) {
developers_all[h].skill_levels[i]++;
foundSkill = true;
break;
}
}
if (!foundSkill) {
developers_all[h].skills.push(_skill);
developers_all[h].skill_levels.push(1);
}
Do note that looping through the whole array and doing the comparisons is quite costly, and can become impossible if the array size gets too big. You might want consider changing the structure to something like:
struct Developer {
address owner;
string name;
bytes32 namehash;
mapping(bytes32 => uint) skill_levels;
uint wallet;
}
That way you could just replace the whole thing with
developers_all[h].skill_levels[skill]++;
But you wouldn't be able to loop over the skills.

Dynamic Array Stack Corruption

This contract appears to be a game offering 1/16 odds at the balance of the contract. However, when running the code in a debugger it appears as if the 'secretNumber' variable is being overwritten before it is used.
pragma solidity ^0.4.19;
contract CryptoRoulette {
uint256 private secretNumber;
uint256 public lastPlayed;
uint256 public betPrice = 0.1 ether;
address public ownerAddr;
struct Game {
address player;
uint256 number;
}
Game[] public gamesPlayed;
function CryptoRoulette() public {
ownerAddr = msg.sender;
shuffle();
}
function shuffle() internal {
// initialize secretNumber with a value between 0 and 15
secretNumber = uint8(sha3(now, block.blockhash(block.number-1))) % 16;
}
function play(uint256 number) payable public {
require(msg.value >= betPrice && number < 16);
Game game;
game.player = msg.sender;
game.number = number;
gamesPlayed.push(game);
if (number == secretNumber) {
// win!
msg.sender.transfer(this.balance);
}
shuffle();
lastPlayed = now;
}
function kill() public {
if (msg.sender == ownerAddr && now > lastPlayed + 1 days) {
suicide(msg.sender);
}
}
function() public payable { }
}
The way secretNumber is updated, it should always be less than 16
secretNumber = uint8(sha3(now, block.blockhash(block.number-1))) % 16;
This debugger output shows that during execution of if (number == secretNumber) { the value of secretNumber has been updated to, oddly enough, the callers address (msg.sender).
`
(243) PUSH1 0x00
000000000000000000000000000000000000000000000000000000006898f82b
0000000000000000000000000000000000000000000000000000000000000143
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000000 (top)
40: if (number == secretNumber) {
^^^^^^^^^^^^
debug(develop:0x98cacf83...)> i
CryptoRoulette.sol | 0xbd2c938b9f6bfc1a66368d08cb44dc3eb2ae27be:
40: if (number == secretNumber) {
^^^^^^
debug(develop:0x98cacf83...)> p
CryptoRoulette.sol | 0xbd2c938b9f6bfc1a66368d08cb44dc3eb2ae27be:
(245) DUP3
000000000000000000000000000000000000000000000000000000006898f82b
0000000000000000000000000000000000000000000000000000000000000143
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000627306090abab3a6e1400e9345bc60c78a8bef57 (top)
40: if (number == secretNumber) {
^^^^^^
My guess is that the storage access before the condition is causing the stack to be corrupted somehow.
Is this a known vulnerability? Can someone please explain what is going on?
This is a common issue when attempting to create a local reference without specifying the correct storage location.
From the Solidity docs:
There are defaults for the storage location depending on which type of variable it concerns:
state variables are always in storage
function arguments are in memory by default
local variables of struct, array or mapping type reference storage by default
local variables of value type (i.e. neither array, nor struct nor mapping) are stored in the stack
The bolded comment indicates that the line Game game; defaults to storage. If you don't initialize a storage variable, it will point to storage slot 0 by default. The end result is when you make a change to game (with game.player = msg.sender;), it will write the value out to the first slot, which will be whatever the first variable is in your contract (in this case, secretNumber).
The correct way to write this is to use Game memory game;. The compiler will give you a warning if you omit the memory keyword for this exact reason.

Ethereum smart contract no function return value

I'm new to smart contracts and I've deployed this test contract
contract test {
function callme(address dest, uint num, bytes data, uint nonce)
public
returns (bytes32 myhash)
{
myhash = sha3(dest, num, data, nonce);
return (myhash);
}
}
I then call test.callme(eth.accounts[0], 10, 0xaaaaa, 1234) expecting it to return the sha3 hash of the passed parameters but there's no return value.
> test.callme(eth.accounts[0], 10, 0xaaaaa, 1234)
INFO [12-24|19:35:40] Submitted transaction fullhash=0x694e0e38d0cf8744e62113750339a65f1d5a35cdc634eeb02b93581a926fea1a recipient=0xed712462999f8f68BbF618C3845F4333eDC31cD5
"0x694e0e38d0cf8744e62113750339a65f1d5a35cdc634eeb02b93581a926fea1a"
Any help is appreciated
Your syntax is a little off - you don't need to name your return value myhash. Something like this should do the trick:
contract test {
function callme(address dest, uint num, bytes data, uint nonce)
public
constant
returns (bytes32)
{
bytes32 myhash = sha3(dest, num, data, nonce);
return myhash;
}
}
I also threw in a constant keyword since the function isn't planning on changing anything in your contract's storage. It's a small change, but necessary for what you're trying to do.
Including the constant enables you to get a 'return' value, so to speak, because it says that you won't need to be modifying the blockchain - in essence, you're 'reading' the chain, not 'writing' to it.
Imagine a contract that did something like this:
contract test {
uint example;
function callme()
public
returns (uint)
{
example = example + 1;
return example;
}
}
The transaction we send to callme actually has to be executed before we return the value (since we're modifying the blockchain). Therefore, we can't really return the final value instantly (and we instead return information about the transaction), since we have to wait for the blockchain to be updated first.

How to get return values when function with argument is called

I am writing below simple contract which stores all results of questionnaires of each ID.
contract answer{
mapping(address => mapping(string => bool)) voters;
struct qList {
uint count; //The number of respondents
mapping(address => mapping(uint => uint)) answer;
}
mapping(string => qList) questionnaires;
function vote(string ID, uint qNum, uint ans) returns (bool) {
if(voters[msg.sender][ID]) throw;
voters[msg.sender][ID] = true;
questionnaires[ID].count += 1;
questionnaires[ID].answer[msg.sender][qNum] = ans;
return true;
}
function getNumResult(string ID) constant returns (uint res) {
return questionnaires[ID].count;
}
}
The function "vote" including arguments can be called and mined successfully, howerver I cannot get the return value with the status message "Waiting for transaction to be mined..." in solidity-browser screen when I call "getNumResult" with ID which has already registered via the function "vote".
Appreciate it if someone would advise the cause of this and solution to get the return value of function with arguments.
The author also asked on the Ethereum Stack Exchange and here's one answer.
With non-constant function vote, you can only get the transaction hash back immediately because the transaction may never get mined. Or it could take several blocks as indicated by "Waiting for transaction to be mined..."
Recommend to check: https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call
Events
Events are needed to get the "return value" of vote.
Example of how to add and trigger an event:
contract answer{
// ...
event VoteEvent(string ID, bool returnValue);
function vote(string ID, uint qNum, uint ans) returns (bool) {
// ...
VoteEvent(ID, true);
return true;
}
}
See Contract Events for the different ways to watch for and get event data using web3.js.