-
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 <shaleva.ann@nspcc.ru>
- Loading branch information
1 parent
c2f52d3
commit 890c324
Showing
9 changed files
with
398 additions
and
5 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 must not be called for amevBlock. | ||
func (b *amevBlock) SetTransactions(txx []dbft.Transaction[crypto.Uint256]) { | ||
panic("MUST NOT BE CALLED BY DBFT") | ||
} | ||
|
||
// 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/binary" | ||
"encoding/gob" | ||
|
||
"github.com/nspcc-dev/dbft" | ||
) | ||
|
||
type ( | ||
amevCommit struct { | ||
magic uint32 // some magic data CN have to exchange to properly construct final amevBlock. | ||
} | ||
// commitAux is an auxiliary structure for commit encoding. | ||
amevCommitAux struct { | ||
Magic uint32 | ||
} | ||
) | ||
|
||
var _ dbft.Commit = (*amevCommit)(nil) | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (c amevCommit) EncodeBinary(w *gob.Encoder) error { | ||
return w.Encode(amevCommitAux{ | ||
Magic: c.magic, | ||
}) | ||
} | ||
|
||
// 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.magic = aux.Magic | ||
return nil | ||
} | ||
|
||
// Signature implements Commit interface. | ||
func (c amevCommit) Signature() []byte { | ||
res := make([]byte, 4) | ||
binary.BigEndian.PutUint32(res, c.magic) | ||
return res | ||
} |
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 ( | ||
// commitAck implements dbft.CommitAck and holds some side data. | ||
commitAck struct { | ||
data [dataSize]byte | ||
} | ||
// commitAckAux is an auxiliary structure for commitAck encoding. | ||
commitAckAux struct { | ||
Data [dataSize]byte | ||
} | ||
) | ||
|
||
const dataSize = 20 | ||
|
||
var _ dbft.CommitAck = (*commitAck)(nil) | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (c commitAck) EncodeBinary(w *gob.Encoder) error { | ||
return w.Encode(commitAckAux{ | ||
Data: c.data, | ||
}) | ||
} | ||
|
||
// DecodeBinary implements Serializable interface. | ||
func (c *commitAck) DecodeBinary(r *gob.Decoder) error { | ||
aux := new(commitAckAux) | ||
if err := r.Decode(aux); err != nil { | ||
return err | ||
} | ||
c.data = aux.Data | ||
return nil | ||
} | ||
|
||
// Data implements CommitAck interface. | ||
func (c commitAck) Data() []byte { | ||
return c.data[:] | ||
} |
Oops, something went wrong.