Skip to content
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/finalizer #61

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open

Feat/finalizer #61

wants to merge 39 commits into from

Conversation

tak1827
Copy link
Contributor

@tak1827 tak1827 commented Jul 28, 2024

Fast FinalityをBSCのv1.4.6から移植しました。
差分が多いです。新規のファイルは基本的にいじっていません。僕が加えた変更は主にConsensusに集中しています。

気になるところをコメントに残しました。また、処理メモも下に残しました。

動作確認は、private-opstackのfeat/support-finalizerで可能です。updateBLSKeyボタンを押して、BLS鍵を登録する必要があります。

@tak1827
Copy link
Contributor Author

tak1827 commented Aug 20, 2024

Finalizerの処理メモ

フラグ

  • VotingEnabledFlag
    • NewVoteManagerを作るかどうかのフラグ。オンならVoteをする。Voteするだけで、Vote結果を集約させるかは次のフラグで制御
    • default: false
  • DisableVoteAttestationFlag
    • vote結果を集約してBlockに含めることをしない。つまり、Finalizeさせないようにする
    • default: false

各フロー

  • 自分で署名を作る場合のフロー
    • Ethereum.New(backend.go)でNewVoteManagerを作る
    • NewVoteManager
      • VoteSignerを作る
        • signer鍵が複数ある場合、リストの最初のものが自動で選択される
      • VoteJournalを作る
        • 自身のVoteをFileに書き出す
      • loopを起動
        • 新しいHeadがくる(ブロックが積み上がるたびに)とそのHeadのVoteを作る
          • 起動してから5ブロックはSkipされる
          • BLSPubKeyがValidatorセットに登録されているものと一致しない場合はSkip
          • VoteEnvelope
            • SourceBlock
              • Consensusが持つsnapのAttestationのTargetBlockをセット
                • 1回投票されたBlock、つまりJustifyされたBlockをセットする
            • TargetBlock
              • 最新Head
          • AttestationのVote
          • 作られたVoteはPoolにPutされる(pool.PutVote)
  • 署名を受け取る場合のフロー
    • Ethereum.New(backend.go)でbsc/protocolをRegisterProtocolsする
      • どんなプロトコル?
        • VotesMsgをhandleVotesするプロトコル
    • Voteを受け取るとbscHandlerのHandleが呼ばれる
      • 同じPeerが30秒間に300個以上の署名を送ることはできない
      • PoolにPutする(pool.PutVote)
  • Voteを集計してBlockに入れるフロー
    • SealでVoteを集計してHeaderにAttestationを付与する(assembleVoteAttestation)
  • BlockをFinalizeするフロー
    • worker.goのWriteBlockAndSetHeadでSetFinalizedする
      • Finalize対象の高さはConsensusのsnapが持つAttestationのSouceBlockが指定される
        • つまり、2回投票されたBlock
          • SouceはJustifyさられたBlockが指定される。Souceが入ってるAttestaion自体を含めて2回
    • なお、SafeHeadは更新しない

@@ -48,7 +50,8 @@ var deploymentSets = map[common.Hash]map[uint64]deploymentSet{
deployments8,
deployments9,
deployments10,
deployments11,
// deployments11, // Disable this feature as it changes the epoch, which can impact development.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

private opstackではdeployments11をdisableにしました。

@@ -80,59 +80,3 @@ func TestEnvironmentValue(t *testing.T) {
}
}
}

func TestNewEnvironmentValue(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

v1.5.0の消し忘れ

@@ -1078,6 +1078,7 @@ func (w *worker) commitWork(interrupt *atomic.Int32, timestamp int64) {
coinbase: coinbase,
})
if err != nil {
log.Error("Failed to prepare work", "in", "commitWork", "err", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ロギングの改善

votesSub event.Subscription

voteMonitorSub event.Subscription
maliciousVoteMonitor *monitor.MaliciousVoteMonitor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MuliciousVoteMonitorはマージしただけ。動作未確認。

@@ -165,7 +165,7 @@ func FromECDSA(priv *ecdsa.PrivateKey) []byte {

// UnmarshalPubkey converts bytes to a secp256k1 public key.
func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) {
x, y := elliptic.Unmarshal(S256(), pub)
x, y := elliptic.Unmarshal(S256(), pub) //nolint:all //TODO
Copy link
Contributor Author

Choose a reason for hiding this comment

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

LinterのWarningを無視。BSCと同じ形にした。

if err != nil {
log.Error("Failed to get scheduler", "in", "verifyCascadingFields", "number", number, "err", err)
return err
return fmt.Errorf("failed to get scheduler. blockNumber: %d, in: verifyCascadingFields, err: %v", number, err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ログでエラーを吐くよりも、Errorの中身を詳細にした。

}
}
// the voter's total stake should be greater than 2/3 of the total stake
threshold := new(big.Int).Mul(totalStake, big.NewInt(2))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

stake量の重みづけで2/3以上必要

// getValidatorsFromHeader returns the next validators extracted from the header's extra field if exists.
// The validators bytes would be contained only in the epoch block's header, and its each validator bytes length is fixed.
// Layout: |--Extra Vanity--|--EnvironmentValue--| --Validator Number--|--Owner(or Empty)--|--Operator(or Empty)--|---Stake(or Empty)--|--Vote Address(or Empty)--|--Vote Attestation(or Empty)--|--Extra Seal--|
func getValidatorsFromHeader(header *types.Header) (*nextValidators, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Env/Validator/VoteAttestationをHeaderに詰め込む

@@ -816,10 +1174,25 @@ func (c *Oasys) Seal(chain consensus.ChainHeaderReader, block *types.Block, resu
case <-time.After(delay):
}

err := c.assembleVoteAttestation(chain, header, validators)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

delayの間に投票を集める

for i, voteAddr := range validators.VoteAddresses {
if _, ok := voteAddrSet[voteAddr]; ok {
voterIndex := i + 1
attestation.VoteAddressSet |= 1 << voterIndex
Copy link
Contributor Author

Choose a reason for hiding this comment

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

AttestationのVoterIndexはOwner鍵でソートされた順序で決まる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant