Hashgraph : An over view with example

29
HASHGRAPH CONSENSUS: DETAILED EXAMPLES LEEMON BAIRD [email protected] DECEMBER 11, 2016 SWIRLDS TECH REPORT SWIRLDS-TR-2016-02 ABSTRACT : The Swirlds hashgraph consensus algorithm is explained through a series of examples on a hashgraph. Each page shows the hashgraph with annotations explaining a step of the algorithm. This covers the core algorithm, from creating transactions, through finding their consensus order and timestamps. The important terms are defined and illustrated on the following pages: April 7, 2017: this version includes corrections. 8 Round created 9 Witness 10 Famous witness 10 Election 10 Vote 11 See 15 Strongly see 15 Supermajority 19 Decide 19 Round created 24 Round received 24 Consensus timestamp 28 Consensus order 2 Hashgraph 2 Member 2 Event 2 Transaction 2 Consensus 2 Order 2 Timestamp 3 Gossip protocol 4 Self-parent 4 Other-parent 7 Graph 7 Hash 7 Hashgraph

Transcript of Hashgraph : An over view with example

Page 1: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 1

HASHGRAPHCONSENSUS:[email protected]

DECEMBER11,2016

SWIRLDSTECHREPORTSWIRLDS-TR-2016-02

ABSTRACT: The Swirlds hashgraph consensus algorithm is explained through a seriesof examples on a hashgraph. Each page shows the hashgraph with annotationsexplaining a step of the algorithm. This covers the core algorithm, from creatingtransactions, through finding their consensus order and timestamps. Theimportant terms are defined and illustrated on the following pages:

April7,2017:thisversionincludescorrections.

8 Round created9 Witness10 Famous witness10 Election10 Vote11 See15 Strongly see15 Supermajority19 Decide19 Round created24 Round received24 Consensus timestamp28 Consensus order

2 Hashgraph2 Member2 Event2 Transaction2 Consensus2 Order2 Timestamp3 Gossip protocol4 Self-parent4 Other-parent7 Graph7 Hash7 Hashgraph

Page 2: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 2A B C D=

Thisfigureisahashgraph.Itgrowsupwardovertime.Everyparticipantkeepsacopyofitinmemory.

Inthisexample,therearefourmembers (fullnodes)inthenetwork.ThemembersareAlice,Bob,Carol,Dave,andarerepresentedbythe4lineslabeledA,B,C,D.

Eachmemberstartsbycreatinganevent,whichisasmalldatastructureinmemory,andwhichisrepresentedherebyagraycircle.

Eacheventisacontainerforzeroormoretransactions.ThegoaloftheSwirldshashgraphconsensusalgorithmisforthemembersofthecommunitytocometoaconsensus(agreement)ontheorder oftheevents(andthustheorderoftransactionsinsidetheevents),andtoagreeonatimestampforeachevent(andsoforeachtransaction).Itshouldbehardforattackerstopreventconsensus,ortoforcedifferentmemberstocometoadifferent“consensus”,ortounfairlyinfluencetheorderandtimestampsthatareagreed.

Page 3: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 3A B C D=

Thecommunityrunsagossipprotocol,whichmeansthateachmemberrepeatedlycallsothersatrandomtosyncwiththem.

Inthiscase,BobrandomlychosetocallDave.Whentheyconnectedovertheinternet,BobsentDavealltheeventsheknewthatDavedidnotyetknow.Inthiscase,itwasjustoneevent:theonethatBobhadcreatedatthestart.

Daverecordsthefactthatthissynchappenedbycreatinganewevent.Thisisthenewcircle,whichhaslinesgoingstraightdowntohisownlastevent,anddiagonallydowntoBob’slastevent.Thus,thegraphofeventsformsarecordofhowthemembershavecommunicated.

TECHNICALDETAIL:BobcanavoidsendingDaveeventshealreadyknows.BobfirsttellsDavehowmanyeventsheknowsaboutthatwerecreatedbyeachmember(i.e.,4integers).DavetellsBobthesame.Thentheywillbothknowexactlywhicheventseachshouldsendtheother.IfBobhas13eventsbyAliceandDavehas10,thenBobsendsAlice’slast3events.

Page 4: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 4A B C D=

hashhash

timestamp

transactions

Event(signedbycreator):

Dave’sneweventisillustratedhere.

Aneventisadatastructurecontainingthetwohashesofthetwoeventsbelowitself(itsself-parentanditsother-parent).Inthiscase,theself-parentisDave’sfirstevent,andtheother-parentisBob’sfirstevent.

