12 ms·
Just skimming through the Solidity docs, I see a lot of unwise decisions there aside from the weird visibility defaults. All state is mutable by default (this
by int_19h 9y ago
Just skimming through the Solidity docs, I see a lot of unwise decisions there aside from the weird visibility defaults.
All state is mutable by default (this includes struct fields, array elements, and locals). Functions can mutate state by default. Both are overridable by explicit specifiers, much like C++ "const", but you have to remember to do so. Even then, the current implementation doesn't enforce this for functions.
Integers are fixed-size and wrap around, so it's possible to have overflow and underflow bugs. Granted, with 256 bits of precision by default that's harder to do than usual... but still pretty easy if you e.g. do arithmetic on two inputs.
Operators have different semantics depending on whether the operands are literals or not. For example, 1/2 is 0.5, but x/y for x==1 and y==2 is 0. Precision of the operation is also determined in this manner - literals are arbitrary-precision, other values are constrained by their types.
Copy is by reference or by value depending on where the operands are stored. This is implicit - the operation looks exactly the same in code, so unless you look at declarations, you don't know what it actually does. Because mutability is pervasive, this can can have far-reaching effects.
Map data type doesn't throw on non-existing keys, it just returns the default value.
The language has suffixes for literals to denote various units (e.g. "10 seconds" or "1000 ether"). This is purely syntactic sugar, however, and is not reflected in the type system in any way, so "10 second + 1000 ether" is valid code.
Statements allow, but do not require, braces around bodies. This means that dangling "else" is potentially an issue, as is anything else from the same class of bugs (such as the infamous Apple "goto fail" bug).
Functions can be called recursively with no special effort, but the stack size is rather limited, and it looks like there are no tail calls. So there's the whole class of bugs where recursion depth is defined by contract inputs.
Order of evaluation is not defined for expressions. This in a language that has value-returning mutating operators like ++!
Scoping rules are inherited from JS, meaning that you can declare variables inside blocks, but their scope is always the enclosing function. This is more of an annoyance than a real problem, because they don't have closures, which is where JS makes it very easy to shoot yourself in the foot with this approach to scoping.
- vazhifarer 9y ago> Scoping rules are inherited from JS, meaning that you can declare variables inside blocks, but their scope is always the enclosing function That's not still the case with `const` and `let` in Javascript, is it? Lexical scoping still exists, but Block scoping is default now in JS
- MaxfordAndSons 9y ago> Block scoping is default now in JS The default best practice, you mean? Declaring with 'var' will still get you the same lexical scoping rules as always. I believe OP meant 'inherited from pre-ES6 JS'
- positivecomment 9y agoI thought using "var" is deprecated... If there's anyone who has the chance to use let/const but still doesn't, I'd be very curious to learn about their reasoning
- seangrogg 9y agoBad habits, arguably. Been using JS for years so occasionally instances where a more vigilant self would use "let" still get "var" instead. I think part of this is because of issues with previous versions of v8/node where using let at the global level or without 'use strict' threw errors. That being said, "const" is already hardwired.
- hippich 9y agoto get variable defined in function scope? :)
- vazhifarer 9y agoI know right. Principle of least Privilege. Simple.
- matt_wulfeck 9y agoThis is an amazing write up. How long did you spend in the code before jotting this down?
- int_19h 9y agoI was literally writing this list down as I was reading through this: https://solidity.readthedocs.io/en/develop/solidity-in-depth.html https://solidity.readthedocs.io/en/develop/solidity-in-depth...
- vog 9y agoI never understood why they chose such a hacky language (an a VM model that encourages these kinds of languages), and expect people to write supposedly secure (in the sense of: obviously correct!) code with it. Any remotely popular functional programming language created over the last years shows a better design (and taste) than this one. And if that only attracts a certain type of programmers? (pun intended) That is, programmers valuing languages like Haskell/OCaml/F#/Kotlin/Rust, who would only ever touch C with verified compilers and 5 different static analysis tools? Well, that's exactly kind of people you to attract to write your security-sensitive code.
- avaer 9y ago99 out of 100 questions. Solidity is ostensibly designed to let people write smart contracts for Ethereum. More realistically, it is a marketing tool for enabling and onboarding people onto the Ethereum platform, which Ethereum benefits monetarily (enormously so) from. Security and design are secondary goals to the extent that they help prevent disasters which hurt adoption or churn developers away. Through this lens it's not a mystery why the language is "hacky". Perhaps being a good language is not even the driving goal.
- samblr 9y ago>> it is a marketing tool for enabling and onboarding people onto the Ethereum platform +1 : My feelings after going thru basics of ethereum.
- vog 9y ago> 99 out of 100 questions. Curious question of a non-native speaker: What does this phrase mean? (in general, and in this context)
- thegavsie 9y agoThe answer to why most things are done is... Money !
- 9y ago
- kensai 9y agoSeriously, these observations should be submitted to the Solidity team for consideration.
- joncampbelldev 9y agothey are excellent observations, but it seems pretty clear the team would be unable to implement them properly this late in the game, or worse unable to understand why they are a good idea.
- riffraff 9y agoIsn't the team also working on a new language targeting the same VM? If so, they might be useful.
- gradys 9y agoCan people outside of the Ethereum org implement their own languages that target that VM?
- tscs37 9y agoYes. The VM has a public specification and you can implement your own compilers for it. On the blockchain, the VM only cares about the bytecodes you submit, not the code itself.
- gst 9y agoThere are lots of different languages targeting EVM. The (IMO) most promising so far is https://github.com/ethereum/viper https://github.com/ethereum/viper
- mbrock 9y agoYou can also just write bytecode directly. It's a simple stack machine, and contracts should be as simple as possible.
- trevor-e 9y agoCan someone explain to me why they chose to make their own language instead of creating a DSL inside of an existing language?
- lugg 9y agoHubris.
- blunte 9y agoNaivety, youth.
- asaka 9y agoToo young, too simple, sometimes naive.
- creshal 9y agoNow who the hell gave them any money for that?
- mtgx 9y agoInvestors who have FOMO (fear of missing out).
- eternalban 9y ago+No Clue.
- ikken 9y agoThis is because running code on Ethereum VM and storing data is hugely expensive (rightly so, as it's being done on all nodes in the world). Therefore Solidity will try to compile into a VM code that uses the least numbers of cheapest instructions and pack data into as small memory package as possible.
- 9y ago
- quickthrower 9y agoWe need a Haskell transpiler.
- alper 9y agoI am amazed/not amazed that they write financial applications in this language (and do not have the common sense to not do it).
- h1d 9y agoEverything about the blockchain is a test now, it's just the investors who mindlessly invest hoping for a quick 5x is making it bigger than they should be.
- arjo1 9y agoFixing the language is one step. It will still not prevent hax0rs from targeting the bytecode of the VM itself.
- lmm 9y agoPerfect is the enemy of good.
- pmarreck 9y agoIf that BS was true, we should all just switch to PHP right now
- lmm 9y agoOn the contrary. No other language is perfect, so if you can only think of perfect you would keep using PHP; it's only if you recognise the idea that a language can be better while still being imperfect that you can switch to a better language.
- naasking 9y agoThe EVM has been formalized in theorem provers [1], so they're already taking that threat seriously. One threat also mentioned in the above thread that is often overlooked are full abstraction failures, ie. exploitable mismatches between high level language constructs and the machine code to which it gets compiled. [1] http://lambda-the-ultimate.org/node/5003#comment-94646 http://lambda-the-ultimate.org/node/5003#comment-94646
- mifeng 9y agoThank you for succinctly summarizing all my frustrations with Solidity. I am developing a fairly complex smart contract (i.e. not a crowdsale) and it's fucking painful. Trivial computations that would take less than an hour to do in any other language take days to implement correctly. Debugging is also a nightmare. There's no way to step through function calls or set break points, even though it's basically just Javascript. Multiple classes of bugs will generate the same "invalid opcode" error that is only visible at run-time with no indication of what line even triggered the error. This is why I invested in the Tezos crowdsale. Smart contracts are a great idea but there's gotta be a better way.
- pmarreck 9y agoHow do you create unit tests for Solidity code? This would eliminate much of the need for a debugger while also leading to safer code
- mifeng 9y agoUnit tests do help, and they are relatively easy in Truffle, the most used JS framework for Solidity. I guess I need to move to more of a TDD style than I'm used to.
- hrpnk 9y agoIn fact the tolling around Solidity and smart contracts is quite rich. It's possible to write unit tests, execute contracts code on a private chain in a similar way to integration/API tests, and measure the code coverage. Sadly, it's not a common practice to develop contracts with full use of such tooling. See [0] for an analysis of the recent 2017 ICOs and their use of the tooling in crowdsale contracts. A few gems exist, that do quite thorough testing including a CI approach. [0] https://medium.com/@bocytko/would-you-trust-your-money-to-a-smart-contract-3581063d52b1 https://medium.com/@bocytko/would-you-trust-your-money-to-a-...
- pmarreck 9y agoI cannot say enough good things about TDD, but YMMV. I also don't stick to it always... it's just that when I take the time to, my code clearly ends up better. I would imagine it's even MORE beneficial in code that deals with a money...
- lngnmn 9y agoYet another homegrown PHP-sque language. The Fractal Of Bad Design is actually a pattern. My bet they never took any PL course, and have very vague understanding of what consequences certain design decisions would produce. "20 years of PHP coding" or something like that. Any decent PL course would emphasize the first-class-everything (procedures , obviously), lexical scooping for proper closures, strong but dynamic typing (no implicit coersions) etc. No matter which sect one belongs to - ML or Scheme - these are universal fundamentals, the well-researched foundation. PHP/Javascript coders usually do not understand the principles and implications behind languages they use. Otherwise they would develop a healthy aversion, like to a tasteless, vulgar porn. Sane people with PL background would use a dialect of Scheme as a DSL (the numerical tower and all the nice things) or at least Lua (because packers are afraid of parenthesis and see nothing behind of them). Actually, Scheme (or a simplified dialect such as the one inside Julia - flisp) would be almost ideal choice for financial DSLs. For those who are cosplaying a PL expert (with 20 years of PHP and Javascript coding under their belt) there is a good read: http://users.rcn.com/david-moon/PLOT3/ http://users.rcn.com/david-moon/PLOT3/
- sethrin 9y agoI feel like what you say is sensible, but that your tone is needlessly derogatory.
- oxryly1 9y agoNeedlessly? I disagree... the idea that yet another person decided to utilize Javascript as the basis for a new language is distressing and totally justifies such a response.
- sethrin 9y agoI think the tone of that comment is completely unjustifiable in any polite context. Also, bile obscures text: adding invective just lowers the SNR of your speech. Your comment is an example of virtue signaling, which is equally boring.
- deleted 9y ago
- ojr 9y agoIt is interesting to see but I think Solidity is the first software language/framework that people with no experience writing it or running it, comment and write about it on hackernews as if they did. A common criticism is the way for loops and arrays are implemented are broken? Well actually they should rarely be used in contracts, its not like you can send ether/btc to an array recipients or in a for loop. You do not need a complete language like F# and C++ to write contracts, The hacked contract should have had a simple modifier describing only the owner can run this command. Once again the type system doesn't need to be perfect, if your code on the blockchain is broken you pay a transaction fee and the contract doesn't execute it. For a language that was made less than 3 years ago, it amazing to see the progression. It will be a language that people hire for in the near future (already happening). Companies are notorious looking for 5 years experience. Its hard to read the "I told you so replies", when the price of ETH has remained relatively stable during the news
- knocte 9y ago> It is interesting to see but I think Solidity is the first software language/framework that people with no experience writing it or running it, comment and write about it on hackernews as if they did. It's interesting to see how the first virtualmachine specification for the first financial smart contract platform has overlooked all the pitfalls of the language-design industry and it doesn't look that a redesign is going to be addressed very soon because of the difficulty of the redeployment (as naturally backwards compatibility must be the first concern). > Its hard to read the "I told you so replies", when the price of ETH has remained relatively stable during the news Haha, so if the price is stable, this means that Solidity is not embarrassing? I'm sorry but this just means that people only care about security when their funds are the ones affected. Not about "potential" security problems or "design flaws" which sound too technical to them.
- ojr 9y agoThe language was not made from "scratch" but a progression from bitcoin scripts, Ethereum is the like to blockchain dev like Unity is to gamedev, it lowers the barrier to entry, it has a lot of the similar apis and functions as bitcoin, I don't really consider this a bad thing
- wslh 9y agoAnd you are not saying anything about the VM itself which behaves differently in certain cases... Look at our last article on this specific topic: https://blog.coinfabrik.com/smart-contract-short-address-attack-mitigation-failure/ https://blog.coinfabrik.com/smart-contract-short-address-att...
- int_19h 9y agoThe devs really need to understand that in the niche that they're targeting, unless they can be absolutely sure that some default is safe, there should be no default. Most of these issues stem from this same fundamental problem - they pick some default, or some implicit behavior, ostensibly for convenience, but with unanticipated undesirable side effects.
- wslh 9y agoI don't agree, you cannot expect to handle millions of dollars with those tools in the same way you don't write mission critical software in bash.
- pdexter 9y agoTime for the Underhanded Solidity Contest? (http://www.underhanded-c.org/ http://www.underhanded-c.org/)
- mannykannot 9y agoIt is kind of implicit, from the core principles of Ethereum, that there is a global 24/7 Underhanded Solidity contest going on, with valuable prizes to be won.
- thinkmassive 9y agoAlready happening: http://u.solidity.cc/ http://u.solidity.cc/
- z3t4 9y agoYour opinion about javascript and closures in particular makes me question the rest of your comment. Closures in javascript makes it easy to write async code.
- int_19h 9y agoWhat I said about the intersection of closures and variable scoping in JS is not an opinion, it's an objective fact. All too often, people try to do something like declare a var inside a loop, and spin off a closure that captures that var. The reasonable assumption is that the scope of the variable is the body of the loop, and thus every iteration gets its own new copy, and all closures are independent. In reality, all closures get the same variable, and if it is the loop counter or derived from it, and closures are executed later, then they will all see the same value - the one that said variable had after the last iteration. It's even worse if closures try to write to it. This is something that is peculiar to JS. In most other languages, there's no similar issue, because either local variables can't be declared inside loops (or, indeed, at all, treating assignment as implicit declaration), as in Python or Ruby; or if they can, then their scope is what you'd expect it to be, as in C#, Java etc. Writing async code using closures as continuations is not unique to JS, either. All mainstream languages support this now.
- z3t4 9y agoWhat you describe is the moment someone would usually go from PITA to Ninja! From programming sequential to async using functions. The WTF code: for(var i=0; i < textures.length; i++) { textures[i].onload = function (i) { pattern[i] = ctx.createPattern(textures[i], 'repeat'); } } The enlightenment: for(var i=0; i < textures.length; i++) { createPattern(i); } function createPattern(i) { textures[i].onload = function() { pattern[i] = ctx.createPattern(textures[i], 'repeat'); } } In JavaScript you can now however replace var with let and it will be block scoped and the WTF code will work. It's sad though as people learning JavaScript now will not get the beauty of it and will be stuck in callback hell. Function scope in JavaScript pushes you in the right direction. It's like instead of teaching people how to fly, we give them roads so they can walk, like they are used to.
- nicksdjohnson 9y agoSpeaking as an Ethereum core developer here - I see a lot of misdirected criticism and misunderstandings of Ethereum and the platform in the wider tech community. That said, your criticisms here are absolutely spot on; these are definite issues with Solidity as a smart contract programming language. The only small correction I would make concerns this: > Functions can mutate state by default. Both are overridable by explicit specifiers, much like C++ "const", but you have to remember to do so. Even then, the current implementation doesn't enforce this for functions. When interacting with Ethereum you can do two things: send a transaction, or call a function against your local copy of the blockchain. The former is mutating, costs ether (in gas fees) and requires the transaction to be included in a block. The latter is entirely local, cost-free, and any mutations to state are ignored. The 'constant' modifier in Solidity serves only to tag the resulting ABI of a function so that calls to it from common interfaces default to local calls instead of sending transactions. It's not intended to enforce that calls to that contract are read-only, only to indicate to callers that it should be called locally instead of creating a transaction for it.
- int_19h 9y agoDo you guys plan to address these at some point? Actually, let me rephrase this. For better or worse, Solidity is a shipping product now, so you're constrained in your ability to make breaking changes. However, its status as the "official" blessed EVM language can be changed without breaking compatibility. So I guess a better question is, are you planning to replace Solidity with a language that has more emphasis on safety in its design (even if it's called Solidity 2.0 or whatever... clearly it would have to significantly break backwards compatibility). Now, I know that most feedback that you hear is to just take an existing functional language. But after looking at Solidity and EVM specs, I also see why this is not necessarily feasible. And, as one of your contributors has pointed out in this thread, functional style can often be too high-level to keep track of the underlying opcodes and their gas cost. But even sticking with the imperative approach, I think it's clear that there's a lot that could be changed, starting with adopting some key principles like "explicit is better than implicit", "don't pick a default unless it's absolutely safe" etc. Looking at the design of Solidity, it's clear to me that the primary inspiration was JS with a dash of C++. The one thing that's common to both is that they allow and encourage "clever" code. Perhaps looking at Ada, Eiffel, and even COBOL for inspiration would be more appropriate for this niche. In any case, if you're interested in specific suggestions on how to redesign, I do have some thoughts, and would be happy to share them.
- phreeza 9y agoHow bound are people to using Solidity? How hard would it be to come up with a competing, more verifiable langauge specification? I don't know much about Ethereum, but I assume the Solidity gets compiled to some sort of bytecode to run on the VM?
- peternicky 9y agoThis was exactly the first thought that came to mind when I first read the Solidity docs.
- 5chdn 9y agoNot at all. Solidity is just the most widely used language for the EVM. There are also Mutan (dead), Serpent (dying), LLL (recently revived), and Viper (being drafted).
- masklinn 9y ago> Order of evaluation is not defined for expressions. This in a language that has value-returning mutating operators like ++! I don't think that one even matters, that the language has side-effecting expressions (through impure functions) is enough to make an undefined OOE problematic. > Integers are fixed-size and wrap around, so it's possible to have overflow and underflow bugs. Granted, with 256 bits of precision by default that's harder to do than usual... but still pretty easy if you e.g. do arithmetic on two inputs. That ignores the problematically trivial "type inference" of `var`: type is decided based on the smallest type which can hold the literal. So any implicitly typed variable initialised to 0 is an 8-bit unsigned integer.