Exemplo n.º 1
0
func HandleV2RawData(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	hashkey := new(HashRequest)
	err := MapToObject(params, hashkey)
	if err != nil {
		panic(reflect.TypeOf(params))
		return nil, NewInvalidParamsError()
	}

	h, err := primitives.HexToHash(hashkey.Hash)
	if err != nil {
		return nil, NewInvalidHashError()
	}

	var block interfaces.BinaryMarshallable
	var b []byte

	if block, _ = state.FetchECTransactionByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	} else if block, _ = state.FetchFactoidTransactionByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	} else if block, _ = state.FetchEntryByHash(h); block != nil {
		b, _ = block.MarshalBinary()
	}

	if b == nil {
		dbase := state.GetAndLockDB()
		defer state.UnlockDB()

		// try to find the block data in db and return the first one found
		if block, _ = dbase.FetchFBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchDBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchABlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchEBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchECBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchFBlock(h); block != nil {
			b, _ = block.MarshalBinary()
		} else if block, _ = dbase.FetchEntry(h); block != nil {
			b, _ = block.MarshalBinary()
		} else {
			return nil, NewEntryNotFoundError()
		}
	}

	d := new(RawDataResponse)
	d.Data = hex.EncodeToString(b)
	return d, nil
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
func (m *MissingEntryBlocks) FollowerExecute(state interfaces.IState) {
	if len(state.NetworkOutMsgQueue()) > 1000 {
		return
	}
	start := m.DBHeightStart
	end := m.DBHeightEnd
	if end-start > 20 {
		end = start + 20
	}
	db := state.GetAndLockDB()
	defer state.UnlockDB()

	resp := NewEntryBlockResponse(state).(*EntryBlockResponse)

	for i := start; i <= end; i++ {
		dblk, err := db.FetchDBlockByHeight(i)
		if err != nil {
			return
		}
		if dblk == nil {
			return
		}
		for _, v := range dblk.GetDBEntries() {
			if v.GetChainID().IsMinuteMarker() == true {
				continue
			}
			eBlock, err := db.FetchEBlock(v.GetKeyMR())
			if err != nil {
				return
			}
			resp.EBlocks = append(resp.EBlocks, eBlock)
			for _, v := range eBlock.GetBody().GetEBEntries() {
				entry, err := db.FetchEntry(v)
				if err != nil {
					return
				}
				resp.Entries = append(resp.Entries, entry)
			}
		}
	}

	resp.SetOrigin(m.GetOrigin())
	resp.SetNetworkOrigin(m.GetNetworkOrigin())
	resp.SendOut(state, resp)
	state.IncDBStateAnswerCnt()

	return
}
Exemplo n.º 4
0
func (m *EntryBlockResponse) FollowerExecute(state interfaces.IState) {
	if len(state.NetworkOutMsgQueue()) > 1000 {
		return
	}

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

	for _, v := range m.EBlocks {
		db.ProcessEBlockBatchWithoutHead(v, true)
	}
	for _, v := range m.Entries {
		db.InsertEntry(v)
	}

	return
}
Exemplo n.º 5
0
func HandleV2DirectoryBlock(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	keymr := new(KeyMRRequest)
	err := MapToObject(params, keymr)
	if err != nil {
		return nil, NewInvalidParamsError()
	}

	h, err := primitives.HexToHash(keymr.KeyMR)
	if err != nil {
		return nil, NewInvalidHashError()
	}

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

	block, err := dbase.FetchDBlock(h)
	if err != nil {
		return nil, NewInvalidHashError()
	}
	if block == nil {
		block, err = dbase.FetchDBlock(h)
		if err != nil {
			return nil, NewInvalidHashError()
		}
		if block == nil {
			return nil, NewBlockNotFoundError()
		}
	}

	d := new(DirectoryBlockResponse)
	d.Header.PrevBlockKeyMR = block.GetHeader().GetPrevKeyMR().String()
	d.Header.SequenceNumber = int64(block.GetHeader().GetDBHeight())
	d.Header.Timestamp = block.GetHeader().GetTimestamp().GetTimeSeconds()
	for _, v := range block.GetDBEntries() {
		l := new(EBlockAddr)
		l.ChainID = v.GetChainID().String()
		l.KeyMR = v.GetKeyMR().String()
		d.EntryBlockList = append(d.EntryBlockList, *l)
	}

	return d, nil
}
Exemplo n.º 6
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
}
Exemplo n.º 7
0
func HandleV2Entry(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	hashkey := new(HashRequest)
	err := MapToObject(params, hashkey)
	if err != nil {
		return nil, NewInvalidParamsError()
	}
	e := new(EntryResponse)

	h, err := primitives.HexToHash(hashkey.Hash)
	if err != nil {
		return nil, NewInvalidHashError()
	}

	entry, err := state.FetchEntryByHash(h)
	if err != nil {
		return nil, NewInternalError()
	}
	if entry == nil {
		dbase := state.GetAndLockDB()
		defer state.UnlockDB()

		entry, err = dbase.FetchEntry(h)
		if err != nil {
			return nil, NewInvalidHashError()
		}
		if entry == nil {
			return nil, NewEntryNotFoundError()
		}
	}

	e.ChainID = entry.GetChainIDHash().String()
	e.Content = hex.EncodeToString(entry.GetContent())
	for _, v := range entry.ExternalIDs() {
		e.ExtIDs = append(e.ExtIDs, hex.EncodeToString(v))
	}

	return e, nil
}
Exemplo n.º 8
0
func HandleV2Receipt(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	hashkey := new(HashRequest)
	err := MapToObject(params, hashkey)
	if err != nil {
		return nil, NewInvalidParamsError()
	}

	h, err := primitives.HexToHash(hashkey.Hash)
	if err != nil {
		return nil, NewInvalidHashError()
	}

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

	receipt, err := receipts.CreateFullReceipt(dbase, h)
	if err != nil {
		return nil, NewReceiptError()
	}
	resp := new(ReceiptResponse)
	resp.Receipt = receipt

	return resp, nil
}
Exemplo n.º 9
0
func HandleV2GetTranasction(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	hashkey := new(HashRequest)
	err := MapToObject(params, hashkey)
	if err != nil {
		return nil, NewInvalidParamsError()
	}
	h, err := primitives.HexToHash(hashkey.Hash)
	if err != nil {
		return nil, NewInvalidHashError()
	}

	fTx, err := state.FetchFactoidTransactionByHash(h)
	if err != nil {
		if err.Error() != "Block not found, should not happen" {
			return nil, NewInternalError()
		}
	}

	ecTx, err := state.FetchECTransactionByHash(h)
	if err != nil {
		if err.Error() != "Block not found, should not happen" {
			return nil, NewInternalError()
		}
	}

	e, err := state.FetchEntryByHash(h)
	if err != nil {
		return nil, NewInternalError()
	}

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

	if fTx == nil {
		fTx, err = dbase.FetchFactoidTransaction(h)
		if err != nil {
			if err.Error() != "Block not found, should not happen" {
				return nil, NewInternalError()
			}
		}
	}

	if ecTx == nil {
		ecTx, err = dbase.FetchECTransaction(h)
		if err != nil {
			if err.Error() != "Block not found, should not happen" {
				return nil, NewInternalError()
			}
		}
	}

	if e == nil {
		e, err = dbase.FetchEntry(h)
		if err != nil {
			return nil, NewInternalError()
		}
	}

	blockHash, err := dbase.FetchIncludedIn(h)
	if err != nil {
		return nil, NewInternalError()
	}

	answer := new(TransactionResponse)
	answer.ECTranasction = ecTx
	answer.FactoidTransaction = fTx
	answer.Entry = e

	answer.IncludedInTransactionBlock = blockHash.String()

	blockHash, err = dbase.FetchIncludedIn(blockHash)
	if err != nil {
		return nil, NewInternalError()
	}

	answer.IncludedInDirectoryBlock = blockHash.String()

	dBlock, err := dbase.FetchDBlock(blockHash)
	if err != nil {
		return nil, NewInternalError()
	}
	answer.IncludedInDirectoryBlockHeight = int64(dBlock.GetDatabaseHeight())

	return answer, nil
}
Exemplo n.º 10
0
func HandleV2EntryBlock(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	keymr := new(KeyMRRequest)
	err := MapToObject(params, keymr)
	if err != nil {
		return nil, NewInvalidParamsError()
	}
	e := new(EntryBlockResponse)

	h, err := primitives.HexToHash(keymr.KeyMR)
	if err != nil {
		return nil, NewInvalidHashError()
	}

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

	block, err := dbase.FetchEBlock(h)
	if err != nil {
		return nil, NewInvalidHashError()
	}
	if block == nil {
		block, err = dbase.FetchEBlock(h)
		if err != nil {
			return nil, NewInvalidHashError()
		}
		if block == nil {
			return nil, NewBlockNotFoundError()
		}
	}

	e.Header.BlockSequenceNumber = int64(block.GetHeader().GetEBSequence())
	e.Header.ChainID = block.GetHeader().GetChainID().String()
	e.Header.PrevKeyMR = block.GetHeader().GetPrevKeyMR().String()
	e.Header.DBHeight = int64(block.GetHeader().GetDBHeight())

	if dblock, err := dbase.FetchDBlockByHeight(block.GetHeader().GetDBHeight()); err == nil {
		e.Header.Timestamp = dblock.GetHeader().GetTimestamp().GetTimeSeconds()
	}

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

	estack := make([]EntryAddr, 0)
	for _, v := range block.GetBody().GetEBEntries() {
		if n, exist := mins[v.String()]; exist {
			// the entry is a minute marker. add time to all of the
			// previous entries for the minute
			t := int64(e.Header.Timestamp + 60*int64(n))
			for _, w := range estack {
				w.Timestamp = t
				e.EntryList = append(e.EntryList, w)
			}
			estack = make([]EntryAddr, 0)
		} else {
			l := new(EntryAddr)
			l.EntryHash = v.String()
			estack = append(estack, *l)
		}
	}

	return e, nil
}