diff --git a/docs/fabric/fabricdev/core/fabricdev/vault/vault.go b/docs/fabric/fabricdev/core/fabricdev/vault/vault.go index 6e043a68d..d032743bc 100644 --- a/docs/fabric/fabricdev/core/fabricdev/vault/vault.go +++ b/docs/fabric/fabricdev/core/fabricdev/vault/vault.go @@ -46,6 +46,7 @@ func newInterceptor(logger vault.Logger, qe vault.VersionedQueryExecutor, txIDSt txID, &fdriver.ValidationCodeProvider{}, &marshaller{}, + &vault.BlockTxIndexVersionComparator{}, ) } diff --git a/platform/common/core/generic/vault/fver/version.go b/platform/common/core/generic/vault/fver/version.go deleted file mode 100644 index 614969938..000000000 --- a/platform/common/core/generic/vault/fver/version.go +++ /dev/null @@ -1,28 +0,0 @@ -/* -Copyright IBM Corp. All Rights Reserved. - -SPDX-License-Identifier: Apache-2.0 -*/ - -package fver - -import ( - "bytes" - - "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver" -) - -var zeroVersion = []byte{0, 0, 0, 0, 0, 0, 0, 0} - -func IsEqual(a, b driver.RawVersion) bool { - if bytes.Equal(a, b) { - return true - } - if len(a) == 0 && bytes.Equal(zeroVersion, b) { - return true - } - if len(b) == 0 && bytes.Equal(zeroVersion, a) { - return true - } - return false -} diff --git a/platform/common/core/generic/vault/interceptor.go b/platform/common/core/generic/vault/interceptor.go index 697f51509..99a715df6 100644 --- a/platform/common/core/generic/vault/interceptor.go +++ b/platform/common/core/generic/vault/interceptor.go @@ -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" @@ -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 } @@ -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, } } @@ -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) } } @@ -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 { @@ -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 { diff --git a/platform/common/core/generic/vault/vault_test.go b/platform/common/core/generic/vault/vault_test.go index 4e5a9f7f8..445c3223f 100644 --- a/platform/common/core/generic/vault/vault_test.go +++ b/platform/common/core/generic/vault/vault_test.go @@ -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" @@ -83,6 +82,7 @@ func newInterceptor( txid, &VCProvider{}, &marshaller{}, + &BlockTxIndexVersionComparator{}, ) } @@ -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) diff --git a/platform/common/core/generic/vault/version.go b/platform/common/core/generic/vault/version.go index b35699ff7..61f8b983e 100644 --- a/platform/common/core/generic/vault/version.go +++ b/platform/common/core/generic/vault/version.go @@ -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 { @@ -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 +} diff --git a/platform/fabric/core/generic/vault/vault.go b/platform/fabric/core/generic/vault/vault.go index da7487913..8515bcfa6 100644 --- a/platform/fabric/core/generic/vault/vault.go +++ b/platform/fabric/core/generic/vault/vault.go @@ -60,6 +60,7 @@ func newInterceptor(logger vault.Logger, qe vault.VersionedQueryExecutor, txIDSt txID, &fdriver.ValidationCodeProvider{}, &marshaller{}, + &vault.BlockTxIndexVersionComparator{}, ) } diff --git a/platform/orion/core/generic/vault/vault.go b/platform/orion/core/generic/vault/vault.go index f6e9f4f3e..1aaf2d1c4 100644 --- a/platform/orion/core/generic/vault/vault.go +++ b/platform/orion/core/generic/vault/vault.go @@ -62,6 +62,7 @@ func newInterceptor( txid, &odriver.ValidationCodeProvider{}, nil, + &vault.BlockTxIndexVersionComparator{}, )} }