Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Hey Habr!

Ka marang-rang a Bitcoin, li-node tsohle, ka tumellano, li lumellana ka sete sa UTXOs: ke lichelete tse kae tse fumanehang bakeng sa ho sebelisa chelete, ho mang hantle, le tlas'a maemo afe. Sehlopha sa UTXO ke palo e fokolang ea data e hlokahalang bakeng sa node ea validator, ntle le hore node e ke ke ea khona ho netefatsa bonnete ba litšebelisano tse kenang le li-blocks tse nang le tsona.

Tabeng ena, boiteko bo ntse bo etsoa ka litsela tsohle tse ka khonehang ho fokotsa boemeli bo bolokiloeng ba sete sena, ho se hatella ntle le ho lahleheloa ke litiiso tsa tšireletso. Ho fokotseha ha palo ea data e bolokiloeng, ho fokotsa litlhoko tsa sebaka sa disk sa node ea validator, e leng se etsang hore ho qalisoa ha node ea validator e theko e tlaase, e u lumella ho atolosa marang-rang 'me kahoo u eketsa botsitso ba marang-rang.

Ka poso ena re tla beha mohlala oa Rust oa tlhahiso ea morao-rao e tsoang ho sengoli-'moho Lehalima Network Pampiri, Thaddeus Dryja - Utreexo: e matla hash-based accumulator optimized bakeng sa sete Bitcoin UTXO, e lumellang ho fokotsa litlhoko tsa sebaka sa disk bakeng sa node ea validator.

Bothata ke bofe?

E 'ngoe ea mathata a sa feleng a Bitcoin e bile scalability ea eona. Mohopolo oa "banka ea hau" o hloka hore barupeluoa ba marang-rang ba boloke litlaleho tsa lichelete tsohle tse teng bakeng sa tšebeliso. Ho Bitcoin, lichelete tse fumanehang li hlahisoa e le sete sa lihlahisoa tse sa sebelisoang - e leng UTXO-set. Le hoja sena e se setšoantšo se hlakileng, se na le molemo mabapi le ts'ebetso ea ts'ebetsong holim'a setšoantšo seo "sepache" se seng le se seng se nang le "balance" e le mokhoa o ikhethileng, hape o eketsa boinotši (mohlala. Chelete ea tšepe).

Ke habohlokoa ho khetholla pakeng tsa histori ea litšebelisano (seo ho thoeng ke blockchain) le boemo ba hona joale ba tsamaiso. Nalane ea transaction ea Bitcoin hajoale e nka 200 GB ea sebaka sa disk, mme e ntse e tsoela pele ho hola. Leha ho le joalo, boemo ba tsamaiso bo nyane haholo, ka tatellano ea 4 GB, 'me bo nka feela taba ea hore motho o na le lichelete tsa tšepe hona joale. Bophahamo ba data ena bo boetse bo eketseha ha nako e ntse e feta, empa ka lebelo le fokolang haholo 'me ka linako tse ling le atisa ho fokotseha (sheba CDPV).

Bareki ba bobebe (SPVs) ba netefatsa ts'ireletso ea khoebo bakeng sa bokhoni ba ho boloka boemo bo tlase (UTXO-set) ntle le linotlolo tsa poraefete.

UTXO le UTXO-set

UTXO (Unspent Transaction Output) ke sehlahisoa sa transaction se sa sebelisoang, ntlha ea ho qetela ea leeto la Satoshi e 'ngoe le e' ngoe e fetisitsoeng ka litšebelisano. Litholoana tse sa sebelisoang li fetoha lisebelisoa tsa litšebelisano tse ncha 'me ka hona li sebelisoa (ho sebelisa chelete) le ho tlosoa ho UTXO-set.

Li-UTXO tse ncha li lula li etsoa ke litšebelisano:

  • coinbase transactions ntle le ho kenya letsoho: theha li-UTXO tse ncha ha basebetsi ba merafong ba ntša lichelete tsa tšepe
  • litšebelisano tsa kamehla: theha li-UTXO tse ncha ha u ntse u sebelisa sehlopha se itseng sa li-UTXO tse teng

