Skip to content

Commit

Permalink
reorganize version marshalling
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 c2956ca commit 3d0c754
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 184 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 @@ -34,6 +34,7 @@ func NewVault(store vault.VersionedPersistence, txIDStore TXIDStore, metricsProv
&populator{},
metricsProvider,
tracerProvider,
&vault.BlockTxIndexVersionBuilder{},
)
}

Expand Down
21 changes: 0 additions & 21 deletions platform/common/core/generic/vault/fver/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package fver

import (
"bytes"
"encoding/binary"

"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
)

Expand All @@ -28,22 +26,3 @@ func IsEqual(a, b driver.RawVersion) bool {
}
return false
}

func ToBytes(Block driver.BlockNum, TxNum driver.TxNum) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint32(buf[:4], uint32(Block))
binary.BigEndian.PutUint32(buf[4:], uint32(TxNum))
return buf
}

func FromBytes(data []byte) (driver.BlockNum, driver.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 := driver.BlockNum(binary.BigEndian.Uint32(data[:4]))
TxNum := driver.TxNum(binary.BigEndian.Uint32(data[4:]))
return Block, TxNum, nil
}
86 changes: 47 additions & 39 deletions platform/common/core/generic/vault/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ package vault

import (
"context"
"encoding/binary"
"testing"

"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"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/common/utils/collections"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
Expand Down Expand Up @@ -132,7 +132,7 @@ func TTestInterceptorConcurrency(t *testing.T, ddb VersionedPersistence, vp arti

err = ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetState(ns, k, VersionedValue{Raw: []byte("val"), Version: fver.ToBytes(35, 1)})
err = ddb.SetState(ns, k, VersionedValue{Raw: []byte("val"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand Down Expand Up @@ -256,13 +256,13 @@ func TTestQueryExecutor(t *testing.T, ddb VersionedPersistence, vp artifactsProv

err = ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetState(ns, "k2", VersionedValue{Raw: []byte("k2_value"), Version: fver.ToBytes(35, 1)})
err = ddb.SetState(ns, "k2", VersionedValue{Raw: []byte("k2_value"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = ddb.SetState(ns, "k3", VersionedValue{Raw: []byte("k3_value"), Version: fver.ToBytes(35, 2)})
err = ddb.SetState(ns, "k3", VersionedValue{Raw: []byte("k3_value"), Version: toBytes(35, 2)})
assert.NoError(t, err)
err = ddb.SetState(ns, "k1", VersionedValue{Raw: []byte("k1_value"), Version: fver.ToBytes(35, 3)})
err = ddb.SetState(ns, "k1", VersionedValue{Raw: []byte("k1_value"), Version: toBytes(35, 3)})
assert.NoError(t, err)
err = ddb.SetState(ns, "k111", VersionedValue{Raw: []byte("k111_value"), Version: fver.ToBytes(35, 4)})
err = ddb.SetState(ns, "k111", VersionedValue{Raw: []byte("k111_value"), Version: toBytes(35, 4)})
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand All @@ -289,10 +289,10 @@ func TTestQueryExecutor(t *testing.T, ddb VersionedPersistence, vp artifactsProv
}
assert.Len(t, res, 4)
assert.ElementsMatch(t, []VersionedRead{
{Key: "k1", Raw: []byte("k1_value"), Version: fver.ToBytes(35, 3)},
{Key: "k111", Raw: []byte("k111_value"), Version: fver.ToBytes(35, 4)},
{Key: "k2", Raw: []byte("k2_value"), Version: fver.ToBytes(35, 1)},
{Key: "k3", Raw: []byte("k3_value"), Version: fver.ToBytes(35, 2)},
{Key: "k1", Raw: []byte("k1_value"), Version: toBytes(35, 3)},
{Key: "k111", Raw: []byte("k111_value"), Version: toBytes(35, 4)},
{Key: "k2", Raw: []byte("k2_value"), Version: toBytes(35, 1)},
{Key: "k3", Raw: []byte("k3_value"), Version: toBytes(35, 2)},
}, res)

itr, err = ddb.GetStateRangeScanIterator(ns, "k1", "k3")
Expand All @@ -306,9 +306,9 @@ func TTestQueryExecutor(t *testing.T, ddb VersionedPersistence, vp artifactsProv
}
assert.Len(t, res, 3)
assert.Equal(t, []VersionedRead{
{Key: "k1", Raw: []byte("k1_value"), Version: fver.ToBytes(35, 3)},
{Key: "k111", Raw: []byte("k111_value"), Version: fver.ToBytes(35, 4)},
{Key: "k2", Raw: []byte("k2_value"), Version: fver.ToBytes(35, 1)},
{Key: "k1", Raw: []byte("k1_value"), Version: toBytes(35, 3)},
{Key: "k111", Raw: []byte("k111_value"), Version: toBytes(35, 4)},
{Key: "k2", Raw: []byte("k2_value"), Version: toBytes(35, 1)},
}, res)

itr, err = ddb.GetStateSetIterator(ns, "k1", "k2", "k111")
Expand All @@ -322,9 +322,9 @@ func TTestQueryExecutor(t *testing.T, ddb VersionedPersistence, vp artifactsProv
}
assert.Len(t, res, 3)
assert.ElementsMatch(t, []VersionedRead{
{Key: "k1", Raw: []byte("k1_value"), Version: fver.ToBytes(35, 3)},
{Key: "k2", Raw: []byte("k2_value"), Version: fver.ToBytes(35, 1)},
{Key: "k111", Raw: []byte("k111_value"), Version: fver.ToBytes(35, 4)},
{Key: "k1", Raw: []byte("k1_value"), Version: toBytes(35, 3)},
{Key: "k2", Raw: []byte("k2_value"), Version: toBytes(35, 1)},
{Key: "k111", Raw: []byte("k111_value"), Version: toBytes(35, 4)},
}, res)

itr, err = ddb.GetStateSetIterator(ns, "k1", "k5")
Expand All @@ -337,7 +337,7 @@ func TTestQueryExecutor(t *testing.T, ddb VersionedPersistence, vp artifactsProv
res = append(res, *n)
}
var expected = RemoveNils([]VersionedRead{
{Key: "k1", Raw: []byte("k1_value"), Version: fver.ToBytes(35, 3)},
{Key: "k1", Raw: []byte("k1_value"), Version: toBytes(35, 3)},
})
assert.Equal(t, expected, res)
}
Expand All @@ -350,9 +350,9 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
// Populate the DB with some data at some height
err := ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("k1val"), Version: fver.ToBytes(35, 1)})
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("k1val"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = ddb.SetState(ns, k2, VersionedValue{Raw: []byte("k2val"), Version: fver.ToBytes(37, 3)})
err = ddb.SetState(ns, k2, VersionedValue{Raw: []byte("k2val"), Version: toBytes(37, 3)})
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand All @@ -375,8 +375,8 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
MetaWrites: map[string]KeyedMetaWrites{},
},
}
rwsb.ReadSet.Add(ns, k1, fver.ToBytes(35, 1))
rwsb.ReadSet.Add(ns, k2, fver.ToBytes(37, 2))
rwsb.ReadSet.Add(ns, k1, toBytes(35, 1))
rwsb.ReadSet.Add(ns, k2, toBytes(37, 2))
rwsb.WriteSet.Add(ns, k1, []byte("k1FromTxidInvalid"))
rwsb.WriteSet.Add(ns, k2, []byte("k2FromTxidInvalid"))
marshaller := vp.NewMarshaller()
Expand Down Expand Up @@ -421,8 +421,8 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
MetaWrites: map[string]KeyedMetaWrites{},
},
}
rwsb.ReadSet.Add(ns, k1, fver.ToBytes(35, 1))
rwsb.ReadSet.Add(ns, k2, fver.ToBytes(37, 3))
rwsb.ReadSet.Add(ns, k1, toBytes(35, 1))
rwsb.ReadSet.Add(ns, k2, toBytes(37, 3))
rwsb.WriteSet.Add(ns, k1, []byte("k1FromTxidValid"))
rwsb.WriteSet.Add(ns, k2, []byte("k2FromTxidValid"))
rwsBytes, err = marshaller.Marshal(rwsb)
Expand Down Expand Up @@ -456,11 +456,11 @@ func TTestShardLikeCommit(t *testing.T, ddb VersionedPersistence, vp artifactsPr
// check the content of the kvs after that
vv, err := ddb.GetState(ns, k1)
assert.NoError(t, err)
assert.Equal(t, VersionedValue{Raw: []byte("k1FromTxidValid"), Version: fver.ToBytes(38, 10)}, vv)
assert.Equal(t, VersionedValue{Raw: []byte("k1FromTxidValid"), Version: toBytes(38, 10)}, vv)

vv, err = ddb.GetState(ns, k2)
assert.NoError(t, err)
assert.Equal(t, VersionedValue{Raw: []byte("k2FromTxidValid"), Version: fver.ToBytes(38, 10)}, vv)
assert.Equal(t, VersionedValue{Raw: []byte("k2FromTxidValid"), Version: toBytes(38, 10)}, vv)

// all Interceptors should be gone
assert.Len(t, aVault.Interceptors, 0)
Expand All @@ -480,7 +480,7 @@ func TTestVaultErr(t *testing.T, ddb VersionedPersistence, vp artifactsProvider)
OrderedReads: map[string][]string{},
},
}
rws.ReadSet.Add("pineapple", "key", fver.ToBytes(35, 1))
rws.ReadSet.Add("pineapple", "key", toBytes(35, 1))
m := vp.NewMarshaller()
rwsBytes, err := m.Marshal(rws)
assert.NoError(t, err)
Expand Down Expand Up @@ -527,7 +527,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
assert.NoError(t, err)
err = ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand Down Expand Up @@ -558,8 +558,8 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
MetaWrites: map[string]KeyedMetaWrites{},
},
}
rwsb.ReadSet.Add(ns, k1, fver.ToBytes(35, 1))
rwsb.ReadSet.Add(ns, ne2Key, fver.ToBytes(0, 0))
rwsb.ReadSet.Add(ns, k1, toBytes(35, 1))
rwsb.ReadSet.Add(ns, ne2Key, toBytes(0, 0))
rwsb.WriteSet.Add(ns, k1, []byte("newv1"))
rwsb.MetaWriteSet.Add(ns, k1, map[string][]byte{"k1": []byte("v1")})
m := vp.NewMarshaller()
Expand All @@ -580,9 +580,9 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
}}, rws.RWs().Writes)
assert.Equal(t, Reads{
"namespace": {
"key1": fver.ToBytes(35, 1),
"key1": toBytes(35, 1),
"notexist1": nil,
"notexist2": fver.ToBytes(0, 0),
"notexist2": toBytes(0, 0),
},
}, rws.RWs().Reads)

