Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This document outlines major changes between releases.
New features:

Behaviour changes:
* specify rejected hashes on ChangeView construction (#158)

Improvements:
* minimum required Go version is 1.25 (#144, #156)
Expand Down
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type Config[H Hash] struct {
GetVerified func() []Transaction[H]
// VerifyPreBlock verifies if preBlock is valid.
VerifyPreBlock func(b PreBlock[H]) bool
// VerifyBlock verifies if block is valid.
VerifyBlock func(b Block[H]) bool
// VerifyBlock verifies if block is valid and optionally returns the hash of invalid transaction.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single one. NeoGo fails verification after first invalid transaction.

VerifyBlock func(b Block[H]) (bool, H)
// Broadcast should broadcast payload m to the consensus nodes.
Broadcast func(m ConsensusPayload[H])
// ProcessBlock is called every time new preBlock is accepted.
Expand All @@ -86,7 +86,7 @@ type Config[H Hash] struct {
// NewPrepareResponse is a constructor for payload.PrepareResponse.
NewPrepareResponse func(preparationHash H) PrepareResponse[H]
// NewChangeView is a constructor for payload.ChangeView.
NewChangeView func(newViewNumber byte, reason ChangeViewReason, timestamp uint64) ChangeView
NewChangeView func(newViewNumber byte, reason ChangeViewReason, timestamp uint64, rejectedHash ...H) ChangeView
// NewPreCommit is a constructor for payload.PreCommit.
NewPreCommit func(data []byte) PreCommit
// NewCommit is a constructor for payload.Commit.
Expand Down Expand Up @@ -124,7 +124,7 @@ func defaultConfig[H Hash]() *Config[H] {
StopTxFlow: func() {},
GetTx: func(H) Transaction[H] { return nil },
GetVerified: func() []Transaction[H] { return make([]Transaction[H], 0) },
VerifyBlock: func(Block[H]) bool { return true },
VerifyBlock: func(Block[H]) (bool, H) { return true, *new(H) },
Broadcast: func(ConsensusPayload[H]) {},
ProcessBlock: func(Block[H]) error { return nil },
GetBlock: func(H) Block[H] { return nil },
Expand Down Expand Up @@ -317,7 +317,7 @@ func WithVerifyPreBlock[H Hash](f func(b PreBlock[H]) bool) func(config *Config[
}

// WithVerifyBlock sets VerifyBlock.
func WithVerifyBlock[H Hash](f func(b Block[H]) bool) func(config *Config[H]) {
func WithVerifyBlock[H Hash](f func(b Block[H]) (bool, H)) func(config *Config[H]) {
return func(cfg *Config[H]) {
cfg.VerifyBlock = f
}
Expand Down Expand Up @@ -402,7 +402,7 @@ func WithNewPrepareResponse[H Hash](f func(preparationHash H) PrepareResponse[H]
}

// WithNewChangeView sets NewChangeView.
func WithNewChangeView[H Hash](f func(newViewNumber byte, reason ChangeViewReason, ts uint64) ChangeView) func(config *Config[H]) {
func WithNewChangeView[H Hash](f func(newViewNumber byte, reason ChangeViewReason, ts uint64, rejectedHash ...H) ChangeView) func(config *Config[H]) {
return func(cfg *Config[H]) {
cfg.NewChangeView = f
}
Expand Down
9 changes: 6 additions & 3 deletions dbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ func (d *DBFT[H]) processMissingTx() {
// with it, it sends a changeView request and returns false. It's only valid to
// call it when all transactions for this block are already collected.
func (d *DBFT[H]) createAndCheckBlock() bool {
var blockOK bool
var (
blockOK bool
invalidH H
)
if d.isAntiMEVExtensionEnabled() {
b := d.CreatePreBlock()
blockOK = d.VerifyPreBlock(b)
Expand All @@ -397,13 +400,13 @@ func (d *DBFT[H]) createAndCheckBlock() bool {
}
} else {
b := d.CreateBlock()
blockOK = d.VerifyBlock(b)
blockOK, invalidH = d.VerifyBlock(b)
if !blockOK {
d.Logger.Warn("proposed block fails verification")
}
}
if !blockOK {
d.sendChangeView(CVTxInvalid)
d.sendChangeView(CVTxInvalid, invalidH)
return false
}
return true
Expand Down
12 changes: 6 additions & 6 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type testState struct {
pool *testPool
preBlocks []dbft.PreBlock[crypto.Uint256]
blocks []dbft.Block[crypto.Uint256]
verify func(b dbft.Block[crypto.Uint256]) bool
verify func(b dbft.Block[crypto.Uint256]) (bool, crypto.Uint256)
}

type (
Expand Down Expand Up @@ -136,14 +136,14 @@ func TestDBFT_SingleNode(t *testing.T) {

func TestDBFT_OnReceiveRequestSendResponse(t *testing.T) {
s := newTestState(2, 7)
s.verify = func(b dbft.Block[crypto.Uint256]) bool {
s.verify = func(b dbft.Block[crypto.Uint256]) (bool, crypto.Uint256) {
for _, tx := range b.Transactions() {
if tx.(testTx)%10 == 0 {
return false
return false, crypto.Uint256{}
}
}

return true
return true, crypto.Uint256{}
}

t.Run("receive request from primary", func(t *testing.T) {
Expand Down Expand Up @@ -551,7 +551,7 @@ func TestDBFT_Invalid(t *testing.T) {
require.Error(t, err)
})

opts = append(opts, dbft.WithNewChangeView[crypto.Uint256](func(byte, dbft.ChangeViewReason, uint64) dbft.ChangeView {
opts = append(opts, dbft.WithNewChangeView[crypto.Uint256](func(byte, dbft.ChangeViewReason, uint64, ...crypto.Uint256) dbft.ChangeView {
return nil
}))
t.Run("without NewCommit", func(t *testing.T) {
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func (s *testState) getOptions() []func(*dbft.Config[crypto.Uint256]) {

verify := s.verify
if verify == nil {
verify = func(dbft.Block[crypto.Uint256]) bool { return true }
verify = func(dbft.Block[crypto.Uint256]) (bool, crypto.Uint256) { return true, crypto.Uint256{} }
}

opts = append(opts, dbft.WithVerifyBlock(verify))
Expand Down
2 changes: 1 addition & 1 deletion internal/consensus/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewPrepareResponse(preparationHash crypto.Uint256) dbft.PrepareResponse[cry
}

// NewChangeView returns minimal ChangeView implementation.
func NewChangeView(newViewNumber byte, _ dbft.ChangeViewReason, ts uint64) dbft.ChangeView {
func NewChangeView(newViewNumber byte, _ dbft.ChangeViewReason, ts uint64, rejected ...crypto.Uint256) dbft.ChangeView {
return &changeView{
newViewNumber: newViewNumber,
timestamp: nanoSecToSec(ts),
Expand Down
8 changes: 4 additions & 4 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ func (d *DBFT[H]) sendPrepareRequest(force bool) {
d.checkPrepare()
}

func (c *Context[H]) makeChangeView(ts uint64, reason ChangeViewReason) ConsensusPayload[H] {
cv := c.Config.NewChangeView(c.ViewNumber+1, reason, ts)
func (c *Context[H]) makeChangeView(ts uint64, reason ChangeViewReason, invalidH ...H) ConsensusPayload[H] {
cv := c.Config.NewChangeView(c.ViewNumber+1, reason, ts, invalidH...)

msg := c.Config.NewConsensusPayload(c, ChangeViewType, cv)
c.ChangeViewPayloads[c.MyIndex] = msg

return msg
}

func (d *DBFT[H]) sendChangeView(reason ChangeViewReason) {
func (d *DBFT[H]) sendChangeView(reason ChangeViewReason, invalidH ...H) {
if d.Context.WatchOnly() {
return
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func (d *DBFT[H]) sendChangeView(reason ChangeViewReason) {
zap.Int("nc", nc),
zap.Int("nf", nf))

msg := d.makeChangeView(uint64(d.Timer.Now().UnixNano()), reason)
msg := d.makeChangeView(uint64(d.Timer.Now().UnixNano()), reason, invalidH...)
d.StopTxFlow()
d.broadcast(msg)
d.checkChangeView(newView)
Expand Down
Loading