Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Fork mechanics and median time in vectorindex #286

Merged
merged 9 commits into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/inter/idx/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type (
// Event numeration.
Event uint64

// Member numeration.
Member uint64

// Txn numeration.
Txn uint64

Expand All @@ -28,6 +31,11 @@ func (e Event) Bytes() []byte {
return common.IntToBytes(uint64(e))
}

// Bytes gets the byte representation of the index.
func (m Member) Bytes() []byte {
return common.IntToBytes(uint64(m))
}

// Bytes gets the byte representation of the index.
func (t Txn) Bytes() []byte {
return common.IntToBytes(uint64(t))
Expand All @@ -47,6 +55,15 @@ func BytesToEvent(b []byte) Event {
return res
}

// BytesToMember converts bytes to member index.
func BytesToMember(b []byte) Member {
var res Member
for i := 0; i < len(b); i++ {
res += Member(b[i]) << uint(i*8)
}
return res
}

// BytesToTxn converts bytes to transaction index.
func BytesToTxn(b []byte) Txn {
var res Txn
Expand Down
38 changes: 6 additions & 32 deletions src/posposet/event_ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,26 @@ func (p *Poset) fareOrdering(frame idx.Frame, sfWitness hash.Event, unordered in
selectedEvents = selectedEvents[:nodeCount-1]
}

// 3. Get Stake from each creator
stakes := map[hash.Peer]inter.Stake{}
var jointStake inter.Stake
for _, event := range selectedEvents {
stake := p.StakeOf(event.Creator)

stakes[event.Creator] = stake
jointStake += stake
}

halfStake := jointStake / 2

// 4. Calculate weighted median
var currStake inter.Stake
var median *inter.Event
for _, event := range selectedEvents {
currStake += stakes[event.Creator]
if currStake < halfStake {
continue
}

if median == nil {
median = event
}
}

highestTimestamp := selectedEvents[len(selectedEvents)-1].LamportTime
lowestTimestamp := selectedEvents[0].LamportTime
highestTimestamp := selectedEvents[len(selectedEvents)-1].LamportTime // TODO .ClaimedTime
lowestTimestamp := selectedEvents[0].LamportTime // TODO .ClaimedTime

// 5. Calculate time ratio & time offset
// 3. Calculate time ratio & time offset
if p.LastConsensusTime == 0 {
p.LastConsensusTime = genesisTimestamp
}

frameTimePeriod := inter.MaxTimestamp(median.LamportTime-p.LastConsensusTime, 1)
frameTimePeriod := inter.MaxTimestamp(p.vi.MedianTime(sfWitness, genesisTimestamp)-p.LastConsensusTime, 1)
frameLamportPeriod := inter.MaxTimestamp(highestTimestamp-lowestTimestamp, 1)

timeRatio := inter.MaxTimestamp(frameTimePeriod/frameLamportPeriod, 1)

lowestConsensusTime := p.LastConsensusTime + timeRatio
timeOffset := lowestConsensusTime - lowestTimestamp*timeRatio

// 6. Calculate consensus timestamp
// 4. Calculate consensus timestamp
p.LastConsensusTime = p.input.GetEvent(sfWitness).LamportTime*timeRatio + timeOffset

// 7. Save new timeRatio & timeOffset to frame
// 5. Save new timeRatio & timeOffset to frame
p.frames[frame].SetTimes(timeOffset, timeRatio)

return sortEvents(unordered)
Expand Down
26 changes: 20 additions & 6 deletions src/posposet/internal/members.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/Fantom-foundation/go-lachesis/src/inter/idx"
"sort"

"github.com/Fantom-foundation/go-lachesis/src/hash"
Expand All @@ -19,18 +20,22 @@ type (
func (mm *Members) Add(addr hash.Peer, stake inter.Stake) {
(*mm)[addr] = stake
}

// Top gets top subset.
func (mm Members) Top() Members {
top := make(members, 0, len(mm))
func (mm Members) sortedArray() members {
array := make(members, 0, len(mm))
for n, s := range mm {
top = append(top, member{
array = append(array, member{
Addr: n,
Stake: s,
})
}
sort.Sort(array)
return array
}

// Top gets top subset.
func (mm Members) Top() Members {
top := mm.sortedArray()

sort.Sort(top)
if len(top) > MembersCount {
top = top[:MembersCount]
}
Expand All @@ -43,6 +48,15 @@ func (mm Members) Top() Members {
return res
}

// Deterministic total order of members.
func (mm Members) Idxs() map[hash.Peer]idx.Member {
idxs := make(map[hash.Peer]idx.Member, len(mm))
for i, m := range mm.sortedArray() {
idxs[m.Addr] = idx.Member(i)
}
return idxs
}

// Quorum limit of members.
func (mm Members) Quorum() inter.Stake {
return mm.TotalStake()*2/3 + 1
Expand Down
4 changes: 2 additions & 2 deletions src/posposet/poset.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (p *Poset) superFrameSealed(sfWitness hash.Event) bool {
// and returns frame where event is root.
// It is not safe for concurrent use.
func (p *Poset) checkIfRoot(e *Event) (frame *Frame, isRoot bool) {
p.strongly.Cache(e.Event)
p.vi.Add(e.Event)

var frameI idx.Frame
if e.Seq == 1 {
Expand Down Expand Up @@ -282,7 +282,7 @@ func (p *Poset) checkIfRoot(e *Event) (frame *Frame, isRoot bool) {
sSeenCounter := p.members.NewCounter()
for member, memberRoots := range p.frames[maxParentsFrame].Roots {
for root := range memberRoots {
if p.strongly.See(e.Hash(), root) {
if p.vi.StronglySee(e.Hash(), root) {
sSeenCounter.Count(member)
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/posposet/seeing/event.go

This file was deleted.

179 changes: 0 additions & 179 deletions src/posposet/seeing/strongly.go

This file was deleted.

Loading