Exemple #1
0
// Validate the message, given the state.  Three possible results:
//  < 0 -- Message is invalid.  Discard
//  0   -- Cannot tell if message is Valid
//  1   -- Message is valid
// Also return the matching commit, if 1 (Don't put it back into the Commit List)
func (m *RevealEntryMsg) ValidateRTN(state interfaces.IState) (interfaces.IMsg, int) {
	commit := state.NextCommit(m.Entry.GetHash())

	if commit == nil {
		return nil, 0
	}
	//
	// Make sure one of the two proper commits got us here.
	var okChain, okEntry bool
	m.commitChain, okChain = commit.(*CommitChainMsg)
	m.commitEntry, okEntry = commit.(*CommitEntryMsg)
	if !okChain && !okEntry { // Discard any invalid entries in the map.  Should never happen.
		fmt.Println("dddd Bad EB Commit", state.GetFactomNodeName())
		return m.ValidateRTN(state)
	}

	// Now make sure the proper amount of credits were paid to record the entry.
	// The chain must exist
	if okEntry {
		m.IsEntry = true
		ECs := int(m.commitEntry.CommitEntry.Credits)
		if m.Entry.KSize() > ECs {
			fmt.Println("dddd EB Commit is short", state.GetFactomNodeName())
			return m.ValidateRTN(state) // Discard commits that are not funded properly.
		}

		// Make sure we have a chain.  If we don't, then bad things happen.
		db := state.GetAndLockDB()
		dbheight := state.GetLeaderHeight()
		eb := state.GetNewEBlocks(dbheight, m.Entry.GetChainID())
		eb_db := state.GetNewEBlocks(dbheight-1, m.Entry.GetChainID())
		if eb_db == nil {
			eb_db, _ = db.FetchEBlockHead(m.Entry.GetChainID())
		}

		if eb_db == nil && eb == nil {
			// If we don't have a chain, put the commit back.  Don't want to lose it.
			state.PutCommit(m.Entry.GetHash(), commit)
			return nil, 0
		}
	} else {
		m.IsEntry = false
		ECs := int(m.commitChain.CommitChain.Credits)
		if m.Entry.KSize()+10 > ECs { // Discard commits that are not funded properly
			return m.ValidateRTN(state)
		}
	}

	return commit, 1
}
Exemple #2
0
func HandleV2ChainHead(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	chainid := new(ChainIDRequest)
	err := MapToObject(params, chainid)
	if err != nil {
		return nil, NewInvalidParamsError()
	}
	h, err := primitives.HexToHash(chainid.ChainID)
	if err != nil {
		return nil, NewInvalidHashError()
	}

	dbase := state.GetAndLockDB()
	defer state.UnlockDB()

	c := new(ChainHeadResponse)

	// get the pending chain head from the current or previous process list in
	// the state
	lh := state.GetLeaderHeight()
	pend1 := state.GetNewEBlocks(lh, h)
	pend2 := state.GetNewEBlocks(lh-1, h)
	if pend1 != nil || pend2 != nil {
		c.ChainInProcessList = true
	}

	// get the chain head from the database
	mr, err := dbase.FetchHeadIndexByChainID(h)
	if err != nil {
		return nil, NewInvalidHashError()
	}
	if mr == nil {
		if c.ChainInProcessList == false {
			return nil, NewMissingChainHeadError()
		}
	} else {
		c.ChainHead = mr.String()
	}

	return c, nil
}