github.com/fiagdao/tendermint@v0.32.11-0.20220824195748-2087fcc480c1/lite/proxy/wrapper.go (about) 1 package proxy 2 3 import ( 4 "context" 5 6 "github.com/tendermint/tendermint/crypto/merkle" 7 "github.com/tendermint/tendermint/libs/bytes" 8 "github.com/tendermint/tendermint/lite" 9 rpcclient "github.com/tendermint/tendermint/rpc/client" 10 ctypes "github.com/tendermint/tendermint/rpc/core/types" 11 rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" 12 ) 13 14 var _ rpcclient.Client = Wrapper{} 15 16 // Wrapper wraps a rpcclient with a Verifier and double-checks any input that is 17 // provable before passing it along. Allows you to make any rpcclient fully secure. 18 type Wrapper struct { 19 rpcclient.Client 20 cert *lite.DynamicVerifier 21 prt *merkle.ProofRuntime 22 } 23 24 // SecureClient uses a given Verifier to wrap an connection to an untrusted 25 // host and return a cryptographically secure rpc client. 26 // 27 // If it is wrapping an HTTP rpcclient, it will also wrap the websocket interface 28 func SecureClient(c rpcclient.Client, cert *lite.DynamicVerifier) Wrapper { 29 prt := defaultProofRuntime() 30 wrap := Wrapper{c, cert, prt} 31 // TODO: no longer possible as no more such interface exposed.... 32 // if we wrap http client, then we can swap out the event switch to filter 33 // if hc, ok := c.(*rpcclient.HTTP); ok { 34 // evt := hc.WSEvents.EventSwitch 35 // hc.WSEvents.EventSwitch = WrappedSwitch{evt, wrap} 36 // } 37 return wrap 38 } 39 40 // ABCIQueryWithOptions exposes all options for the ABCI query and verifies the returned proof 41 func (w Wrapper) ABCIQueryWithOptions(path string, data bytes.HexBytes, 42 opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) { 43 44 res, err := GetWithProofOptions(w.prt, path, data, opts, w.Client, w.cert) 45 return res, err 46 } 47 48 // ABCIQuery uses default options for the ABCI query and verifies the returned proof 49 func (w Wrapper) ABCIQuery(path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) { 50 return w.ABCIQueryWithOptions(path, data, rpcclient.DefaultABCIQueryOptions) 51 } 52 53 // Tx queries for a given tx and verifies the proof if it was requested 54 func (w Wrapper) Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) { 55 res, err := w.Client.Tx(hash, prove) 56 if !prove || err != nil { 57 return res, err 58 } 59 h := res.Height 60 sh, err := GetCertifiedCommit(h, w.Client, w.cert) 61 if err != nil { 62 return res, err 63 } 64 err = res.Proof.Validate(sh.DataHash) 65 return res, err 66 } 67 68 // BlockchainInfo requests a list of headers and verifies them all... 69 // Rather expensive. 70 // 71 // TODO: optimize this if used for anything needing performance 72 func (w Wrapper) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { 73 r, err := w.Client.BlockchainInfo(minHeight, maxHeight) 74 if err != nil { 75 return nil, err 76 } 77 78 // go and verify every blockmeta in the result.... 79 for _, meta := range r.BlockMetas { 80 // get a checkpoint to verify from 81 res, err := w.Commit(&meta.Header.Height) 82 if err != nil { 83 return nil, err 84 } 85 sh := res.SignedHeader 86 err = ValidateBlockMeta(meta, sh) 87 if err != nil { 88 return nil, err 89 } 90 } 91 92 return r, nil 93 } 94 95 // Block returns an entire block and verifies all signatures 96 func (w Wrapper) Block(height *int64) (*ctypes.ResultBlock, error) { 97 resBlock, err := w.Client.Block(height) 98 if err != nil { 99 return nil, err 100 } 101 // get a checkpoint to verify from 102 resCommit, err := w.Commit(height) 103 if err != nil { 104 return nil, err 105 } 106 sh := resCommit.SignedHeader 107 108 err = ValidateBlock(resBlock.Block, sh) 109 if err != nil { 110 return nil, err 111 } 112 return resBlock, nil 113 } 114 115 // Commit downloads the Commit and certifies it with the lite. 116 // 117 // This is the foundation for all other verification in this module 118 func (w Wrapper) Commit(height *int64) (*ctypes.ResultCommit, error) { 119 if height == nil { 120 resStatus, err := w.Client.Status() 121 if err != nil { 122 return nil, err 123 } 124 // NOTE: If resStatus.CatchingUp, there is a race 125 // condition where the validator set for the next height 126 // isn't available until some time after the blockstore 127 // has height h on the remote node. This isn't an issue 128 // once the node has caught up, and a syncing node likely 129 // won't have this issue esp with the implementation we 130 // have here, but we may have to address this at some 131 // point. 132 height = new(int64) 133 *height = resStatus.SyncInfo.LatestBlockHeight 134 } 135 rpcclient.WaitForHeight(w.Client, *height, nil) 136 res, err := w.Client.Commit(height) 137 // if we got it, then verify it 138 if err == nil { 139 sh := res.SignedHeader 140 err = w.cert.Verify(sh) 141 } 142 return res, err 143 } 144 145 func (w Wrapper) RegisterOpDecoder(typ string, dec merkle.OpDecoder) { 146 w.prt.RegisterOpDecoder(typ, dec) 147 } 148 149 // SubscribeWS subscribes for events using the given query and remote address as 150 // a subscriber, but does not verify responses (UNSAFE)! 151 func (w Wrapper) SubscribeWS(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, error) { 152 out, err := w.Client.Subscribe(context.Background(), ctx.RemoteAddr(), query) 153 if err != nil { 154 return nil, err 155 } 156 157 go func() { 158 for { 159 select { 160 case resultEvent := <-out: 161 // XXX(melekes) We should have a switch here that performs a validation 162 // depending on the event's type. 163 ctx.WSConn.TryWriteRPCResponse( 164 rpctypes.NewRPCSuccessResponse( 165 ctx.WSConn.Codec(), 166 ctx.JSONReq.ID, 167 resultEvent, 168 )) 169 case <-w.Client.Quit(): 170 return 171 } 172 } 173 }() 174 175 return &ctypes.ResultSubscribe{}, nil 176 } 177 178 // UnsubscribeWS calls original client's Unsubscribe using remote address as a 179 // subscriber. 180 func (w Wrapper) UnsubscribeWS(ctx *rpctypes.Context, query string) (*ctypes.ResultUnsubscribe, error) { 181 err := w.Client.Unsubscribe(context.Background(), ctx.RemoteAddr(), query) 182 if err != nil { 183 return nil, err 184 } 185 return &ctypes.ResultUnsubscribe{}, nil 186 } 187 188 // UnsubscribeAllWS calls original client's UnsubscribeAll using remote address 189 // as a subscriber. 190 func (w Wrapper) UnsubscribeAllWS(ctx *rpctypes.Context) (*ctypes.ResultUnsubscribe, error) { 191 err := w.Client.UnsubscribeAll(context.Background(), ctx.RemoteAddr()) 192 if err != nil { 193 return nil, err 194 } 195 return &ctypes.ResultUnsubscribe{}, nil 196 } 197 198 // // WrappedSwitch creates a websocket connection that auto-verifies any info 199 // // coming through before passing it along. 200 // // 201 // // Since the verification takes 1-2 rpc calls, this is obviously only for 202 // // relatively low-throughput situations that can tolerate a bit extra latency 203 // type WrappedSwitch struct { 204 // types.EventSwitch 205 // client rpcclient.Client 206 // } 207 208 // // FireEvent verifies any block or header returned from the eventswitch 209 // func (s WrappedSwitch) FireEvent(event string, data events.EventData) { 210 // tm, ok := data.(types.TMEventData) 211 // if !ok { 212 // fmt.Printf("bad type %#v\n", data) 213 // return 214 // } 215 216 // // check to validate it if possible, and drop if not valid 217 // switch t := tm.(type) { 218 // case types.EventDataNewBlockHeader: 219 // err := verifyHeader(s.client, t.Header) 220 // if err != nil { 221 // fmt.Printf("Invalid header: %#v\n", err) 222 // return 223 // } 224 // case types.EventDataNewBlock: 225 // err := verifyBlock(s.client, t.Block) 226 // if err != nil { 227 // fmt.Printf("Invalid block: %#v\n", err) 228 // return 229 // } 230 // // TODO: can we verify tx as well? anything else 231 // } 232 233 // // looks good, we fire it 234 // s.EventSwitch.FireEvent(event, data) 235 // } 236 237 // func verifyHeader(c rpcclient.Client, head *types.Header) error { 238 // // get a checkpoint to verify from 239 // commit, err := c.Commit(&head.Height) 240 // if err != nil { 241 // return err 242 // } 243 // check := certclient.CommitFromResult(commit) 244 // return ValidateHeader(head, check) 245 // } 246 // 247 // func verifyBlock(c rpcclient.Client, block *types.Block) error { 248 // // get a checkpoint to verify from 249 // commit, err := c.Commit(&block.Height) 250 // if err != nil { 251 // return err 252 // } 253 // check := certclient.CommitFromResult(commit) 254 // return ValidateBlock(block, check) 255 // }