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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }