Hot answers tagged

68

Creating a private testnet for Ethereum is actually pretty easy. In recent versions of geth, you don't even need to create your own genesis block, or any other complicated steps (many of the online tutorials are out of date). First, make sure you have geth installed. On OSX, you would do this by first installing Homebrew, then running: $ brew tap ethereum/...


41

One way is to use Remix, the browser-based Solidity realtime compiler and runtime Solidity (formerly named browser-solidity). Paste the code in Remix. Wrap the function inside a contract if needed. (Example: contract C { ... }.) Click Contract tab and then Create to instantiate the contract. Enter the desired input next to the function. For bytes1 ... ...


28

Via a Framework There are currently four primary frameworks available that can facilitate writing unit tests for your contracts. Listed in order of Github stars as of 2016-01-25. Disclaimer: I'm the author of the Populus testing library. Embark website: https://iurimatias.github.io/embark-framework/ written in: javascript tests: javascript via Mocha ...


24

As of November 20 2016, the "official" testnet is now Ropsten. Geth 1.5.3 has been released. Simply run geth --testnet: With 1.5.3 --testnet now selects the Ropsten network. If you have a blockchain database for the Morden network, run geth --testnet removedb to remove it There is no fixed schedule when they are reset. Some faucets are: http://...


22

What is Truffle? Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. It is one of the most widely used IDEs in the Ethereum community. Developers can use it to build and deploy DApps for testing purposes with many features that make it more attractive to users with a ...


19

The Remix IDE has a great integrated block-by-block simulator and debugger. Ganache allows fast contract testing without the hassle of running a node. Once the contract is live, the etherscan.io block explorer can be used to view internal state and EVM execution logs.


19

