func HandleFactoidBalance(ctx *web.Context, address string) { type x struct { Response string Success bool } ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := AddressRequest{Address: address} req := primitives.NewJSON2Request("factoid-balance", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } r := new(x) r.Response = fmt.Sprint(jsonResp.Result.(*FactoidBalanceResponse).Balance) r.Success = true returnMsg(ctx, r, true) }
func HandleProperties(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } req := primitives.NewJSON2Request("properties", 1, nil) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } type x struct { Protocol_Version string Factomd_Version string } d := new(x) d.Factomd_Version = jsonResp.Result.(*PropertiesResponse).FactomdVersion d.Protocol_Version = "0.0.0.0" // meaningless after v1 returnMsg(ctx, d, true) }
func HandleEntry(ctx *web.Context, hashkey string) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := HashRequest{Hash: hashkey} req := primitives.NewJSON2Request("entry", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } d := new(EntryStruct) d.ChainID = jsonResp.Result.(*EntryResponse).ChainID d.Content = jsonResp.Result.(*EntryResponse).Content d.ExtIDs = jsonResp.Result.(*EntryResponse).ExtIDs returnMsg(ctx, d, true) }
func HandleChainHead(ctx *web.Context, chainid string) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := ChainIDRequest{ChainID: chainid} req := primitives.NewJSON2Request("chain-head", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } // restatement of chead from structs file // v1 doesn't like the lcase in v2' type CHead struct { ChainHead string } d := new(CHead) d.ChainHead = jsonResp.Result.(*ChainHeadResponse).ChainHead returnMsg(ctx, d, true) }
func HandleDirectoryBlockHeight(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } req := primitives.NewJSON2Request("heights", 1, nil) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } resp := "{\"Height\":0}" // get the HeightsResponse from the return object p, err := json.Marshal(jsonResp.Result) if err != nil { returnV1Msg(ctx, resp, true) } h := new(HeightsResponse) if err := json.Unmarshal(p, h); err != nil { returnV1Msg(ctx, resp, true) } // return just the DirectoryBlockHeight from the HeightsResponse resp = fmt.Sprintf("{\"Height\":%d}", h.DirectoryBlockHeight) returnV1Msg(ctx, resp, true) }
func HandleDirectoryBlockHead(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } req := primitives.NewJSON2Request("directory-block-head", 1, nil) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } tmp, err := json.Marshal(jsonResp.Result) resp := string(tmp) if err != nil { resp = "{\"KeyMR\",0}" returnV1Msg(ctx, resp, true) } resp = strings.Replace(resp, "keymr", "KeyMR", -1) returnV1Msg(ctx, resp, true) }
func HandleHeights(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) req := primitives.NewJSON2Request("heights", 1, nil) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } returnMsg(ctx, jsonResp.Result, true) }
func HandleGetReceipt(ctx *web.Context, hashkey string) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := HashRequest{Hash: hashkey} req := primitives.NewJSON2Request("receipt", 1, param) jsonResp, jsonError := HandleV2Request(state, req) returnV1(ctx, jsonResp, jsonError) }
func HandleDirectoryBlock(ctx *web.Context, hashkey string) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := KeyMRRequest{KeyMR: hashkey} req := primitives.NewJSON2Request("directory-block", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } type DBlock struct { Header struct { PrevBlockKeyMR string SequenceNumber int64 Timestamp int64 } EntryBlockList []EBlockAddr } d := new(DBlock) d.Header.PrevBlockKeyMR = jsonResp.Result.(*DirectoryBlockResponse).Header.PrevBlockKeyMR d.Header.SequenceNumber = jsonResp.Result.(*DirectoryBlockResponse).Header.SequenceNumber d.Header.Timestamp = jsonResp.Result.(*DirectoryBlockResponse).Header.Timestamp d.EntryBlockList = jsonResp.Result.(*DirectoryBlockResponse).EntryBlockList // conflict if I use local structs. using a string replace on the structs that would be pointer handled (*DirectoryBlockResponse) bResp, err := json.Marshal(d) if err != nil { returnMsg(ctx, d, true) } resp := string(bResp) resp = strings.Replace(resp, "{\"chainid\"", "{\"ChainID\"", -1) resp = strings.Replace(resp, ",\"keymr\":", ",\"KeyMR\":", -1) returnV1Msg(ctx, resp, true) }
func HandleFactoidSubmit(ctx *web.Context) { type x struct { Response string Success bool } type transaction struct{ Transaction string } t := new(transaction) ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } var p []byte var err error if p, err = ioutil.ReadAll(ctx.Request.Body); err != nil { handleV1Error(ctx, NewInvalidParamsError()) return } else { if err := json.Unmarshal(p, t); err != nil { handleV1Error(ctx, NewInvalidParamsError()) return } } param := TransactionRequest{Transaction: t.Transaction} req := primitives.NewJSON2Request("factoid-submit", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } r := new(x) r.Response = jsonResp.Result.(*FactoidSubmitResponse).Message r.Success = true returnMsg(ctx, r, true) }
func TestHandleV2CommitChain(t *testing.T) { msg := new(MessageRequest) // Can replace with any Chain message msg.Message = "00015507b2f70bd0165d9fa19a28cfaafb6bc82f538955a98c7b7e60d79fbf92655c1bff1c76466cb3bc3f3cc68d8b2c111f4f24c88d9c031b4124395c940e5e2c5ea496e8aaa2f5c956749fc3eba4acc60fd485fb100e601070a44fcce54ff358d606698547340b3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da2946c901273e616bdbb166c535b26d0d446bc69b22c887c534297c7d01b2ac120237086112b5ef34fc6474e5e941d60aa054b465d4d770d7f850169170ef39150b" req := primitives.NewJSON2Request("commit-chain", 0, msg) resp, err := v2Request(req) if err != nil { t.Errorf("%v", err) } respObj := new(CommitChainResponse) if err := MapToObject(resp.Result, respObj); err != nil { t.Error(err) } txID := "76e123d133a841fe3e08c5e3f3d392f8431f2d7668890c03f003f541efa8fc61" if respObj.TxID != txID { t.Errorf("Error: TxID returned during Commit Chain is incorrect - %v vs %v", respObj.TxID, txID) } }
func TestHandleV2CommitEntry(t *testing.T) { msg := new(EntryRequest) // Can replace with any Entry message msg.Entry = "00015507C1024BF5C956749FC3EBA4ACC60FD485FB100E601070A44FCCE54FF358D60669854734013B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29F4CBD953E6EBE684D693FDCA270CE231783E8ECC62D630F983CD59E559C6253F84D1F54C8E8D8665D493F7B4A4C1864751E3CDEC885A64C2144E0938BF648A00" req := primitives.NewJSON2Request("commit-entry", 0, msg) resp, err := v2Request(req) if err != nil { t.Errorf("%v", err) } respObj := new(CommitEntryResponse) if err := MapToObject(resp.Result, respObj); err != nil { t.Error(err) } txID := "8b751bc182766e6187d39b1eca538d9ece0b8ff662e408cd4e45f89359f8c7e7" if respObj.TxID != txID { t.Errorf("Error: TxID returned during Commit Entry is incorrect - %v vs %v", respObj.TxID, txID) } }
func HandleEntryBlock(ctx *web.Context, hashkey string) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } param := KeyMRRequest{KeyMR: hashkey} req := primitives.NewJSON2Request("entry-block", 1, param) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } type EBlock struct { Header struct { BlockSequenceNumber int64 ChainID string PrevKeyMR string Timestamp int64 DBHeight int64 } EntryList []EntryAddr } d := new(EBlock) d.Header.BlockSequenceNumber = jsonResp.Result.(*EntryBlockResponse).Header.BlockSequenceNumber d.Header.ChainID = jsonResp.Result.(*EntryBlockResponse).Header.ChainID d.Header.PrevKeyMR = jsonResp.Result.(*EntryBlockResponse).Header.PrevKeyMR d.Header.Timestamp = jsonResp.Result.(*EntryBlockResponse).Header.Timestamp d.Header.DBHeight = jsonResp.Result.(*EntryBlockResponse).Header.DBHeight d.EntryList = jsonResp.Result.(*EntryBlockResponse).EntryList returnMsg(ctx, d, true) }
func HandleCommitEntry(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } type commitentry struct { CommitEntryMsg string } c := new(commitentry) if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil { handleV1Error(ctx, NewInvalidParamsError()) return } else { if err := json.Unmarshal(p, c); err != nil { handleV1Error(ctx, NewInvalidParamsError()) return } } param := EntryRequest{Entry: c.CommitEntryMsg} req := primitives.NewJSON2Request("commit-entry", 1, param) _, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } //log.Print( jsonResp.Result.(*RevealEntryResponse).Message) // this is the blank '200 ok' that is returned for V1 returnV1Msg(ctx, "", true) }
func HandleGetFee(ctx *web.Context) { ServersMutex.Lock() defer ServersMutex.Unlock() state := ctx.Server.Env["state"].(interfaces.IState) if !checkHttpPasswordOkV1(state, ctx) { return } req := primitives.NewJSON2Request("entry-credit-rate", 1, nil) jsonResp, jsonError := HandleV2Request(state, req) if jsonError != nil { returnV1(ctx, nil, jsonError) return } type x struct{ Fee int64 } d := new(x) d.Fee = int64(jsonResp.Result.(*EntryCreditRateResponse).Rate) returnMsg(ctx, d, true) }
func TestHandleV2GetRaw(t *testing.T) { type RawData struct { Hash1 string Hash2 string Raw string } toTest := []RawData{} var err error blockSet := testHelper.CreateTestBlockSet(nil) aBlock := blockSet.ABlock raw := RawData{} raw.Hash1 = aBlock.DatabasePrimaryIndex().String() raw.Hash2 = aBlock.DatabaseSecondaryIndex().String() hex, err := aBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //1 eBlock := blockSet.EBlock raw = RawData{} raw.Hash1 = eBlock.DatabasePrimaryIndex().String() raw.Hash2 = eBlock.DatabaseSecondaryIndex().String() hex, err = eBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //2 ecBlock := blockSet.ECBlock raw = RawData{} raw.Hash1 = ecBlock.(interfaces.DatabaseBatchable).DatabasePrimaryIndex().String() raw.Hash2 = ecBlock.(interfaces.DatabaseBatchable).DatabaseSecondaryIndex().String() hex, err = ecBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //3 fBlock := blockSet.FBlock raw = RawData{} raw.Hash1 = fBlock.(interfaces.DatabaseBatchable).DatabasePrimaryIndex().String() raw.Hash2 = fBlock.(interfaces.DatabaseBatchable).DatabaseSecondaryIndex().String() hex, err = fBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //4 dBlock := blockSet.DBlock raw = RawData{} raw.Hash1 = dBlock.DatabasePrimaryIndex().String() raw.Hash2 = dBlock.DatabaseSecondaryIndex().String() hex, err = dBlock.MarshalBinary() if err != nil { panic(err) } raw.Raw = primitives.EncodeBinary(hex) toTest = append(toTest, raw) //5 //initializing server state := testHelper.CreateAndPopulateTestState() Start(state) for i, v := range toTest { data := new(HashRequest) data.Hash = v.Hash1 req := primitives.NewJSON2Request("raw-data", 1, data) resp, err := v2Request(req) if err != nil { t.Errorf("%v", err) } if strings.Contains(resp.String(), v.Raw) == false { t.Errorf("Looking for %v", v.Hash1) t.Errorf("GetRaw %v/%v from Hash1 failed - %v", i, len(toTest), resp.String()) } data.Hash = v.Hash2 req = primitives.NewJSON2Request("raw-data", 1, data) resp, err = v2Request(req) if err != nil { t.Errorf("%v", err) } if strings.Contains(resp.String(), v.Raw) == false { t.Errorf("Looking for %v", v.Hash1) t.Errorf("GetRaw %v/%v from Hash2 failed - %v", i, len(toTest), resp.String()) } } }