Multiparty State Channels - SPK Network Team Meeting

avatar

▶️ Watch on 3Speak


multiparty.png

Join us as we discuss some design choices and challenges with building multi-party transactions. We are hoping the community has every opportunity to understand and contribute feedback. For your clarity the code below is what's being referenced in the video... with some additional //comments.


/*
json => 
from => 3spk or account with broca
to => person who can upload a file
broker => account that can recieve an upload
broca => amount of broca to place into contract
*/

exports.channel_open = (json, from, active, pc) => { //the contract json:payload, from:@hiveaccount, active: active key used, pc:promise chain(for contract ordering)
  if (active && json.to && json.broker){ 
    var Pbroca = getPathNum(["broca", from]); //read memory to compute contract
    var Pproffer = getPathObj([json.contract,'proffer', from, json.to])
    var Pstats = getPathObj(["stats"])
    var PauthB = getPathObj(["authorities", from])
    var PauthT = getPathObj(["authorities", json.to]);
    var PauthF = getPathObj(["authorities", json.from]);
    var Ptemplate = getPathObj(["template", json.contract]);
    Promise.all([Pbroca, Pproffer, Pstats, PauthF, PauthT, PauthB, Ptemplate]).then(mem => {
        var broca = mem[0],
            proffer = mem[1],
            stats = mem[2],
            authF = mem[3],
            authT = mem[4],
            authB = mem[5],
            template = mem[6],
            ops = [],
            err = '' //no log no broca?
        broca = broca_calc(broca, stats, json.block_num) //function is below
        if (typeof template.i != "string")err += `Contract doesn't exist.`
        if (typeof authF != 'string')err += `@${from} hasn't registered a public key. `
        if (typeof authT != "string")err += `@${json.to} hasn't registered a public key. `;
        if (typeof authB != "string")err += `@${json.broker} hasn't registered a public key. `;
        if (proffer.ex)err += `This channel exists: ${proffer.ex.split(':')[1]} `
        if (json.broca > broca.b || json.broca < stats.channel_min)err += `@${from} doesn't have enough BROCA to build a channel`;
        if (!err) {
            proffer.t = json.to //to
            proffer.f = from //from
            proffer.b = json.broker //broker
            proffer.r = parseInt(json.broca) //resource credit
            proffer.a = parseInt(
              (json.broca / stats.channel_min) * stats.channel_bytes
            );
            proffer.s = 1 //status codes 1: exists; 2: following steps
            broca.b -= parseInt(json.broca);
            chronAssign(parseInt(json.block_num + 28800 ), { //builds a "virtual op" to process contract phase expiration
              block: parseInt(json.block_num + 28800 ),
              op: 'channel_check',
              from,
              to: json.to,
              c: json.contract,
              ensure: 1
            }).then(exp_path=>{
              proffer.e = exp_path
              ops.push({
                type: "put",
                path: ["broca", from],
                data: broca,
              });
              const msg = `@${json.to} authorized to upload ${proffer.a} bytes to @${json.broker} by @${from} for ${parseFloat(json.broca / 1000).toFixed(3)} BROCA`;
              ops.push({
                type: "put",
                path: ["feed", `${json.block_num}:${json.transaction_id}`],
                data: msg,
              });
              if (config.hookurl || config.status)
                postToDiscord(msg, `${json.block_num}:${json.transaction_id}`);
              ops.push({
                type: "put",
                path: [json.contract, 'proffer', from, json.to],
                data: proffer,
              });
              if (process.env.npm_lifecycle_event == "test") pc[2] = ops;
              store.batch(ops);
            })

        } else {
        ops.push({
            type: "put",
            path: ["feed", `${json.block_num}:${json.transaction_id}`],
            data: err,
        });
        if (config.hookurl || config.status)
            postToDiscord(err, `${json.block_num}:${json.transaction_id}`);
        if (process.env.npm_lifecycle_event == "test") pc[2] = ops;
        store.batch(ops); // stores memory, called the next contract in the promise chain
        }
    })
  } else {
    pc[0](pc[2]); //ignores this failed contract, processes the next contract in the block.
  }
};

exports.register_authority = (json, from, active, pc) => {//the contract json:payload, from:@hiveaccount, active: active key used, pc:promise chain(for contract ordering)
  if (
    active &&
    json.pubKey &&
    typeof json.pubKey == "string" &&
    json.pubKey.subStr(0, 3) == "STM" && json.pubKey.length == 53 ) {
    var ops = [{ type: "put", path: ["authorities", from], data: json.pubKey }];
    store.batch(ops, pc);
  } else {
    pc[0](pc[2]);
  }
};

const broca_calc = (obj, stats, block_num) => { //broca calculation called by many contracts
    const last_calc = require('./helpers').Base64.toNumber(obj.t) //decode terse memory
    const max = require("./helpers").Base64.toNumber(obj.m); 
    const accured = parseInt((parseFloat(stats.broca_refill) * (block_num - last_calc))/max)
    obj.b += accured
    if(obj.b > max)obj.b = max //places the current earned broca into the object, that will only be updated upon contract completion. This process saves unnecessary overhead for idle accounts. 
    obj.t = require("./helpers").Base64.fromNumber(block_num);
    return obj
}


![image.png](


About the SPK Network:

The SPK Network is a decentralized Web 3.0 protocol that rewards value creators and infrastructure providers appropriately and autonomously by distributing reward tokens so that every user, creator, and platform, will be able to earn rewards on a level playing field.


▶️ 3Speak



0
0
0.000
1 comments