TheeventcanoptionallycontainzeroormoretransactionsthatDavewantstosendtothenetwork.Soaneventisacontainerfortransactions.Davealsoputsatimestampofwhenhecreatedtheevent.Hethendigitallysignsit.Whenthiseventisgossiped,thesignaturewillbesentalongwithit.

Page 5: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 5A B C D=

DavethensendsBoballhisevents(includingthenewonehejustcreated).Bobthencreatesaneweventrecordingthefacttheysynced,andincludingthehashesofthemostrecenteventbyhimselfandthemostrecenteventbyDave.

Page 6: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 6A B C D=

BobthenrandomlychoosesAlice,andsendsherall4eventsheknowsabout.Shecreatesanewone.

Page 7: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 7A B C D=

Thiscontinuesforever,growingadirectedacyclicgraphupwardsforever.

Thisisagraph connectedbycryptographichashes,soitiscalledahashgraph.

Eacheventcontainsthehashesoftheeventsbelowitandisdigitallysignedbyitscreator.Sotheentiregraphofhashesiscryptographicallysecure.Itcanalwaysgrow,buttheolderpartsareimmutable,asstrongasthecryptographichashandsignaturesystemused.

Page 8: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 8A B C D=

4

3

2

1

RoundCreated:

Itisusefultodefinearoundcreatedforeachevent.Achildneverhasaroundcreatedbeforeoneofitsparents.Soastimeflowsupwardinthediagram,theroundcreatedcanonlystaythesameorincrease.

Alaterslidewilldescribehowtheroundcreatediscalculated.Theimportantpointisthatassoonasyoureceiveaneventinasync,youcanimmediatelycalculateitsroundcreated.Andanyoneelsereceivingitwillcalculatethesamenumber.Guaranteed.

TECHNICALDETAIL:Thedefinitionis:theroundcreatedforaneventisRorR+1,whereRisthemaxoftheroundcreatedofitsparents.ItisR+1ifandonlyifitcanstronglyseeasupermajorityofroundRwitnesses.Thisisexplainedingreaterdetailinlaterslides.

Page 9: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 9A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

4

3

2

1

RoundCreated:

ThefirsteventthatAlicecreatesineachroundiscalledawitness.HerwitnessesarelabeledA1,A2,andA3here.Theothermemberscreatewitnessessimilarly.

Itispossibleforamembertohavenowitnessesinagivenround.

TECHNICALDETAIL:Itispossibleforamembertocheatbyforking,orcreatingtwoeventswiththesameselfparent.Inthatcase,theremightbetwowitnessesinthesameroundbythesamemember.Therearetheoremsprovingthatthiswon’tmatter.

Page 10: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 10A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Foreachwitness,weneedtodetermineifitisafamouswitness.Forexample,wewilldetermineifthewitnessB2isafamouswitness.

Thisisdonebyconsideringthewitnessesinthenextround.SothefameofB2willbedeterminedbyfirstconsideringthewitnessesA3,B3,C3,andD3.TheideaisforB2tocountasfamousifitisseenbymanyofthewitnessesinthenextround.

Thereisnowanelection,inwhicheachofthosewitnesseswillvote onwhetherB2isfamous.Therewillbeaseparateelectionforeverywitness,todetermineitsfame.

Page 11: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 11A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YES

ThewitnessA3cansee B2.Thatmeansthatthereisanentirely-downwardpathfromA3toB2.Inotherwords,B2isanancestorofA3.AndA3isadescendentofB2

A3canseeB2,soA3willvoteYESintheelectionforwhetherB2isfamous.

TECHNICALDETAIL:A3seesallitsancestorsexceptforthosecreatedbyamemberwhocreatedaforkthatisanancestorofA3.Inotherwords,A3canseeB2ifB2isanancestorofA3,andthecreatorofB2(whoisBob)didnotcreatetwoeventsXandYthatbothhavethesameself-parentandarebothancestorsofA3.So“seeing”isthesameas“ancestor”,exceptyoucan’t“see”cheaters.

Page 12: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 12A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

B3seesB2,soitvotesYES.

Page 13: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 13A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

C3seesB2,soitvotesYES.

Page 14: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 14A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

YES

D3seesB2,soitvotesYES.

AllfourwitnessesvotedYES,sowewouldexpectthatB2willbedeclaredtobefamous.Buttheelectionisn’toveryet!Anelectionisn’toveruntilthevotesarecounted.

Thevoteswillbecountedbythewitnessesinthefollowinground.SoB4willcountthevotes.AndD4willalsocountthevotes.

Thehashgraphdoesn’tyethaveanA4orC4.Butastimegoesonandmoregossipingoccurs,theremayeventuallybeanA4andC4,andthentheywillcountthevotes,too.

Page 15: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 15A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

YES