Mokhoa oa ho sebetsa le UTXO:
Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Li-wallet li bala palo ea lichelete tsa tšepe tse fumanehang bakeng sa tšebeliso ea chelete (ho leka-lekana) ho latela palo ea UTXO e fumanehang sepacheng sena bakeng sa tšebeliso ea chelete.

Node e 'ngoe le e' ngoe ea validator, ho thibela boiteko ba ho sebelisa chelete habeli, e tlameha ho beha leihlo sete всех UTXO ha u hlahloba ka 'ngoe ditransekshene ea e 'ngoe le e' ngoe thibela.

Node e tlameha ho ba le logic:

  • Keketso ho UTXO-set
  • Hlakola ho tsoa ho UTXO-set
  • Ho hlahloba boteng ba UTXO e le 'ngoe ka sete

Ho na le mekhoa ea ho fokotsa litlhoko tsa tlhaiso-leseling e bolokiloeng mabapi le sete, ha o ntse o boloka bokhoni ba ho eketsa le ho tlosa likarolo, hlahloba le ho paka boteng ba ntho ka sete e sebelisang. li-accumulators tsa cryptographic.

Libetri tsa UTXO

Mohopolo oa ho sebelisa libeteri ho boloka li-UTXO tse ngata ho ile ha buisanoa ka eona pejana.

The UTXO-set e hahiloe ka fofa, nakong ea pele thibela download (IBD), bolokoa ka ho feletseng le ka ho sa feleng, ha likahare tsa eona li fetoha ka mor'a ho sebetsana le ditransekshene ho tswa ho e 'ngoe le e' ngoe e ncha le e nepahetseng thibela ea marang-rang. Ts'ebetso ena e hloka ho khoasolla hoo e ka bang 200 GB ea data block le ho netefatsa limilione tse makholo tsa li-signature tsa dijithale. Ka mor'a hore ts'ebetso ea IBD e phethoe, ntlha ea bohlokoa ke hore UTXO-set e tla nka hoo e ka bang 4 GB.

Leha ho le joalo, ka li-accumulators, melao ea tumellano ea lichelete e fokotsehile ho netefatsa le ho hlahisa bopaki ba li-cryptographic, 'me moroalo oa ho latella chelete e fumanehang o fetisetsoa ho mong'a lichelete tseo, ea fanang ka bopaki ba ho ba teng le ho ba beng ba tsona.

Accumulator e ka bitsoa setšoantšo se kopaneng sa sete. Boholo ba setšoantšo se bolokiloeng bo tlameha ho ba bo sa fetoheng Utreexo: ho hatella tse ngata tsa UTXO Bitcoin, kapa eketsa sublinearly mabapi le cardinality ea sete le boholo ba element ka boeona, mohlala Utreexo: ho hatella tse ngata tsa UTXO Bitcoin, moo n ke cardinality ea sete e bolokiloeng.

Tabeng ena, accumulator e lokela ho lumella ho hlahisa bopaki ba ho kenyelletsa ntho e itseng ka har'a sete (bopaki ba kenyelletso) le ho etsa hore ho khonehe ho netefatsa bopaki bona ka katleho.

Betri e bitsoa matla haeba e u lumella ho eketsa likarolo le ho tlosa likarolo ho sete.

Mohlala oa betri e joalo e ka ba RSA accumulator e sisintsweng ke Boneh, Bunz, Fisch ka Tshitwe 2018. Accumulator e joalo e na le boholo bo sa feleng ba boemeli bo bolokiloeng, empa bo hloka boteng sephiri se arolelanoeng (seta se tšeptjoang). Tlhokahalo ena e hanana le ho sebetsa ha accumulator e joalo bakeng sa marang-rang a sa tšepahaleng joaloka Bitcoin, kaha ho tsoa ha data nakong ea moloko oa sekhukhu ho ka lumella bahlaseli ho etsa bopaki ba bohata ba ho ba teng ha UTXO, li-node tse thetsang tse nang le UTXO-set e thehiloeng ho accumulator e joalo.

