Skip to content

Commit

Permalink
#186 Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarri committed Jun 26, 2019
1 parent 78b3a3b commit da8980b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 614 deletions.
18 changes: 17 additions & 1 deletion consensus/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
ethTypes "github.com/ethereum/go-ethereum/core/types"
tmtAbciTypes "github.com/tendermint/tendermint/abci/types"
tmtLog "github.com/tendermint/tendermint/libs/log"
"time"
)

// maxTransactionSize is 32KB in order to prevent DOS attacks
const maxTransactionSize = 32768
const minBlockDurationInSeconds int64 = 1;

// TendermintABCI is the main hook of application layer (blockchain) for connecting to consensus (Tendermint) using ABCI.
//
Expand Down Expand Up @@ -100,7 +102,14 @@ func (abci *TendermintABCI) InitChain(req tmtAbciTypes.RequestInitChain) tmtAbci
// - Optional Key-Value tags for filtering and indexing
func (abci *TendermintABCI) BeginBlock(req tmtAbciTypes.RequestBeginBlock) tmtAbciTypes.ResponseBeginBlock {
abci.logger.Debug("Beginning new block", "hash", req.Hash)
abci.db.UpdateBlockState(&req.Header)
err := abci.db.UpdateBlockState(req.Header)

// Note: Tendermint does not expect errors from BeginBlock. In case this block is achieve consensus
// the database chain will end up corrupted, therefore exit immediately seems to be the only alternative
// ISSUE: https://github.com/tendermint/tendermint/issues/3755
if err != nil {
panic(err)
}

return tmtAbciTypes.ResponseBeginBlock{}
}
Expand Down Expand Up @@ -253,6 +262,13 @@ func (abci *TendermintABCI) DeliverTx(txBytes []byte) tmtAbciTypes.ResponseDeliv
func (abci *TendermintABCI) EndBlock(req tmtAbciTypes.RequestEndBlock) tmtAbciTypes.ResponseEndBlock {
abci.logger.Debug(fmt.Sprintf("Ending new block at height '%d'", req.Height))

bDurationInSec := time.Now().Unix() - int64(abci.db.GetBlockStateHeader().Time)
if bDurationInSec < minBlockDurationInSeconds {
sleepDurationInSec := time.Duration(minBlockDurationInSeconds - bDurationInSec)
abci.logger.Debug(fmt.Sprintf("Waiting for %d seconds to end block...", sleepDurationInSec))
time.Sleep(sleepDurationInSec * time.Second)
}

return tmtAbciTypes.ResponseEndBlock{}
}

Expand Down
15 changes: 15 additions & 0 deletions consensus/abci/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package abci

import "github.com/tendermint/tendermint/abci/types"

var _ types.Application = (*Application)(nil)

type Application struct {
types.BaseApplication
}

func (app *Application) BeginBlock(req types.Request_BeginBlock) (types.ResponseBeginBlock, error) {
return types.ResponseBeginBlock{}, nil
}


6 changes: 3 additions & 3 deletions database/blockstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func (bs *blockState) persist(bc *core.BlockChain, db ethdb.Database) (ethTypes.
return *block, nil
}

func (bs *blockState) updateBlockState(config *params.ChainConfig, parentTime uint64, numTx uint64) {
func (bs *blockState) updateBlockState(config params.ChainConfig, blockTime uint64, numTx uint64) {
parentHeader := bs.parent.Header()
bs.header.Time = new(big.Int).SetUint64(parentTime).Uint64()
bs.header.Difficulty = ethash.CalcDifficulty(config, parentTime, parentHeader)
bs.header.Time = new(big.Int).SetUint64(blockTime).Uint64()
bs.header.Difficulty = ethash.CalcDifficulty(&config, blockTime, parentHeader)
bs.transactions = make([]*ethTypes.Transaction, 0, numTx)
bs.receipts = make([]*ethTypes.Receipt, 0, numTx)
}
17 changes: 14 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,24 @@ func (db *Database) ResetBlockState(receiver common.Address) error {
}

// UpdateBlockState uses the tendermint header to update the eth header.
func (db *Database) UpdateBlockState(tmHeader *tmtAbciTypes.Header) {
func (db *Database) UpdateBlockState(tmHeader tmtAbciTypes.Header) error {
db.logger.Debug("Updating DB BlockState")
db.ethState.UpdateBlockState(
db.eth.APIBackend.ChainConfig(),
err := db.ethState.UpdateBlockState(
*db.eth.APIBackend.ChainConfig(),
uint64(tmHeader.Time.Unix()),
uint64(tmHeader.GetNumTxs()),
)

if err != nil {
db.logger.Error("Invalid block header", "msg", err.Error())
return err
}

return nil
}

func (db *Database) GetBlockStateHeader() ethTypes.Header {
return *db.ethState.blockState.header
}

// GasLimit returns the maximum gas per block.
Expand Down
6 changes: 4 additions & 2 deletions database/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ func (es *EthState) resetBlockState(receiver common.Address) error {
return nil
}

func (es *EthState) UpdateBlockState(config *params.ChainConfig, parentTime uint64, numTx uint64) {
func (es *EthState) UpdateBlockState(config params.ChainConfig, blockTime uint64, numTx uint64) error {
es.mtx.Lock()
defer es.mtx.Unlock()

es.blockState.updateBlockState(config, parentTime, numTx)
es.blockState.updateBlockState(config, blockTime, numTx)
bc := es.ethereum.BlockChain()
return bc.Engine().VerifyHeader(bc, es.blockState.header, false)
}

func (es *EthState) GasLimit() *core.GasPool {
Expand Down
Loading

0 comments on commit da8980b

Please sign in to comment.