github.com/bartle-stripe/trillian@v1.2.1/storage/tools/log_client/main.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // The log_client binary retrieves leaves from a log. 16 package main 17 18 import ( 19 "context" 20 "flag" 21 "fmt" 22 "time" 23 24 "github.com/golang/glog" 25 "github.com/google/trillian" 26 "google.golang.org/grpc" 27 ) 28 29 var ( 30 treeIDFlag = flag.Int64("treeid", 3, "The tree id to use") 31 serverPortFlag = flag.Int("port", 8090, "Log server port (must be on localhost)") 32 startLeafFlag = flag.Int64("start_leaf", 0, "The first leaf index to fetch") 33 numLeavesFlag = flag.Int64("num_leaves", 1, "The number of leaves to fetch") 34 ) 35 36 func buildGetLeavesByIndexRequest(logID int64, startLeaf, numLeaves int64) *trillian.GetLeavesByIndexRequest { 37 if startLeaf < 0 || numLeaves <= 0 { 38 panic("Start leaf index and num_leaves must be >= 0") 39 } 40 41 var leafIndices []int64 42 43 for l := int64(0); l < numLeaves; l++ { 44 leafIndices = append(leafIndices, l+startLeaf) 45 } 46 47 return &trillian.GetLeavesByIndexRequest{LogId: logID, LeafIndex: leafIndices} 48 } 49 50 // TODO: Move this code out to a better place when we tidy up the initial test main stuff 51 // It's just a basic skeleton at the moment. 52 func main() { 53 flag.Parse() 54 defer glog.Flush() 55 56 ctx := context.Background() 57 58 // TODO: Other options apart from insecure connections 59 port := *serverPortFlag 60 dialCtx, cancel := context.WithTimeout(ctx, time.Second*5) 61 defer cancel() 62 conn, err := grpc.DialContext(dialCtx, fmt.Sprintf("localhost:%d", port), grpc.WithInsecure()) 63 64 if err != nil { 65 panic(err) 66 } 67 68 defer conn.Close() 69 70 client := trillian.NewTrillianLogClient(conn) 71 72 req := buildGetLeavesByIndexRequest(*treeIDFlag, *startLeafFlag, *numLeavesFlag) 73 getLeafByIndexResponse, err := client.GetLeavesByIndex(ctx, req) 74 75 if err != nil { 76 fmt.Printf("Got error in call: %v", err) 77 } else { 78 fmt.Printf("Got server response: %v", getLeafByIndexResponse) 79 } 80 }