Browse Source

Add getRoot method

feature_chain_module
p4u 5 years ago
parent
commit
6fd9aea6c8
2 changed files with 15 additions and 4 deletions
  1. +11
    -4
      service/processHttp.go
  2. +4
    -0
      tree/tree.go

+ 11
- 4
service/processHttp.go

@ -79,6 +79,10 @@ func claimHandler(w http.ResponseWriter, req *http.Request, op string) {
resp.Response, err = T.GenProof([]byte(c.ClaimData)) resp.Response, err = T.GenProof([]byte(c.ClaimData))
} }
if op == "root" {
resp.Response = T.GetRoot()
}
if op == "check" { if op == "check" {
if len(c.ProofData) < 1 { if len(c.ProofData) < 1 {
resp.Error = true resp.Error = true
@ -109,18 +113,21 @@ func claimHandler(w http.ResponseWriter, req *http.Request, op string) {
func Listen(port int, proto string) { func Listen(port int, proto string) {
srv := &http.Server{ srv := &http.Server{
Addr: fmt.Sprintf(":%d", port), Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: 4 * time.Second,
ReadHeaderTimeout: 4 * time.Second,
ReadTimeout: 4 * time.Second, ReadTimeout: 4 * time.Second,
WriteTimeout: 4 * time.Second, WriteTimeout: 4 * time.Second,
IdleTimeout: 3 * time.Second, IdleTimeout: 3 * time.Second,
} }
http.HandleFunc("/addClaim", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/addClaim", func(w http.ResponseWriter, r *http.Request) {
claimHandler(w, r, "add")})
claimHandler(w, r, "add")})
http.HandleFunc("/genProof", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/genProof", func(w http.ResponseWriter, r *http.Request) {
claimHandler(w, r, "gen")})
claimHandler(w, r, "gen")})
http.HandleFunc("/checkProof", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/checkProof", func(w http.ResponseWriter, r *http.Request) {
claimHandler(w, r, "check")})
claimHandler(w, r, "check")})
http.HandleFunc("/getRoot", func(w http.ResponseWriter, r *http.Request) {
claimHandler(w, r, "root")})
if proto == "https" { if proto == "https" {
log.Print("Starting server in https mode") log.Print("Starting server in https mode")

+ 4
- 0
tree/tree.go

@ -60,3 +60,7 @@ func (t *Tree) CheckProof(data []byte, mpHex string) (bool, error) {
claim := mkcore.NewGenericClaim(t.Namespace, "default", data, nil) claim := mkcore.NewGenericClaim(t.Namespace, "default", data, nil)
return merkletree.CheckProof(t.Tree.Root(), mp, claim.Hi(), claim.Ht(), t.Tree.NumLevels()), nil return merkletree.CheckProof(t.Tree.Root(), mp, claim.Hi(), claim.Ht(), t.Tree.NumLevels()), nil
} }
func (t *Tree) GetRoot() (string) {
return t.Tree.Root().Hex()
}

Loading…
Cancel
Save