All it needed was to add a package.json file to the project directory with some babel dependencies and doing an 'npm install'. Also, adding a '.babelrc' file to the truffle project directory. Finally, adding some requires to truffle.js file. package.json file { "name": "game-token", "devDependencies": { "babel-preset-es2015": "^6.18.0", "babel-...


19

I would use the analogy of unit testing vs integration testing to distinguish these two methods. In my opinion, a Truffle solidity test can be used to cover a small piece of code, basically you will be able to test every single function in your contract in an isolated way (isolated from Web3 essentially). On the other hand, a Truffle Javascript test (Mocha)...


17

Just to clarify: You would like to run a geth node starting up with the accounts all locked as per the default You later want to run a 'geth attach' command to unlock one or more accounts for a period of time You can use use the following command to attach to your geth node to unlock an account using the personal.unlockAccount(...) JavaScript API (https://...


17

You have some errors, so consider reading the solidity docs. Also, remix is a convenient tool for coding and debugging and coding exercises. I modified your code, as follows: pragma solidity ^0.5.0; contract SimpleEnum { enum SomeData {DEFAULT,ONE,TWO} SomeData someData; constructor() public { someData = SomeData.DEFAULT; } function ...


16

I personally use Dapple for writing Solidity unit tests. It has the advantage of not requiring a Javascript testing framework layer, so you can stay entirely in Solidity. Plus I can use it to test contracts that use imports, which I'm not sure is possible in chriseth's browser-based Solidity. Edit: The accepted answer is definitely a better option for ...


16

Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get: Built-in smart contract compilation, linking, deployment and binary management. Automated contract testing with Mocha and Chai. Configurable build pipeline with support for custom build ...


16

I wrote the truffle-assertions package just for this. It has an assertion to check that an event has been emitted. Essentially, it is similar to the answer below, but it does not need to check a specific index of the logs, and it has the option add complex conditions in a straightforward way by passing a filter function. npm install truffle-assertions You ...


15

Embark is a development framework. It's used to make development faster and more streamlined by providing a set of tools / functionalities to make you more productive. In the case of embark it makes/will make the process of creating a dapp faster by simplifying the interaction between your app front end and the contract (e.g. running your own blockchain for ...


15

Some of the main steps involve specifying a networkId, and creating a genesis file (you can give your account a lot of ether!). Rather than repeat what's been written, there are guides here and here.


15

You can use OpenZeppelin's expectThrow helper - Source: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/test/helpers/expectThrow.js export default async promise => { try { await promise; } catch (error) { // TODO: Check jump destination to destinguish between a throw // and an actual invalid jump. ...


14

Those are all good tools, but I have found the logging events is much easier method for debugging, plus if you are doing anything with money you want to keep a record of what happened. In Solidity, you log with Events. You can then listen to all events on your contract with web3.js "allEvents" callback This is how I debug my (currently) 780 lines of ...


14

If you are just testing contracts I would recommend using a lightweight Ethereum node like https://github.com/trufflesuite/ganache-cli. This way getting your network set up is as simple as running: npm install -g ganache-cli Once it is installed, you can start the testnet with: ganache-cli This Ethereum Node simulator will have a few accounts which are ...


13

There are a few ways to do testing of Solidity contracts. The easiest, at least in my opinion, is blackbox testing with Truffle. Contracts tend to be relatively small and self-contained, so blackbox testing seems appropriate here. Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like ...


12

B9lab's IPFS faucet with command line access B9lab have one deployed via IPFS. this is pretty neat. long description below, here is the one-liner for rushed users: curl -X POST -H "Content-Type: application/json" -d '{"toWhom":"0xdcf407eae88d480e280db2d0deaa3a11c82eaa9b"}' https://ropsten.faucet.b9lab.com/tap Replace 0xdcf407... with your testnet ...


11

You can write a JavaScript script that controls geth's behavior related to mining. There is a simple script that mines only nonempty blocks on go-ethereum wiki. Another one is mine.js script from Embark Framework. This one is feature-rich and configurable for periodic mining, mining reward cap, number of mining threads and more (via glider). Scripts are ...


11

This can be done with the ganache-cli db command. For example, to run a setup similar to the GUI client, I do ganache-cli --db="./data/save/" -i="5777" -d --mnemonic="YOUR_12_WORDS_HERE" Which will dave the network data to ./data/save directory. If the data is already present in that directory, it will return to the same state (provided deterministic and ...


10

If you are on a private net/chain you actually don't need to mine to get ethers, but just to confirm transactions. You can fund your account(s) by specifying the amount in wei you want that account to start with, in your genesis json file: "alloc": { "<your account address e.g. 0xaaabd38c8f1a188a0b8bbf93bdca420cfdd760aa>": { "balance": "...


10

Another option is using EthereumJ implementation. We have recently released version 1.2.0 and it has nice feature specially for such cases. You may check the sample here StandaloneBlockchain bc = new StandaloneBlockchain(); SolidityContract contract = bc.submitNewContract( "contract A { uint a; ... }" ); contract.callFunction("funcName", "arg1", 2,...


10

You need to put the payable modifier in your function. In the latest version of solidity, if a function does not have that modifier, it will throw (i.e. perform an invalid jump) if it receives ether.


9

The Solution Set up 2 connected geth instances on your private network and start them mining. There is an admin.addPeer(...) command in geth to add peer nodes to the geth's list of peers, but there does not seem to be any commands to remove these peers. To simulate the fork, block the network ports used by both instances of geth for their peer-to-peer ...


9

Here's the pattern I currently use to test expected throws (e.g., on invalid input). Solidity implements throw by JUMPing to an invalid destination, so we catch the error and then look for the string "invalid JUMP" in the error message... I'd rather have a more robust way but haven't found anything else yet. var EthWall = artifacts.require("./EthWall.sol");...


9

The other answers in this thread appear to be valid, however I believe this code is more succinct and readable. This works with solidity 0.4.12-develop it("should throw if the car is not blue", function() { return CarFactory.deployed() .then(function(factory) { return factory.createCar("red"); }) .then(assert.fail) ...


9

Not really an answer to what you're asking, but... Decreasing the size of a uint to less that 256 bits will increase the associated gas costs, not decrease it like you might be expecting. It'll be best to leave it as a uint256. See: Why does uint8 cost more gas than uint256? Edit: Noting that timestamps are the number of seconds since the Unix epoch: ...


9

If they are internal functions you can inherit them and test them, if they are private functions, I believe the only way to unit test them is to make them public/internal, test them and then change them back to private once the tests are passing. Another way to go about it would be to refactor your code so that the private functions are part of a library ...


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