Skip to content

Commit

Permalink
renamed core to mesh, fixes minot syntax issues and removed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
antonlerner committed Nov 12, 2018
1 parent 44688cd commit d8a8c16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
26 changes: 13 additions & 13 deletions core/block.go → mesh/block.go
@@ -1,34 +1,34 @@
package core
package mesh

import (
"time"
"github.com/google/uuid"
)

type BLockId uint32//uuid.UUID
type BLockID uint32

var layerCounter uint64 = 0

type Block struct {
id BLockId
id BLockID
layerNum uint64
blockVotes map[BLockId]bool
blockVotes map[BLockID]bool
timestamp time.Time
coin bool
data []byte
proVotes uint64
conVotes uint64
proVotes uint64
conVotes uint64
}

func NewBlock(coin bool, data []byte, ts time.Time) *Block{
b := Block{
id: BLockId(uuid.New().ID()),
blockVotes: make(map[BLockId]bool),
timestamp: ts,
data:data,
coin:coin,
proVotes:0,
conVotes:0,
id: BLockID(uuid.New().ID()),
blockVotes: make(map[BLockID]bool),
timestamp: ts,
data: data,
coin: coin,
proVotes: 0,
conVotes: 0,
}
return &b
}
Expand Down
51 changes: 9 additions & 42 deletions core/tortoise.go → mesh/tortoise.go
@@ -1,4 +1,4 @@
package core
package mesh

