github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/vecmt/vector_ops.go (about) 1 package vecmt 2 3 import ( 4 "github.com/unicornultrafoundation/go-helios/native/dag" 5 "github.com/unicornultrafoundation/go-helios/native/idx" 6 "github.com/unicornultrafoundation/go-helios/vecengine" 7 "github.com/unicornultrafoundation/go-helios/vecfc" 8 9 "github.com/unicornultrafoundation/go-u2u/native" 10 ) 11 12 type CreationTimer interface { 13 CreationTime() native.Timestamp 14 } 15 16 func (b *HighestBefore) InitWithEvent(i idx.Validator, e dag.Event) { 17 b.VSeq.InitWithEvent(i, e) 18 b.VTime.Set(i, e.(CreationTimer).CreationTime()) 19 } 20 21 func (b *HighestBefore) IsEmpty(i idx.Validator) bool { 22 return b.VSeq.IsEmpty(i) 23 } 24 25 func (b *HighestBefore) IsForkDetected(i idx.Validator) bool { 26 return b.VSeq.IsForkDetected(i) 27 } 28 29 func (b *HighestBefore) Seq(i idx.Validator) idx.Event { 30 return b.VSeq.Seq(i) 31 } 32 33 func (b *HighestBefore) MinSeq(i idx.Validator) idx.Event { 34 return b.VSeq.MinSeq(i) 35 } 36 37 func (b *HighestBefore) SetForkDetected(i idx.Validator) { 38 b.VSeq.SetForkDetected(i) 39 } 40 41 func (self *HighestBefore) CollectFrom(_other vecengine.HighestBeforeI, num idx.Validator) { 42 other := _other.(*HighestBefore) 43 for branchID := idx.Validator(0); branchID < num; branchID++ { 44 hisSeq := other.VSeq.Get(branchID) 45 if hisSeq.Seq == 0 && !hisSeq.IsForkDetected() { 46 // hisSeq doesn't observe anything about this branchID 47 continue 48 } 49 mySeq := self.VSeq.Get(branchID) 50 51 if mySeq.IsForkDetected() { 52 // mySeq observes the maximum already 53 continue 54 } 55 if hisSeq.IsForkDetected() { 56 // set fork detected 57 self.SetForkDetected(branchID) 58 } else { 59 if mySeq.Seq == 0 || mySeq.MinSeq > hisSeq.MinSeq { 60 // take hisSeq.MinSeq 61 mySeq.MinSeq = hisSeq.MinSeq 62 self.VSeq.Set(branchID, mySeq) 63 } 64 if mySeq.Seq < hisSeq.Seq { 65 // take hisSeq.Seq 66 mySeq.Seq = hisSeq.Seq 67 self.VSeq.Set(branchID, mySeq) 68 self.VTime.Set(branchID, other.VTime.Get(branchID)) 69 } 70 } 71 } 72 } 73 74 func (self *HighestBefore) GatherFrom(to idx.Validator, _other vecengine.HighestBeforeI, from []idx.Validator) { 75 other := _other.(*HighestBefore) 76 // read all branches to find highest event 77 highestBranchSeq := vecfc.BranchSeq{} 78 highestBranchTime := native.Timestamp(0) 79 for _, branchID := range from { 80 vseq := other.VSeq.Get(branchID) 81 vtime := other.VTime.Get(branchID) 82 if vseq.IsForkDetected() { 83 highestBranchSeq = vseq 84 break 85 } 86 if vseq.Seq > highestBranchSeq.Seq { 87 highestBranchSeq = vseq 88 highestBranchTime = vtime 89 } 90 } 91 self.VSeq.Set(to, highestBranchSeq) 92 self.VTime.Set(to, highestBranchTime) 93 }