aboutsummaryrefslogtreecommitdiffstats
path: root/dex/app.go
diff options
context:
space:
mode:
authorbojie <bojie@dexon.org>2019-01-07 15:40:56 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:21 +0800
commitbc788bdf15656f8e867cf9a81769fb280b5bfb49 (patch)
treebd8ef519f56308878520bb7a5589dbdcb3970bfa /dex/app.go
parent4ca846ead9a66792a6b6b0e1ab8323b2f056b6f3 (diff)
downloadgo-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar.gz
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar.bz2
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar.lz
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar.xz
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.tar.zst
go-tangerine-bc788bdf15656f8e867cf9a81769fb280b5bfb49.zip
app: implement logic for prepare/verify correctly when chain number change (#118)
Diffstat (limited to 'dex/app.go')
-rw-r--r--dex/app.go56
1 files changed, 50 insertions, 6 deletions
diff --git a/dex/app.go b/dex/app.go
index c52a6c79b..b1a0b4548 100644
--- a/dex/app.go
+++ b/dex/app.go
@@ -29,6 +29,7 @@ import (
"github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/core"
+ "github.com/dexon-foundation/dexon/core/rawdb"
"github.com/dexon-foundation/dexon/core/types"
"github.com/dexon-foundation/dexon/ethdb"
"github.com/dexon-foundation/dexon/event"
@@ -175,6 +176,22 @@ func (d *DexconApp) preparePayload(ctx context.Context, position coreTypes.Posit
default:
}
+ if position.Round > 0 {
+ // If round chain number changed but new round is not delivered yet, payload must be nil.
+ previousNumChains := d.gov.Configuration(position.Round - 1).NumChains
+ currentNumChains := d.gov.Configuration(position.Round).NumChains
+ if previousNumChains != currentNumChains {
+ deliveredRound, err := rawdb.ReadLastRoundNumber(d.chainDB)
+ if err != nil {
+ panic(fmt.Errorf("read current round error: %v", err))
+ }
+
+ if deliveredRound < position.Round {
+ return nil, nil
+ }
+ }
+ }
+
if position.Height != 0 {
// Check if chain block height is strictly increamental.
chainLastHeight, ok := d.blockchain.GetChainLastConfirmedHeight(position.ChainID)
@@ -186,8 +203,7 @@ func (d *DexconApp) preparePayload(ctx context.Context, position coreTypes.Posit
}
b, latestState := d.blockchain.GetPending()
- log.Debug("Prepare payload", "chain", position.ChainID, "height", position.Height, "state",
- b.Root().String())
+ log.Debug("Prepare payload", "chain", position.ChainID, "height", position.Height, "state", b.Root().String())
txsMap, err := d.txPool.Pending()
if err != nil {
@@ -345,6 +361,26 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta
}
}
+ if block.Position.Round > 0 {
+ // If round chain number changed but new round is not delivered yet, payload must be nil.
+ previousNumChains := d.gov.Configuration(block.Position.Round - 1).NumChains
+ currentNumChains := d.gov.Configuration(block.Position.Round).NumChains
+ if previousNumChains != currentNumChains {
+ deliveredRound, err := rawdb.ReadLastRoundNumber(d.chainDB)
+ if err != nil {
+ panic(fmt.Errorf("read current round error: %v", err))
+ }
+
+ if deliveredRound < block.Position.Round {
+ if len(block.Payload) > 0 {
+ return coreTypes.VerifyInvalidBlock
+ }
+
+ return coreTypes.VerifyOK
+ }
+ }
+ }
+
// Get latest pending state.
b, latestState := d.blockchain.GetPending()
log.Debug("Verify block", "chain", block.Position.ChainID, "height", block.Position.Height, "state",
@@ -482,10 +518,18 @@ func (d *DexconApp) BlockDelivered(
}, txs, nil, nil)
h := d.blockchain.CurrentBlock().NumberU64() + 1
- _, err = d.blockchain.ProcessPendingBlock(newBlock, &block.Witness)
- if err != nil {
- log.Error("Failed to process pending block", "error", err)
- panic(err)
+ if block.IsEmpty() {
+ err = d.blockchain.ProcessEmptyBlock(newBlock)
+ if err != nil {
+ log.Error("Failed to process empty block", "error", err)
+ panic(err)
+ }
+ } else {
+ _, err = d.blockchain.ProcessPendingBlock(newBlock, &block.Witness)
+ if err != nil {
+ log.Error("Failed to process pending block", "error", err)
+ panic(err)
+ }
}
d.blockchain.RemoveConfirmedBlock(chainID, blockHash)