def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, ops: list, noWait: bool): request_ops = [] for op in ops: if type(op) is datatypes.KeyValue: request_ops.append( schema_pb2.Op( kv=schema_pb2.KeyValue(key=op.key, value=op.value))) elif type(op) is datatypes.ZAddRequest: request_ops.append( schema_pb2.Op(zAdd=schema_pb2.ZAddRequest(set=op.set, score=op.score, key=op.key, atTx=op.atTx, boundRef=op.boundRef, noWait=op.noWait))) elif type(op) is datatypes.ReferenceRequest: request_ops.append( schema_pb2.Op(ref=schema_pb2.ReferenceRequest( key=op.key, referencedKey=op.referencedKey, atTx=op.atTx, boundRef=op.boundRef, noWait=op.noWait))) else: raise ("unknown op for execAll") request = schema_pb2.ExecAllRequest(Operations=request_ops, noWait=noWait) msg = service.ExecAll(request) return msg
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, zset: bytes, score: float, key: bytes, atTx: int = 0, verifying_key=None): state = rs.get() request = schema_pb2.VerifiableZAddRequest( zAddRequest=schema_pb2.ZAddRequest( set=zset, score=score, key=key, atTx=atTx, ), proveSinceTx=state.txId) vtx = service.VerifiableZAdd(request) if vtx.tx.header.nentries != 1: raise ErrCorruptedData tx = schema.TxFromProto(vtx.tx) entrySpecDigest = store.EntrySpecDigestFor(tx.header.version) ekv = database.EncodeZAdd(zset, score, key, atTx) inclusionProof = tx.Proof(ekv.key) verifies = store.VerifyInclusion(inclusionProof, entrySpecDigest(ekv), tx.header.eh) if not verifies: raise ErrCorruptedData if tx.header.eh != schema.DigestFromProto(vtx.dualProof.targetTxHeader.eH): raise ErrCorruptedData sourceID = state.txId sourceAlh = schema.DigestFromProto(state.txHash) targetID = tx.header.iD targetAlh = tx.header.Alh() if state.txId > 0: verifies = store.VerifyDualProof( schema.DualProofFromProto(vtx.dualProof), sourceID, targetID, sourceAlh, targetAlh, ) if not verifies: raise ErrCorruptedData newstate = State( db=state.db, txId=targetID, txHash=targetAlh, publicKey=vtx.signature.publicKey, signature=vtx.signature.signature, ) if verifying_key != None: newstate.Verify(verifying_key) rs.set(newstate) return datatypes.SetResponse( id=vtx.tx.header.id, verified=True, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, action, user, database, permissions): # Parameter names as of go-client. request = schema_pb2.ChangePermissionRequest( action=action, username=user, database=database, permission=permissions) msg = service.ChangePermission(request) return changePermissionResponse( reply=msg )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, kv: dict): request = schema_pb2.SetRequest(KVs=[ schema_pb2.KeyValue(key=key, value=value) for key, value in kv.items() ]) msg = service.Set(request) return datatypes.SetResponse( id=msg.id, verified=False, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, refkey: bytes, key: bytes, atTx=0, verifying_key=None): state = rs.get() req = schema_pb2_grpc.schema__pb2.ReferenceRequest(referencedKey=refkey, key=key, atTx=atTx, boundRef=atTx > 0) vreq = schema_pb2_grpc.schema__pb2.VerifiableReferenceRequest( referenceRequest=req, proveSinceTx=state.txId) vtx = service.VerifiableSetReference(vreq) if vtx.tx.header.nentries != 1: raise ErrCorruptedData tx = schema.TxFromProto(vtx.tx) entrySpecDigest = store.EntrySpecDigestFor(tx.header.version) inclusionProof = tx.Proof(database.EncodeKey(key)) e = database.EncodeReference(key, None, refkey, atTx) verifies = store.VerifyInclusion(inclusionProof, entrySpecDigest(e), tx.header.eh) if not verifies: raise ErrCorruptedData sourceID = state.txId sourceAlh = schema.DigestFromProto(state.txHash) targetID = tx.header.iD targetAlh = tx.header.Alh() if state.txId > 0: verifies = store.VerifyDualProof( schema.DualProofFromProto(vtx.dualProof), sourceID, targetID, sourceAlh, targetAlh, ) if not verifies: raise ErrCorruptedData newstate = State( db=state.db, txId=targetID, txHash=targetAlh, publicKey=vtx.signature.publicKey, signature=vtx.signature.signature, ) if verifying_key != None: newstate.Verify(verifying_key) rs.set(newstate) return datatypes.SetResponse( id=vtx.tx.header.id, verified=True, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, key: bytes): request = schema_pb2.KeyRequest(key=key) try: msg = service.Get(request) except Exception as e: if hasattr(e, 'details') and e.details() == 'key not found': return None raise e return datatypes.GetResponse(tx=msg.tx, key=msg.key, value=msg.value)
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, keys: list): request = schema_pb2.KeyListRequest( keys=keys # [schema_pb2_grpc.schema__pb2.Key(key=k) for k in keys] ) msg = service.GetAll(request) ret = {} for i in msg.entries: element = batchElement(tx=i.tx, key=i.key, value=i.value) ret[i.key] = element return ret
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, tx: int): request = schema_pb2.TxRequest( tx=tx ) try: msg = service.TxById(request) except Exception as e: if hasattr(e, 'details') and e.details() == 'tx not found': return None raise e ret = [] for t in msg.entries: ret.append(t.key[1:]) return ret
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, key: bytes, offset: int, limit: int, desc: bool): state = rs.get() request = schema_pb2_grpc.schema__pb2.HistoryRequest(key=key, offset=offset, limit=limit, desc=desc, sinceTx=state.txId) histo = service.History(request) histolist = [] for i in histo.entries: histolist.append( datatypes.historyResponseItem(key=i.key, value=i.value, tx=i.tx)) return histolist
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, table): res = service.DescribeTable(schema_pb2.Table(tableName=table.encode())) result = [] for row in res.rows: result.append( ColumnDescription( name=sqlvalue_to_py(row.values[0]), type=sqlvalue_to_py(row.values[1]), nullable=sqlvalue_to_py(row.values[2]), index=sqlvalue_to_py(row.values[3]), autoincrement=sqlvalue_to_py(row.values[4]), unique=sqlvalue_to_py(row.values[5]), )) return result
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, key: bytes, value: bytes, metadata: KVMetadata = None): schemaMetadata = MetadataToProto(metadata) request = schema_pb2.SetRequest(KVs=[ schema_pb2.KeyValue(key=key, value=value, metadata=schemaMetadata) ]) msg = service.Set(request) return datatypes.SetResponse( id=msg.id, verified=False, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, key: bytes, prefix: bytes, desc: bool, limit: int, sinceTx: int): if sinceTx == None: state = rs.get() sinceTx = state.txId request = schema_pb2_grpc.schema__pb2.ScanRequest(seekKey=key, prefix=prefix, desc=desc, limit=limit, sinceTx=sinceTx, noWait=False) msg = service.Scan(request) ret = {} for i in msg.entries: ret[i.key] = i.value return ret
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, zset: bytes, seekKey: bytes, seekScore: float, seekAtTx: int, inclusive: bool, limit: int, desc: bool, minscore: float, maxscore: float, sinceTx, nowait): request = schema_pb2.ZScanRequest( set=zset, seekKey=seekKey, seekScore=seekScore, seekAtTx=seekAtTx, inclusiveSeek=inclusive, limit=limit, desc=desc, minScore=schema_pb2.Score(score=minscore), maxScore=schema_pb2.Score(score=maxscore), sinceTx=sinceTx, noWait=nowait, ) msg = service.ZScan(request) return msg
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, tx: int, verifying_key=None): state = rs.get() request = schema_pb2.VerifiableTxRequest(tx=tx, proveSinceTx=state.txId) try: vtx = service.VerifiableTxById(request) except Exception as e: if hasattr(e, 'details') and e.details() == 'tx not found': return None raise e dualProof = schema.DualProofFromProto(vtx.dualProof) if state.txId <= vtx.tx.header.id: sourceid = state.txId sourcealh = schema.DigestFromProto(state.txHash) targetid = vtx.tx.header.id targetalh = dualProof.targetTxHeader.Alh() else: sourceid = vtx.tx.header.id sourcealh = dualProof.sourceTxHeader.Alh() targetid = state.txId targetalh = schema.DigestFromProto(state.txHash) verifies = store.VerifyDualProof(dualProof, sourceid, targetid, sourcealh, targetalh) if not verifies: raise exceptions.ErrCorruptedData newstate = State( db=state.db, txId=targetid, txHash=targetalh, publicKey=vtx.signature.publicKey, signature=vtx.signature.signature, ) if verifying_key != None: newstate.Verify(verifying_key) rs.set(newstate) ret = [] for t in vtx.tx.entries: ret.append(t.key[1:]) return ret
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, zset: bytes, score: float, key: bytes, atTx: int = 0): request = schema_pb2.ZAddRequest( set=zset, score=score, key=key, atTx=atTx, boundRef=atTx > 0, ) msg = service.ZAdd(request) if msg.nentries != 1: raise ErrCorruptedData return datatypes.SetResponse( id=msg.id, verified=False, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService): resp = service.ListTables(Empty()) result = [] for row in resp.rows: result.append([sqlvalue_to_py(i) for i in row.values][0]) return result
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: None): immudbstate = service.CurrentState(g_empty.Empty()) state = State.FromGrpc(immudbstate) rs.set(state) return state
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: schema_pb2.Database): root = rs.get() msg = service.CreateDatabase(request) return dbCreateResponse(reply=msg)
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: schema_pb2.Database): msg = service.UseDatabase(request) return dbUseResponse(reply=msg)
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: None): NoRequest = Empty() msg = service.DatabaseList(NoRequest) return dbListResponse(dblist=msg)
def call(service: schema_pb2_grpc.ImmuServiceStub, request: None): NoRequest = Empty() msg = service.ListUsers(NoRequest) return listUsersResponse(userlist=msg)
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: schema_pb2.ChangePasswordRequest): msg = service.ChangePassword(request) return changePasswordResponse( reply=msg )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, requestkey: bytes, atTx: int = None, verifying_key=None, sinceTx: int = None): state = rs.get() req = schema_pb2.VerifiableGetRequest(keyRequest=schema_pb2.KeyRequest( key=requestkey, atTx=atTx, sinceTx=sinceTx), proveSinceTx=state.txId) ventry = service.VerifiableGet(req) entrySpecDigest = store.EntrySpecDigestFor( int(ventry.verifiableTx.tx.header.version)) inclusionProof = schema.InclusionProofFromProto(ventry.inclusionProof) dualProof = schema.DualProofFromProto(ventry.verifiableTx.dualProof) if not ventry.entry.HasField("referencedBy"): vTx = ventry.entry.tx e = database.EncodeEntrySpec( requestkey, schema.KVMetadataFromProto(ventry.entry.metadata), ventry.entry.value) else: ref = ventry.entry.referencedBy vTx = ref.tx e = database.EncodeReference(ref.key, schema.KVMetadataFromProto(ref.metadata), ventry.entry.key, ref.atTx) if state.txId <= vTx: eh = schema.DigestFromProto( ventry.verifiableTx.dualProof.targetTxHeader.eH) sourceid = state.txId sourcealh = schema.DigestFromProto(state.txHash) targetid = vTx targetalh = dualProof.targetTxHeader.Alh() else: eh = schema.DigestFromProto( ventry.verifiableTx.dualProof.sourceTxHeader.eH) sourceid = vTx sourcealh = dualProof.sourceTxHeader.Alh() targetid = state.txId targetalh = schema.DigestFromProto(state.txHash) verifies = store.VerifyInclusion(inclusionProof, entrySpecDigest(e), eh) if not verifies: raise ErrCorruptedData if state.txId > 0: verifies = store.VerifyDualProof(dualProof, sourceid, targetid, sourcealh, targetalh) if not verifies: raise ErrCorruptedData newstate = State( db=state.db, txId=targetid, txHash=targetalh, publicKey=ventry.verifiableTx.signature.publicKey, signature=ventry.verifiableTx.signature.signature, ) if verifying_key != None: newstate.Verify(verifying_key) rs.set(newstate) if ventry.entry.HasField("referencedBy"): refkey = ventry.entry.referencedBy.key else: refkey = None return datatypes.SafeGetResponse( id=vTx, key=ventry.entry.key, value=ventry.entry.value, timestamp=ventry.verifiableTx.tx.header.ts, verified=verifies, refkey=refkey)
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: schema_pb2.CreateUserRequest): msg = service.CreateUser(request) return createUserResponse( reply=msg )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, refkey: bytes, key: bytes): request = schema_pb2_grpc.schema__pb2.ReferenceRequest( referencedKey=refkey, key=key, atTx=0, boundRef=False) msg = service.SetReference(request) return msg
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, key: bytes, value: bytes, verifying_key=None, metadata=None): schemaMetadata = MetadataToProto(metadata) state = rs.get() # print(base64.b64encode(state.SerializeToString())) kv = schema_pb2.KeyValue(key=key, value=value, metadata=schemaMetadata) rawRequest = schema_pb2.VerifiableSetRequest( setRequest=schema_pb2.SetRequest(KVs=[kv]), proveSinceTx=state.txId, ) verifiableTx = service.VerifiableSet(rawRequest) # print(base64.b64encode(verifiableTx.SerializeToString())) if verifiableTx.tx.header.nentries != 1 or len( verifiableTx.tx.entries) != 1: raise ErrCorruptedData tx = schema.TxFromProto(verifiableTx.tx) entrySpecDigest = store.EntrySpecDigestFor(tx.header.version) inclusionProof = tx.Proof(database.EncodeKey(key)) md = tx.entries[0].metadata() if md != None and md.Deleted(): raise ErrCorruptedData e = database.EncodeEntrySpec(key, md, value) verifies = store.VerifyInclusion(inclusionProof, entrySpecDigest(e), tx.header.eh) if not verifies: raise ErrCorruptedData if tx.header.eh != schema.DigestFromProto( verifiableTx.dualProof.targetTxHeader.eH): raise ErrCorruptedData sourceID = state.txId sourceAlh = schema.DigestFromProto(state.txHash) targetID = tx.header.iD targetAlh = tx.header.Alh() if state.txId > 0: verifies = store.VerifyDualProof( schema.DualProofFromProto(verifiableTx.dualProof), sourceID, targetID, sourceAlh, targetAlh, ) if not verifies: raise ErrCorruptedData newstate = State( db=state.db, txId=targetID, txHash=targetAlh, publicKey=verifiableTx.signature.publicKey, signature=verifiableTx.signature.signature, ) if verifying_key != None: newstate.Verify(verifying_key) rs.set(newstate) return datatypes.SetResponse( id=verifiableTx.tx.header.id, verified=verifies, )
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService): resp = service.Health(empty_pb2.Empty()) return resp