-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add testing infrastructure for Anti-MEV dBFT extension
Add custom PreBlock and Block interfaces implementation, custom Commit and CommitAck, adjust testing logic. WIP, not finished, not buildable, but the idea can be traced. Continue testing infrastructure finalisation. Signed-off-by: Anna Shaleva <[email protected]>
- Loading branch information
1 parent
b7f4cc0
commit 659de52
Showing
8 changed files
with
459 additions
and
0 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 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,126 @@ | ||
package consensus | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"encoding/gob" | ||
"math" | ||
|
||
"github.com/nspcc-dev/dbft" | ||
"github.com/nspcc-dev/dbft/internal/crypto" | ||
"github.com/nspcc-dev/dbft/internal/merkle" | ||
) | ||
|
||
type amevBlock struct { | ||
base | ||
|
||
transactions []dbft.Transaction[crypto.Uint256] | ||
signature []byte | ||
hash *crypto.Uint256 | ||
} | ||
|
||
// NewAMEVBlock returns new block based on PreBlock and additional Commit-level data | ||
// collected from M consensus nodes. | ||
func NewAMEVBlock(pre dbft.PreBlock[crypto.Uint256], cnData [][]byte, m int) dbft.Block[crypto.Uint256] { | ||
preB := pre.(*preBlock) | ||
res := new(amevBlock) | ||
res.base = preB.base | ||
|
||
// Based on the provided cnData we'll add one more transaction to the resulting block. | ||
// Some artificial rules of new tx creation are invented here, but in Neo X there will | ||
// be well-defined custom rules for Envelope transactions. | ||
var sum uint32 | ||
for i := 0; i < m; i++ { | ||
sum += binary.BigEndian.Uint32(cnData[i]) | ||
} | ||
tx := Tx64(math.MaxInt64 - int64(sum)) | ||
res.transactions = append(preB.initialTransactions, &tx) | ||
|
||
// Rebuild Merkle root for the new set of transations. | ||
txHashes := make([]crypto.Uint256, len(res.transactions)) | ||
for i := range txHashes { | ||
txHashes[i] = res.transactions[i].Hash() | ||
} | ||
mt := merkle.NewMerkleTree(txHashes...) | ||
res.base.MerkleRoot = mt.Root().Hash | ||
|
||
return res | ||
} | ||
|
||
// PrevHash implements Block interface. | ||
func (b *amevBlock) PrevHash() crypto.Uint256 { | ||
return b.base.PrevHash | ||
} | ||
|
||
// Index implements Block interface. | ||
func (b *amevBlock) Index() uint32 { | ||
return b.base.Index | ||
} | ||
|
||
// MerkleRoot implements Block interface. | ||
func (b *amevBlock) MerkleRoot() crypto.Uint256 { | ||
return b.base.MerkleRoot | ||
} | ||
|
||
// Transactions implements Block interface. | ||
func (b *amevBlock) Transactions() []dbft.Transaction[crypto.Uint256] { | ||
return b.transactions | ||
} | ||
|
||
// SetTransactions implements Block interface. This method is special since it's | ||
// left for dBFT 2.0 compatibility and transactions from this method must not be | ||
// reused to fill final Block's transactions. | ||
func (b *amevBlock) SetTransactions(txx []dbft.Transaction[crypto.Uint256]) { | ||
Check warning on line 73 in internal/consensus/amev_block.go GitHub Actions / Lint
|
||
} | ||
|
||
// Signature implements Block interface. | ||
func (b *amevBlock) Signature() []byte { | ||
return b.signature | ||
} | ||
|
||
// GetHashData returns data for hashing and signing. | ||
// It must be an injection of the set of blocks to the set | ||
// of byte slices, i.e: | ||
// 1. It must have only one valid result for one block. | ||
// 2. Two different blocks must have different hash data. | ||
func (b *amevBlock) GetHashData() []byte { | ||
buf := bytes.Buffer{} | ||
w := gob.NewEncoder(&buf) | ||
_ = b.EncodeBinary(w) | ||
|
||
return buf.Bytes() | ||
} | ||
|
||
// Sign implements Block interface. | ||
func (b *amevBlock) Sign(key dbft.PrivateKey) error { | ||
data := b.GetHashData() | ||
|
||
sign, err := key.Sign(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b.signature = sign | ||
|
||
return nil | ||
} | ||
|
||
// Verify implements Block interface. | ||
func (b *amevBlock) Verify(pub dbft.PublicKey, sign []byte) error { | ||
data := b.GetHashData() | ||
return pub.(*crypto.ECDSAPub).Verify(data, sign) | ||
} | ||
|
||
// Hash implements Block interface. | ||
func (b *amevBlock) Hash() (h crypto.Uint256) { | ||
if b.hash != nil { | ||
return *b.hash | ||
} else if b.transactions == nil { | ||
return | ||
} | ||
|
||
hash := crypto.Hash256(b.GetHashData()) | ||
b.hash = &hash | ||
|
||
return hash | ||
} |
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,44 @@ | ||
package consensus | ||
|
||
import ( | ||
"encoding/gob" | ||
|
||
"github.com/nspcc-dev/dbft" | ||
) | ||
|
||
type ( | ||
// amevCommit implements dbft.Commit. | ||
amevCommit struct { | ||
data [dataSize]byte | ||
} | ||
// amevCommitAux is an auxiliary structure for amevCommit encoding. | ||
amevCommitAux struct { | ||
Data [dataSize]byte | ||
} | ||
) | ||
|
||
const dataSize = 64 | ||
|
||
var _ dbft.Commit = (*amevCommit)(nil) | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (c amevCommit) EncodeBinary(w *gob.Encoder) error { | ||
return w.Encode(amevCommitAux{ | ||
Data: c.data, | ||
}) | ||
} | ||
|
||
// DecodeBinary implements Serializable interface. | ||
func (c *amevCommit) DecodeBinary(r *gob.Decoder) error { | ||
aux := new(amevCommitAux) | ||
if err := r.Decode(aux); err != nil { | ||
return err | ||
} | ||
c.data = aux.Data | ||
return nil | ||
} | ||
|
||
// Signature implements Commit interface. | ||
func (c amevCommit) Signature() []byte { | ||
return c.data[:] | ||
} |
Oops, something went wrong.