Expand All @@ -592,7 +592,7 @@ func TTestMerge(t *testing.T, ddb VersionedPersistence, vp artifactsProvider) {
OrderedReads: map[string][]string{},
},
}
rwsb.ReadSet.Add(ns, k1, fver.ToBytes(36, 1))
rwsb.ReadSet.Add(ns, k1, toBytes(36, 1))
rwsBytes, err = m.Marshal(rwsb)
assert.NoError(t, err)

Expand Down Expand Up @@ -650,7 +650,7 @@ func TTestInspector(t *testing.T, ddb VersionedPersistence, vp artifactsProvider
assert.NoError(t, err)
err = ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
err = ddb.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand Down Expand Up @@ -716,7 +716,7 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)
// create and populate 2 DBs
err := db1.BeginUpdate()
assert.NoError(t, err)
err = db1.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
err = db1.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = db1.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, nil)
assert.NoError(t, err)
Expand All @@ -725,7 +725,7 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)

err = db2.BeginUpdate()
assert.NoError(t, err)
err = db2.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
err = db2.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: toBytes(35, 1)})
assert.NoError(t, err)
err = db2.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -1076,16 +1076,17 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)
assert.NoError(t, err)
vv2, err = db2.GetState(ns, k2)
assert.NoError(t, err)
assert.Equal(t, VersionedValue{Raw: []byte("v2_updated"), Version: fver.ToBytes(35, 2)}, vv1)
assert.Equal(t, VersionedValue{Raw: []byte("v2_updated"), Version: toBytes(35, 2)}, vv1)
assert.Equal(t, vv1, vv2)

