diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-01 14:53:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-01 14:53:31 +0800 |
commit | ebfa4a6164dab7db29859538c1aa0e9659bd951a (patch) | |
tree | 317ee8ee45194ec63b4475565bf91cc7862494db /core/test/app.go | |
parent | 56fe2ee9435a89a46c0f3d527580aac43c85dc65 (diff) | |
download | dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar.gz dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar.bz2 dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar.lz dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar.xz dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.tar.zst dexon-consensus-ebfa4a6164dab7db29859538c1aa0e9659bd951a.zip |
core: core.Lattice supports config change (#276)
Besides making core.Lattice supports config change,
This PR also include the first test for below scenario:
- Configuration changes are registered before test
running
- Those changes are carried/broadcasted as payload
of blocks
- Only one node would initiate these changes, however,
all nodes would finally receive/apply those changes
to their own test.Governance instance.
Diffstat (limited to 'core/test/app.go')
-rw-r--r-- | core/test/app.go | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/core/test/app.go b/core/test/app.go index 546c9e5..4976ae0 100644 --- a/core/test/app.go +++ b/core/test/app.go @@ -88,22 +88,30 @@ type App struct { Delivered map[common.Hash]*AppDeliveredRecord DeliverSequence common.Hashes deliveredLock sync.RWMutex + blocks map[common.Hash]*types.Block + blocksLock sync.Mutex + state *State } // NewApp constructs a TestApp instance. -func NewApp() *App { +func NewApp(state *State) *App { return &App{ Acked: make(map[common.Hash]*AppAckedRecord), TotalOrdered: []*AppTotalOrderRecord{}, TotalOrderedByHash: make(map[common.Hash]*AppTotalOrderRecord), Delivered: make(map[common.Hash]*AppDeliveredRecord), DeliverSequence: common.Hashes{}, + blocks: make(map[common.Hash]*types.Block), + state: state, } } // PreparePayload implements Application interface. func (app *App) PreparePayload(position types.Position) ([]byte, error) { - return []byte{}, nil + if app.state == nil { + return []byte{}, nil + } + return app.state.PackRequests() } // PrepareWitness implements Application interface. @@ -119,7 +127,10 @@ func (app *App) VerifyBlock(block *types.Block) types.BlockVerifyStatus { } // BlockConfirmed implements Application interface. -func (app *App) BlockConfirmed(_ types.Block) { +func (app *App) BlockConfirmed(b types.Block) { + app.blocksLock.Lock() + defer app.blocksLock.Unlock() + app.blocks[b.Hash] = &b } // StronglyAcked implements Application interface. @@ -152,15 +163,30 @@ func (app *App) TotalOrderingDelivered(blockHashes common.Hashes, mode uint32) { // BlockDelivered implements Application interface. func (app *App) BlockDelivered( blockHash common.Hash, result types.FinalizationResult) { - app.deliveredLock.Lock() - defer app.deliveredLock.Unlock() - - app.Delivered[blockHash] = &AppDeliveredRecord{ - ConsensusTime: result.Timestamp, - ConsensusHeight: result.Height, - When: time.Now().UTC(), - } - app.DeliverSequence = append(app.DeliverSequence, blockHash) + func() { + app.deliveredLock.Lock() + defer app.deliveredLock.Unlock() + app.Delivered[blockHash] = &AppDeliveredRecord{ + ConsensusTime: result.Timestamp, + ConsensusHeight: result.Height, + When: time.Now().UTC(), + } + app.DeliverSequence = append(app.DeliverSequence, blockHash) + }() + // Apply packed state change requests in payload. + func() { + if app.state == nil { + return + } + app.blocksLock.Lock() + defer app.blocksLock.Unlock() + b := app.blocks[blockHash] + if err := app.state.Apply(b.Payload); err != nil { + if err != ErrDuplicatedChange { + panic(err) + } + } + }() } // Compare performs these checks against another App instance |