Skip to content

Commit

Permalink
Merge pull request #9 from Loverush/fast_finality
Browse files Browse the repository at this point in the history
Fix first round final review PR comments
  • Loading branch information
pythonberg1997 authored Apr 26, 2022
2 parents 6fe828f + a0ac519 commit fe2cc16
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 59 deletions.
10 changes: 6 additions & 4 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,15 +1110,17 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
}
}

err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
if err != nil {
return nil, nil, err
}

if p.chainConfig.IsLynn(header.Number) {
if err := p.distributeFinalityReward(chain, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true); err != nil {
return nil, nil, err
}
}
err := p.distributeIncoming(p.val, state, header, cx, &txs, &receipts, nil, &header.GasUsed, true)
if err != nil {
return nil, nil, err
}

// should not happen. Once happen, stop the node is better than broadcast the block
if header.GasLimit < header.GasUsed {
return nil, nil, errors.New("gas consumption of system txs exceed the gas limit")
Expand Down
57 changes: 22 additions & 35 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},

common.BytesToAddress([]byte{100}): &finalitySignatureVerify{},
common.BytesToAddress([]byte{100}): &voteSignatureVerify{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
Expand Down Expand Up @@ -1051,63 +1051,50 @@ func (c *bls12381MapG2) Run(input []byte) ([]byte, error) {
return g.EncodePoint(r), nil
}

var errFinalitySignatureVerify = errors.New("invalid signatures")
var errVoteSignatureVerify = errors.New("invalid signatures")

// finalitySignatureVerify implements BEP-126 finality signature verification precompile.
type finalitySignatureVerify struct{}
// voteSignatureVerify implements BEP-126 finality signature verification precompile.
type voteSignatureVerify struct{}

// RequiredGas returns the gas required to execute the pre-compiled contract.
func (c *finalitySignatureVerify) RequiredGas(input []byte) uint64 {
return params.SignatureVerifyGas
func (c *voteSignatureVerify) RequiredGas(input []byte) uint64 {
return params.VoteSignatureVerifyGas
}

func (c *finalitySignatureVerify) Run(input []byte) ([]byte, error) {
func (c *voteSignatureVerify) Run(input []byte) ([]byte, error) {
var (
srcNumA = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
tarNumA = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
srcHashA = getData(input, 64, 32)
tarHashA = getData(input, 96, 32)
sigA = getData(input, 128, 96)
srcNumB = new(big.Int).SetBytes(getData(input, 224, 32)).Uint64()
tarNumB = new(big.Int).SetBytes(getData(input, 256, 32)).Uint64()
srcHashB = getData(input, 288, 32)
tarHashB = getData(input, 320, 32)
sigB = getData(input, 352, 96)
BLSKey = getData(input, 448, 48)
srcNum = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
tarNum = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
srcHash = getData(input, 64, 32)
tarHash = getData(input, 96, 32)
sig = getData(input, 128, 96)
BLSKey = getData(input, 224, 48)
)

sigs := make([][]byte, 2)
msgs := make([][32]byte, 2)
pubKeys := make([]bls.PublicKey, 2)
sigs := make([][]byte, 1)
msgs := make([][32]byte, 1)
pubKeys := make([]bls.PublicKey, 1)

pubKey, err := bls.PublicKeyFromBytes(BLSKey)
if err != nil {
return nil, err
}
pubKeys[0] = pubKey
pubKeys[1] = pubKey

msgs[0] = rlpHash(&types.VoteData{
SourceNumber: srcNumA,
SourceHash: common.BytesToHash(srcHashA),
TargetNumber: tarNumA,
TargetHash: common.BytesToHash(tarHashA),
SourceNumber: srcNum,
SourceHash: common.BytesToHash(srcHash),
TargetNumber: tarNum,
TargetHash: common.BytesToHash(tarHash),
})
msgs[1] = rlpHash(&types.VoteData{
SourceNumber: srcNumB,
SourceHash: common.BytesToHash(srcHashB),
TargetNumber: tarNumB,
TargetHash: common.BytesToHash(tarHashB),
})
sigs[0] = sigA
sigs[1] = sigB
sigs[0] = sig

success, err := bls.VerifyMultipleSignatures(sigs, msgs, pubKeys)
if err != nil {
return nil, err
}
if !success {
return nil, errFinalitySignatureVerify
return nil, errVoteSignatureVerify
}
return big1.Bytes(), nil
}
Expand Down
8 changes: 4 additions & 4 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{16}): &bls12381Pairing{},
common.BytesToAddress([]byte{17}): &bls12381MapG1{},
common.BytesToAddress([]byte{18}): &bls12381MapG2{},
common.BytesToAddress([]byte{100}): &finalitySignatureVerify{},
common.BytesToAddress([]byte{100}): &voteSignatureVerify{},
}

// EIP-152 test vectors
Expand Down Expand Up @@ -232,10 +232,10 @@ func BenchmarkPrecompiledIdentity(bench *testing.B) {
benchmarkPrecompiled("04", t, bench)
}

// Benchmarks the sample inputs from the finalitySignatureVerify precompile.
func BenchmarkPrecompiledFinalitySignatureVerify(bench *testing.B) {
// Benchmarks the sample inputs from the voteSignatureVerify precompile.
func BenchmarkPrecompiledVoteSignatureVerify(bench *testing.B) {
t := precompiledTest{
Input: "000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c555d45d77921e0f26487706179f73c5f8539744b55147c73a3621366bf809c066c8781959ce3621f67c9345da9b5e01ce5113d7b5ae3c6dcd3ca88ad4ed9023fa55c8188060c74f1791eefc78e8aacf0c044e3f6317fe5eadce8ba9db2d19da83e41364c3d6802175acaa392d576a95206d83e5bbfc6022b53c288dc5b60cfe1722298007f1a4b97f47383a9fe1cb7bb5250783e89f3720b7a37bec026ece0b60000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000b0284d0d1efd5baad5cec3da093e963eeba7827f3d9aacf332c0081393d5cc107cd4dfcdc99332fd9b96b275acaf3fd8dc79645ccebddb510788b179c260bde66aeba2a3ab07ae223b0b3db25a6184f3b48fd3f1881cfb5eac483fa4ae64190bf9dd9b1374cd8242f5a7e98974157b9e3013b8c32f4a007ad9d908e73aa5a524a495536c459b776f0123214423fc3898559d21ce48b73a875ff3320b1bb6054c1b32d4d46a7127dcc865f0d30f2ee3dcd5983b686f4e3a9202afc8b608652001c9938906ae1ff1417486096e32511f1bc0x000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c555d45d77921e0f26487706179f73c5f8539744b55147c73a3621366bf809c066c8781959ce3621f67c9345da9b5e01ce5113d7b5ae3c6dcd3ca88ad4ed9023fa55c8188060c74f1791eefc78e8aacf0c044e3f6317fe5eadce8ba9db2d19da83e41364c3d6802175acaa392d576a95206d83e5bbfc6022b53c288dc5b60cfe1722298007f1a4b97f47383a9fe1cb7bb5250783e89f3720b7a37bec026ece0b60000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000b0284d0d1efd5baad5cec3da093e963eeba7827f3d9aacf332c0081393d5cc107cd4dfcdc99332fd9b96b275acaf3fd8dc79645ccebddb510788b179c260bde66aeba2a3ab07ae223b0b3db25a6184f3b48fd3f1881cfb5eac483fa4ae64190bf9dd9b1374cd8242f5a7e98974157b9e3013b8c32f4a007ad9d908e73aa5a524a495536c459b776f0123214423fc3898559d21ce48b73a875ff3320b1bb6054c1b32d4d46a7127dcc865f0d30f2ee3dcd5983b686f4e3a9202afc8b608652001c9938906ae1ff1417486096e32511f1bc",
Input: "000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c555d45d77921e0f26487706179f73c5f8539744b55147c73a3621366bf809c066c8781959ce3621f67c9345da9b5e01ce5113d7b5ae3c6dcd3ca88ad4ed9023fa55c8188060c74f1791eefc78e8aacf0c044e3f6317fe5eadce8ba9db2d19da83e41364c3d6802175acaa392d576a95206d83e5bbfc6022b53c288dc5b60cfe1722298007f1a4b97f47383a9fe1cb7bb5250783e89f3720b7a37bec026ece0b6b32d4d46a7127dcc865f0d30f2ee3dcd5983b686f4e3a9202afc8b608652001c9938906ae1ff1417486096e32511f1bc",
Expected: "01",
Name: "",
}
Expand Down
12 changes: 6 additions & 6 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,23 +624,23 @@ func (h *handler) BroadcastVote(vote *types.VoteEnvelope) {
directCount int // Count of announcements made
directPeers int

voteset = make(map[*ethPeer]*types.VoteEnvelope) // Set peer->hash to transfer directly
voteMap = make(map[*ethPeer]*types.VoteEnvelope) // Set peer->hash to transfer directly
)

// Broadcast vote to a batch of peers not knowing about it
peers := h.peers.peersWithoutVote(vote.Hash())
for _, peer := range peers {
_, peerTD := peer.Head()
if deltaTD := new(big.Int).Abs(new(big.Int).Sub(h.chain.GetTdByHash(h.chain.CurrentHeader().Hash()), peerTD)); deltaTD.Cmp(big.NewInt(deltaTdThreshold)) < 1 {
voteset[peer] = vote
deltaTD := new(big.Int).Abs(new(big.Int).Sub(h.chain.GetTdByHash(h.chain.CurrentHeader().Hash()), peerTD))
if deltaTD.Cmp(big.NewInt(deltaTdThreshold)) < 1 {
voteMap[peer] = vote
}
}

for peer, _vote := range voteset {
for peer, _vote := range voteMap {
directPeers++
directCount += 1
votes := make([]*types.VoteEnvelope, 1)
votes[0] = _vote
votes := []*types.VoteEnvelope{_vote}
peer.AsyncSendVotes(votes)
}
log.Debug("Vote broadcast", "vote packs", directPeers, "broadcast vote", directCount)
Expand Down
4 changes: 2 additions & 2 deletions eth/protocols/eth/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (p *Peer) AsyncSendNewBlock(block *types.Block, td *big.Int) {

// SendVotes propagates a batch of votes to the remote peer.
func (p *Peer) SendVotes(votes []*types.VoteEnvelope) error {
// Mark all the transactions as known, but ensure we don't overflow our limits
// Mark all the votes as known, but ensure we don't overflow our limits
for p.knownVotes.Cardinality() > max(0, maxKnownTxs-len(votes)) {
p.knownVotes.Pop()
}
Expand All @@ -417,7 +417,7 @@ func (p *Peer) SendVotes(votes []*types.VoteEnvelope) error {
func (p *Peer) AsyncSendVotes(votes []*types.VoteEnvelope) {
select {
case p.voteBroadcast <- votes:
// Mark all the transactions as known, but ensure we don't overflow our limits
// Mark all the votes as known, but ensure we don't overflow our limits
for p.knownVotes.Cardinality() > max(0, maxKnownVotes-len(votes)) {
p.knownVotes.Pop()
}
Expand Down
16 changes: 8 additions & 8 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ const (
TendermintHeaderValidateGas uint64 = 3000 // Gas for validate tendermiint consensus state
IAVLMerkleProofValidateGas uint64 = 3000 // Gas for validate merkle proof

EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price
Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation
Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation
Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation
Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation
IdentityBaseGas uint64 = 15 // Base price for a data copy operation
IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation
SignatureVerifyGas uint64 = 70000 // Finality signature verify gas price
EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price
Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation
Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation
Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation
Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation
IdentityBaseGas uint64 = 15 // Base price for a data copy operation
IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation
VoteSignatureVerifyGas uint64 = 35000 // Finality signature verify gas price

Bn256AddGasByzantium uint64 = 500 // Byzantium gas needed for an elliptic curve addition
Bn256AddGasIstanbul uint64 = 150 // Gas needed for an elliptic curve addition
Expand Down

0 comments on commit fe2cc16

Please sign in to comment.