New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implemented mesh traversal for validation of active set #780
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3e82a15
implemented mesh traversal for validation of active set stated in rec…
antonlerner 5bd00f3
added tests to activation db, for: atxEpochCounter, atx active state …
antonlerner 7910761
added a bit more tests, formatted code
antonlerner 1aa4ff1
added more tests to not traverse layers in current epoch
antonlerner 711d4bb
fixed counting atxs in current epoch while travesing bug and added un…
antonlerner f373ed7
after mesh->block refactor
antonlerner 8e4b6b6
after merge with develop
antonlerner d9e0858
more fixed after merge
antonlerner e03b881
fixed cr comments
antonlerner 87059cf
after fmt
antonlerner d17189f
after another megre and cr comments
antonlerner e6e5e2a
renamed package block to types
antonlerner File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
package activation | ||
|
||
import ( | ||
"github.com/spacemeshos/go-spacemesh/common" | ||
"github.com/spacemeshos/go-spacemesh/database" | ||
"github.com/spacemeshos/go-spacemesh/log" | ||
"github.com/spacemeshos/go-spacemesh/mesh" | ||
"github.com/spacemeshos/go-spacemesh/miner" | ||
"github.com/spacemeshos/go-spacemesh/nipst" | ||
"github.com/spacemeshos/go-spacemesh/rand" | ||
"github.com/spacemeshos/go-spacemesh/types" | ||
) | ||
|
||
type Network interface { | ||
Broadcast(channel string, data []byte) error | ||
} | ||
const AtxProtocol = "AtxGossip" | ||
|
||
var activesetCache = NewActivesetCache(1000) | ||
|
||
type ActiveSetProvider interface { | ||
GetActiveSetSize(l mesh.LayerID) uint32 | ||
GetActiveSetSize(l types.LayerID) uint32 | ||
} | ||
|
||
type MeshProvider interface { | ||
GetLatestView() []mesh.BlockID | ||
LatestLayerId() mesh.LayerID | ||
GetLatestView() []types.BlockID | ||
LatestLayerId() types.LayerID | ||
} | ||
|
||
type EpochId uint64 | ||
|
||
func (l EpochId) ToBytes() []byte { return common.Uint64ToBytes(uint64(l)) } | ||
|
||
type EpochProvider interface { | ||
Epoch(l mesh.LayerID) EpochId | ||
Epoch(l types.LayerID) types.EpochId | ||
} | ||
|
||
type Broadcaster interface { | ||
Broadcast(channel string, data []byte) error | ||
} | ||
|
||
type Builder struct { | ||
nodeId mesh.NodeId | ||
nodeId types.NodeId | ||
db *ActivationDb | ||
net Network | ||
net Broadcaster | ||
activeSet ActiveSetProvider | ||
mesh MeshProvider | ||
epochProvider EpochProvider | ||
|
@@ -45,56 +44,51 @@ type Processor struct { | |
epochProvider EpochProvider | ||
} | ||
|
||
func (p *Processor) ProcessBlockATXs(block *mesh.Block) { | ||
for _, atx := range block.ATXs { | ||
err := p.db.StoreAtx(p.epochProvider.Epoch(atx.LayerIndex), atx) | ||
if err != nil { | ||
log.Error("cannot store atx: %v", atx) | ||
} | ||
} | ||
} | ||
|
||
func NewBuilder(nodeId mesh.NodeId, db database.DB, net Network, activeSet ActiveSetProvider, view MeshProvider, epochDuration EpochProvider) *Builder { | ||
func NewBuilder(nodeId types.NodeId, db database.DB, meshdb *mesh.MeshDB, net Broadcaster, activeSet ActiveSetProvider, view MeshProvider, epochDuration EpochProvider, layersPerEpoch uint64) *Builder { | ||
return &Builder{ | ||
nodeId, &ActivationDb{db}, net, activeSet, view, epochDuration, | ||
nodeId, NewActivationDb(db, meshdb, layersPerEpoch), net, activeSet, view, epochDuration, | ||
} | ||
} | ||
|
||
func (b *Builder) PublishActivationTx(nipst *nipst.NIPST) error { | ||
seq := b.GetLastSequence(b.nodeId) | ||
var seq uint64 | ||
prevAtx, err := b.GetPrevAtxId(b.nodeId) | ||
if seq > 0 && err != nil { | ||
log.Error("cannot find prev ATX for nodeid %v ", b.nodeId) | ||
return err | ||
if prevAtx == nil { | ||
log.Info("previous ATX not found") | ||
prevAtx = &types.EmptyAtx | ||
seq = 0 | ||
} else { | ||
seq = b.GetLastSequence(b.nodeId) | ||
if seq > 0 && prevAtx == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
log.Error("cannot find prev ATX for nodeid %v ", b.nodeId) | ||
return err | ||
} | ||
seq++ | ||
} | ||
|
||
l := b.mesh.LatestLayerId() | ||
ech := b.epochProvider.Epoch(l) | ||
var posAtx *mesh.AtxId = nil | ||
var posAtx *types.AtxId = nil | ||
if ech > 0 { | ||
posAtx, err = b.GetPositioningAtxId(ech - 1) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
posAtx = &mesh.EmptyAtx | ||
} | ||
|
||
if prevAtx == nil { | ||
log.Info("previous ATX not found") | ||
prevAtx = &mesh.EmptyAtx | ||
posAtx = &types.EmptyAtx | ||
} | ||
|
||
atx := mesh.NewActivationTx(b.nodeId, seq+1, *prevAtx, l, 0, *posAtx, b.activeSet.GetActiveSetSize(l-1), b.mesh.GetLatestView(), nipst) | ||
atx := types.NewActivationTx(b.nodeId, seq, *prevAtx, l, 0, *posAtx, b.activeSet.GetActiveSetSize(l-1), b.mesh.GetLatestView(), nipst) | ||
|
||
buf, err := mesh.AtxAsBytes(atx) | ||
buf, err := types.AtxAsBytes(atx) | ||
if err != nil { | ||
return err | ||
} | ||
//todo: should we do something about it? wait for something? | ||
return b.net.Broadcast(miner.AtxProtocol, buf) | ||
return b.net.Broadcast(AtxProtocol, buf) | ||
} | ||
|
||
func (b *Builder) GetPrevAtxId(node mesh.NodeId) (*mesh.AtxId, error) { | ||
func (b *Builder) GetPrevAtxId(node types.NodeId) (*types.AtxId, error) { | ||
ids, err := b.db.GetNodeAtxIds(node) | ||
|
||
if err != nil || len(ids) == 0 { | ||
|
@@ -103,7 +97,7 @@ func (b *Builder) GetPrevAtxId(node mesh.NodeId) (*mesh.AtxId, error) { | |
return &ids[len(ids)-1], nil | ||
} | ||
|
||
func (b *Builder) GetPositioningAtxId(epochId EpochId) (*mesh.AtxId, error) { | ||
func (b *Builder) GetPositioningAtxId(epochId types.EpochId) (*types.AtxId, error) { | ||
atxs, err := b.db.GetEpochAtxIds(epochId) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -113,7 +107,7 @@ func (b *Builder) GetPositioningAtxId(epochId EpochId) (*mesh.AtxId, error) { | |
return &atxId, nil | ||
} | ||
|
||
func (b *Builder) GetLastSequence(node mesh.NodeId) uint64 { | ||
func (b *Builder) GetLastSequence(node types.NodeId) uint64 { | ||
atxId, err := b.GetPrevAtxId(node) | ||
if err != nil { | ||
return 0 | ||
|
@@ -125,3 +119,14 @@ func (b *Builder) GetLastSequence(node mesh.NodeId) uint64 { | |
} | ||
return atx.Sequence | ||
} | ||
|
||
/*func (m *Mesh) UniqueAtxs(lyr *Layer) map[*ActivationTx]struct{} { | ||
atxMap := make(map[*ActivationTx]struct{}) | ||
for _, blk := range lyr.blocks { | ||
for _, atx := range blk.ATXs { | ||
atxMap[atx] = struct{}{} | ||
} | ||
|
||
} | ||
return atxMap | ||
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't it make more sense to pass the
atxID
(and rename thisGetSequence
) instead of looking for theprevAtx
again inside this method? Not a big deal, if you disagree.