<li>Editor: Oskar Thorén <ahref="mailto:oskar@status.im">oskar@status.im</a></li>
</ul><p>This specification outlines how we do accounting and settlement based on the provision and usage of resources, most immediately bandwidth usage and/or storing and retrieving of Waku message. This enables nodes to cooperate and efficiently share resources, and in the case of unequal nodes to settle the difference through a relaxed payment mechanism in the form of sending cheques.</p>
<p>The Waku network makes up a service network, and some nodes provide a useful service to other nodes. We want to account for that, and when imbalances arise, settle this. The core of this approach has some theoretical backing in game theory, and variants of it have practically been proven to work in systems such as Bittorrent. The specific model use was developed by the Swarm project (previously part of Ethereum), and we re-use contracts that were written for this purpose.</p>
<p>By using a delayed payment mechanism in the form of cheques, a barter-like mechanism can arise, and nodes can decide on their own policy as opposed to be strictly tied to a specific payment scheme. Additionally, this delayed settlement eases requirements on the underlying network in terms of transaction speed or costs.</p>
<p>Theoretically, nodes providing and using resources over a long, indefinite, period of time can be seen as an iterated form of <ahref="https://en.wikipedia.org/wiki/Prisoner%27s_dilemma">Prisoner’s Dilemma (PD)</a>. Specifically, and more intuitively, since we have a cost and benefit profile for each provision/usage (of Waku Message’s, e.g.), and the pricing can be set such that mutual cooperation is incentivized, this can be analyzed as a form of donations game.</p>
<p>What follows is a sketch of what the game looks like between two nodes. We can look at it as a special case of iterated prisoner’s dilemma called a <ahref="https://en.wikipedia.org/wiki/Prisoner%27s_dilemma#Special_case:_donation_game">Donation game</a> where each node can cooperate with some benefit <code>b</code> at a personal cost <code>c</code>, where <code>b>c</code>.</p>
<p>From A’s point of view:</p>
<table>
<thead>
<tr>
<th>A/B</th>
<th>Cooperate</th>
<th>Defect</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cooperate</td>
<td>b-c</td>
<td>-c</td>
</tr>
<tr>
<td>Defect</td>
<td>b</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>What this means is that if A and B cooperates, A gets some benefit <code>b</code> minus a cost <code>c</code>. If A cooperates and B defects she only gets the cost, and if she defects and B cooperates A only gets the benefit. If both defect they get neither benefit nor cost.</p>
<p>In our case, this means <code>b>b-c>0>-c</code> and <code>2(b-c)> b-c</code> which is trivially true.</p>
<p>As this is an iterated game with no clear finishing point in most circumstances, a tit-for-tat strategy is simple, elegant and functional. To be more theoretically precise, this also requires reasonable assumptions on error rate and discount parameter. This captures notions such as “does the perceived action reflect the intended action” and “how much do you value future (uncertain) actions compared to previous actions”. See <ahref="https://en.wikipedia.org/wiki/The_Evolution_of_Cooperation">Axelrod - Evolution of Cooperation (book)</a> for more details. In specific circumstances, nodes can choose slightly different policies if there’s a strong need for it. A policy is simply how a node chooses to act given a set of circumstances.</p>
<p>A tit-for-tat strategy basically means:</p>
<ul>
<li>cooperate first (perform service/beneficial action to other node)</li>
<li>defect when node stops cooperating (disconnect and similar actions), i.e. when it stops performing according to set parameters re settlement</li>
<li>resume cooperation if other node does so</li>
</ul>
<p>This can be complemented with node selection mechanisms.</p>
<p>We use SWAP for accounting and settlement in conjunction with other request/reply protocols in Waku v2,
where accounting is done in a pairwise manner.
It is an acronym with several possible meanings (as defined in the Book
of Swarm), for example:</p>
<ul>
<li>service wanted and provided</li>
<li>settle with automated payments</li>
<li>send waiver as payment</li>
<li>start without a penny</li>
</ul>
<p>This approach is based on communicating payment thresholds and sending cheques as indications of later payments.
Communicating payment thresholds MAY be done out-of-band or as part of the handshake.
Sending cheques is done once payment threshold is hit.</p>
<p>See <ahref="https://web.archive.org/web/20210126130038/https://gateway.ethswarm.org/bzz/latest.bookofswarm.eth">Book of Swarm</a> section 3.2. on Peer-to-peer accounting etc., for more context and details.</p>
<h2id="accounting">
Accounting
<aclass="anchor"href="#accounting">#</a>
</h2>
<p>Nodes perform their own accounting for each relevant peer based on some “volume”/bandwidth metric. For now we take this to mean the number of <code>WakuMessage</code>s exchanged.</p>
<p>Additionally, a price is attached to each unit. Currently, this is simply a “karma counter” and equal to 1 per message.</p>
<p>Each accounting balance SHOULD be w.r.t. to a given protocol it is accounting for.</p>
<p>NOTE: This may later be complemented with other metrics, either as part of SWAP or more likely outside of it. For example, online time can be communicated and attested to as a form of enhanced quality of service to inform peer selection.</p>
<h2id="flow">
Flow
<aclass="anchor"href="#flow">#</a>
</h2>
<p>Assuming we have two store nodes, one operating mostly as a client (A) and another as server (B).</p>
<ol>
<li>Node A performs a handshake with B node. B node responds and both nodes communicate their payment threshold.</li>
<li>Node A and B creates an accounting entry for the other peer, keep track of peer and current balance.</li>
<li>Node A issues a <code>HistoryRequest</code>, and B responds with a <code>HistoryResponse</code>. Based on the number of WakuMessages in the response, both nodes update their accounting records.</li>
<li>When payment threshold is reached, Node A sends over a cheque to reach a neutral balance. Settlement of this is currently out of scope, but would occur through a SWAP contract (to be specified). (mock and hard phase).</li>
<li>If disconnect threshold is reached, Node B disconnects Node A (mock and hard phase).</li>
</ol>
<p>Note that not all of these steps are mandatory in initial stages, see below for more details. For example, the payment threshold MAY initially be set out of bounds, and policy is only activated in the mock and hard phase.</p>
<h2id="protobufs">
Protobufs
<aclass="anchor"href="#protobufs">#</a>
</h2>
<p>We use protobuf to specify the handshake and signature. This current protobuf is a work in progress. This is needed for mock and hard phase.</p>
<p>A handshake gives initial information about payment thresholds and possibly other information. A cheque is best thought of as a promise to pay at a later date.</p>