Utreexo

Moralo oa Utreexo o hlahisitsoeng ke Thaddeus Dryja o etsa hore ho khonehe ho theha matla аккумулятор ntle le tshepo-tlala.

Utreexo ke moru oa phethahetseng binary Lifate tsa Merkle mme ke ntshetsopele ya mehopolo e hlahiswang ho Li-accumulators tse sebetsang hantle tsa asynchronous bakeng sa pki e ajoang, ho eketsa bokhoni ba ho tlosa likarolo ho sete.

Sebopeho sa Battery Logical

Lisele tsa betri li hlophisitsoe ka morung oa lifate tse ntle tsa binary. Lifate li laeloa ka bolelele. Setšoantšo sena se khethiloe e le se bonahalang ka ho fetisisa 'me se u lumella ho bona ka mahlo a kelello ho kopanngoa ha lifate nakong ea ts'ebetso ea betri.

Mongoli o hlokomela hore kaha lifate tsohle tse morung li loketse, bolelele ba tsona bo hlalosoa e le matla a mabeli, feela joalokaha palo leha e le efe ea tlhaho e ka emeloa e le kakaretso ea matla a mabeli. Ka lebaka leo, sehlopha leha e le sefe sa makhasi se ka aroloa ka lifate tsa binary, 'me maemong 'ohle, ho eketsa ntho e ncha ho hloka tsebo feela ka metso ea metso ea lifate tse bolokiloeng.

Kahoo, setšoantšo se bolokiloeng sa pokello ea Utreexo ke lethathamo la li-node tsa metso (motso oa Merkle), eseng moru oohle oa lifate.

Ha re emele lenane la likarolo tsa motso joalo ka Vec<Option<Hash>>. Mofuta oa boikhethelo Option<Hash> e bontša hore motsoako oa metso o ka 'na oa haella, ho bolelang hore ha ho na sefate se nang le bophahamo bo loketseng ho accumulator.

/// SHA-256 хеш
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
pub struct Hash(pub [u8; 32]);

#[derive(Debug, Clone)]
pub struct Utreexo {
    pub roots: Vec<Option<Hash>>,
}

impl Utreexo {
    pub fn new(capacity: usize) -> Self {
        Utreexo {
            roots: vec![None; capacity],
        }
    }
}

Ho eketsa lintlha

Pele, a re hlaloseng mosebetsi parent(), e hlokomelang node ea motsoali bakeng sa likarolo tse peli tse fanoeng.

motsoali () mosebetsi

Kaha re sebelisa lifate tsa Merkle, motsoali oa node e 'ngoe le e' ngoe ea li-node tse peli ke node e le 'ngoe e bolokang hashe ea concatenation ea li-hashe tsa li-node tsa bana:

fn hash(bytes: &[u8]) -> Hash {
    let mut sha = Sha256::new();
    sha.input(bytes);
    let res = sha.result();
    let mut res_bytes = [0u8; 32];
    res_bytes.copy_from_slice(res.as_slice());

    Hash(res_bytes)
}

fn parent(left: &Hash, right: &Hash) -> Hash {
    let concat = left
        .0
        .into_iter()
        .chain(right.0.into_iter())
        .map(|b| *b)
        .collect::<Vec<_>>();

    hash(&concat[..])
}

Mongoli o hlokomela hore ho thibela litlhaselo tse hlalositsoeng ke Charles Bouillaguet, Pierre-Alain Fouque, Adi Shamir, le Sebastien Zimmer ho.
Litlhaselo tsa bobeli tsa preimage mesebetsing ea dithered hash, ho phaella ho li-hashes tse peli, bophahamo bo ka hare ho sefate bo boetse bo lokela ho kenngoa ho concatenation.

Ha o ntse o eketsa likarolo ho accumulator, o hloka ho boloka tlaleho ea hore na ke likarolo life tse fetotsoeng. Ka ho latela tsela ea ho fetola likarolo tsa motso bakeng sa ntho e 'ngoe le e' ngoe eo u e eketsang, hamorao u ka etsa bopaki ba boteng ba likarolo tsena.

