Ejemplo n.º 1
0
// FetchAllECBlocks gets all of the entry credit blocks
func (db *Overlay) FetchAllECBlocks() ([]interfaces.IEntryCreditBlock, error) {
	list, err := db.FetchAllBlocksFromBucket([]byte{byte(ENTRYCREDITBLOCK)}, entryCreditBlock.NewECBlock())
	if err != nil {
		return nil, err
	}
	return toECBlocksList(list), nil
}
Ejemplo n.º 2
0
func GenerateGenesisBlocks(networkID uint32) (interfaces.IDirectoryBlock, interfaces.IAdminBlock, interfaces.IFBlock, interfaces.IEntryCreditBlock) {
	dblk := directoryBlock.NewDirectoryBlock(nil)
	ablk := adminBlock.NewAdminBlock(nil)
	fblk := factoid.GetGenesisFBlock(networkID)
	ecblk := entryCreditBlock.NewECBlock()

	if networkID != constants.MAIN_NETWORK_ID {
		if networkID == constants.TEST_NETWORK_ID {
			ablk.AddFedServer(primitives.NewZeroHash())
		} else {
			ablk.AddFedServer(primitives.Sha([]byte("FNode0")))
		}
	} else {
		ecblk.GetBody().AddEntry(entryCreditBlock.NewServerIndexNumber())
		for i := 1; i < 11; i++ {
			minute := entryCreditBlock.NewMinuteNumber(uint8(i))
			ecblk.GetBody().AddEntry(minute)
		}
	}

	dblk.SetABlockHash(ablk)
	dblk.SetECBlockHash(ecblk)
	dblk.SetFBlockHash(fblk)
	dblk.GetHeader().SetNetworkID(networkID)

	dblk.GetHeader().SetTimestamp(primitives.NewTimestampFromMinutes(24018960))

	return dblk, ablk, fblk, ecblk
}
Ejemplo n.º 3
0
// FetchECBlockByKeyMR gets an Entry Credit block by hash from the database.
func (db *Overlay) FetchECBlockByKeyMR(hash interfaces.IHash) (interfaces.IEntryCreditBlock, error) {
	block, err := db.FetchBlock([]byte{byte(ENTRYCREDITBLOCK)}, hash, entryCreditBlock.NewECBlock())
	if err != nil {
		return nil, err
	}
	if block == nil {
		return nil, nil
	}
	return block.(interfaces.IEntryCreditBlock), nil
}
Ejemplo n.º 4
0
func (db *Overlay) FetchECBlockHead() (interfaces.IEntryCreditBlock, error) {
	blk := entryCreditBlock.NewECBlock()
	block, err := db.FetchChainHeadByChainID([]byte{byte(ENTRYCREDITBLOCK)}, primitives.NewHash(blk.GetChainID()), blk)
	if err != nil {
		return nil, err
	}
	if block == nil {
		return nil, nil
	}
	return block.(interfaces.IEntryCreditBlock), nil
}
Ejemplo n.º 5
0
// FetchECBlockByHeaderHash gets an Entry Credit block by hash from the database.
func (db *Overlay) FetchECBlockBySecondary(hash interfaces.IHash) (interfaces.IEntryCreditBlock, error) {
	block, err := db.FetchBlockBySecondaryIndex(ENTRYCREDITBLOCK_SECONDARYINDEX, ENTRYCREDITBLOCK, hash, entryCreditBlock.NewECBlock())
	if err != nil {
		return nil, err
	}
	if block == nil {
		return nil, nil
	}
	return block.(interfaces.IEntryCreditBlock), nil
}
Ejemplo n.º 6
0
func (m *DBStateMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
	//defer func() {
	//	if r := recover(); r != nil {
	//		err = fmt.Errorf("Error unmarshalling Directory Block State Message: %v", r)
	//	}
	//}()
	newData = data
	if newData[0] != m.Type() {
		return nil, fmt.Errorf("Invalid Message type")
	}
	newData = newData[1:]

	m.Peer2Peer = true

	m.Timestamp = new(primitives.Timestamp)
	newData, err = m.Timestamp.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.DirectoryBlock = new(directoryBlock.DirectoryBlock)
	newData, err = m.DirectoryBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.AdminBlock = new(adminBlock.AdminBlock)
	newData, err = m.AdminBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.FactoidBlock = new(factoid.FBlock)
	newData, err = m.FactoidBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.EntryCreditBlock = entryCreditBlock.NewECBlock()
	newData, err = m.EntryCreditBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	eBlockCount, newData := binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := uint32(0); i < eBlockCount; i++ {
		eBlock := entryBlock.NewEBlock()
		newData, err = eBlock.UnmarshalBinaryData(newData)
		if err != nil {
			panic(err.Error())
		}
		m.EBlocks = append(m.EBlocks, eBlock)
	}

	entryCount, newData := binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := uint32(0); i < entryCount; i++ {
		var entrySize uint32
		entrySize, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
		entry := entryBlock.NewEntry()
		newData, err = newData[int(entrySize):], entry.UnmarshalBinary(newData[:int(entrySize)])
		if err != nil {
			panic(err.Error())
		}
		m.Entries = append(m.Entries, entry)
	}

	return
}
Ejemplo n.º 7
0
func (s *State) loadDatabase() {
	dblk, err := s.DB.FetchDirectoryBlockHead()
	if err != nil {
		panic(err.Error())
	}

	if dblk == nil && s.NetworkNumber == constants.NETWORK_LOCAL {
		dblk, err = s.CreateDBlock()
		if err != nil {
			panic("Failed to initialize Factoids: " + err.Error())
		}

		//TODO Also need to set Admin block and EC Credit block

		fblk := block.GetGenesisFBlock()

		dblk.GetDBEntries()[2].SetKeyMR(fblk.GetKeyMR())

		s.SetCurrentDirectoryBlock(dblk)

		if s.FactoidState == nil {
			fs := new(FactoidState)
			fs.ValidationService = NewValidationService()
			s.FactoidState = fs
		}
		if err := s.FactoidState.AddTransactionBlock(fblk); err != nil {
			panic("Failed to initialize Factoids: " + err.Error())
		}

		s.EntryCreditBlock = entryCreditBlock.NewECBlock()

		dblk, err = s.CreateDBlock()
		if dblk == nil {
			panic("dblk should never be nil")
		}
		s.ProcessEndOfBlock()
		dblk = s.GetCurrentDirectoryBlock()

	} else {
		if dblk.GetHeader().GetDBHeight() > 0 {
			dbPrev, err := s.DB.FetchDBlockByKeyMR(dblk.GetHeader().GetPrevKeyMR())
			if err != nil {
				panic("Failed to load the Previous Directory Block: " + err.Error())
			}
			if dbPrev == nil {
				panic("Did not find the Previous Directory Block in the database")
			}
			s.PreviousDirectoryBlock = dbPrev.(interfaces.IDirectoryBlock)
		}

		fBlocks, err := s.DB.FetchAllFBlocks()

		fmt.Printf("Processing %d FBlocks\n", len(fBlocks))

		if err != nil {
			panic(err.Error())
		}
		for _, block := range fBlocks {
			s.GetFactoidState().AddTransactionBlock(block)
		}

		ecBlocks, err := s.DB.FetchAllECBlocks()
		if err != nil {
			panic(err.Error())
		}

		fmt.Printf("Processing %d ECBlocks\n", len(ecBlocks))

		for _, block := range ecBlocks {
			s.EntryCreditBlock = block
			s.GetFactoidState().AddECBlock(block)
		}
	}
	s.SetCurrentDirectoryBlock(dblk)
}