DELEGATECALL
basically says that I'm a contract and I'm allowing (delegating) you to do whatever you want to my storage. DELEGATECALL
is a security risk for the sending contract which needs to trust that the receiving contract will treat the storage well.
DELEGATECALL
was a new opcode that was a bug fix for CALLCODE
which did not preserve msg.sender
and msg.value
. If Alice invokes Bob who does DELEGATECALL
to Charlie, the msg.sender
in the DELEGATECALL
is Alice (whereas if CALLCODE
was used the msg.sender
would be Bob).
Details
When D does CALL on E, the code runs in the context of E: the storage of E is used.
When D does CALLCODE on E, the code runs in the context of D. So imagine that the code of E is in D. Whenever the code writes to storage, it writes to the storage of account D, instead of E.
contract D {
uint public n;
address public sender;
function callSetN(address _e, uint _n) {
_e.call(bytes4(sha3("setN(uint256)")), _n); // E's storage is set, D is not modified
}
function callcodeSetN(address _e, uint _n) {
_e.callcode(bytes4(sha3("setN(uint256)")), _n); // D's storage is set, E is not modified
}
function delegatecallSetN(address _e, uint _n) {
_e.delegatecall(bytes4(sha3("setN(uint256)")), _n); // D's storage is set, E is not modified
}
}
contract E {
uint public n;
address public sender;
function setN(uint _n) {
n = _n;
sender = msg.sender;
// msg.sender is D if invoked by D's callcodeSetN. None of E's storage is updated
// msg.sender is C if invoked by C.foo(). None of E's storage is updated
// the value of "this" is D, when invoked by either D's callcodeSetN or C.foo()
}
}
contract C {
function foo(D _d, E _e, uint _n) {
_d.delegatecallSetN(_e, _n);
}
}
When D does CALLCODE on E, msg.sender
inside E is D as commented in the code above.
When an account C invokes D, and D does DELEGATECALL on E, msg.sender
inside E is C. That is, E has the same msg.sender
and msg.value
as D.
You can quickly test above in Solidity Browser.
1) Why are opcodes in Ethereum priced the way they are priced and why is die EVM so expensive?
Opcodes are priced in units of gas, which is a measure of work. They have no intrinsic cost to the user: the cost to the user depends on the gas price. The gas price is in units of wei (or gwei), which can be translated to a value in fiat currency.
2) Right upfront, I do not say that it is to expensive to perform a tx on Ethereum, I just say it the prices is not based on any fundamental reason, so why not make it different (for example cheaper)?
As mentioned above, the opcode cost is not directly related to a specific cost to the user. Only when gas price is taken into account - which the user is free to choose - does the a real cost become evident.
Furthermore only one node gets the Gas fees from the tx, so the other nodes that perform that action (tx) do not see anything of that price.
Correct, but it is in the best interest of any given full node to run all transactions to ensure the state transitions are valid. If nodes didn't do this, they could not rely on their own state. Yes, only the miner who solves the proof of work puzzle gets the transaction fees, but all miners must ensure they are attempting to mine subsequent blocks on a valid foundation.
So why is an action not as expensive as an action would cost on one node?
It is, given the previous part of the answer.
I guess WEI opcode prices are set relative to each other, but as alredy pointed out the economics behind this make no sense to me.
Okay, here's your problem. Opcode prices aren't in wei. They are in abstract units of gas. To get the total cost of an opcode (i.e. what the user is going to pay, in gwei), you use its gas cost (in units of 'gas') and multiply it by the gas price you want to use.
So: gas * gasPrice = transaction fee
Related: How to calculate transaction fee?
You are free to choose any gas price you like. However, you have to understand that gas prices are a market. If you pick a gas price at the low end of the market, miners won't choose to mine your transactions. As with any healthy market, gas prices change with supply and demand. (Of use: https://ethgasstation.info/)
Best Answer
Additionally,