Пример #1
0
func MakeFEREntryWithHeightFromContent(passedResidentHeight uint32, passedTargetActivationHeight uint32,
	passedTargetPrice uint64, passedExpirationHeight uint32, passedPriority uint32) *FEREntryWithHeight {

	// Create and format the signing private key
	var signingPrivateKey [64]byte
	SigningPrivateKey := "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
	signingBytes, err := hex.DecodeString(SigningPrivateKey)
	if err != nil {
		fmt.Println("Signing private key isn't parsable")
		return nil
	}
	copy(signingPrivateKey[:], signingBytes[:])
	_ = ed.GetPublicKey(&signingPrivateKey) // Needed to format the public half of the key set

	anFEREntry := new(specialEntries.FEREntry)

	anFEREntry.SetExpirationHeight(passedExpirationHeight)
	anFEREntry.SetTargetActivationHeight(passedTargetActivationHeight)
	anFEREntry.SetPriority(passedPriority)
	anFEREntry.SetTargetPrice(passedTargetPrice)

	entryJson, err := json.Marshal(anFEREntry)
	if err != nil {
		fmt.Println("Bad marshal of anFEREntry")
		return nil
	}

	// Create the factom entry with the signing private key
	signingSignature := ed.Sign(&signingPrivateKey, entryJson)

	// Make a new factom entry and populate it
	anEntry := new(factom.Entry)
	anEntry.ChainID = "111111118d918a8be684e0dac725493a75862ef96d2d3f43f84b26969329bf03"
	anEntry.ExtIDs = append(anEntry.ExtIDs, signingSignature[:])
	anEntry.Content = entryJson

	// ce := common.NewEntry()
	emb, _ := anEntry.MarshalBinary()
	// ce.UnmarshalBinary(emb)

	EBEntry := entryBlock.NewEntry()
	_, err = EBEntry.UnmarshalBinaryData(emb)
	if err != nil {
		fmt.Println("Error 3:  couldn't unmarshal binary")
		return nil
	}

	ewh := new(FEREntryWithHeight)
	// Don't set the resident height in the actual FEREntry yet because the state validate loop will handle all that
	ewh.Height = passedResidentHeight
	ewh.AnFEREntry = EBEntry

	return ewh
}
Пример #2
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
}