Latela liphetoho ha u li kenya

Ho latela liphetoho tse entsoeng, a re phatlalatseng sebopeho Update, e tla boloka data mabapi le liphetoho tsa node.

#[derive(Debug)]
pub struct Update<'a> {
    pub utreexo: &'a mut Utreexo,
    // ProofStep хранит "соседа" элемента и его положение
    pub updated: HashMap<Hash, ProofStep>,
}

Ho eketsa element ho betri, o hloka:

  • Theha letoto la libaskete tsa likarolo tsa motso new_roots 'me u behe likarolo tse teng tsa metso moo, e le' ngoe bakeng sa bakete ka 'ngoe:

khoutu

let mut new_roots = Vec::new();

for root in self.roots.iter() {
    let mut vec = Vec::<Hash>::new();
    if let Some(hash) = root {
        vec.push(*hash);
    }

    new_roots.push(vec);
}

  • Kenya lintlha tse tla eketsoa (array insertions) ho isa koloing ea pele new_roots[0]:

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

khoutu

new_roots[0].extend_from_slice(insertions);

  • Kopanya lintho tse kentsoeng serotong sa pele le tse ling kaofela:
    • Bakeng sa likariki tsohle tse nang le lintho tse fetang bonngoe:
      1. Nka likarolo tse peli ho tloha qetellong ea basketeng, bala motsoali oa bona, tlosa likarolo tse peli
      2. Kenya motsoali ea balloang kariking e latelang

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

khoutu

for i in 0..new_roots.len() {
    while new_roots[i].len() > 1 {
        // Объединяем два элемента в один и удаляем их
        let a = new_roots[i][new_roots[i].len() - 2];
        let b = new_roots[i][new_roots[i].len() - 1];
        new_roots[i].pop();
        new_roots[i].pop();
        let hash = self.parent(&a, &b);

        // Наращиваем количество корзин если требуется
        if new_roots.len() <= i + 1 {
            new_roots.push(vec![]);
        }

        // Помещаем элемент в следующую корзину
        new_roots[i + 1].push(hash);

        // Не забываем отслеживать изменения;
        // это пригодится для генерации доказательства добавления элементов
        updated.insert(a, ProofStep { hash: b, is_left: false });
        updated.insert(b, ProofStep {hash: a, is_left: true });
    }
}

  • Tsamaisa likarolo tsa motso ho tloha meqomong ho ea ho pokello ea li-accumulator

khoutu

for (i, bucket) in new_roots.into_iter().enumerate() {
    // Наращиваем аккумулятор если требуется
    if self.roots.len() <= i {
        self.roots.push(None);
    }

    if bucket.is_empty() {
        self.roots[i] = None;
    } else {
        self.roots[i] = Some(bucket[0]);
    }
}

Ho etsa bopaki ba lintlha tse ekelitsoeng

Bopaki ba ho kenngoa ha sele ka har'a betri (Proof) e tla sebetsa joalo ka Merkle Path, e nang le ketane ProofStep. Haeba tsela e sa lebise kae kapa kae, joale bopaki ha boa nepahala.

/// Единичный шаг на пути к элементу в дереве Меркла.
#[derive(Debug, Copy, Clone)]
pub struct ProofStep {
    pub hash: Hash,
    pub is_left: bool,
}

/// Доказательство включения элемента. Содержит сам элемент и путь к нему.
#[derive(Debug, Clone)]
pub struct Proof {
    pub steps: Vec<ProofStep>,
    pub leaf: Hash,
}

Ho sebelisa tlhaiso-leseling e fumanoeng pejana ha u eketsa ntho (sebopeho Update), o ka etsa bopaki ba hore element e kentsoe betri. Ho etsa sena, re feta lethathamong la liphetoho tse entsoeng ebe re eketsa mohato o mong le o mong tseleng ea Merkle, eo hamorao e tla sebetsa e le bopaki:

