From 09f943d9d4ebd151a8c6fdbd15ca3282edbc7e94 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Fri, 12 Oct 2018 19:36:32 +0800 Subject: core: modify interface (#194) * Add a new method to notify full node about round cutting. * Modify interface to return error when preparing block --- core/interfaces.go | 8 ++++++-- core/lattice.go | 8 ++++++-- core/nodeset-cache_test.go | 5 +++-- core/nonblocking.go | 4 ++-- core/nonblocking_test.go | 8 ++++---- core/test/app.go | 8 ++++---- core/test/governance.go | 4 ++++ simulation/app.go | 8 ++++---- simulation/governance.go | 5 +++++ 9 files changed, 38 insertions(+), 20 deletions(-) diff --git a/core/interfaces.go b/core/interfaces.go index 7a5859e..4a20354 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -29,10 +29,10 @@ import ( // consensus core. type Application interface { // PreparePayload is called when consensus core is preparing a block. - PreparePayload(position types.Position) []byte + PreparePayload(position types.Position) ([]byte, error) // PrepareWitness will return the witness data no lower than consensusHeight. - PrepareWitness(consensusHeight uint64) types.Witness + PrepareWitness(consensusHeight uint64) (types.Witness, error) // VerifyBlock verifies if the block is valid. VerifyBlock(block *types.Block) bool @@ -103,6 +103,10 @@ type Governance interface { // Return the genesis node set if round == 0. NodeSet(round uint64) []crypto.PublicKey + // NotifyRoundHeight notifies governance contract to generate configuration + // for that round with the block on that consensus height. + NotifyRoundHeight(targetRound, consensusHeight uint64) + //// DKG-related methods. // AddDKGComplaint adds a DKGComplaint. diff --git a/core/lattice.go b/core/lattice.go index d634650..ab8aaec 100644 --- a/core/lattice.go +++ b/core/lattice.go @@ -79,8 +79,12 @@ func (s *Lattice) PrepareBlock( if err = s.data.prepareBlock(b); err != nil { return } - b.Payload = s.app.PreparePayload(b.Position) - b.Witness = s.app.PrepareWitness(b.Witness.Height) + if b.Payload, err = s.app.PreparePayload(b.Position); err != nil { + return + } + if b.Witness, err = s.app.PrepareWitness(b.Witness.Height); err != nil { + return + } if err = s.authModule.SignBlock(b); err != nil { return } diff --git a/core/nodeset-cache_test.go b/core/nodeset-cache_test.go index 6324421..73f0dee 100644 --- a/core/nodeset-cache_test.go +++ b/core/nodeset-cache_test.go @@ -40,8 +40,9 @@ func (g *testGov) Configuration(round uint64) (cfg *types.Config) { NumChains: 4, } } -func (g *testGov) CRS(round uint64) (b common.Hash) { return g.crs } -func (g *testGov) ProposeCRS([]byte) {} +func (g *testGov) CRS(round uint64) (b common.Hash) { return g.crs } +func (g *testGov) ProposeCRS([]byte) {} +func (g *testGov) NotifyRoundHeight(round, height uint64) {} func (g *testGov) NodeSet(round uint64) []crypto.PublicKey { // Randomly generating keys, and check them for verification. g.curKeys = []crypto.PublicKey{} diff --git a/core/nonblocking.go b/core/nonblocking.go index 9bedb4b..532bcb9 100644 --- a/core/nonblocking.go +++ b/core/nonblocking.go @@ -119,12 +119,12 @@ func (nb *nonBlocking) wait() { } // PreparePayload cannot be non-blocking. -func (nb *nonBlocking) PreparePayload(position types.Position) []byte { +func (nb *nonBlocking) PreparePayload(position types.Position) ([]byte, error) { return nb.app.PreparePayload(position) } // PrepareWitness cannot be non-blocking. -func (nb *nonBlocking) PrepareWitness(height uint64) types.Witness { +func (nb *nonBlocking) PrepareWitness(height uint64) (types.Witness, error) { return nb.app.PrepareWitness(height) } diff --git a/core/nonblocking_test.go b/core/nonblocking_test.go index 8cd42d0..50f580a 100644 --- a/core/nonblocking_test.go +++ b/core/nonblocking_test.go @@ -45,12 +45,12 @@ func newSlowApp(sleep time.Duration) *slowApp { } } -func (app *slowApp) PreparePayload(_ types.Position) []byte { - return []byte{} +func (app *slowApp) PreparePayload(_ types.Position) ([]byte, error) { + return []byte{}, nil } -func (app *slowApp) PrepareWitness(_ uint64) types.Witness { - return types.Witness{} +func (app *slowApp) PrepareWitness(_ uint64) (types.Witness, error) { + return types.Witness{}, nil } func (app *slowApp) VerifyBlock(_ *types.Block) bool { diff --git a/core/test/app.go b/core/test/app.go index 60bc26f..d9582c9 100644 --- a/core/test/app.go +++ b/core/test/app.go @@ -97,15 +97,15 @@ func NewApp() *App { } // PreparePayload implements Application interface. -func (app *App) PreparePayload(position types.Position) []byte { - return []byte{} +func (app *App) PreparePayload(position types.Position) ([]byte, error) { + return []byte{}, nil } // PrepareWitness implements Application interface. -func (app *App) PrepareWitness(height uint64) types.Witness { +func (app *App) PrepareWitness(height uint64) (types.Witness, error) { return types.Witness{ Height: height, - } + }, nil } // VerifyBlock implements Application. diff --git a/core/test/governance.go b/core/test/governance.go index de0ec21..88aab37 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -115,6 +115,10 @@ func (g *Governance) CRS(round uint64) common.Hash { return g.crs[round] } +// NotifyRoundHeight notifies governace contract to snapshot config. +func (g *Governance) NotifyRoundHeight(round, height uint64) { +} + // ProposeCRS propose a CRS. func (g *Governance) ProposeCRS(signedCRS []byte) { g.lock.Lock() diff --git a/simulation/app.go b/simulation/app.go index 0e3e184..0ca65c9 100644 --- a/simulation/app.go +++ b/simulation/app.go @@ -98,18 +98,18 @@ func (a *simApp) getAckedBlocks(ackHash common.Hash) (output common.Hashes) { } // PreparePayload implements core.Application. -func (a *simApp) PreparePayload(position types.Position) []byte { - return []byte{} +func (a *simApp) PreparePayload(position types.Position) ([]byte, error) { + return []byte{}, nil } // PrepareWitness implements core.Application. -func (a *simApp) PrepareWitness(height uint64) types.Witness { +func (a *simApp) PrepareWitness(height uint64) (types.Witness, error) { a.latestWitnessReady.L.Lock() defer a.latestWitnessReady.L.Unlock() for a.latestWitness.Height < height { a.latestWitnessReady.Wait() } - return a.latestWitness + return a.latestWitness, nil } // StronglyAcked is called when a block is strongly acked by DEXON diff --git a/simulation/governance.go b/simulation/governance.go index 2b0f56b..0a2c886 100644 --- a/simulation/governance.go +++ b/simulation/governance.go @@ -114,6 +114,11 @@ func (g *simGovernance) CRS(round uint64) common.Hash { return g.crs[round] } +// NotifyRoundHeight notifies governance contract to snapshot configuration +// for that round with the block on that consensus height. +func (g *simGovernance) NotifyRoundHeight(round, height uint64) { +} + // ProposeCRS proposes a CRS of round. func (g *simGovernance) ProposeCRS(signedCRS []byte) { crs := crypto.Keccak256Hash(signedCRS) -- cgit v1.2.3