Build Your First Ethereum DApp by Bruno Skvorc
Author:Bruno Skvorc [Bruno Skvorc]
Language: eng
Format: epub
Publisher: SitePoint
Published: 2018-08-15T21:00:00+00:00
Ahem, Excuse Me ...
Technically, we could also check to make sure that the submitter’s address isn’t a zero-address.
While we’re here, let’s define two events: one for deleting an entry, one for creating it.
event SubmissionCreated(uint256 index, bytes content, bool image, address submitter); event SubmissionDeleted(uint256 index, bytes content, bool image, address submitter);
There’s a problem, though. Mappings in Ethereum are not iterable: we can’t loop through them without significant hacking.
To loop through them all, we’ll create an array of identifiers for these submissions wherein the keys of the array will be the index of the submission, while the values will be unique hashes we’ll generate for each submission. Solidity provides us with the keccak256 hashing algorithm for generating hashes from arbitrary values, and we can use that in tandem with the current block number to make sure an entry isn’t duplicated in the same block and get some degree of uniqueness for each entry. We use it like this: keccak256(abi.encodePacked(_content, block.number));. We need to encodePacked the variables passed to the algorithm because it needs a single parameter from us. That’s what this function does.
We also need to store the submissions somewhere, so let’s define two more contract variables.
mapping (bytes32 => Submission) public submissions; bytes32[] public submissionIndex;
Okay, let’s try and build the createSubmission function now.
function createSubmission(bytes _content, bool _image) external payable { uint256 fee = calculateSubmissionFee(); require(msg.value >= fee, "Fee for submitting an entry must be sufficient."); bytes32 hash = keccak256(abi.encodePacked(_content, block.number)); require(!submissions[hash].exists, "Submission must not already exist in same block!"); submissions[hash] = Submission( _content, _image, submissionIndex.push(hash), msg.sender, true ); emit SubmissionCreated( submissions[hash].index, submissions[hash].content, submissions[hash].image, submissions[hash].submitter ); nonDeletedSubmissions += 1; }
Let’s go through this line by line:
function createSubmission(bytes _content, bool _image) external payable {
The function accepts bytes of content (bytes is a dynamically sized array of bytes, useful for storing arbitrary amounts of data) and a boolean flag for whether or not this input is an image. The function is only callable from the outside world and is payable which means it accepts Ether alongside the transaction call.
uint256 fee = calculateSubmissionFee(); require(msg.value >= fee, "Fee for submitting an entry must be sufficient.");
Next, we calculate how much it costs to submit a new entry and then check if the value passed along with the transaction is equal to or greater than the fee.
bytes32 hash = keccak256(abi.encodePacked(_content, block.number)); require(!submissions[hash].exists, "Submission must not already exist in same block!");
We then calculate the hash of this entry (bytes32 is a fixed-size array of 32 bytes, so 32 characters which is also the length of the output of keccak256). We use this hash to find out if a submission with that hash already exists and cancel everything if it does.
submissions[hash] = Submission( _content, _image, submissionIndex.push(hash), msg.sender, true );
This part creates a new submission at the hash location in the submissions mapping. It simply passes in the values via a new struct as defined above in the contract. Note that while you might be used to the new keyword from other languages, it isn’t necessary (or allowed) here. We then emit
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7808)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6442)
Kotlin in Action by Dmitry Jemerov(5089)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3944)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3354)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3101)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2887)
The Art Of Deception by Kevin Mitnick(2621)
Drugs Unlimited by Mike Power(2478)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2452)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2421)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2320)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2275)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2200)
JavaScript by Example by S Dani Akash(2152)
DarkMarket by Misha Glenny(2095)
Hands-On Cybersecurity with Blockchain by Rajneesh Gupta(2093)
Wireless Hacking 101 by Karina Astudillo(2091)
