Exemple #1
0
func GetDBlock(keymr string) (interfaces.IDirectoryBlock, error) {
	for i := 0; i < 100; i++ {
		raw, err := GetRaw(keymr)
		if err != nil {
			continue
		}
		dblock, err := directoryBlock.UnmarshalDBlock(raw)
		if err != nil {
			continue
		}
		return dblock, nil
	}
	panic("Failed 100 times to get the data " + keymr)
	return nil, nil
}
// Gets all the recent transctions. Will only keep the most recent 100.
func getRecentTransactions(time.Time) {
	/*defer func() {
		if r := recover(); r != nil {
			fmt.Println("Control Panel has encountered a panic in GetRecentTransactions.\n", r)
		}
	}()*/

	if DoingRecentTransactions {
		return
	}
	toggleDCT()
	defer toggleDCT()

	if StatePointer == nil {
		return
	}

	DisplayStateMutex.RLock()
	if DisplayState.LastDirectoryBlock == nil {
		DisplayStateMutex.RUnlock()
		return
	}
	data, err := DisplayState.LastDirectoryBlock.MarshalBinary()
	if err != nil {
		DisplayStateMutex.RUnlock()
		return
	}
	last, err := directoryBlock.UnmarshalDBlock(data)
	err = last.UnmarshalBinary(data)
	if err != nil {
		DisplayStateMutex.RUnlock()
		return
	}
	//last := DisplayState.LastDirectoryBlock
	DisplayStateMutex.RUnlock()

	if last == nil {
		return
	}

	RecentTransactionsMutex.Lock()
	defer RecentTransactionsMutex.Unlock()

	if RecentTransactions == nil {
		return
	}

	RecentTransactions.DirectoryBlock = struct {
		KeyMR     string
		BodyKeyMR string
		FullHash  string
		DBHeight  string
		Timestamp string

		PrevFullHash string
		PrevKeyMR    string
	}{last.GetKeyMR().String(), last.BodyKeyMR().String(), last.GetFullHash().String(), fmt.Sprintf("%d", last.GetDatabaseHeight()), last.GetTimestamp().String(), last.GetHeader().GetPrevFullHash().String(), last.GetHeader().GetPrevKeyMR().String()}
	// Process list items
	DisplayStateMutex.RLock()
	for _, entry := range DisplayState.PLEntry {
		e := new(EntryHolder)
		e.Hash = entry.EntryHash
		e.ChainID = "Processing"
		has := false
		for _, ent := range RecentTransactions.Entries {
			if ent.Hash == e.Hash {
				has = true
				break
			}
		}
		if !has {
			RecentTransactions.Entries = append(RecentTransactions.Entries, *e)
		}
	}

	for _, fTrans := range DisplayState.PLFactoid {
		if fTrans.TotalInputs == 0 {
			continue
		}
		has := false
		for _, trans := range RecentTransactions.FactoidTransactions {
			if fTrans.TxID == trans.TxID {
				has = true
				break
			}
		}
		if !has {
			RecentTransactions.FactoidTransactions = append(RecentTransactions.FactoidTransactions, struct {
				TxID         string
				Hash         string
				TotalInput   string
				Status       string
				TotalInputs  int
				TotalOutputs int
			}{fTrans.TxID, fTrans.Hash, fTrans.TotalInput, "Processing", fTrans.TotalInputs, fTrans.TotalOutputs})
		}
	}
	DisplayStateMutex.RUnlock()

	entries := last.GetDBEntries()
	for _, entry := range entries {
		if entry == nil {
			continue
		}
		if entry.GetChainID().String() == "000000000000000000000000000000000000000000000000000000000000000f" {
			mr := entry.GetKeyMR()
			dbase := StatePointer.GetAndLockDB()
			fblock, err := dbase.FetchFBlock(mr)
			StatePointer.UnlockDB()
			if err != nil || fblock == nil {
				continue
			}
			transactions := fblock.GetTransactions()
			for _, trans := range transactions {
				input, err := trans.TotalInputs()
				if err != nil || input == 0 {
					continue
				}
				totalInputs := len(trans.GetInputs())
				totalOutputs := len(trans.GetECOutputs())
				totalOutputs = totalOutputs + len(trans.GetOutputs())
				inputStr := fmt.Sprintf("%f", float64(input)/1e8)
				has := false
				for i, fact := range RecentTransactions.FactoidTransactions {
					if fact.TxID == trans.GetHash().String() {
						RecentTransactions.FactoidTransactions[i] = struct {
							TxID         string
							Hash         string
							TotalInput   string
							Status       string
							TotalInputs  int
							TotalOutputs int
						}{trans.GetSigHash().String(), trans.GetHash().String(), inputStr, "Confirmed", totalInputs, totalOutputs}
						has = true
						break
					}
				}
				if !has {
					RecentTransactions.FactoidTransactions = append(RecentTransactions.FactoidTransactions, struct {
						TxID         string
						Hash         string
						TotalInput   string
						Status       string
						TotalInputs  int
						TotalOutputs int
					}{trans.GetSigHash().String(), trans.GetHash().String(), inputStr, "Confirmed", totalInputs, totalOutputs})
				}
			}
		} else if entry.GetChainID().String() == "000000000000000000000000000000000000000000000000000000000000000c" {
			mr := entry.GetKeyMR()

			dbase := StatePointer.GetAndLockDB()
			ecblock, err := dbase.FetchECBlock(mr)
			StatePointer.UnlockDB()
			if err != nil || ecblock == nil {
				continue
			}
			ents := ecblock.GetEntries()
			for _, entry := range ents {
				if entry.GetEntryHash() != nil {
					e := getEntry(entry.GetEntryHash().String())
					if e != nil {
						has := false
						for i, ent := range RecentTransactions.Entries {
							if ent.Hash == e.Hash {
								RecentTransactions.Entries[i] = *e
								has = true
								break
							}
						}
						if !has {
							RecentTransactions.Entries = append(RecentTransactions.Entries, *e)
						}
					}
				}
			}
		}
	}

	if last.GetHeader().GetDBHeight() > RecentTransactions.LastHeightChecked {
		entriesNeeded := 100 - len(RecentTransactions.Entries)
		factoidsNeeded := 100 - len(RecentTransactions.FactoidTransactions)
		// If we do not have 100 of each transaction, we will look into the past to get 100
		if (entriesNeeded + factoidsNeeded) > 0 {
			getPastEntries(last, entriesNeeded, factoidsNeeded)
		} else {
			RecentTransactions.LastHeightChecked = last.GetHeader().GetDBHeight()
		}
	}

	if len(RecentTransactions.Entries) > 100 {
		overflow := len(RecentTransactions.Entries) - 100
		if overflow > 0 {
			RecentTransactions.Entries = RecentTransactions.Entries[overflow:]
		}
	}
	if len(RecentTransactions.FactoidTransactions) > 100 {
		overflow := len(RecentTransactions.FactoidTransactions) - 100
		if overflow > 0 {
			RecentTransactions.FactoidTransactions = RecentTransactions.FactoidTransactions[overflow:]
		}
	}

	// Check if we missed any processing
	for i, e := range RecentTransactions.Entries {
		if e.ChainID == "Processing" {
			entry := getEntry(e.Hash)
			if entry != nil {
				RecentTransactions.Entries[i] = *entry
			}
		}
	}
}
func DeepStateDisplayCopy(s *State) (*DisplayState, error) {
	ds := NewDisplayState()

	ds.NodeName = s.GetFactomNodeName()
	ds.ControlPanelPort = s.ControlPanelPort
	ds.ControlPanelSetting = s.ControlPanelSetting

	// DB Info
	ds.CurrentNodeHeight = s.GetHighestCompletedBlock()
	ds.CurrentLeaderHeight = s.GetLeaderHeight()
	ds.CurrentEBDBHeight = s.EntryBlockDBHeightProcessing
	ds.ProcessListHeight = uint32(int(s.ProcessLists.DBHeightBase) + len(s.ProcessLists.Lists) - 1)
	dir := s.GetDirectoryBlockByHeight(s.GetLeaderHeight())
	if dir == nil {
		dir = s.GetDirectoryBlockByHeight(s.GetLeaderHeight() - 1)
	}
	if dir != nil {
		data, err := dir.MarshalBinary()
		if err != nil || dir == nil {
		} else {
			newDBlock, err := directoryBlock.UnmarshalDBlock(data)
			if err != nil {
				ds.LastDirectoryBlock = nil
			} else {
				ds.LastDirectoryBlock = newDBlock
			}
		}
	}

	// Identities
	ds.IdentityChainID = s.GetIdentityChainID().Copy()
	for _, id := range s.Identities {
		ds.Identities = append(ds.Identities, id)
	}
	for _, auth := range s.Authorities {
		ds.Authorities = append(ds.Authorities, auth)
	}
	if pubkey, err := s.GetServerPublicKey().Copy(); err != nil {

	} else {
		ds.PublicKey = pubkey
	}

	vms := s.LeaderPL.VMs
	for _, v := range vms {
		list := v.List
		for _, msg := range list {
			if msg == nil {
				continue
			}
			switch msg.Type() {
			case constants.REVEAL_ENTRY_MSG:
				data, err := msg.MarshalBinary()
				if err != nil {
					continue
				}
				rev := new(messages.RevealEntryMsg)
				err = rev.UnmarshalBinary(data)
				if rev.Entry == nil || err != nil {
					continue
				}

				var entry EntryTransaction
				entry.ChainID = "Processing..."
				entry.EntryHash = rev.Entry.GetHash().String()

				ds.PLEntry = append(ds.PLEntry, entry)
			case constants.FACTOID_TRANSACTION_MSG:
				data, err := msg.MarshalBinary()
				if err != nil {
					continue
				}
				transMsg := new(messages.FactoidTransaction)
				err = transMsg.UnmarshalBinary(data)
				if transMsg.Transaction == nil || err != nil {
					continue
				}
				trans := transMsg.Transaction
				input, err := trans.TotalInputs()
				if err != nil {
					continue
				}
				totalInputs := len(trans.GetInputs())
				totalOutputs := len(trans.GetECOutputs())
				totalOutputs = totalOutputs + len(trans.GetOutputs())
				inputStr := fmt.Sprintf("%f", float64(input)/1e8)

				ds.PLFactoid = append(ds.PLFactoid, struct {
					TxID         string
					Hash         string
					TotalInput   string
					Status       string
					TotalInputs  int
					TotalOutputs int
				}{trans.GetSigHash().String(), trans.GetHash().String(), inputStr, "Process List", totalInputs, totalOutputs})
			}
		}
	}

	prt := "===SummaryStart===\n"
	s.Status = 1
	prt = prt + fmt.Sprintf("%s \n", s.ShortString())
	fnodes := make([]*State, 0)
	fnodes = append(fnodes, s)
	prt = prt + messageLists(fnodes)
	prt = prt + "===SummaryEnd===\n"

	ds.RawSummary = prt

	b := s.GetHighestCompletedBlock()
	pl := s.ProcessLists.Get(b + 1)
	if pl == nil {
		pl = s.ProcessLists.Get(b)
	}
	if pl != nil && pl.FedServers != nil {
		ds.PrintMap = pl.PrintMap()
		ds.ProcessList = pl.String()
	} else {
		ds.PrintMap = ""
		ds.ProcessList = ""
	}

	return ds, nil
}