What exactly is a single unit of bandwidth, what’s the difference between net and cpu, how do we determine how much bandwidth we need to perform an action, and how is it priced with demand?
Bandwidth is made up of CPU and NET. CPU is computing time measured in microseconds, and NET is data storage measured in bytes. You need NET according to how much space it requires to store your action in the blockchain, and you need CPU according to how much time it takes to the producers to process your action. They're both priced based on the relevant resource supply and the recent demand in the network of said resource.
Some information about resource supply for the chain can be found using $ cleos get table eosio eosio global
, and demand can be computed by each individual BP based on the transactions they process, and, according to the whitepaper, the algorithm "would typically be implemented by a block producer via the writing of a custom plugin."
The whitepaper has a section explaining this:
Token Model and Resource Usage
Bandwidth and computation have two components, instantaneous usage and
long-term usage. A blockchain maintains a log of all Actions and this
log is ultimately stored and downloaded by all full nodes. With the
log of Actions, it is possible to reconstruct the state of all
applications.
The computational debt is calculations that must be performed to
regenerate state from the Action log. If the computational debt grows
too large then, it becomes necessary to take snapshots of the
blockchain's state and discard the blockchain's history. If
computational debt grows too quickly then it may take 6 months to
replay 1 year worth of transactions. It is critical, therefore, that
the computational debt be carefully managed.
...
Block producers publish their available capacity for bandwidth,
computation, and state. The EOS.IO software allows each account to
consume a percentage of the available capacity proportional to the
amount of tokens held in a 3-day staking contract. For example, if a
blockchain based on the EOS.IO software is launched and if an account
holds 1% of the total tokens distributable pursuant to that
blockchain, then that account has the potential to utilize 1% of the
state storage capacity.
And about costs:
Objective and Subjective Measurements
As discussed earlier, instrumenting computational usage has a
significant impact on performance and optimization; therefore, all
resource usage constraints are ultimately subjective, and enforcement
is done by block producers according to their individual algorithms
and estimates. These would typically be implemented by a block
producer via the writing of a custom plugin.
That said, there are certain things that are trivial to measure
objectively. The number of Actions delivered, and the size of the data
stored in the internal database are cheap to measure objectively. The
EOS.IO software enables block producers to apply the same algorithm
over these objective measures but may choose to apply stricter
subjective algorithms over subjective measurements.
Also, there is a video of Dan explaining some of these concepts, and a website by EOS New York tracking the costs and allowing you to estimate potential bandwidth costs. In EOS Resource Planner source code, you can see how the costs are estimated:
var cpuStaked = xDoc.total_resources.cpu_weight.substr(0,xDoc.total_resources.cpu_weight.indexOf(' '));
var cpuAvailable = xDoc.cpu_limit.max / 1000; // convert microseconds to milliseconds
var cpuPrice = cpuStaked / cpuAvailable;
target.innerHTML = cpuPrice.toFixed(8) + " EOS per Ms";
target = document.getElementById("cpu-price-usd");
target.innerHTML = "~ $" + (cpuPrice.toFixed(8) * eosPriceUsd).toFixed(3) + " USD per Ms";
where xDoc
is the JSON response you get when you use the get_account
end-point; example:
$ curl --request POST \
--url https://api.eosnewyork.io/v1/chain/get_account \
--data '{"account_name":"eosnewyorkio"}'
and the formula is resource_staked / resource_available * eos_price