meta1, ver1, err := db1.GetStateMetadata(ns, k1Meta)
assert.NoError(t, err)
b1, t1, err := fver.FromBytes(ver1)
versionMarshaller := BlockTxIndexVersionMarshaller{}
b1, t1, err := versionMarshaller.FromBytes(ver1)
assert.NoError(t, err)
meta2, ver2, err := db2.GetStateMetadata(ns, k1Meta)
assert.NoError(t, err)
b2, t2, err := fver.FromBytes(ver2)
b2, t2, err := versionMarshaller.FromBytes(ver2)
assert.NoError(t, err)
assert.Equal(t, map[string][]byte{"newmetakey": []byte("newmetavalue")}, meta1)
assert.Equal(t, uint64(35), b1)
Expand Down Expand Up @@ -1133,7 +1134,7 @@ func queryVault(v *Vault[ValidationCode], ns driver.Namespace, key driver.PKey,
if err != nil {
return nil, nil, 0, 0, err
}
blkNum, txNum, err := fver.FromBytes(kVersion)
blkNum, txNum, err := BlockTxIndexVersionMarshaller{}.FromBytes(kVersion)
if err != nil {
return nil, nil, 0, 0, err
}
Expand Down Expand Up @@ -1223,3 +1224,10 @@ func (db *duplicateErrorPersistence) GetStateRangeScanIterator(namespace driver.
func (db *duplicateErrorPersistence) GetStateSetIterator(ns driver.Namespace, keys ...driver.PKey) (collections.Iterator[*VersionedRead], error) {
return db.VersionedPersistence.GetStateSetIterator(ns, keys...)
}

func toBytes(Block driver.BlockNum, TxNum driver.TxNum) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint32(buf[:4], uint32(Block))
binary.BigEndian.PutUint32(buf[4:], uint32(TxNum))
return buf
}
1 change: 1 addition & 0 deletions platform/common/core/generic/vault/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 Down
5 changes: 2 additions & 3 deletions platform/common/core/generic/vault/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"testing"

"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/common/utils/collections"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert"
Expand All @@ -21,7 +20,7 @@ func newMockQE() mockQE {
return mockQE{
State: VersionedValue{
Raw: []byte("raw"),
Version: fver.ToBytes(1, 1),
Version: blockTxIndexToBytes(1, 1),
},
Metadata: map[string][]byte{
"md": []byte("meta"),
Expand All @@ -35,7 +34,7 @@ type mockQE struct {
}

func (qe mockQE) GetStateMetadata(driver.Namespace, driver.PKey) (driver.Metadata, driver.RawVersion, error) {
return qe.Metadata, fver.ToBytes(1, 1), nil
return qe.Metadata, blockTxIndexToBytes(1, 1), nil
}
func (qe mockQE) GetState(driver.Namespace, driver.PKey) (VersionedValue, error) {
return qe.State, nil
Expand Down
2 changes: 1 addition & 1 deletion platform/common/core/generic/vault/rwset.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (w *WriteSet) Clear(ns string) {
w.OrderedWrites[ns] = []string{}
}

type Version = []byte
type Version = driver.RawVersion

type NamespaceReads map[string]Version

Expand Down
Loading

0 comments on commit 3d0c754

Please sign in to comment.