better error handling, remove unused DarksideTx

This commit is contained in:
Larry Ruane 2020-05-18 10:37:49 -06:00 committed by Larry Ruane
parent 7420d565a8
commit 391e886afc
2 changed files with 23 additions and 37 deletions

View File

@ -20,7 +20,6 @@ import (
type darksideState struct { type darksideState struct {
resetted bool resetted bool
startHeight int // activeBlocks[0] corresponds to this height startHeight int // activeBlocks[0] corresponds to this height
saplingActivation int // must be <= startHeight
branchID string branchID string
chainName string chainName string
cache *BlockCache cache *BlockCache
@ -72,7 +71,7 @@ func DarksideReset(sa int, bi, cn string) error {
stopIngestor() stopIngestor()
state = darksideState{ state = darksideState{
resetted: true, resetted: true,
saplingActivation: sa, startHeight: sa,
latestHeight: -1, latestHeight: -1,
branchID: bi, branchID: bi,
chainName: cn, chainName: cn,
@ -100,30 +99,15 @@ func addBlockActive(blockBytes []byte) error {
blockHeight := block.GetHeight() blockHeight := block.GetHeight()
// first block, add to existing blocks slice if possible // first block, add to existing blocks slice if possible
if blockHeight > state.startHeight+len(state.activeBlocks) { if blockHeight > state.startHeight+len(state.activeBlocks) {
// The new block can't contiguously extend the existing return errors.New(fmt.Sprint("adding block at height ", blockHeight,
// range, so we have to drop the existing range. " would create a gap in the blockchain"))
state.activeBlocks = state.activeBlocks[:0] }
} else if blockHeight < state.startHeight { if blockHeight < state.startHeight {
// This block will replace the entire existing range. return errors.New(fmt.Sprint("adding block at height ", blockHeight,
state.activeBlocks = state.activeBlocks[:0] " is lower than Sapling activation height"))
} else { }
// Drop the block that will be overwritten, and its children. // Drop the block that will be overwritten, and its children, then add block.
state.activeBlocks = state.activeBlocks[:blockHeight-state.startHeight] state.activeBlocks = state.activeBlocks[:blockHeight-state.startHeight]
}
if len(state.activeBlocks) == 0 {
state.startHeight = blockHeight
} else {
// Set this block's prevhash.
prevblock := parser.NewBlock()
rest, err := prevblock.ParseFromSlice(state.activeBlocks[len(state.activeBlocks)-1])
if err != nil {
return err
}
if len(rest) != 0 {
return errors.New("block is too long")
}
copy(blockBytes[4:4+32], prevblock.GetEncodableHash())
}
state.activeBlocks = append(state.activeBlocks, blockBytes) state.activeBlocks = append(state.activeBlocks, blockBytes)
return nil return nil
} }
@ -162,6 +146,11 @@ func DarksideApplyStaged(height int) error {
} }
} }
state.stagedBlocks = state.stagedBlocks[:0] state.stagedBlocks = state.stagedBlocks[:0]
if height >= state.startHeight+len(state.activeBlocks) {
return errors.New(fmt.Sprint("ApplyStaged height ", height,
" is greater or equal to highest height ",
state.startHeight+len(state.activeBlocks)))
}
// Add staged transactions into blocks. Note we're not trying to // Add staged transactions into blocks. Note we're not trying to
// recover to the initial state; maybe it's better to just crash // recover to the initial state; maybe it's better to just crash
@ -188,6 +177,9 @@ func DarksideApplyStaged(height int) error {
state.stagedTransactions = state.stagedTransactions[:0] state.stagedTransactions = state.stagedTransactions[:0]
setPrevhash() setPrevhash()
state.latestHeight = height state.latestHeight = height
Log.Info("active blocks from ", state.startHeight,
" to ", state.startHeight+len(state.activeBlocks)-1,
", latest height ", state.latestHeight)
// The block ingestor can only run if there are blocks // The block ingestor can only run if there are blocks
if len(state.activeBlocks) > 0 { if len(state.activeBlocks) > 0 {
@ -310,7 +302,7 @@ func darksideRawRequest(method string, params []json.RawMessage) (json.RawMessag
blockchaininfo := Blockchaininfo{ blockchaininfo := Blockchaininfo{
Chain: state.chainName, Chain: state.chainName,
Upgrades: map[string]Upgradeinfo{ Upgrades: map[string]Upgradeinfo{
"76b809bb": {ActivationHeight: state.saplingActivation}, "76b809bb": {ActivationHeight: state.startHeight},
}, },
Headers: state.latestHeight, Headers: state.latestHeight,
Consensus: ConsensusInfo{state.branchID, state.branchID}, Consensus: ConsensusInfo{state.branchID, state.branchID},

View File

@ -23,12 +23,6 @@ message DarksideBlocksURL {
string url = 2; string url = 2;
} }
// A single transaction that should appear to be mined at the given height.
message DarksideTx {
int32 height = 1;
string transaction = 2;
}
message DarksideHeight { message DarksideHeight {
int32 height = 1; int32 height = 1;
} }