B4stronglysees:YES

B4willcollectthevotesfromeveryround-3witnessthatitstronglysees.Tostronglyseeawitness,itisn’tenoughfortheretobeasingledownwardpathtoit.Theremustbeenoughdifferentpathstoitsothattogether,thepathsgothroughasupermajorityofthepopulation.

Asupermajority isanynumberthatismorethantwothirdsofthepopulation.Inthisexample,thereare4membersinthepopulation,soany3ofthemconstituteasupermajority.

Inthisexample,B4isabletostronglyseeA3.TheredpathgoesfromB4toA3throughAliceandBob.ThegreenpathgoesthroughAlice,Bob,andDave.TherearenopathsfromB4thatgothroughCaroltogettoA3.Butthat’sOK,becauseAlice,Bob,andDavemakeupasupermajority.SoCarolisn’tneeded.Infact,thegreenpathalonewouldhavebeenenough.Theredpathwasn’tneeded.

So,B4canstronglyseeA3.Therefore,B4collectsthevotefromA3(whichisYES).

Page 16: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 16A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

YES

B4stronglysees:YES,YES,

B4stronglyseesB3,becausetheredpathgoesthroughAliceandBob,andthegreenpathgoesthroughBobandDave.Inthiscase,bothpathswereneededtoreachthesupermajority.

Page 17: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 17A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

YES

B4stronglysees:YES,YES,YES,

B4stronglyseesC3,andsocollectsanotherYESvote.

TECHNICALDETAIL:ifapathstartsatBobandendsatCarol,thenitautomaticallycountsasgoingthroughBobandCarol.Inotherwords,theendpointsofthepatharecounted,too.

Page 18: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 18A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

YESYES

YES

YES

B4stronglysees:YES,YES,YES,YES

B4stronglyseesD3,andsocollectsanotherYESvote.

Page 19: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 19A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Atthispoint,B4hasreceivedYESnotesfromasupermajority,soB4decides thattheelectionresultisYES.ColorB2greentoshowthatitisnowfamous.Thatistheconsensusdecision.

IfB4hadseen3YESand1NO,itwouldstilldecideYES,becausethat’sasupermajority.

IfB4hadseen3YESvotesandnoothervotes(becauseitcouldn’tstronglyseeoneofthewitnesses),itwouldstilldecideYES,becausethat’sasupermajority.

WeneedforB4tostronglyseeasupermajorityofwitnesses,inordertoevenhaveachanceatdeciding.Therefore,weusethistodefinetheroundcreated.IfaneventXhasparentswithamaximumroundcreatedofR,thenthateventwillusuallyberoundR,too.ButifthateventcanstronglyseeasupermajorityofroundRwitnesses,thenthateventisdefinedtoberoundR+1,andsoisawitness.Inotherwords,aneventispromotedtothenextroundwhenitcanstronglyseeasupermajorityofwitnessesinthecurrentround.

Page 20: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 20A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

NONO

YES

NO

NowwerunanelectionforwhetherC2isfamous.

TheyellowpathshowsthatC3canseeC2,andsoC3votesyes.

TherearenodownwardpathsfromA3,B3,orD3toC2,sotheyallvoteNO.

Page 21: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 21A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

B4stronglysees:NO,NO,YES,NO

NONO

YES

NO

SinceB4stronglyseesallofA3,B3,C3,andD3,itwillthereforecollectvotesfromallofthem.

ThevotesareNO,NO,YES,NO.SoasupermajorityisNO.SoitdecidesNO.

Theelectionisover.C2isnotfamous.Coloritbluetoshowthatitisnotfamous.

Page 22: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 22A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Thereisatheoremthatifanywitnessisableto“decide”yesorno,thenthatistheresultoftheelection,anditisguaranteedthatallotherwitnessesthatdecidearegoingtodecidethesameway.

Inthisexample,B4wasabletodecidetheelection.IfithadcollectedvotesthatweremoreevenlysplitbetweenYESandNO,thenitwouldhavefailedtodecide.Inthatcase,wecanconsiderD4.IfD4alsofailstodecide,thenperhapsA4orC4mightdecide.

Ifnoneoftheround-4witnessescandecide,theneachofthemwillsimplyvoteinaccordancewiththemajorityofthevotestheycollected(votingYESincaseofatie).Inthatcase,itwillbeuptotheround-5witnessestocollectvotesfromtheround-4witnesses.Perhapstheround-5witnesseswillbeabletodecide.

Page 23: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 23A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Thevotingcontinuesuntiliteventuallyreachesaroundwheresomewitnesscandecidetheelection.

Thereisatheoremsayingthattheelectionwilleventuallyend(withprobabilityone)aslongasweaddinacoinroundevery10th roundofvoting.