import ( "github.com/golang-collections/go-datastructures/bitarray"
"github.com/spacemeshos/go-spacemesh/log"
Expand All @@ -15,8 +15,8 @@ type BlockPosition struct {
}

type Algorithm struct {
block2Id map[BLockId]uint32
allBlocks map[BLockId]*Block
block2Id map[BLockID]uint32
allBlocks map[BLockID]*Block
layerQueue LayerQueue
idQueue NewIdQueue
posVotes []bitarray.BitArray
Expand All @@ -31,8 +31,8 @@ type Algorithm struct {
func NewAlgorithm(layerSize uint32, cachedLayers uint32) Algorithm{
totBlocks := layerSize*cachedLayers
trtl := Algorithm{
block2Id: make(map[BLockId]uint32),
allBlocks: make(map[BLockId]*Block),
block2Id: make(map[BLockID]uint32),
allBlocks: make(map[BLockID]*Block),
layerQueue: make(LayerQueue, cachedLayers +1),
idQueue: make(NewIdQueue,layerSize),
remainingBlockIds: totBlocks,
Expand All @@ -55,13 +55,10 @@ func (alg *Algorithm) LayerVotingAvg() uint64 {
return 30
}

func (alg *Algorithm) IsTortoiseValid(originBlock *Block, targetBlock BLockId, targetBlockIdx uint64, visibleBlocks bitarray.BitArray) bool {
//log.Info("calculating votes for: %v on target id: %v",alg.block2Id[originBlock.id], targetBlockId)
func (alg *Algorithm) IsTortoiseValid(originBlock *Block, targetBlock BLockID, targetBlockIdx uint64, visibleBlocks bitarray.BitArray) bool {
voteFor, voteAgainst := alg.countTotalVotesForBlock(targetBlockIdx, visibleBlocks)

/*if err != nil {
log.Error("Failed to count votes")
}*/

if voteFor > alg.GlobalVotingAvg() {
return true
}
Expand All @@ -71,9 +68,7 @@ func (alg *Algorithm) IsTortoiseValid(originBlock *Block, targetBlock BLockId, t

voteFor, voteAgainst = alg.CountVotesInLastLayer(alg.allBlocks[targetBlock]) //??

/*if err != nil {
log.Error("Failed to count votes")
}*/

if voteFor > alg.LayerVotingAvg() {
return true
}
Expand All @@ -94,29 +89,11 @@ func (alg *Algorithm) getLayerById(layerId uint64) (*Layer, error){

func (alg *Algorithm) CountVotesInLastLayer(block *Block) (uint64, uint64){
return block.conVotes, block.proVotes
/*
var voteFor, voteAgainst uint64 = 0,0
l, er := alg.getLayerById(block.layerNum +1)
if er != nil {
log.Error("%v", er)
return 0,0
}
for i :=0; i < len(l.blocks); i++ {//_,visibleBlock := range l.blocks {
if vote, ok := l.blocks[i].blockVotes[block.id]; ok {
if vote {
voteFor++
} else {
voteAgainst++
}
}
}
return voteFor, voteAgainst*/
}



func (alg *Algorithm) createBlockVotingMap(origin *Block) (*bitarray.BitArray, *bitarray.BitArray){
//log.Info("creating block voting map for %v", origin.id)
blockMap := bitarray.NewBitArray(uint64(alg.totalBlocks))
visibilityMap := bitarray.NewBitArray(uint64(alg.totalBlocks))
// Count direct voters
Expand All @@ -127,7 +104,6 @@ func (alg *Algorithm) createBlockVotingMap(origin *Block) (*bitarray.BitArray, *
visibilityMap.SetBit(targetBlockId)
targetPosition := alg.visibilityMap[targetBlockId]
visibilityMap = visibilityMap.Or(targetPosition.visibility)
//log.Info("target map %v new visibility map %v", targetMap.ToNums(), visibilityMap.ToNums())
if vote{
blockMap.SetBit(targetBlockId)
block.proVotes++
Expand Down Expand Up @@ -159,11 +135,10 @@ func (alg *Algorithm) createBlockVotingMap(origin *Block) (*bitarray.BitArray, *
}

func (alg *Algorithm) countTotalVotesForBlock(targetIdx uint64, visibleBlocks bitarray.BitArray) (uint64, uint64){
//targetId := alg.block2Id[target.id]
var posVotes, conVotes uint64 = 0,0
targetLayer := alg.visibilityMap[targetIdx].layer
ln := len(alg.block2Id)
for blockIdx:=0; blockIdx< ln; blockIdx++{//_, blockIdx := range alg.block2Id { possible bug what if there is an id > len(alg.block2id)
for blockIdx:=0; blockIdx< ln; blockIdx++{ // possible bug what if there is an id > len(alg.block2id)
//if this block sees our
//if alg.allBlocks[targetID].layerNum
blockPosition := &alg.visibilityMap[blockIdx]
Expand All @@ -174,20 +149,13 @@ func (alg *Algorithm) countTotalVotesForBlock(targetIdx uint64, visibleBlocks bi
if val, err = blockPosition.visibility.GetBit(targetIdx); err == nil && val {
if set, _ := alg.posVotes[blockIdx].GetBit(targetIdx); set {
posVotes++
/*if posVotes >= alg.GlobalVotingAvg() {
return posVotes, conVotes
}*/
} else {
conVotes++
/*if conVotes >= alg.GlobalVotingAvg() {
return posVotes, conVotes
}*/
}
}
}

}
//log.Info("target block id: %v, for :%v, against %v", targetId, posVotes, conVotes)
return posVotes, conVotes
}

Expand Down Expand Up @@ -236,7 +204,6 @@ func (alg *Algorithm) HandleIncomingLayer(l *Layer){
//todo: thread safety
alg.layers[l.layerNum] = l
alg.layerQueue <- l
//blockBitArrays := make(map[Block]bitarray.BitArray)
if len(alg.layerQueue) >= int(alg.cachedLayers ) {
layer := <-alg.layerQueue
alg.recycleLayer(layer)
Expand Down
4 changes: 2 additions & 2 deletions core/tortoise_test.go → mesh/tortoise_test.go
@@ -1,4 +1,4 @@
package core
package mesh

import (
"testing"
Expand All @@ -8,7 +8,7 @@ import (
)

func TestAlgorithm_Sanity(t *testing.T) {
layerSize := 200
layerSize := 50
cachedLayers := 100

alg := NewAlgorithm(uint32(layerSize),uint32(cachedLayers))
Expand Down

0 comments on commit d8a8c16

Please sign in to comment.