khoutu

impl<'a> Update<'a> {
    pub fn prove(&self, leaf: &Hash) -> Proof {
        let mut proof = Proof {
            steps: vec![],
            leaf: *leaf,
        };

        let mut item = *leaf;
        while let Some(s) = self.updated.get(&item) {
            proof.steps.push(*s);
            item = parent(&item, &s);
        }

        proof
    }
}

Mokhoa oa ho theha bopaki

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Ho hlahloba bopaki bakeng sa element

Ho hlahloba bopaki ba ho kenyelletsoa ha element ho theohela ho latela tsela ea Merkle ho fihlela ho lebisa ho motsoako o teng:

pub fn verify(&self, proof: &Proof) -> bool {
    let n = proof.steps.len();
    if n >= self.roots.len() {
        return false;
    }

    let expected = self.roots[n];
    if let Some(expected) = expected {
        let mut current_parent = proof.leaf;
        for s in proof.steps.iter() {
            current_parent = if s.is_left {
                parent(&s.hash, &current_parent)
            } else {
                parent(&current_parent, &s.hash)
            };
        }

        current_parent == expected
    } else {
        false
    }
}

Ka pono:

Mokhoa oa ho hlahloba bopaki ba A

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Ho tlosa lintho

Ho tlosa sele ho betri, o tlameha ho fana ka bopaki bo nepahetseng ba hore sele e teng. U sebelisa lintlha tse tsoang bopaking, hoa khoneha ho bala likarolo tse ncha tsa metso ea accumulator eo bopaki bo fanoeng bo ke keng ba hlola bo e-ba 'nete.

Algorithm e tjena:

  1. Joalo ka ho eketsa, re hlophisa sete ea libaskete tse se nang letho tse tsamaellanang le lifate tsa Merkle tse nang le bophahamo bo lekanang le matla a tse peli ho tsoa ho index ea baskete.
  2. Re kenya likarolo ho tloha mehatong ea tsela ea Merkle ka har'a libaskete; index ea baskete e lekana le palo ea mohato oa hajoale
  3. Re tlosa karolo ea motso eo tsela e tsoang ho bopaki e lebisang ho eona
  4. Joalo ka ho eketsa, re bala likarolo tse ncha tsa motso ka ho kopanya likarolo ho tsoa ho libaskete ka bobeli le ho tsamaisa sephetho sa kopano ho seroto se latelang.

khoutu

fn delete(&self, proof: &Proof, new_roots: &mut Vec<Vec<Hash>>) -> Result<(), ()> {
    if self.roots.len() < proof.steps.len() || self.roots.get(proof.steps.len()).is_none() {
        return Err(());
    }

    let mut height = 0;
    let mut hash = proof.leaf;
    let mut s;

    loop {
        if height < new_roots.len() {
            let (index, ok) = self.find_root(&hash, &new_roots[height]);
            if ok {
                // Remove hash from new_roots
                new_roots[height].remove(index);

                loop {
                    if height >= proof.steps.len() {
                        if !self.roots[height]
                            .and_then(|h| Some(h == hash))
                            .unwrap_or(false)
                        {
                            return Err(());
                        }

                        return Ok(());
                    }

                    s = proof.steps[height];
                    hash = self.parent(&hash, &s);
                    height += 1;
                }
            }
        }

        if height >= proof.steps.len() {
            return Err(());
        }

        while height > new_roots.len() {
            new_roots.push(vec![]);
        }

        s = proof.steps[height];
        new_roots[height].push(s.hash);
        hash = self.parent(&hash, &s);
        height += 1;
    }
}

Mokhoa oa ho tlosa ntho "A":
Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Ho kopanya marang-rang a teng

U sebelisa accumulator e reriloeng, li-node li ka qoba ho sebelisa DB ho boloka li-UTXO tsohle ha li ntse li khona ho fetola UTXO-set. Leha ho le joalo, bothata ba ho sebetsa ka bopaki bo hlaha.

