Example #1
0
func CreateAnchorRecordFromDBlock(dBlock interfaces.IDirectoryBlock) *AnchorRecord {
	ar := new(AnchorRecord)
	ar.AnchorRecordVer = 1
	ar.DBHeight = dBlock.GetHeader().GetDBHeight()
	ar.KeyMR = dBlock.DatabasePrimaryIndex().String()
	ar.RecordHeight = ar.DBHeight
	return ar
}
Example #2
0
// NewDirBlockInfoFromDirBlock creates a DirDirBlockInfo from DirectoryBlock
func NewDirBlockInfoFromDirBlock(dirBlock interfaces.IDirectoryBlock) *DirBlockInfo {
	dbic := new(DirBlockInfo)
	dbic.DBHash = dirBlock.GetHash()
	dbic.DBHeight = dirBlock.GetDatabaseHeight()
	dbic.DBMerkleRoot = dirBlock.GetKeyMR()
	dbic.Timestamp = int64(dirBlock.GetHeader().GetTimestamp()) // * 60 ???
	dbic.BTCTxHash = primitives.NewZeroHash()
	dbic.BTCBlockHash = primitives.NewZeroHash()
	dbic.BTCConfirmed = false
	return dbic
}
Example #3
0
// Validate this directory block given the next Directory Block.  Need to check the
// signatures as being from the authority set, and valid. Also check that this DBState holds
// a previous KeyMR that matches the previous DBState KeyMR.
//
// Return a -1 on failure.
//
func (d *DBState) ValidNext(state interfaces.IState, dirblk interfaces.IDirectoryBlock) int {
	dbheight := dirblk.GetHeader().GetDBHeight()
	if dbheight == 0 {
		// The genesis block is valid by definition.
		return 1
	}
	if d == nil || !d.Saved {
		// Must be out of order.  Can't make the call if valid or not yet.
		return 0
	}
	// Get the keymr of the Previous DBState
	pkeymr := d.DirectoryBlock.GetKeyMR()
	// Get the Previous KeyMR pointer in the possible new Directory Block
	prevkeymr := dirblk.GetHeader().GetPrevKeyMR()
	if !pkeymr.IsSameAs(prevkeymr) {
		// If not the same, this is a bad new Directory Block
		return -1
	}
	return 1
}
Example #4
0
func NewDirectoryBlock(prev interfaces.IDirectoryBlock) interfaces.IDirectoryBlock {
	newdb := new(DirectoryBlock)

	newdb.Header = new(DBlockHeader)
	newdb.Header.SetVersion(constants.VERSION_0)

	if prev != nil {
		newdb.GetHeader().SetPrevFullHash(prev.GetFullHash())
		newdb.GetHeader().SetPrevKeyMR(prev.GetKeyMR())
		newdb.GetHeader().SetDBHeight(prev.GetHeader().GetDBHeight() + 1)
	} else {
		newdb.Header.SetPrevFullHash(primitives.NewZeroHash())
		newdb.Header.SetPrevKeyMR(primitives.NewZeroHash())
		newdb.GetHeader().SetDBHeight(0)
	}

	newdb.SetDBEntries(make([]interfaces.IDBEntry, 0))

	newdb.AddEntry(primitives.NewHash(constants.ADMIN_CHAINID), primitives.NewZeroHash())
	newdb.AddEntry(primitives.NewHash(constants.EC_CHAINID), primitives.NewZeroHash())
	newdb.AddEntry(primitives.NewHash(constants.FACTOID_CHAINID), primitives.NewZeroHash())

	return newdb
}
Example #5
0
func CheckBlockPairIntegrity(block interfaces.IDirectoryBlock, prev interfaces.IDirectoryBlock) error {
	if block == nil {
		return fmt.Errorf("No block specified")
	}

	if prev == nil {
		if block.GetHeader().GetPrevKeyMR().IsZero() == false {
			return fmt.Errorf("Invalid PrevKeyMR")
		}
		if block.GetHeader().GetPrevFullHash().IsZero() == false {
			return fmt.Errorf("Invalid PrevFullHash")
		}
		if block.GetHeader().GetDBHeight() != 0 {
			return fmt.Errorf("Invalid DBHeight")
		}
	} else {
		if block.GetHeader().GetPrevKeyMR().IsSameAs(prev.GetKeyMR()) == false {
			return fmt.Errorf("Invalid PrevKeyMR")
		}
		if block.GetHeader().GetPrevFullHash().IsSameAs(prev.GetFullHash()) == false {
			return fmt.Errorf("Invalid PrevFullHash")
		}
		if block.GetHeader().GetDBHeight() != (prev.GetHeader().GetDBHeight() + 1) {
			return fmt.Errorf("Invalid DBHeight")
		}
	}

	return nil
}
Example #6
0
// Control Panel shows the last 100 entry and factoid transactions. This will look into the past if we do not
// currently have 100 of each transaction type. A checkpoint is set each time we check a new height, so we will
// not check a directory block in the past twice.
func getPastEntries(last interfaces.IDirectoryBlock, eNeeded int, fNeeded int) {
	height := last.GetHeader().GetDBHeight()

	next := last.GetHeader().GetPrevKeyMR()
	zero := primitives.NewZeroHash()

	newCheckpoint := height

	for height > RecentTransactions.LastHeightChecked && (eNeeded > 0 || fNeeded > 0) {
		if next.IsSameAs(zero) {
			break
		}
		dbase := StatePointer.GetAndLockDB()
		dblk, err := dbase.FetchDBlock(next)
		StatePointer.UnlockDB()
		if err != nil || dblk == nil {
			break
		}
		height = dblk.GetHeader().GetDBHeight()
		ents := dblk.GetDBEntries()
		if len(ents) > 3 && eNeeded > 0 {
			for _, eblock := range ents[3:] {
				dbase := StatePointer.GetAndLockDB()
				eblk, err := dbase.FetchEBlock(eblock.GetKeyMR())
				StatePointer.UnlockDB()
				if err != nil || eblk == nil {
					break
				}
				for _, hash := range eblk.GetEntryHashes() {
					if RecentTransactions.ContainsEntry(hash) {
						continue
					}
					e := getEntry(hash.String())
					if e != nil && eNeeded > 0 {
						eNeeded--
						RecentTransactions.Entries = append(RecentTransactions.Entries, *e)
						//RecentTransactions.Entries = append([]EntryHolder{*e}, RecentTransactions.Entries...)
					}
				}
			}
		}
		if fNeeded > 0 {
			fChain := primitives.NewHash(constants.FACTOID_CHAINID)
			for _, entry := range ents {
				if entry.GetChainID().IsSameAs(fChain) {
					dbase := StatePointer.GetAndLockDB()
					fblk, err := dbase.FetchFBlock(entry.GetKeyMR())
					StatePointer.UnlockDB()
					if err != nil || fblk == nil {
						break
					}
					transList := fblk.GetTransactions()
					for _, trans := range transList {
						if RecentTransactions.ContainsTrans(trans.GetSigHash()) {
							continue
						}
						if trans != nil {
							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)
							fNeeded--
							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})
						}
					}
				}
			}
		}
		next = dblk.GetHeader().GetPrevKeyMR()
	}

	DisplayStateMutex.Lock()
	if newCheckpoint < DisplayState.CurrentEBDBHeight && newCheckpoint > RecentTransactions.LastHeightChecked {
		RecentTransactions.LastHeightChecked = newCheckpoint
	}
	DisplayStateMutex.Unlock()
}