diff --git a/beacon/beacon.go b/beacon/beacon.go index d388340..e1a1503 100644 --- a/beacon/beacon.go +++ b/beacon/beacon.go @@ -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 -} diff --git a/blockchain/types/block.go b/blockchain/types/block.go index c48f148..09af79d 100644 --- a/blockchain/types/block.go +++ b/blockchain/types/block.go @@ -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 { diff --git a/consensus/miner.go b/consensus/miner.go index 29313c6..b007e81 100644 --- a/consensus/miner.go +++ b/consensus/miner.go @@ -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 diff --git a/node/node.go b/node/node.go index d8dca1b..bf4d649 100644 --- a/node/node.go +++ b/node/node.go @@ -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) diff --git a/node/node_dep_providers.go b/node/node_dep_providers.go index af14769..212658d 100644 --- a/node/node_dep_providers.go +++ b/node/node_dep_providers.go @@ -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) { diff --git a/types/beacon.go b/types/beacon.go index b10bc9c..d3cc45f 100644 --- a/types/beacon.go +++ b/types/beacon.go @@ -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,