-
Notifications
You must be signed in to change notification settings - Fork 82
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
feat(blockManager): refactor and use state as single source of truth for height #847
Changes from 1 commit
e36a9a5
0d4c641
5803cda
aef3bc4
1e766ab
8c4df90
f3592e5
6973282
81baf80
24d8b85
6c1741a
b1c0131
9364c7c
9f76e0b
c7ffbf1
a501fa2
72a8893
9815da3
5c7c454
f53ddc4
8402076
90031e7
d2be2ce
a095024
d9698f8
051a4e7
cecf106
b896eaa
bb66c2c
efcf193
3992f2a
5b3cda0
953a9b8
576325e
403ec9e
31c8906
5ab2f15
74bfba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package block | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/dymensionxyz/dymint/p2p" | ||
"github.com/dymensionxyz/dymint/types" | ||
"github.com/tendermint/tendermint/libs/pubsub" | ||
) | ||
|
||
// onNewGossippedBlock will take a block and apply it | ||
func (m *Manager) onNewGossipedBlock(event pubsub.Message) { | ||
m.retrieverMutex.Lock() // needed to protect blockCache access | ||
eventData := event.Data().(p2p.GossipedBlock) | ||
block := eventData.Block | ||
commit := eventData.Commit | ||
m.logger.Debug("Received new block via gossip", "height", block.Header.Height, "n cachedBlocks", len(m.blockCache)) | ||
|
||
nextHeight := m.State.NextHeight() | ||
if block.Header.Height >= nextHeight { | ||
m.blockCache[block.Header.Height] = CachedBlock{ | ||
Block: &block, | ||
Commit: &commit, | ||
} | ||
m.logger.Debug("caching block", "block height", block.Header.Height, "store height", m.State.Height()) | ||
} | ||
m.retrieverMutex.Unlock() // have to give this up as it's locked again in attempt apply, and we're not re-entrant | ||
err := m.attemptApplyCachedBlocks() | ||
if err != nil { | ||
m.logger.Error("applying cached blocks", "err", err) | ||
} | ||
} | ||
|
||
func (m *Manager) attemptApplyCachedBlocks() error { | ||
m.retrieverMutex.Lock() | ||
defer m.retrieverMutex.Unlock() | ||
|
||
for { | ||
expectedHeight := m.State.NextHeight() | ||
|
||
cachedBlock, blockExists := m.blockCache[expectedHeight] | ||
if !blockExists { | ||
break | ||
} | ||
if err := m.validateBlock(cachedBlock.Block, cachedBlock.Commit); err != nil { | ||
delete(m.blockCache, cachedBlock.Block.Header.Height) | ||
/// TODO: can we take an action here such as dropping the peer / reducing their reputation? | ||
return fmt.Errorf("block not valid at height %d, dropping it: err:%w", cachedBlock.Block.Header.Height, err) | ||
} | ||
|
||
err := m.applyBlock(cachedBlock.Block, cachedBlock.Commit, blockMetaData{source: gossipedBlock}) | ||
if err != nil { | ||
return fmt.Errorf("apply cached block: expected height: %d: %w", expectedHeight, err) | ||
} | ||
m.logger.Debug("applied cached block", "height", expectedHeight) | ||
|
||
delete(m.blockCache, cachedBlock.Block.Header.Height) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Manager) gossipBlock(ctx context.Context, block types.Block, commit types.Commit) error { | ||
gossipedBlock := p2p.GossipedBlock{Block: block, Commit: commit} | ||
gossipedBlockBytes, err := gossipedBlock.MarshalBinary() | ||
if err != nil { | ||
return fmt.Errorf("marshal binary: %w: %w", err, ErrNonRecoverable) | ||
} | ||
if err := m.p2pClient.GossipBlock(ctx, gossipedBlockBytes); err != nil { | ||
// Although this boils down to publishing on a topic, we don't want to speculate too much on what | ||
// could cause that to fail, so we assume recoverable. | ||
return fmt.Errorf("p2p gossip block: %w: %w", err, ErrRecoverable) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ import ( | |
func (m *Manager) ProduceBlockLoop(ctx context.Context) { | ||
m.logger.Debug("Started produce loop") | ||
|
||
// Main ticker for block production | ||
ticker := time.NewTicker(m.Conf.BlockTime) | ||
defer ticker.Stop() | ||
|
||
|
@@ -90,19 +89,14 @@ func (m *Manager) ProduceAndGossipBlock(ctx context.Context, allowEmpty bool) (* | |
|
||
func (m *Manager) produceBlock(allowEmpty bool) (*types.Block, *types.Commit, error) { | ||
var ( | ||
lastCommit *types.Commit | ||
lastHeaderHash [32]byte | ||
newHeight uint64 | ||
err error | ||
lastHeaderHash [32]byte | ||
lastCommit = &types.Commit{} | ||
newHeight = m.State.NextHeight() | ||
) | ||
|
||
if m.State.IsGenesis() { | ||
newHeight = uint64(m.State.InitialHeight) | ||
lastCommit = &types.Commit{} | ||
m.State.BaseHeight = newHeight | ||
} else { | ||
height := m.State.Height() | ||
newHeight = height + 1 | ||
if !m.State.IsGenesis() { | ||
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. as this 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. refactored it a bit, take a look |
||
height := newHeight - 1 | ||
danwt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lastCommit, err = m.Store.LoadCommit(height) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("load commit: height: %d: %w: %w", height, err, ErrNonRecoverable) | ||
|
@@ -190,6 +184,7 @@ func (m *Manager) createTMSignature(block *types.Block, proposerAddress []byte, | |
} | ||
v := vote.ToProto() | ||
// convert libp2p key to tm key | ||
//TODO: move to types | ||
raw_key, _ := m.ProposerKey.Raw() | ||
tmprivkey := tmed25519.PrivKey(raw_key) | ||
tmprivkey.PubKey().Bytes() | ||
|
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.
I think we should keep this in manager.go because it's used not only in gossip
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.
where else it is used?
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.
in syncUntilTarget
you can check usages in IDE
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.
moved back