Skip to content

Commit

Permalink
change FetchBid to FetchRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarzzz committed Dec 15, 2023
1 parent ea6a831 commit 87f0450
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 137 deletions.
8 changes: 4 additions & 4 deletions suave/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type DataRecord struct {
Signature []byte
}

func (b *DataRecord) ToInnerBid() types.DataRecord {
func (b *DataRecord) ToInnerRecord() types.DataRecord {
return types.DataRecord{
Id: b.Id,
Salt: b.Salt,
Expand All @@ -45,9 +45,9 @@ type BuildBlockArgs = types.BuildBlockArgs
var ConfStoreAllowedAny common.Address = common.HexToAddress("0x42")

var (
ErrBidAlreadyPresent = errors.New("data record already present")
ErrBidNotFound = errors.New("data record not found")
ErrUnsignedFinalize = errors.New("finalize called with unsigned transaction, refusing to propagate")
ErrRecordAlreadyPresent = errors.New("data record already present")
ErrRecordNotFound = errors.New("data record not found")
ErrUnsignedFinalize = errors.New("finalize called with unsigned transaction, refusing to propagate")
)

type ConfidentialStoreBackend interface {
Expand Down
2 changes: 1 addition & 1 deletion suave/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/google/uuid"
)

func RandomBidId() types.DataId {
func RandomDataRecordId() types.DataId {
return types.DataId(uuid.New())
}

Expand Down
20 changes: 10 additions & 10 deletions suave/cstore/backend_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ import (
)

func testBackendStore(t *testing.T, store ConfidentialStorageBackend) {
bid := suave.DataRecord{
Id: suave.RandomBidId(),
record := suave.DataRecord{
Id: suave.RandomDataRecordId(),
DecryptionCondition: 10,
AllowedPeekers: []common.Address{common.HexToAddress("0x424344")},
Version: "default:v0:ethBundles",
}

err := store.InitRecord(bid)
err := store.InitRecord(record)
require.NoError(t, err)

bidRes, err := store.FetchBidByID(bid.Id)
recordRes, err := store.FetchRecordByID(record.Id)
require.NoError(t, err)
require.Equal(t, bid, bidRes)
require.Equal(t, record, recordRes)

_, err = store.Store(bid, bid.AllowedPeekers[0], "xx", []byte{0x43, 0x14})
_, err = store.Store(record, record.AllowedPeekers[0], "xx", []byte{0x43, 0x14})
require.NoError(t, err)

retrievedData, err := store.Retrieve(bid, bid.AllowedPeekers[0], "xx")
retrievedData, err := store.Retrieve(record, record.AllowedPeekers[0], "xx")
require.NoError(t, err)
require.Equal(t, []byte{0x43, 0x14}, retrievedData)

bids := store.FetchBidsByProtocolAndBlock(10, "default:v0:ethBundles")
require.Len(t, bids, 1)
require.Equal(t, bid, bids[0])
records := store.FetchRecordsByProtocolAndBlock(10, "default:v0:ethBundles")
require.Len(t, records, 1)
require.Equal(t, record, records[0])
}
84 changes: 42 additions & 42 deletions suave/cstore/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type ConfidentialStorageBackend interface {
InitRecord(record suave.DataRecord) error
Store(record suave.DataRecord, caller common.Address, key string, value []byte) (suave.DataRecord, error)
Retrieve(record suave.DataRecord, caller common.Address, key string) ([]byte, error)
FetchBidByID(suave.DataId) (suave.DataRecord, error)
FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord
FetchRecordByID(suave.DataId) (suave.DataRecord, error)
FetchRecordsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord
Stop() error
}

Expand Down Expand Up @@ -92,9 +92,9 @@ func NewEngine(backend ConfidentialStorageBackend, transportTopic StoreTransport
// NewTransactionalStore creates a new transactional store.
func (e *CStoreEngine) NewTransactionalStore(sourceTx *types.Transaction) *TransactionalStore {
return &TransactionalStore{
sourceTx: sourceTx,
engine: e,
pendingBids: make(map[suave.DataId]suave.DataRecord),
sourceTx: sourceTx,
engine: e,
pendingRecords: make(map[suave.DataId]suave.DataRecord),
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ func (e *CStoreEngine) InitRecord(record types.DataRecord, creationTx *types.Tra
return suave.DataRecord{}, errors.New("confidential engine: incorrect record id passed")
}

initializedBid := suave.DataRecord{
initializedRecord := suave.DataRecord{
Id: record.Id,
Salt: record.Salt,
DecryptionCondition: record.DecryptionCondition,
Expand All @@ -187,7 +187,7 @@ func (e *CStoreEngine) InitRecord(record types.DataRecord, creationTx *types.Tra
CreationTx: creationTx,
}

bidBytes, err := SerializeDataRecord(&initializedBid)
reocrdBytes, err := SerializeDataRecord(&initializedRecord)
if err != nil {
return suave.DataRecord{}, fmt.Errorf("confidential engine: could not hash record for signing: %w", err)
}
Expand All @@ -197,46 +197,46 @@ func (e *CStoreEngine) InitRecord(record types.DataRecord, creationTx *types.Tra
return suave.DataRecord{}, fmt.Errorf("confidential engine: could not recover execution node from creation transaction: %w", err)
}

initializedBid.Signature, err = e.daSigner.Sign(signingAccount, bidBytes)
initializedRecord.Signature, err = e.daSigner.Sign(signingAccount, reocrdBytes)
if err != nil {
return suave.DataRecord{}, fmt.Errorf("confidential engine: could not sign initialized record: %w", err)
}

return initializedBid, nil
return initializedRecord, nil
}

// FetchBidByID retrieves a bid by its identifier.
func (e *CStoreEngine) FetchBidByID(id suave.DataId) (suave.DataRecord, error) {
return e.storage.FetchBidByID(id)
// FetchRecordByID retrieves a data record by its identifier.
func (e *CStoreEngine) FetchRecordByID(id suave.DataId) (suave.DataRecord, error) {
return e.storage.FetchRecordByID(id)
}

// FetchBidsByProtocolAndBlock fetches bids based on protocol and block number.
func (e *CStoreEngine) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
return e.storage.FetchBidsByProtocolAndBlock(blockNumber, namespace)
// FetchRecordsByProtocolAndBlock fetches data records based on protocol and block number.
func (e *CStoreEngine) FetchRecordsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
return e.storage.FetchRecordsByProtocolAndBlock(blockNumber, namespace)
}

// Retrieve fetches data associated with a bid.
// Retrieve fetches data associated with a record.
func (e *CStoreEngine) Retrieve(id suave.DataId, caller common.Address, key string) ([]byte, error) {
bid, err := e.storage.FetchBidByID(id)
record, err := e.storage.FetchRecordByID(id)
if err != nil {
return []byte{}, fmt.Errorf("confidential engine: could not fetch bid %x while retrieving: %w", id, err)
return []byte{}, fmt.Errorf("confidential engine: could not fetch record %x while retrieving: %w", id, err)
}

if !slices.Contains(bid.AllowedPeekers, caller) && !slices.Contains(bid.AllowedPeekers, suave.AllowedPeekerAny) {
if !slices.Contains(record.AllowedPeekers, caller) && !slices.Contains(record.AllowedPeekers, suave.AllowedPeekerAny) {
return []byte{}, fmt.Errorf("confidential engine: %x not allowed to retrieve %s on %x", caller, key, id)
}

return e.storage.Retrieve(bid, caller, key)
return e.storage.Retrieve(record, caller, key)
}

// Finalize finalizes a transaction and updates the store.
func (e *CStoreEngine) Finalize(tx *types.Transaction, newBids map[suave.DataId]suave.DataRecord, stores []StoreWrite) error {
func (e *CStoreEngine) Finalize(tx *types.Transaction, newRecords map[suave.DataId]suave.DataRecord, stores []StoreWrite) error {
//
for _, bid := range newBids {
err := e.storage.InitRecord(bid)
for _, record := range newRecords {
err := e.storage.InitRecord(record)
if err != nil {
// TODO: deinitialize!
return fmt.Errorf("confidential engine: store backend failed to initialize bid: %w", err)
return fmt.Errorf("confidential engine: store backend failed to initialize record: %w", err)
}
}

Expand Down Expand Up @@ -328,49 +328,49 @@ func (e *CStoreEngine) NewMessage(message DAMessage) error {
Version: sw.DataRecord.Version,
})
if err != nil {
return fmt.Errorf("confidential engine: could not calculate received bids id: %w", err)
return fmt.Errorf("confidential engine: could not calculate received records id: %w", err)
}

if expectedId != sw.DataRecord.Id {
return fmt.Errorf("confidential engine: received bids id (%x) does not match the expected (%x)", sw.DataRecord.Id, expectedId)
return fmt.Errorf("confidential engine: received records id (%x) does not match the expected (%x)", sw.DataRecord.Id, expectedId)
}

bidBytes, err := SerializeDataRecord(&sw.DataRecord)
reocrdBytes, err := SerializeDataRecord(&sw.DataRecord)
if err != nil {
return fmt.Errorf("confidential engine: could not hash received bid: %w", err)
return fmt.Errorf("confidential engine: could not hash received record: %w", err)
}
recoveredBidSigner, err := e.daSigner.Sender(bidBytes, sw.DataRecord.Signature)
recoveredRecordSigner, err := e.daSigner.Sender(reocrdBytes, sw.DataRecord.Signature)
if err != nil {
return fmt.Errorf("confidential engine: incorrect bid signature: %w", err)
return fmt.Errorf("confidential engine: incorrect record signature: %w", err)
}
expectedBidSigner, err := KettleAddressFromTransaction(sw.DataRecord.CreationTx)
expectedRecordSigner, err := KettleAddressFromTransaction(sw.DataRecord.CreationTx)
if err != nil {
return fmt.Errorf("confidential engine: could not recover signer from bid: %w", err)
return fmt.Errorf("confidential engine: could not recover signer from record: %w", err)
}
if recoveredBidSigner != expectedBidSigner {
return fmt.Errorf("confidential engine: bid signer %x, expected %x", recoveredBidSigner, expectedBidSigner)
if recoveredRecordSigner != expectedRecordSigner {
return fmt.Errorf("confidential engine: record signer %x, expected %x", recoveredRecordSigner, expectedRecordSigner)
}

if !slices.Contains(sw.DataRecord.AllowedStores, recoveredMessageSigner) {
return fmt.Errorf("confidential engine: sw signer %x not allowed to store on bid %x", recoveredMessageSigner, sw.DataRecord.Id)
return fmt.Errorf("confidential engine: sw signer %x not allowed to store on record %x", recoveredMessageSigner, sw.DataRecord.Id)
}

if !slices.Contains(sw.DataRecord.AllowedPeekers, sw.Caller) && !slices.Contains(sw.DataRecord.AllowedPeekers, suave.AllowedPeekerAny) {
return fmt.Errorf("confidential engine: caller %x not allowed on bid %x", sw.Caller, sw.DataRecord.Id)
return fmt.Errorf("confidential engine: caller %x not allowed on record %x", sw.Caller, sw.DataRecord.Id)
}

// TODO: move to types.Sender()
_, err = e.chainSigner.Sender(sw.DataRecord.CreationTx)
if err != nil {
return fmt.Errorf("confidential engine: creation tx for bid id %x is not signed properly: %w", sw.DataRecord.Id, err)
return fmt.Errorf("confidential engine: creation tx for record id %x is not signed properly: %w", sw.DataRecord.Id, err)
}
}

for _, sw := range message.StoreWrites {
err = e.storage.InitRecord(sw.DataRecord)
if err != nil {
if !errors.Is(err, suave.ErrBidAlreadyPresent) {
log.Error("confidential engine: unexpected error while initializing bid from transport: %w", err)
if !errors.Is(err, suave.ErrRecordAlreadyPresent) {
log.Error("confidential engine: unexpected error while initializing record from transport: %w", err)
continue // Don't abandon!
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ func KettleAddressFromTransaction(tx *types.Transaction) (common.Address, error)

var emptyId [16]byte

var bidUuidSpace = uuid.UUID{0x42}
var recordUuidSpace = uuid.UUID{0x42}

func calculateRecordId(record types.DataRecord) (types.DataId, error) {
copy(record.Id[:], emptyId[:])
Expand All @@ -445,13 +445,13 @@ func calculateRecordId(record types.DataRecord) (types.DataId, error) {
return types.DataId{}, fmt.Errorf("could not marshal record to calculate its id: %w", err)
}

uuidv5 := uuid.NewSHA1(bidUuidSpace, body)
uuidv5 := uuid.NewSHA1(recordUuidSpace, body)
copy(record.Id[:], uuidv5[:])

return record.Id, nil
}

func RandomBidId() types.DataId {
func RandomRecordId() types.DataId {
return types.DataId(uuid.New())
}

Expand Down
6 changes: 3 additions & 3 deletions suave/cstore/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (*FakeStoreBackend) Start() error { return nil }
func (*FakeStoreBackend) Stop() error { return nil }

func (*FakeStoreBackend) InitRecord(record suave.DataRecord) error { return nil }
func (*FakeStoreBackend) FetchBidByID(id suave.DataId) (suave.DataRecord, error) {
func (*FakeStoreBackend) FetchRecordByID(id suave.DataId) (suave.DataRecord, error) {
return suave.DataRecord{}, errors.New("not implemented")
}

Expand All @@ -44,11 +44,11 @@ func (*FakeStoreBackend) Retrieve(record suave.DataRecord, caller common.Address
return nil, errors.New("not implemented")
}

func (*FakeStoreBackend) FetchBidById(suave.DataId) (suave.DataRecord, error) {
func (*FakeStoreBackend) FetchRecordById(suave.DataId) (suave.DataRecord, error) {
return suave.DataRecord{}, nil
}

func (*FakeStoreBackend) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
func (*FakeStoreBackend) FetchRecordsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions suave/cstore/local_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (l *LocalConfidentialStore) InitRecord(record suave.DataRecord) error {

_, found := l.records[record.Id]
if found {
return suave.ErrBidAlreadyPresent
return suave.ErrRecordAlreadyPresent
}

l.records[record.Id] = record
Expand Down Expand Up @@ -74,7 +74,7 @@ func (l *LocalConfidentialStore) Retrieve(record suave.DataRecord, caller common
return append(make([]byte, 0, len(data)), data...), nil
}

func (l *LocalConfidentialStore) FetchBidByID(dataId suave.DataId) (suave.DataRecord, error) {
func (l *LocalConfidentialStore) FetchRecordByID(dataId suave.DataId) (suave.DataRecord, error) {
l.lock.Lock()
defer l.lock.Unlock()

Expand All @@ -86,7 +86,7 @@ func (l *LocalConfidentialStore) FetchBidByID(dataId suave.DataId) (suave.DataRe
return bid, nil
}

func (l *LocalConfidentialStore) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
func (l *LocalConfidentialStore) FetchRecordsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.DataRecord {
l.lock.Lock()
defer l.lock.Unlock()

Expand Down
Loading

0 comments on commit 87f0450

Please sign in to comment.