Simplify validation process of beacon entries

This commit is contained in:
ChronosX88 2021-06-10 23:31:44 +03:00
parent ad45e0c554
commit ac2bf637a8
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
6 changed files with 15 additions and 87 deletions

View File

@ -4,9 +4,6 @@ import (
"context"
"fmt"
"github.com/Secured-Finance/dione/lib"
"github.com/sirupsen/logrus"
"github.com/Secured-Finance/dione/types"
)
@ -42,70 +39,13 @@ type BeaconAPI interface {
LatestBeaconRound() uint64
}
// ValidateTaskBeacons is a function that verifies dione task randomness
func ValidateTaskBeacons(beaconNetworks BeaconNetworks, t *types.DioneTask, prevEntry types.BeaconEntry) error {
parentBeacon := beaconNetworks.BeaconNetworkForRound(t.DrandRound - 1)
currBeacon := beaconNetworks.BeaconNetworkForRound(t.DrandRound)
if parentBeacon != currBeacon {
if len(t.BeaconEntries) != 2 {
return fmt.Errorf("expected two beacon entries at beacon fork, got %d", len(t.BeaconEntries))
}
err := currBeacon.VerifyEntry(t.BeaconEntries[1], t.BeaconEntries[0])
if err != nil {
return fmt.Errorf("beacon at fork point invalid: (%v, %v): %w",
t.BeaconEntries[1], t.BeaconEntries[0], err)
}
return nil
}
// ValidateBlockBeacons is a function that verifies block randomness
func (bn BeaconNetworks) ValidateBlockBeacons(beaconNetworks BeaconNetworks, curEntry, prevEntry types.BeaconEntry) error {
defaultBeacon := beaconNetworks.BeaconNetworkForRound(0)
// TODO: fork logic
bNetwork := beaconNetworks.BeaconNetworkForRound(t.DrandRound)
if uint64(t.DrandRound) == prevEntry.Round {
if len(t.BeaconEntries) != 0 {
return fmt.Errorf("expected not to have any beacon entries in this task, got %d", len(t.BeaconEntries))
}
return nil
}
if len(t.BeaconEntries) == 0 {
return fmt.Errorf("expected to have beacon entries in this task, but didn't find any")
}
last := t.BeaconEntries[len(t.BeaconEntries)-1]
if last.Round != uint64(t.DrandRound) {
return fmt.Errorf("expected final beacon entry in task to be at round %d, got %d", uint64(t.DrandRound), last.Round)
}
for i, e := range t.BeaconEntries {
if err := bNetwork.VerifyEntry(e, prevEntry); err != nil {
return fmt.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
}
prevEntry = e
if err := defaultBeacon.VerifyEntry(curEntry, prevEntry); err != nil {
return fmt.Errorf("beacon entry was invalid: %w", err)
}
return nil
}
func BeaconEntriesForTask(ctx context.Context, beaconNetworks BeaconNetworks) ([]types.BeaconEntry, error) {
beacon := beaconNetworks.BeaconNetworkForRound(0)
round := beacon.LatestBeaconRound()
start := lib.Clock.Now()
out := make([]types.BeaconEntry, 2)
prevBeaconEntry := beacon.Entry(ctx, round-1)
res := <-prevBeaconEntry
if res.Err != nil {
return nil, fmt.Errorf("getting entry %d returned error: %w", round-1, res.Err)
}
out[0] = res.Entry
curBeaconEntry := beacon.Entry(ctx, round)
res = <-curBeaconEntry
if res.Err != nil {
return nil, fmt.Errorf("getting entry %d returned error: %w", round, res.Err)
}
out[1] = res.Entry
logrus.Debugf("fetching beacon entries: took %v, count of entries: %v", lib.Clock.Since(start), len(out))
return out, nil
}

View File

@ -3,6 +3,8 @@ package types
import (
"time"
"github.com/Secured-Finance/dione/types"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/ethereum/go-ethereum/common"
@ -27,6 +29,8 @@ type BlockHeader struct {
Proposer peer.ID
ProposerEth common.Address
Signature []byte
BeaconEntry types.BeaconEntry
ElectionProof *types.ElectionProof
}
func GenesisBlock() *Block {

View File

@ -12,7 +12,6 @@ import (
types2 "github.com/Secured-Finance/dione/blockchain/types"
"github.com/Secured-Finance/dione/beacon"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/Secured-Finance/dione/ethclient"
@ -24,7 +23,6 @@ type Miner struct {
address peer.ID
ethAddress common.Address
mutex sync.Mutex
beacon beacon.BeaconNetworks
ethClient *ethclient.EthereumClient
minerStake *big.Int
networkStake *big.Int
@ -35,7 +33,6 @@ type Miner struct {
func NewMiner(
address peer.ID,
ethAddress common.Address,
beacon beacon.BeaconNetworks,
ethClient *ethclient.EthereumClient,
privateKey crypto.PrivKey,
mempool *pool.Mempool,
@ -43,7 +40,6 @@ func NewMiner(
return &Miner{
address: address,
ethAddress: ethAddress,
beacon: beacon,
ethClient: ethClient,
privateKey: privateKey,
mempool: mempool,
@ -112,16 +108,6 @@ func (m *Miner) MineBlock(randomness []byte, drandRound uint64, lastBlockHeader
return nil, nil
}
// TODO get rpc responses for oracle requests
//rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType)
//if rpcMethod == nil {
// return nil, xerrors.Errorf("invalid rpc method name/type")
//}
//res, err := rpcMethod(event.RequestParams)
//if err != nil {
// return nil, xerrors.Errorf("couldn't do rpc request: %w", err)
//}
txs := m.mempool.GetAllTransactions()
if txs == nil {
return nil, fmt.Errorf("there is no txes for processing") // skip new consensus round because there is no transaction for processing

View File

@ -158,14 +158,14 @@ func NewNode(config *config.Config, prvKey crypto.PrivKey, pexDiscoveryUpdateTim
logrus.Info("Blockchain synchronization subsystem has been successfully initialized!")
// initialize mining subsystem
miner := provideMiner(n.Host.ID(), *n.Ethereum.GetEthAddress(), n.Beacon, n.Ethereum, prvKey, mp)
miner := provideMiner(n.Host.ID(), *n.Ethereum.GetEthAddress(), n.Ethereum, prvKey, mp)
n.Miner = miner
logrus.Info("Mining subsystem has initialized!")
logrus.Info("Mining subsystem has been initialized!")
// initialize consensus subsystem
consensusManager := provideConsensusManager(psb, miner, ethClient, prvKey, n.Config.ConsensusMinApprovals)
n.ConsensusManager = consensusManager
logrus.Info("Consensus subsystem has initialized!")
logrus.Info("Consensus subsystem has been initialized!")
// initialize random beacon network subsystem
randomBeaconNetwork, err := provideBeacon(psb.Pubsub, consensusManager)
@ -181,7 +181,7 @@ func NewNode(config *config.Config, prvKey crypto.PrivKey, pexDiscoveryUpdateTim
logrus.Fatal(err)
}
n.DisputeManager = disputeManager
logrus.Info("Dispute subsystem has initialized!")
logrus.Info("Dispute subsystem has been initialized!")
// initialize internal eth wallet
//w, err := provideWallet(n.Host.ID(), rawPrivKey)

View File

@ -58,8 +58,8 @@ func provideDisputeManager(ctx context.Context, ethClient *ethclient.EthereumCli
return consensus.NewDisputeManager(ctx, ethClient, pcm, cfg.Ethereum.DisputeVoteWindow)
}
func provideMiner(peerID peer.ID, ethAddress common.Address, beacon beacon.BeaconNetworks, ethClient *ethclient.EthereumClient, privateKey crypto.PrivKey, mempool *pool.Mempool) *consensus.Miner {
return consensus.NewMiner(peerID, ethAddress, beacon, ethClient, privateKey, mempool)
func provideMiner(peerID peer.ID, ethAddress common.Address, ethClient *ethclient.EthereumClient, privateKey crypto.PrivKey, mempool *pool.Mempool) *consensus.Miner {
return consensus.NewMiner(peerID, ethAddress, ethClient, privateKey, mempool)
}
func provideBeacon(ps *pubsub2.PubSub, pcm *consensus.PBFTConsensusManager) (beacon.BeaconNetworks, error) {

View File

@ -6,8 +6,6 @@ type BeaconEntry struct {
Metadata map[string]interface{}
}
type Randomness []byte
func NewBeaconEntry(round uint64, data []byte, metadata map[string]interface{}) BeaconEntry {
return BeaconEntry{
Round: round,