Skip to content

Commit

Permalink
introducing version comparator
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Sep 24, 2024
1 parent 3d0c754 commit f923f6b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions docs/fabric/fabricdev/core/fabricdev/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func newInterceptor(logger vault.Logger, qe vault.VersionedQueryExecutor, txIDSt
txID,
&fdriver.ValidationCodeProvider{},
&marshaller{},
&vault.BlockTxIndexVersionComparator{},
)
}

Expand Down
28 changes: 0 additions & 28 deletions platform/common/core/generic/vault/fver/version.go

This file was deleted.

45 changes: 25 additions & 20 deletions platform/common/core/generic/vault/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ package vault
import (
"sync"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/fver"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/hash"
"github.com/pkg/errors"
Expand All @@ -22,15 +20,20 @@ type VersionedQueryExecutor interface {
Done()
}

type VersionComparator interface {
Equal(v1, v2 driver.RawVersion) bool
}

type Interceptor[V driver.ValidationCode] struct {
Logger Logger
QE VersionedQueryExecutor
TxIDStore TXIDStoreReader[V]
Rws ReadWriteSet
Marshaller Marshaller
Closed bool
TxID string
vcProvider driver.ValidationCodeProvider[V] // TODO
Logger Logger
QE VersionedQueryExecutor
TxIDStore TXIDStoreReader[V]
Rws ReadWriteSet
Marshaller Marshaller
VersionComparator VersionComparator
Closed bool
TxID string
vcProvider driver.ValidationCodeProvider[V] // TODO
sync.RWMutex
}

Expand All @@ -57,17 +60,19 @@ func NewInterceptor[V driver.ValidationCode](
txID driver.TxID,
vcProvider driver.ValidationCodeProvider[V],
marshaller Marshaller,
versionComparator VersionComparator,
) *Interceptor[V] {
logger.Debugf("new interceptor [%s]", txID)

return &Interceptor[V]{
Logger: logger,
TxID: txID,
QE: qe,
TxIDStore: txIDStore,
Rws: EmptyRWSet(),
vcProvider: vcProvider,
Marshaller: marshaller,
Logger: logger,
TxID: txID,
QE: qe,
TxIDStore: txIDStore,
Rws: EmptyRWSet(),
vcProvider: vcProvider,
Marshaller: marshaller,
VersionComparator: versionComparator,
}
}

Expand All @@ -91,7 +96,7 @@ func (i *Interceptor[V]) IsValid() error {
if err != nil {
return err
}
if !fver.IsEqual(v, vv.Version) {
if !i.VersionComparator.Equal(v, vv.Version) {
return errors.Errorf("invalid read: vault at fver %s:%s [%v], read-write set at fver [%v]", ns, k, vv, v)
}
}
Expand Down Expand Up @@ -246,7 +251,7 @@ func (i *Interceptor[V]) GetStateMetadata(namespace, key string, opts ...driver.

version, in := i.Rws.ReadSet.Get(namespace, key)
if in {
if !fver.IsEqual(version, vaultVersion) {
if !i.VersionComparator.Equal(version, vaultVersion) {
return nil, errors.Errorf("invalid metadata read: previous value returned at fver [%v], current value at fver [%v]", version, vaultVersion)
}
} else {
Expand Down Expand Up @@ -302,7 +307,7 @@ func (i *Interceptor[V]) GetState(namespace driver.Namespace, key driver.PKey, o

version, in := i.Rws.ReadSet.Get(namespace, key)
if in {
if !fver.IsEqual(version, vaultVersion) {
if !i.VersionComparator.Equal(version, vaultVersion) {
return nil, errors.Errorf("invalid read [%s:%s]: previous value returned at fver [%v], current value at fver [%v]", namespace, key, version, vaultVersion)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions platform/common/core/generic/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/db"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/fver"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/txidstore"
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
Expand Down Expand Up @@ -83,6 +82,7 @@ func newInterceptor(
txid,
&VCProvider{},
&marshaller{},
&BlockTxIndexVersionComparator{},
)
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func (m *marshaller) Append(destination *ReadWriteSet, raw []byte, nss ...string
}
for s, position := range reads {
v, in := destination.ReadSet.Get(ns, s)
if in && !fver.IsEqual(position, v) {
if in && !Equal(position, v) {
return errors.Errorf("invalid read [%s:%s]: previous value returned at fver [%v], current value at fver [%v]", ns, s, position, v)
}
destination.ReadSet.Add(ns, s, position)
Expand Down
25 changes: 23 additions & 2 deletions platform/common/core/generic/vault/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ SPDX-License-Identifier: Apache-2.0
package vault

import (
"bytes"
"encoding/binary"

"github.com/pkg/errors"

driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/pkg/errors"
)

var zeroVersion = []byte{0, 0, 0, 0, 0, 0, 0, 0}

type BlockTxIndexVersionComparator struct{}

func (b *BlockTxIndexVersionComparator) Equal(a, c driver2.RawVersion) bool {
return Equal(a, c)
}

type BlockTxIndexVersionBuilder struct{}

func (b *BlockTxIndexVersionBuilder) VersionedValues(keyMap NamespaceWrites, block driver2.BlockNum, indexInBloc driver2.TxNum) map[driver2.PKey]driver.VersionedValue {
Expand Down Expand Up @@ -58,3 +66,16 @@ func blockTxIndexToBytes(Block driver2.BlockNum, TxNum driver2.TxNum) []byte {
binary.BigEndian.PutUint32(buf[4:], uint32(TxNum))
return buf
}

func Equal(a, c driver2.RawVersion) bool {
if bytes.Equal(a, c) {
return true
}
if len(a) == 0 && bytes.Equal(zeroVersion, c) {
return true
}
if len(c) == 0 && bytes.Equal(zeroVersion, a) {
return true
}
return false
}
1 change: 1 addition & 0 deletions platform/fabric/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newInterceptor(logger vault.Logger, qe vault.VersionedQueryExecutor, txIDSt
txID,
&fdriver.ValidationCodeProvider{},
&marshaller{},
&vault.BlockTxIndexVersionComparator{},
)
}

Expand Down
1 change: 1 addition & 0 deletions platform/orion/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func newInterceptor(
txid,
&odriver.ValidationCodeProvider{},
nil,
&vault.BlockTxIndexVersionComparator{},
)}
}

Expand Down

0 comments on commit f923f6b

Please sign in to comment.