Inacoinround,collectingasupermajoritycausesawitnesstomerelyvote(notdecide).Andanon-supermajoritycausesittovotepseudorandomly,byusingthemiddlebitofitsownsignatureasitsvote.

Page 24: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 24A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

6moreelectionsarerun.TheydecidethatA2,D2,A1,B1,C1,andD1areallfamous.

Innormaloperation,mosteventsarenotwitnesses,sothereisnoelectionformostevents.Andmostwitnessesaredeclaredfamouswithanalmost-unanimousvoteinthefirstroundofvoting.Somostelectionsdonotlastverylong.

Noticethatinthisexample,wehavenowdecidedthefameofeverywitnessinround2.Oncearoundhasthefamedecidedforallofitswitnesses,itispossibletofindtheroundreceivedandfindtheconsensustimestamp foranewsetofevents.

StartbyconsideringthegrayeventimmediatelybelowA2.

TECHNICALDETAIL:Ifamemberforks,theymighthavetwofamouswitnessesinthesameround.Inthatcase,neitherofthemareusedfurther.Onlytheremainingones(the“uniquefamouswitnesses”)areusedtodetermineroundreceivedandconsensustimestamp.

Page 25: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 25A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Thiseventcanbeseenbyeveryfamouswitnessinround2.Thered,green,andbluepathsshowhowA2,B2,andD2,respectively,canallseetheblackevent.

Thismerelyrequiresseeing,notstronglyseeing.

Thisonlyrequiresseeingbythefamouswitnesses.Itdoesn’tmatterwhetherC2canseetheblackevent,becauseC2isnotfamous.

Sincetheblackeventisseenbyallofthefamouswitnessesinround2(butnotinanyearlierround),itissaidtohavearoundreceived of2.

TECHNICALDETAIL:wedon’tneedtolimitourselvesto“seeing”.Itissufficienttousethe“ancestor”relationshipinstead.Inotherwords,atthisstep,wedon’tworryaboutforking.

Page 26: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 26A B C D

A2

A3

B1

B2

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

Theconsensustimestampoftheblackeventcanbefoundasfollows.

FindtheearliesteventXbyAlicethatisanancestorofA2andadescendentoftheblackevent.

Similarly,findtheearliesteventYbyBobthatisanancestorofB2anddescendentoftheblackevent.AndsimilarlyforeventZbyDave.

TakethetimestampsontheeventsX,Y,Zthatwereputinthoseeventsbytheircreators.Sortallofthetimestampsinorder.Takethemiddleonefromthelist(orthesecondofthetwomiddleones,ifthereareanevennumberofthem).Thismediantimestampistheconsensustimestampfortheblackevent.

Page 27: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 27A B C D

A2

A3

B1

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

B2

NowconsiderthegrayeventbelowB2.ItisseenbyB2,butnotseenbyA2orD2.Soitwasnotseenbyallthefamouswitnessesinround2.Soitsreceivedroundwillbelaterthanround2.Leaveitcoloredgraytoindicatethatitdoesn’tyethaveareceivedround.

Page 28: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 28

Continuing,wefindconsensusforthe6blackeventsandthe4darkgreenevents.Thesearethe10eventsthathavearoundreceivedof2.Weneedtosortthese10eventsintoanorderthatallmemberswillagreeon.Thisagreedorderistheconsensusorder.

Thisisdonebysortingthembyroundreceived.

Tiesarebrokenbysortingbythemediantimestamp(whichistheconsensustimestamp).

Furthertiesarebrokenbysortingbytheextendedmedian(whichlooksatmorethanjustthemiddleelementofeachlist).

Furthertiesarebrokenbysortingthembytheirsignatures,afterallthesignatureshavebeenXORedwithapseudorandomnumber.ThepseudorandomnumberforagivenroundreceivedisfoundbyXORingthesignaturesofallthefamouswitnessesinthatround.

A B C D

A2

A3

B1

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

B2

Page 29: Hashgraph : An over view with example

SwirldsTechnicalReportSWIRLDS-TR-2016-02 29

ThispictureisascreenshotfromtheHashgraphDemoappthatispartoftheSwirldsSDKthatcanbedownloadedfromSwirlds.com.

Thisscreenshotcamefromrunningitinslowmodewith4members,withthecheckboxcheckedtoshowtheroundcreated.

Thisscreenshotshowsthepartofthehashgraphfromaboutround100to105.Theexampleisaslightly-modifiedversionofthetophalfofthisscreenshot.

A B C D

A2

A3

B1

B3

B4

C1

C2

C3

D1

D2

D3

D4

A1

B2