Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro authored and alexandrosfilios committed Oct 5, 2024
1 parent fe9e445 commit e483af8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 33 deletions.
13 changes: 1 addition & 12 deletions docs/fabric/fabricdev/core/fabricdev/vault/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/pkg/errors"
)

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

type CounterBasedVersionBuilder struct{}

func (c *CounterBasedVersionBuilder) VersionedValues(rws *vault.ReadWriteSet, ns driver.Namespace, writes vault.NamespaceWrites, block driver.BlockNum, indexInBloc driver.TxNum) (map[driver.PKey]vault.VersionedValue, error) {
Expand Down Expand Up @@ -72,16 +70,7 @@ func (c *CounterBasedVersionBuilder) VersionedMetaValues(rws *vault.ReadWriteSet
type CounterBasedVersionComparator struct{}

func (c *CounterBasedVersionComparator) Equal(v1, v2 driver.RawVersion) bool {
if bytes.Equal(v1, v2) {
return true
}
if len(v1) == 0 && bytes.Equal(zeroVersion, v2) {
return true
}
if len(v2) == 0 && bytes.Equal(zeroVersion, v1) {
return true
}
return false
return bytes.Equal(v1, v2)
}

func Marshal(v uint32) []byte {
Expand Down
2 changes: 1 addition & 1 deletion platform/common/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type NewInterceptorFunc[V driver.ValidationCode] func(logger Logger, qe Versione
type (
VersionedPersistence = dbdriver.VersionedPersistence
VersionedValue = dbdriver.VersionedValue
VersionedMetaData = dbdriver.VersionedMetaData
VersionedMetadataValue = dbdriver.VersionedMetadataValue
VersionedRead = dbdriver.VersionedRead
VersionedResultsIterator = dbdriver.VersionedResultsIterator
QueryExecutor = dbdriver.QueryExecutor
Expand Down
5 changes: 5 additions & 0 deletions platform/common/driver/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type VersionedRead struct {
Version RawVersion
}

type VersionedValue struct {
Raw RawValue
Version RawVersion
}

type VersionedMetadataValue struct {
Version RawVersion
Metadata Metadata
Expand Down
18 changes: 9 additions & 9 deletions platform/view/services/db/driver/badger/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/dgraph-io/badger/v3"
"github.com/golang/protobuf/proto"
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
cdriver "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/dbtest"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/badger/mock"
Expand Down Expand Up @@ -290,26 +290,26 @@ func BenchmarkBuilder(b *testing.B) {

type BlockTxIndexVersionMarshaller struct{}

func (m BlockTxIndexVersionMarshaller) FromBytes(data driver2.RawVersion) (driver2.BlockNum, driver2.TxNum, error) {
func (m BlockTxIndexVersionMarshaller) FromBytes(data cdriver.RawVersion) (cdriver.BlockNum, cdriver.TxNum, error) {
if len(data) == 0 {
return 0, 0, nil
}
if len(data) != 8 {
return 0, 0, errors.Errorf("block number must be 8 bytes, but got %d", len(data))
}
Block := driver2.BlockNum(binary.BigEndian.Uint32(data[:4]))
TxNum := driver2.TxNum(binary.BigEndian.Uint32(data[4:]))
Block := cdriver.BlockNum(binary.BigEndian.Uint32(data[:4]))
TxNum := cdriver.TxNum(binary.BigEndian.Uint32(data[4:]))
return Block, TxNum, nil

}

func (m BlockTxIndexVersionMarshaller) ToBytes(bn driver2.BlockNum, txn driver2.TxNum) driver2.RawVersion {
func (m BlockTxIndexVersionMarshaller) ToBytes(bn cdriver.BlockNum, txn cdriver.TxNum) cdriver.RawVersion {
return blockTxIndexToBytes(bn, txn)
}

func blockTxIndexToBytes(Block driver2.BlockNum, TxNum driver2.TxNum) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint32(buf[:4], uint32(Block))
binary.BigEndian.PutUint32(buf[4:], uint32(TxNum))
func blockTxIndexToBytes(block cdriver.BlockNum, txNum cdriver.TxNum) []byte {
buf := make([]byte, 16)
binary.BigEndian.PutUint64(buf[:8], block)
binary.BigEndian.PutUint64(buf[8:], txNum)
return buf
}
10 changes: 1 addition & 9 deletions platform/view/services/db/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ var (

type SQLError = error

type VersionedValue struct {
Raw driver.RawValue
Version driver.RawVersion
}

type VersionedMetaData struct {
Metadata driver.Metadata
Version driver.RawVersion
}
type VersionedValue = driver.VersionedValue

type VersionedMetadataValue = driver.VersionedMetadataValue

Expand Down
4 changes: 2 additions & 2 deletions platform/view/services/db/driver/sql/common/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (db *VersionedPersistence) SetStateMetadatas(ns driver2.Namespace, kvs map[

func (db *VersionedPersistence) GetStateMetadata(namespace driver2.Namespace, key driver2.PKey) (driver2.Metadata, driver2.RawVersion, error) {
var m []byte
var meta map[string][]byte
var kversion []byte
var meta driver2.Metadata
var kversion driver2.RawVersion

query := fmt.Sprintf("SELECT metadata, kversion FROM %s WHERE ns = $1 AND pkey = $2", db.table)
logger.Debug(query, namespace, key)
Expand Down

0 comments on commit e483af8

Please sign in to comment.