aboutsummaryrefslogtreecommitdiffstats
path: root/core/blockchain.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2019-03-26 11:59:24 +0800
committerJimmy Hu <jimmy.hu@dexon.org>2019-03-27 15:25:10 +0800
commit7783bc4ba52bfc534d5b4d91e78abb2ddad7d078 (patch)
treef57ea6f4d8b2faa23ab95691717b071f507e621f /core/blockchain.go
parentb8ced165b1fb03394f8758e08148b0e5d06aa07b (diff)
downloadtangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar.gz
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar.bz2
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar.lz
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar.xz
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.tar.zst
tangerine-consensus-7783bc4ba52bfc534d5b4d91e78abb2ddad7d078.zip
core: bring back agreement result (#515)
* core: bring back agreement result * add logger * Fix * fixup
Diffstat (limited to 'core/blockchain.go')
-rw-r--r--core/blockchain.go65
1 files changed, 52 insertions, 13 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 798a080..283d22e 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -127,18 +127,19 @@ type tsigVerifierGetter interface {
}
type blockChain struct {
- lock sync.RWMutex
- ID types.NodeID
- lastConfirmed *types.Block
- lastDelivered *types.Block
- signer *utils.Signer
- vGetter tsigVerifierGetter
- app Application
- logger common.Logger
- configs []blockChainConfig
- pendingBlocks pendingBlockRecords
- confirmedBlocks types.BlocksByPosition
- dMoment time.Time
+ lock sync.RWMutex
+ ID types.NodeID
+ lastConfirmed *types.Block
+ lastDelivered *types.Block
+ signer *utils.Signer
+ vGetter tsigVerifierGetter
+ app Application
+ logger common.Logger
+ pendingRandomnesses map[types.Position]*types.AgreementResult
+ configs []blockChainConfig
+ pendingBlocks pendingBlockRecords
+ confirmedBlocks types.BlocksByPosition
+ dMoment time.Time
}
func newBlockChain(nID types.NodeID, dMoment time.Time, initBlock *types.Block,
@@ -153,6 +154,8 @@ func newBlockChain(nID types.NodeID, dMoment time.Time, initBlock *types.Block,
app: app,
logger: logger,
dMoment: dMoment,
+ pendingRandomnesses: make(
+ map[types.Position]*types.AgreementResult),
}
}
@@ -309,7 +312,9 @@ func (bc *blockChain) addEmptyBlock(position types.Position) (
// addBlock should be called when the block is confirmed by BA, we won't perform
// sanity check against this block, it's ok to add block with skipping height.
func (bc *blockChain) addBlock(b *types.Block) error {
- if b.Position.Round >= DKGDelayRound && len(b.Finalization.Randomness) == 0 {
+ if b.Position.Round >= DKGDelayRound &&
+ len(b.Finalization.Randomness) == 0 &&
+ !bc.setRandomnessFromPending(b) {
return ErrMissingRandomness
}
bc.lock.Lock()
@@ -327,6 +332,7 @@ func (bc *blockChain) addBlock(b *types.Block) error {
} else if b.IsGenesis() {
confirmed = true
}
+ delete(bc.pendingRandomnesses, b.Position)
if !confirmed {
return bc.addPendingBlockRecord(pendingBlockRecord{b.Position, b})
}
@@ -606,3 +612,36 @@ func (bc *blockChain) confirmBlock(b *types.Block) {
bc.confirmedBlocks = append(bc.confirmedBlocks, b)
bc.purgeConfig()
}
+
+func (bc *blockChain) setRandomnessFromPending(b *types.Block) bool {
+ if r, exist := bc.pendingRandomnesses[b.Position]; exist {
+ if !r.BlockHash.Equal(b.Hash) {
+ panic(fmt.Errorf("mismathed randomness: %s %s", b, r))
+ }
+ b.Finalization.Randomness = r.Randomness
+ delete(bc.pendingRandomnesses, b.Position)
+ return true
+ }
+ return false
+}
+
+func (bc *blockChain) processAgreementResult(result *types.AgreementResult) error {
+ if result.Position.Round < DKGDelayRound {
+ return nil
+ }
+ ok, err := bc.verifyRandomness(
+ result.BlockHash, result.Position.Round, result.Randomness)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return ErrIncorrectAgreementResult
+ }
+ bc.lock.RLock()
+ defer bc.lock.RUnlock()
+ if !result.Position.Newer(bc.lastConfirmed.Position) {
+ return nil
+ }
+ bc.pendingRandomnesses[result.Position] = result
+ return nil
+}