Example #1
0
// Go through the factoid exchange rate chain and determine if an FER change should be scheduled
func (this *State) ProcessRecentFERChainEntries() {
	// Find the FER entry chain
	FERChainHash, err := primitives.HexToHash(this.FERChainId)
	if err != nil {
		this.Println("The FERChainId couldn't be turned into a IHASH")
		return
	}

	//  Get the first eblock from the FERChain
	entryBlock, err := this.DB.FetchEBlockHead(FERChainHash)
	if err != nil {
		this.Println("Couldn't find the FER chain for id ", this.FERChainId)
		return
	}
	if entryBlock == nil {
		this.Println("FER Chain head found to be nil")
		return
	}

	this.Println("Checking last e block of FER chain with height of: ", entryBlock.GetHeader().GetDBHeight())
	this.Println("Current block height: ", this.GetDBHeightComplete())
	this.Println("BEFORE processing recent block: ")
	this.Println("    FERChangePrice: ", this.FERChangePrice)
	this.Println("    FERChangeHeight: ", this.FERChangeHeight)
	this.Println("    FERPriority: ", this.FERPriority)
	this.Println("    FERPrioritySetHeight: ", this.FERPrioritySetHeight)
	this.Println("    FER current: ", this.GetFactoshisPerEC())

	// Check to see if a price change targets the next block
	if this.FERChangeHeight == (this.GetDBHeightComplete())+1 {
		this.FactoshisPerEC = this.FERChangePrice
		this.FERChangePrice = 0
		this.FERChangeHeight = 1
	}

	// Check for the need to clear the priority
	// (this.GetDBHeightComplete() >= 12) is import because height is a uint and can't break logic if subtracted into false sub-zero
	if (this.GetDBHeightComplete() >= 12) &&
		(this.GetDBHeightComplete()-12) >= this.FERPrioritySetHeight {
		this.FERPrioritySetHeight = 0
		this.FERPriority = 0
		// Now the next entry to come through with a priority of 1 or more will be considered
	}

	// Check last entry block method
	if entryBlock.GetHeader().GetDBHeight() == this.GetDBHeightComplete()-1 {

		entryHashes := entryBlock.GetEntryHashes()

		// this.Println("Found FER entry hashes in a block as: ", entryHashes)
		// create a map of possible minute markers that may be found in the EBlock Body
		mins := make(map[string]uint8)
		for i := byte(0); i <= 10; i++ {
			h := make([]byte, 32)
			h[len(h)-1] = i
			mins[hex.EncodeToString(h)] = i
		}

		// Loop through the hashes from the last blocks FER entries and evaluate them individually
		for _, entryHash := range entryHashes {
			// if this entryhash is a minute mark then continue
			if _, exist := mins[entryHash.String()]; exist {
				continue
			}

			// Make sure the entry exists
			anEntry, err := this.DB.FetchEntry(entryHash)
			if err != nil {
				this.Println("Error during FetchEntryByHash: ", err)
				continue
			}
			if anEntry == nil {
				this.Println("Nil entry during FetchEntryByHash: ", entryHash)
				continue
			}

			if !this.ExchangeRateAuthorityIsValid(anEntry) {
				this.Println("Skipping non-authority FER chain entry", entryHash)
				continue
			}

			entryContent := anEntry.GetContent()
			// this.Println("Found content of an FER entry is:  ", string(entryContent))
			ferEntry := new(specialEntries.FEREntry)
			err = ferEntry.UnmarshalBinary(entryContent)
			if err != nil {
				this.Println("A FEREntry messgae didn't unmarshal correctly: ", err)
				continue
			}

			// Set it's resident height for validity checking
			ferEntry.SetResidentHeight(this.GetDBHeightComplete())

			if (this.FerEntryIsValid(ferEntry)) && (ferEntry.Priority > this.FERPriority) {
				this.Println(" Processing FER entry : ", string(entryContent))
				this.FERPriority = ferEntry.GetPriority()
				this.FERPrioritySetHeight = this.GetDBHeightComplete()
				this.FERChangePrice = ferEntry.GetTargetPrice()
				this.FERChangeHeight = ferEntry.GetTargetActivationHeight()

				// Adjust the target if needed
				if this.FERChangeHeight < (this.GetDBHeightComplete() + 2) {
					this.FERChangeHeight = this.GetDBHeightComplete() + 2
				}
			} else {
				this.Println(" Failed FER entry : ", string(entryContent))
			}
		}
	}

	this.Println("AFTER processing recent block: ")
	this.Println("    FERChangePrice: ", this.FERChangePrice)
	this.Println("    FERChangeHeight: ", this.FERChangeHeight)
	this.Println("    FERPriority: ", this.FERPriority)
	this.Println("    FERPrioritySetHeight: ", this.FERPrioritySetHeight)
	this.Println("    FER current: ", this.GetFactoshisPerEC())
	this.Println("----------------------------------")

	return
}