Ha re bitse node ea validator e sebelisang pokello ea UTXO compact (compact-state node), le validator ntle le accumulator ke tlatsa (node ​​e felletseng). Ho ba teng ha lihlopha tse peli tsa li-node ho baka bothata ba ho li kopanya ka marang-rang a le mong, kaha li-compact nodes li hloka bopaki ba ho ba teng ha UTXOs, tse sebelisoang ka litšebelisano, ha li-node tse feletseng li sa etse joalo. Haeba li-node tsohle tsa marang-rang li sa sebetse ka nako e le 'ngoe' me ka mokhoa o kopanetsoeng li fetohela ho sebelisa Utreexo, joale li-node tse kopanetsoeng li tla sala morao 'me li ke ke tsa khona ho sebetsa marang-rang a Bitcoin.

Ho rarolla bothata ba ho kopanya li-node tsa compact marang-rang, ho etsoa tlhahiso ea ho hlahisa sehlopha se eketsehileng sa li-node - marokho. Node ea borokho ke node e felletseng e bolokang betri ea Utreexo le bopaki ba matla bakeng sa всех UTXO e tsoang ho UTXO-set. Marokho a bala li-hashes tse ncha 'me a ntlafatsa pokello ea li-accumulator le bopaki ha li-blocks tse ncha tsa transaction li fihla. Ho boloka le ho ntlafatsa accumulator le bopaki ha ho fane ka mojaro o eketsehileng oa computational ho li-node tse joalo. Marokho a nyehela sebaka sa disk: ho hloka ho boloka lintho li hlophisehile Utreexo: ho hatella tse ngata tsa UTXO Bitcoin hashes, bapisoa le Utreexo: ho hatella tse ngata tsa UTXO Bitcoin li-hashes bakeng sa li-compact node, moo n e leng matla a sete ea UTXO.

Mehaho ea marang-rang

Utreexo: ho hatella tse ngata tsa UTXO Bitcoin

Marokho a etsa hore ho khonehe ho eketsa butle-butle li-node tsa compact ho marang-rang ntle le ho fetola software ea li-node tse teng. Li-node tse felletseng li sebetsa joalo ka pele, li aba litšebelisano le li-blocks har'a tsona. Li-Node tsa borokho ke li-node tse felletseng tse bolokang data ea betri ea Utreexo le sete ea bopaki ba ho kenyelletsa всех UTXO hajoale. Node ea borokho ha e ipatsitse joalo, e iketsa eka ke sebaka se felletseng bakeng sa li-node tsohle tse felletseng le node e kopaneng bakeng sa tsohle tse kopaneng. Leha marokho a hokahanya marang-rang ka bobeli, a hloka feela ho a hokahanya ka lehlakoreng le le leng: ho tloha ho li-node tse teng ho isa ho li-compact node. Sena se ka khoneha hobane mokhoa oa transaction ha o hloke ho fetoloa, 'me bopaki ba UTXO bakeng sa li-compact node bo ka lahloa, kahoo node leha e le efe e kopanetsoeng e ka phatlalatsa litšebelisano ho barupeluoa bohle ba marang-rang ntle le ho kenya letsoho ha li-node tsa borokho.

fihlela qeto e

Re shebile betri ea Utreexo mme ra kenya tšebetsong mohlala oa eona ho Rust. Re shebile meralo ea marang-rang e tla lumella ho kopanngoa ha li-node tse thehiloeng ho betri. Molemo oa li-compact catches ke boholo ba data e bolokiloeng, e itšetlehileng ka logarithmically ka matla a sete sa UTXO, e leng ho fokotsang haholo litlhoko tsa sebaka sa disk le ts'ebetso ea polokelo bakeng sa li-node tse joalo. Phoso ke sephethephethe se eketsehileng sa node bakeng sa ho fetisa bopaki, empa mekhoa ea ho bokella bopaki (ha bopaki bo le bong bo paka boteng ba likarolo tse 'maloa) le caching li ka thusa ho boloka sephethephethe ka har'a meeli e amohelehang.

litšupiso:

Source: www.habr.com

Eketsa ka tlhaloso