Hot answers tagged

46

The Solidity FAQ on "memory" is highly recommended reading in entirety, and a snippet is provided below. The Ethereum Virtual Machine has three areas where it can store items. The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to ...


35

Your example shows storing an IPFS identity using it's alphanumeric encoding (Qm...), which is the same Base58 encoding that Bitcoin uses. However, what it's representing at its core is a number (the hash). Storing the identifier in the Base58 format needs to be a String because it includes letters (and what actually gets saved is the ASCII code for each ...


26

You can store structs as the values of your mapping, not as the key. Here is some more info: http://solidity.readthedocs.org/en/latest/types.html#structs struct Funder { address addr; uint amount; } struct Campaign { address beneficiary; uint fundingGoal; uint numFunders; uint amount; mapping (uint => Funder) funders; } uint numCampaigns; ...


25

They are analogous to memory and hard drive storage in a computer. The contract can use any amount of memory (as long as it can pay for it of course) during executing its code, but when execution stops, the entire content of the memory is wiped, and the next execution will start fresh. The storage on the other hand is persisted into the blockchain itself, so ...


21

Memory is temporary. Storage is permanent. For example, you would perform intermediate computations using memory, and then save the result to storage. Details from the EVM perspective, their structure and gas costs. Storage is a key/value store where keys and values are both 32 bytes. It is sparse (like a hash table), and there are no inherent gas ...


15

There is no need to initialize storage arrays in Solidity. Only memory arrays has to be initialized before usage. So in your case, no need to initialize x inside Bar as long as you are not assigning a value to one of the x indexes inside your foobar. Actually, making initialization in your code will consume gas for no reason. The following code works well ...


14

As of Solidity 0.5.0. https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[...


11

It seems like 2 Gigs of swap and swappiness 30 is enough to run geth with no crashes with just 512 Mb of RAM. It is almost 2 months now. I'm in full sync (block 3097083). And had no crashes ever. So I'm marking this answer as the right one. I'm also monitoring geth with monit which will restart geth if it crashes. My config in detail is here - How to run ...


11

The term "storage location" is a bit misleading here, it refers to a more general concept about where data is stored, i.e. in storage or in memory. The error occurs because the "storage location" is only relevant for reference types like structs and arrays and uints are always passed by value, so you cannot hold a reference to a uint in memory or storage. If ...


9

Dynamic arrays are only available in storage, not in memory. In your case, the size of the result array is known upfront (n). So, you can just declare an array with length of n. Then you can fill it up using i, which goes from 0 to n - 1 pragma solidity ^0.4.21; contract Foo { function getRange(uint n) public pure returns(uint[]) { uint[] ...


6

account is a local object to easily track changes on the accounts (with a StateObject db). The size depends on the nbr of transactions you execute with this account. An unused account should be less than 1kB. The maximum theoretical size for a contract is beyond technology limits, and actually around 90kB. The block.Body contains Transactions. The minimum ...


6

Yes, you can. For example, in the solidity code below. There is a struct User that has a mapping of friends, from address to another struct Friend. Then there's a mapping of users, from address to User [note that User is a struct]. struct Friend { string name; string email; string phoneNumber; } struct User { string name; string email;...


6

It is not possible to reduce RAM usage of geth or any other application for that matter. Simply starting a geth node will not consume much of your RAM, but when you start running commands to create accounts, send transactions etc, you can observe a spike in RAM usage. Syncing a lot of blocks at once might also need more RAM. However, you need low-end ...


6

An account address - either an externally owned account (what you're calling a wallet), or a contract account - isn't the same thing as a memory address. It's not possible to know what exists at "contract address + X", because account addresses aren't a mapping into a contiguous piece memory. As a primer, see the big picture (literally) here: https://...


5

Here are some js functions for stripping and re-adding the first two bytes containing the hash function and size, suitable for web3. import bs58 from 'bs58' // Return bytes32 hex string from base58 encoded ipfs hash, // stripping leading 2 bytes from 34 byte IPFS hash // Assume IPFS defaults: function:0x12=sha2, size:0x20=256 bits // E.g. "...


5

With that limited of resources your best bet is to run in light client mode. Yes it is in alpha but is very usable as long as your just playing around with it, and not using large sums. Depending on your processor you may also have issues creating accounts on server, if so then also add --lightkdf, this will allow a less taxing method(although probably ...


5

Running it on such a low amount of RAM might be tricky. I know the devs have made attempts to reduce RAM usage so it could run on Raspberry PI, but even that has 1GB of RAM. In addition to making a large swap file, you might want to try RAM compression. This might help on a VPS that has ample CPU power, but restricted RAM resources. On Ubuntu you can just ...


5

tl;dr You are right that it is not possible currently, but there's no fundamental reason why Solidity couldn't implement the feature in future. Fully dynamically sized arrays don't sit easily with the EVM's memory model. An arbitrary sized array could exist, but it would have to sit higher in memory than everything else to allow it to grow upwards in an ...


5

Each word in the EVM is 32 = 0x20 bytes wide, so when you use PUSH1 40 to store a word at 0x40 it fills up all the bytes up to 0x5f. The EVM uses "big-endian" format, so the least significant bytes of the word are stored at the end. Thus the byte "0x60" is correctly written to location 0x5f in your example. The layout of the data from Browser Solidity is a ...


5

memory and calldata (as well as storage) are keywords that define the data area where a variable is stored. To answer your question directly, memory should be used when declaring variables (both function parameters as well as inside the logic of a function) that you want stored in memory (temporary), and calldata must be used when declaring an external ...


4

contract C1 { function f1() returns(bytes) { bytes memory bb = "\x00\x01\x02"; // you can extend this return(bb); } } Tested using online Solidity Browser, output is: Result: "...


4

As per documentation: Creating arrays with variable length in memory can be done using the new keyword. You can use following code snippet to solve your problem. uint length =10; bytes memory priorityList= new bytes(length);


4

Definitely old blocks will not get deleted, but new blocks will not get added to the Chaindata in local.


4

The compiler thinks you are trying to store the return value of getBetsForMatchup in contract (permanent) storage. Try changing BetProposition[] betsToCancel = getBetsForMatchup(matchupIdentifier); to BetProposition[] memory betsToCancel = getBetsForMatchup(matchupIdentifier); This compiled on remix: pragma solidity ^0.4.20; contract ...


4

Here's a way to do backerList.length--; on an address[] memory backerList by using inline assembly: assembly { mstore(backerList, sub(mload(backerList), 1)) } Some important points to remember: Make sure this assembly code never runs when backerList.length == 0 (don't allow the array length to underflow) Don't try to use this to increase the size of an ...


3

I have handled similiar situation with this util function in web3.py: import base58 def convertIpfsBytes32(hash_string): bytes_array = base58.b58decode(hash_string) return bytes_array[2:] You need base58 module. Concept is same as accepted answer.


3

Based on my Debug observation: I have tested that: Firstly I insert data into my map data-structure as I explained in my question. Secondly, I have returned the address of first mapped index, which is 0, this could be done for other indexes as well (0:10). Without a delete operation I can retrieve the data perfectly fine from the address of mapped Foo[]....


3

Solidity never reallocates storage arrays (memory arrays can't even be resized). Solidity relies on the the fact that all storage array locations are based on a KECCAK256 hash, and that KECCAK256 is believed to be collision resistant, so the odds of two storage arrays ever colliding are infinitesimally small.


3

After reading docs about types thoroughly, I found that calling delete on dynamic array frees all allocated memory. delete a assigns the initial value for the type to a. I.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements ...


3

You should definitely follow Rob Hitchens recommendation. Now to fix the warning a few things to note: You cannot use struct in public functions, it is a limitation in the ABI specification (perhaps it will be supported in a future version). You can only use push for arrays in storage. If the function is not for use from outside of the contract you can ...


Only top voted, non community-wiki answers of a minimum length are eligible