From ea2d10b0c7874e6c1d3dd1cf2487ad2525ebb59c Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Tue, 31 Jul 2018 22:39:05 +0800 Subject: core: refine Application interface and add Governance interface (#24) Add a new Governance interface for interaction with the governance contract. Also remove the ValidateBlock call in application interface as the application should validate it before putting it into the consensus module. A new BlockConverter interface is also added. The consensus module should accept the BlockConverter interface in future implementation, and use the Block() function to get the underlying block info. --- core/application.go | 15 +++++++++++--- core/blocklattice.go | 2 +- core/blocklattice_test.go | 10 ++++----- core/governance.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ core/types/block.go | 6 ++++++ 5 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 core/governance.go (limited to 'core') diff --git a/core/application.go b/core/application.go index 7eca66e..edeb686 100644 --- a/core/application.go +++ b/core/application.go @@ -17,11 +17,20 @@ package core -import "github.com/dexon-foundation/dexon-consensus-core/core/types" +import ( + "time" + + "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core/types" +) // Application describes the application interface that interacts with DEXON // consensus core. type Application interface { - ValidateBlock(b *types.Block) bool - Deliver(blocks []*types.Block, early bool) + // TotalOrderingDeliver is called when the total ordering algorithm deliver + // a set of block. + TotalOrderingDeliver(blocks []*types.Block, early bool) + + // DeliverBlock is called when a block is add to the compaction chain. + DeliverBlock(blockHash common.Hash, timestamp time.Time) } diff --git a/core/blocklattice.go b/core/blocklattice.go index e4a76cc..1f12474 100644 --- a/core/blocklattice.go +++ b/core/blocklattice.go @@ -532,7 +532,7 @@ func (l *BlockLattice) totalOrdering(b *types.Block) { sort.Sort(types.ByHash(output)) if len(output) > 0 { - l.app.Deliver(output, earlyDelivery) + l.app.TotalOrderingDeliver(output, earlyDelivery) } // Rescan pending blocks to add into candidate set. diff --git a/core/blocklattice_test.go b/core/blocklattice_test.go index 0f78dc5..835c35e 100644 --- a/core/blocklattice_test.go +++ b/core/blocklattice_test.go @@ -20,6 +20,7 @@ package core import ( "sort" "testing" + "time" "github.com/stretchr/testify/suite" @@ -42,15 +43,14 @@ type TestApp struct { Early bool } -func (a *TestApp) ValidateBlock(b *types.Block) bool { - return true -} - -func (a *TestApp) Deliver(blocks []*types.Block, early bool) { +func (a *TestApp) TotalOrderingDeliver(blocks []*types.Block, early bool) { a.Outputs = append(a.Outputs, blocks...) a.Early = early } +func (a *TestApp) DeliverBlock(blockHashes common.Hash, timestamp time.Time) { +} + func (a *TestApp) Clear() { a.Outputs = nil a.Early = false diff --git a/core/governance.go b/core/governance.go new file mode 100644 index 0000000..a3bc3af --- /dev/null +++ b/core/governance.go @@ -0,0 +1,53 @@ +// Copyright 2018 The dexon-consensus-core Authors +// This file is part of the dexon-consensus-core library. +// +// The dexon-consensus-core library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus-core library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus-core library. If not, see +// . + +package core + +import ( + "github.com/cobinhood/decimal" + + "github.com/dexon-foundation/dexon-consensus-core/core/types" +) + +// MembershipActionType specifies the action of a membership event. +type MembershipActionType int + +// Event enums. +const ( + MembershipActionAdd MembershipActionType = 0 + MembershipActionDelete MembershipActionType = 1 +) + +// MembershipEvent specifies the event of membership changes. +type MembershipEvent struct { + Epoch int + Action MembershipActionType +} + +// Governance interface specifies interface to control the governance contract. +// Note that there are a lot more methods in the governance contract, that this +// interface only define those that are required to run the consensus algorithm. +type Governance interface { + // Get the current validator set and it's corresponding stake. + GetValidatorSet() map[types.ValidatorID]decimal.Decimal + + // Get block proposing interval (in milliseconds). + GetBlockProposingInterval() int + + // Get membership events after a certain epoch. + GetMembershipEvents(epoch int) []MembershipEvent +} diff --git a/core/types/block.go b/core/types/block.go index 61b6535..c445fd2 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -50,6 +50,12 @@ type Block struct { Status Status `json:"-"` } +// BlockConverter interface define the interface for extracting block +// information from an existing object. +type BlockConverter interface { + Block() *Block +} + func (b *Block) String() string { return fmt.Sprintf("Block(%v)", b.Hash.String()[:6]) } -- cgit v1.2.3