Exemplo n.º 1
0
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService,
         request: schema_pb2.SafeSetOptions):
    root = rs.get()
    index = schema_pb2.Index(index=root.index)
    content = schema_pb2.Content(timestamp=int(time()),
                                 payload=request.kv.value)
    skv = schema_pb2.StructuredKeyValue(key=request.kv.key, value=content)
    rawRequest = schema_pb2.SafeSetSVOptions(skv=skv, rootIndex=index)
    msg = service.SafeSetSV(rawRequest)
    digest = item.digest(msg.index, rawRequest.skv.key,
                         rawRequest.skv.value.SerializeToString())
    if bytes(msg.leaf) != digest:
        raise VerificationException("Proof does not match the given item.")
    verified = proofs.verify(msg, bytes(msg.leaf), root)
    if verified:
        toCache = schema_pb2.Root(index=msg.index, root=msg.root)
        try:
            rs.set(toCache)
        except Exception as e:
            raise e
    return SafeSetResponse(index=msg.index,
                           leaf=msg.leaf,
                           root=msg.root,
                           at=msg.at,
                           inclusionPath=msg.inclusionPath,
                           consistencyPath=msg.consistencyPath,
                           verified=verified)
Exemplo n.º 2
0
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,
    )
Exemplo n.º 3
0
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,
    )
Exemplo n.º 4
0
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService, request: schema_pb2.Database): 
    root = rs.get()
    
    msg = service.CreateDatabase(request)
    return dbCreateResponse(
       reply = msg
    )
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService,
         request: schema_pb2.SafeGetOptions):
    root = rs.get()
    index = schema_pb2.Index(index=root.index)
    rawRequest = schema_pb2.SafeGetOptions(key=request.key, rootIndex=index)
    msg = service.SafeGetSV(rawRequest)
    verified = proofs.verify(
        msg.proof,
        item.digest(msg.item.index, msg.item.key,
                    msg.item.value.SerializeToString()), root)
    if verified:
        toCache = schema_pb2.Root(index=msg.proof.at, root=msg.proof.root)
        try:
            rs.set(toCache)
        except:
            raise VerificationException("Failed to verify")
    i = msg.item
    return SafeGetResponse(index=i.index,
                           key=i.key,
                           timestamp=i.value.timestamp,
                           value=i.value.payload.decode("utf-8"),
                           verified=verified)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
def call(service: schema_pb2_grpc.ImmuServiceStub, rs: RootService,
         request: None):
    root = rs.get()
    return CurrentRootResponse(index=root.index, root=root.root)
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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,
    )