Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic: Invalid Major Version in Block #154

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 50 additions & 34 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,36 @@ package blockchain
// We must not call any packages that can call panic
// NO Panics or FATALs please

import "os"
import "fmt"
import "sync"
import "time"
import "bytes"
import "runtime/debug"
import "strings"

import "runtime"
import "context"
import "golang.org/x/crypto/sha3"
import "golang.org/x/sync/semaphore"
import "github.com/go-logr/logr"

import "sync/atomic"

import "github.com/hashicorp/golang-lru"

import "github.com/deroproject/derohe/rpc"
import "github.com/deroproject/derohe/config"
import "github.com/deroproject/derohe/cryptography/crypto"
import "github.com/deroproject/derohe/errormsg"
import "github.com/deroproject/derohe/metrics"

import "github.com/deroproject/derohe/dvm"
import "github.com/deroproject/derohe/block"
import "github.com/deroproject/derohe/globals"
import "github.com/deroproject/derohe/transaction"
import "github.com/deroproject/derohe/blockchain/mempool"
import "github.com/deroproject/derohe/blockchain/regpool"

import "github.com/deroproject/graviton"
import (
"bytes"
"context"
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/go-logr/logr"
"golang.org/x/crypto/sha3"
"golang.org/x/sync/semaphore"

"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/blockchain/mempool"
"github.com/deroproject/derohe/blockchain/regpool"
"github.com/deroproject/derohe/config"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/dvm"
"github.com/deroproject/derohe/errormsg"
"github.com/deroproject/derohe/globals"
"github.com/deroproject/derohe/metrics"
"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/transaction"
"github.com/deroproject/graviton"
lru "github.com/hashicorp/golang-lru"
)

// all components requiring access to blockchain must use , this struct to communicate
// this structure must be update while mutex
Expand Down Expand Up @@ -203,7 +201,11 @@ func Blockchain_Start(params map[string]interface{}) (*Blockchain, error) {

init_hard_forks(params) // hard forks must be initialized asap

chain.Initialise_Chain_From_DB() // load the chain from the disk
// load the chain from the disk
if ok := chain.Initialise_Chain_From_DB(); !ok {
logger.Error(fmt.Errorf("corrupted top block"), "Top block is broken, rewind 1 block")
chain.Rewind_Chain(1)
}

try_again:
version, err := chain.ReadBlockSnapshotVersion(chain.Get_Top_ID())
Expand Down Expand Up @@ -334,24 +336,38 @@ func (chain *Blockchain) SetIntegratorAddress(addr rpc.Address) {
// this function is called to read blockchain state from DB
// It is callable at any point in time

func (chain *Blockchain) Initialise_Chain_From_DB() {
func (chain *Blockchain) Initialise_Chain_From_DB() (ok bool) {
chain.Lock()
defer chain.Unlock()

ok = true
chain.Pruned = chain.LocatePruneTopo()

// find the tips from the chain , first by reaching top height
// then downgrading to top-10 height
// then reworking the chain to get the tip
best_height := chain.Load_TOP_HEIGHT()

// load and check top block for corruption
blid, err := chain.Load_Block_Topological_order_at_index(best_height)
if err != nil {
panic(err)
} else {
data, err := chain.Store.Block_tx_store.ReadBlock(blid)
if len(data) == 0 || err != nil {
ok = false
}
}

chain.Tips = map[crypto.Hash]crypto.Hash{} // reset the map
// reload top tip from disk
top := chain.Get_Top_ID()

chain.Tips[top] = top // we only can load a single tip from db

logger.V(1).Info("Reloaded Chain from disk", "Tips", chain.Tips, "Height", best_height)

return
}

// before shutdown , make sure p2p is confirmed stopped
Expand Down