-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use atx grading for block proposal (#4717)
## Motivation <!-- Please mention the issue fixed by this PR or detailed motivation --> part of #4089 <!-- `Closes #XXXX, closes #XXXX, ...` links mentioned issues to this PR and automatically closes them when this it's merged --> ## Changes - grade atxs as described in https://community.spacemesh.io/t/grading-atxs-for-the-active-set/335, let s be the start of the epoch, and δ the network propagation time. - grade 0/evil: ATX was received at time t >= s-3δ, or an equivocation proof was received by time s-δ. - grade 1/acceptable: ATX was received at time t < s-3δ before the start of the epoch, and no equivocation proof was received by time s-δ. - grade 2/good: ATX was received at time t < s-4δ, and no equivocation proof was received for that id until time s. - miner only include grade 2/good ATXs in its active set for its first ballot of the epoch - newly sync'ed node uses the first block of epoch for active set in ref ballot, since it doesn't have accurate received timestamp for atxs or malfeasance proof
- Loading branch information
1 parent
21b4768
commit a975106
Showing
20 changed files
with
586 additions
and
150 deletions.
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
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package miner | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
) | ||
|
||
// AtxGrade describes the grade of an ATX as described in | ||
// https://community.spacemesh.io/t/grading-atxs-for-the-active-set/335 | ||
// | ||
// let s be the start of the epoch, and δ the network propagation time. | ||
// grade 0: ATX was received at time t >= s-3δ, or an equivocation proof was received by time s-δ. | ||
// grade 1: ATX was received at time t < s-3δ before the start of the epoch, and no equivocation proof was received by time s-δ. | ||
// grade 2: ATX was received at time t < s-4δ, and no equivocation proof was received for that id until time s. | ||
type AtxGrade int | ||
|
||
const ( | ||
Evil AtxGrade = iota | ||
Acceptable | ||
Good | ||
) | ||
|
||
func GradeAtx(msh mesh, nodeID types.NodeID, atxReceived, epochStart time.Time, delta time.Duration) (AtxGrade, error) { | ||
proof, err := msh.GetMalfeasanceProof(nodeID) | ||
if err != nil && !errors.Is(err, sql.ErrNotFound) { | ||
return Good, err | ||
} | ||
if atxReceived.Before(epochStart.Add(-4*delta)) && (proof == nil || !proof.Received().Before(epochStart)) { | ||
return Good, nil | ||
} | ||
if atxReceived.Before(epochStart.Add(-3*delta)) && (proof == nil || !proof.Received().Before(epochStart.Add(-delta))) { | ||
return Acceptable, nil | ||
} | ||
return Evil, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package miner_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/hare/mocks" | ||
"github.com/spacemeshos/go-spacemesh/miner" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
) | ||
|
||
const layersPerEpoch = 3 | ||
|
||
func TestMain(m *testing.M) { | ||
types.SetLayersPerEpoch(layersPerEpoch) | ||
|
||
res := m.Run() | ||
os.Exit(res) | ||
} | ||
|
||
func TestGradeAtx(t *testing.T) { | ||
const delta = 10 | ||
for _, tc := range []struct { | ||
desc string | ||
malicious bool | ||
// distance in second from the epoch start time | ||
atxReceived, malReceived int | ||
result miner.AtxGrade | ||
}{ | ||
{ | ||
desc: "very early atx", | ||
atxReceived: -41, | ||
result: miner.Good, | ||
}, | ||
{ | ||
desc: "very early atx, late malfeasance", | ||
atxReceived: -41, | ||
malicious: true, | ||
malReceived: 0, | ||
result: miner.Good, | ||
}, | ||
{ | ||
desc: "very early atx, malicious", | ||
atxReceived: -41, | ||
malicious: true, | ||
malReceived: -10, | ||
result: miner.Acceptable, | ||
}, | ||
{ | ||
desc: "very early atx, early malicious", | ||
atxReceived: -41, | ||
malicious: true, | ||
malReceived: -11, | ||
result: miner.Evil, | ||
}, | ||
{ | ||
desc: "early atx", | ||
atxReceived: -31, | ||
result: miner.Acceptable, | ||
}, | ||
{ | ||
desc: "early atx, late malicious", | ||
atxReceived: -31, | ||
malicious: true, | ||
malReceived: -10, | ||
result: miner.Acceptable, | ||
}, | ||
{ | ||
desc: "early atx, early malicious", | ||
atxReceived: -31, | ||
malicious: true, | ||
malReceived: -11, | ||
result: miner.Evil, | ||
}, | ||
{ | ||
desc: "late atx", | ||
atxReceived: -30, | ||
result: miner.Evil, | ||
}, | ||
{ | ||
desc: "very late atx", | ||
atxReceived: 0, | ||
result: miner.Evil, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.desc, func(t *testing.T) { | ||
mockMsh := mocks.NewMockmesh(gomock.NewController(t)) | ||
epochStart := time.Now() | ||
nodeID := types.RandomNodeID() | ||
if tc.malicious { | ||
proof := &types.MalfeasanceProof{} | ||
proof.SetReceived(epochStart.Add(time.Duration(tc.malReceived) * time.Second)) | ||
mockMsh.EXPECT().GetMalfeasanceProof(nodeID).Return(proof, nil) | ||
} else { | ||
mockMsh.EXPECT().GetMalfeasanceProof(nodeID).Return(nil, sql.ErrNotFound) | ||
} | ||
atxReceived := epochStart.Add(time.Duration(tc.atxReceived) * time.Second) | ||
got, err := miner.GradeAtx(mockMsh, nodeID, atxReceived, epochStart, delta*time.Second) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.result, got) | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.