2019-11-15 10:32:40 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2021-03-25 18:46:52 +00:00
|
|
|
"errors"
|
|
|
|
|
2024-03-21 19:49:39 +00:00
|
|
|
"github.com/nspcc-dev/dbft"
|
2021-03-25 18:46:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
coreb "github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-11-15 10:32:40 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// neoBlock is a wrapper of a core.Block which implements
|
2019-11-15 10:32:40 +00:00
|
|
|
// methods necessary for dBFT library.
|
|
|
|
type neoBlock struct {
|
2020-01-14 12:32:07 +00:00
|
|
|
coreb.Block
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
network netmode.Magic
|
2019-11-15 10:32:40 +00:00
|
|
|
signature []byte
|
|
|
|
}
|
|
|
|
|
2024-03-21 19:49:39 +00:00
|
|
|
var _ dbft.Block[util.Uint256] = (*neoBlock)(nil)
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Sign implements the block.Block interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (n *neoBlock) Sign(key dbft.PrivateKey) error {
|
2021-03-25 18:46:52 +00:00
|
|
|
k := key.(*privateKey)
|
|
|
|
sig := k.PrivateKey.SignHashable(uint32(n.network), &n.Block)
|
2019-11-15 10:32:40 +00:00
|
|
|
n.signature = sig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Verify implements the block.Block interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (n *neoBlock) Verify(key dbft.PublicKey, sign []byte) error {
|
2021-03-25 18:46:52 +00:00
|
|
|
k := key.(*publicKey)
|
|
|
|
if k.PublicKey.VerifyHashable(sign, uint32(n.network), &n.Block) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("verification failed")
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Transactions implements the block.Block interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (n *neoBlock) Transactions() []dbft.Transaction[util.Uint256] {
|
|
|
|
txes := make([]dbft.Transaction[util.Uint256], len(n.Block.Transactions))
|
2019-11-15 10:32:40 +00:00
|
|
|
for i, tx := range n.Block.Transactions {
|
|
|
|
txes[i] = tx
|
|
|
|
}
|
|
|
|
|
|
|
|
return txes
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SetTransactions implements the block.Block interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (n *neoBlock) SetTransactions(txes []dbft.Transaction[util.Uint256]) {
|
2019-11-15 10:32:40 +00:00
|
|
|
n.Block.Transactions = make([]*transaction.Transaction, len(txes))
|
|
|
|
for i, tx := range txes {
|
|
|
|
n.Block.Transactions[i] = tx.(*transaction.Transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PrevHash implements the block.Block interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (n *neoBlock) PrevHash() util.Uint256 { return n.Block.PrevHash }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MerkleRoot implements the block.Block interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (n *neoBlock) MerkleRoot() util.Uint256 { return n.Block.MerkleRoot }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Timestamp implements the block.Block interface.
|
2020-07-11 12:22:14 +00:00
|
|
|
func (n *neoBlock) Timestamp() uint64 { return n.Block.Timestamp * nsInMs }
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Index implements the block.Block interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (n *neoBlock) Index() uint32 { return n.Block.Index }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Signature implements the block.Block interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (n *neoBlock) Signature() []byte { return n.signature }
|