func getAll() error {
	dbs := make([]*common.DirectoryBlock, 0, 100)
	next := DBHeadStr

	for {
		blk, err := factom.GetRaw(next)
		if err != nil {
			panic(err.Error())
		}
		db := new(common.DirectoryBlock)
		err = db.UnmarshalBinary(blk)
		if err != nil {
			panic(err.Error())
		}
		dbs = append(dbs, db)
		if bytes.Equal(db.Header.PrevKeyMR.Bytes(), DBHeadLast) {
			break
		}
		next = hex.EncodeToString(db.Header.PrevKeyMR.Bytes())
	}

	DBHeadLast = DBHead

	for i := len(dbs) - 1; i >= 0; i-- {
		DirectoryBlocks = append(DirectoryBlocks, dbs[i])
		fb := new(block.FBlock)
		var fcnt int
		for _, dbe := range dbs[i].DBEntries {
			if bytes.Equal(dbe.ChainID.Bytes(), common.FACTOID_CHAINID) {
				fcnt++
				hashstr := hex.EncodeToString(dbe.KeyMR.Bytes())
				fdata, err := factom.GetRaw(hashstr)
				if err != nil {
					panic(err.Error())
				}
				err = fb.UnmarshalBinary(fdata)
				if err != nil {
					panic(err.Error())
				}
				FactoidBlocks = append(FactoidBlocks, fb)
				break
			}
		}
		if fb == nil {
			panic("Missing Factoid Block from a directory block")
		}
		if fcnt > 1 {
			panic("More than one Factom Block found in a directory block.")
		}
		if err := ProcessFB(fb); err != nil {
			return err
		}
	}
	return nil
}
Exemple #2
0
func getAll() error {
	dbs := make([]*common.DirectoryBlock, 0, 100)
	next := DBHeadStr

	for {
		blk, err := factom.GetRaw(next)
		if err != nil {
			panic(err.Error())
		}
		db := new(common.DirectoryBlock)
		err = db.UnmarshalBinary(blk)
		if err != nil {
			panic(err.Error())
		}
		dbs = append(dbs, db)
		if bytes.Equal(db.Header.PrevKeyMR.Bytes(), common.ZERO_HASH[:]) {
			break
		}
		next = hex.EncodeToString(db.Header.PrevKeyMR.Bytes())
	}

	for i := len(dbs) - 1; i >= 0; i-- {
		DirectoryBlocks = append(DirectoryBlocks, dbs[i])
		fb := new(block.FBlock)
		for _, dbe := range dbs[i].DBEntries {
			if bytes.Equal(dbe.ChainID.Bytes(), common.FACTOID_CHAINID) {
				hashstr := hex.EncodeToString(dbe.KeyMR.Bytes())
				fdata, err := factom.GetRaw(hashstr)
				if err != nil {
					panic(err.Error())
				}
				err = fb.UnmarshalBinary(fdata)
				if err != nil {
					panic(err.Error())
				}
				FactoidBlocks = append(FactoidBlocks, fb)
				break
			}
		}
		if fb == nil {
			fmt.Println("Missing Factoid Block")
		}
	}
	return nil
}
func FetchAndParseEntry(hash, blockTime string, isFirstEntry bool) (*Entry, error) {
	e := new(Entry)
	raw, err := factom.GetRaw(hash)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	entry := new(common.Entry)
	_, err = entry.UnmarshalBinaryData(raw)
	if err != nil {
		Log("Error unmarshalling data - %v, %x - %v", hash, err, raw)
		return nil, err
	}

	e.ChainID = entry.ChainID.String()
	e.Hash = hash
	str, err := entry.JSONString()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	e.JSONString = str
	e.SpewString = entry.Spew()
	e.BinaryString = fmt.Sprintf("%x", raw)
	e.Timestamp = blockTime

	e.Content = ByteSliceToDecodedStringPointer(entry.Content)
	e.ExternalIDs = make([]DecodedString, len(entry.ExtIDs))
	for i, v := range entry.ExtIDs {
		e.ExternalIDs[i] = ByteSliceToDecodedString(v)
	}

	if isFirstEntry == true {
		//TODO: parse the first entry somehow perhaps?
	} else {
		if IsAnchorChainID(e.ChainID) {
			ar, err := ParseAnchorChainData(e.Content.Decoded)
			if err != nil {
				Log("Error - %v", err)
				return nil, err
			}
			e.AnchorRecord = ar
		}
	}

	err = SaveEntry(e)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	return e, nil
}
func FetchBlock(chainID, hash, blockTime string) (*Block, error) {
	block := new(Block)

	raw, err := factom.GetRaw(hash)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	switch chainID {
	case "000000000000000000000000000000000000000000000000000000000000000a":
		block, err = ParseAdminBlock(chainID, hash, raw, blockTime)
		if err != nil {
			Log("Error - %v", err)
			return nil, err
		}
		break
	case "000000000000000000000000000000000000000000000000000000000000000c":
		block, err = ParseEntryCreditBlock(chainID, hash, raw, blockTime)
		if err != nil {
			Log("Error - %v", err)
			return nil, err
		}
		break
	case "000000000000000000000000000000000000000000000000000000000000000f":
		block, err = ParseFactoidBlock(chainID, hash, raw, blockTime)
		if err != nil {
			Log("Error - %v", err)
			return nil, err
		}
		break
	default:
		block, err = ParseEntryBlock(chainID, hash, raw, blockTime)
		if err != nil {
			Log("Error - %v", err)
			return nil, err
		}
		break
	}

	err = SaveBlock(block)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	return block, nil
}