Esempio n. 1
0
func CheckDatabaseForMissingEntries(dbo interfaces.DBOverlay) {
	fmt.Printf("\t\tIterating over DBlocks\n")

	prevD, err := dbo.FetchDBlockHead()
	if err != nil {
		panic(err)
	}

	HashMap = map[string]string{}

	for {
		CheckDBlockEntries(prevD, dbo)
		HashMap[prevD.DatabasePrimaryIndex().String()] = "OK"

		if prevD.GetHeader().GetPrevKeyMR().String() == "0000000000000000000000000000000000000000000000000000000000000000" {
			break
		}
		dBlock, err := dbo.FetchDBlock(prevD.GetHeader().GetPrevKeyMR())
		if err != nil {
			panic(err)
		}
		if dBlock == nil {
			fmt.Printf("Found a missing dblock - %v\n", prevD.GetHeader().GetPrevKeyMR().String())
			dblock, err := GetDBlock(prevD.GetHeader().GetPrevKeyMR().String())
			if err != nil {
				panic(err)
			}
			err = dbo.ProcessDBlockBatchWithoutHead(dblock)
			if err != nil {
				panic(err)
			}
		} else {
			//only iterate to the next block if it was properly fetched from the database
			prevD = dBlock
		}
	}

	fmt.Printf("\t\tIterating over ECBlocks\n")
	prevEC, err := dbo.FetchECBlockHead()
	if err != nil {
		panic(err)
	}
	for {
		HashMap[prevEC.DatabasePrimaryIndex().String()] = "OK"
		if prevEC.GetHeader().GetPrevHeaderHash().String() == "0000000000000000000000000000000000000000000000000000000000000000" {
			break
		}
		ecBlock, err := dbo.FetchECBlock(prevEC.GetHeader().GetPrevHeaderHash())
		if err != nil {
			panic(err)
		}
		if ecBlock == nil {
			fmt.Printf("Found a missing ecblock - %v\n", prevEC.GetHeader().GetPrevHeaderHash().String())
			ecblock, err := GetECBlock(prevEC.GetHeader().GetPrevHeaderHash().String())
			if err != nil {
				panic(err)
			}
			err = dbo.ProcessECBlockBatchWithoutHead(ecblock, true)
			if err != nil {
				panic(err)
			}
		} else {
			//only iterate to the next block if it was properly fetched from the database
			prevEC = ecBlock
		}
	}

	fmt.Printf("\t\tIterating over FBlocks\n")
	prevF, err := dbo.FetchFBlockHead()
	if err != nil {
		panic(err)
	}
	for {
		HashMap[prevF.DatabasePrimaryIndex().String()] = "OK"
		if prevF.GetPrevKeyMR().String() == "0000000000000000000000000000000000000000000000000000000000000000" {
			break
		}
		fBlock, err := dbo.FetchFBlock(prevF.GetPrevKeyMR())
		if err != nil {
			panic(err)
		}
		if fBlock == nil {
			fmt.Printf("Found a missing fblock - %v\n", prevF.GetPrevKeyMR().String())
			fBlock, err := GetFBlock(prevF.GetPrevKeyMR().String())
			if err != nil {
				panic(err)
			}
			err = dbo.ProcessFBlockBatchWithoutHead(fBlock)
			if err != nil {
				panic(err)
			}
		} else {
			//only iterate to the next block if it was properly fetched from the database
			prevF = fBlock
		}
	}

	fmt.Printf("\t\tIterating over ABlocks\n")
	prevA, err := dbo.FetchABlockHead()
	if err != nil {
		panic(err)
	}
	for {
		HashMap[prevA.DatabasePrimaryIndex().String()] = "OK"
		if prevA.GetHeader().GetPrevBackRefHash().String() == "0000000000000000000000000000000000000000000000000000000000000000" {
			break
		}
		aBlock, err := dbo.FetchABlock(prevA.GetHeader().GetPrevBackRefHash())
		if err != nil {
			panic(err)
		}
		if aBlock == nil {
			fmt.Printf("Found a missing ablock - %v\n", prevA.GetHeader().GetPrevBackRefHash().String())
			aBlock, err := GetABlock(prevA.GetHeader().GetPrevBackRefHash().String())
			if err != nil {
				panic(err)
			}
			err = dbo.ProcessABlockBatchWithoutHead(aBlock)
			if err != nil {
				panic(err)
			}
		} else {
			//only iterate to the next block if it was properly fetched from the database
			prevA = aBlock
		}
	}

	fmt.Printf("\t\tFinding unused blocks\n")

	hashes, err := dbo.FetchAllDBlockKeys()
	if err != nil {
		panic(err)
	}
	for _, h := range hashes {
		if HashMap[h.String()] == "" {
			fmt.Printf("Superfluous DBlock - %v\n", h)
			dbo.Delete(databaseOverlay.DIRECTORYBLOCK, h.Bytes())
		}
	}

	hashes, err = dbo.FetchAllABlockKeys()
	if err != nil {
		panic(err)
	}
	for _, h := range hashes {
		if HashMap[h.String()] == "" {
			fmt.Printf("Superfluous ABlock - %v\n", h)
			dbo.Delete(databaseOverlay.ADMINBLOCK, h.Bytes())
		}
	}

	hashes, err = dbo.FetchAllECBlockKeys()
	if err != nil {
		panic(err)
	}
	for _, h := range hashes {
		if HashMap[h.String()] == "" {
			fmt.Printf("Superfluous ECBlock - %v\n", h)
			dbo.Delete(databaseOverlay.ENTRYCREDITBLOCK, h.Bytes())
		}
	}

	hashes, err = dbo.FetchAllFBlockKeys()
	if err != nil {
		panic(err)
	}
	for _, h := range hashes {
		if HashMap[h.String()] == "" {
			fmt.Printf("Superfluous FBlock - %v\n", h)
			dbo.Delete(databaseOverlay.FACTOIDBLOCK, h.Bytes())
		}
	}
}