Skip to content

Commit

Permalink
Return errors from enclave DB, instead of ignoring or using a critica…
Browse files Browse the repository at this point in the history
…l log message (#863)
  • Loading branch information
Joel authored Nov 22, 2022
1 parent a734947 commit e659879
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 120 deletions.
26 changes: 13 additions & 13 deletions go/enclave/db/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type RollupResolver interface {
// FetchRollupByHeight returns the rollup with the given height and true, or (nil, false) if no such rollup is stored.
FetchRollupByHeight(height uint64) (*core.Rollup, bool)
// FetchRollups returns all the proposed rollups with the given height
FetchRollups(height uint64) []*core.Rollup
FetchRollups(height uint64) ([]*core.Rollup, error)
// StoreRollup persists the rollup
StoreRollup(rollup *core.Rollup)
// ParentRollup returns the rollup's parent rollup, or (nil, false) if no such rollup was found.
Expand All @@ -58,21 +58,21 @@ type BlockStateStorage interface {
FetchBlockState(blockHash common.L1RootHash) (*core.BlockState, bool)
// FetchLogs returns the block's logs, or (nil, false) if no such block was found.
FetchLogs(blockHash common.L1RootHash) ([]*types.Log, bool)
// FetchHeadState returns the head block state. Returns nil if nothing recorded yet
FetchHeadState() *core.BlockState
// FetchHeadState returns the head block state.
FetchHeadState() (*core.BlockState, error)
// StoreNewHead saves the block state alongside its rollup, receipts and logs.
StoreNewHead(state *core.BlockState, rollup *core.Rollup, receipts []*types.Receipt, logs []*types.Log)
StoreNewHead(state *core.BlockState, rollup *core.Rollup, receipts []*types.Receipt, logs []*types.Log) error
// CreateStateDB creates a database that can be used to execute transactions
CreateStateDB(hash common.L2RootHash) *state.StateDB
CreateStateDB(hash common.L2RootHash) (*state.StateDB, error)
// EmptyStateDB creates the original empty StateDB
EmptyStateDB() *state.StateDB
}

type SharedSecretStorage interface {
// FetchSecret returns the enclave's secret, returns (nil, false) if not found
FetchSecret() (*crypto.SharedEnclaveSecret, bool)
// FetchSecret returns the enclave's secret.
FetchSecret() (*crypto.SharedEnclaveSecret, error)
// StoreSecret stores a secret in the enclave
StoreSecret(secret crypto.SharedEnclaveSecret)
StoreSecret(secret crypto.SharedEnclaveSecret) error
}

type TransactionStorage interface {
Expand All @@ -81,18 +81,18 @@ type TransactionStorage interface {
// GetTransactionReceipt - returns the receipt of a tx by tx hash
GetTransactionReceipt(txHash gethcommon.Hash) (*types.Receipt, error)
// GetReceiptsByHash retrieves the receipts for all transactions in a given rollup.
GetReceiptsByHash(hash gethcommon.Hash) types.Receipts
GetReceiptsByHash(hash gethcommon.Hash) (types.Receipts, error)
// GetSender returns the sender of the tx by hash
GetSender(txHash gethcommon.Hash) (gethcommon.Address, error)
// GetContractCreationTx returns the hash of the tx that created a contract
GetContractCreationTx(address gethcommon.Address) (gethcommon.Hash, error)
GetContractCreationTx(address gethcommon.Address) (*gethcommon.Hash, error)
}

type AttestationStorage interface {
// FetchAttestedKey returns the public key of an attested aggregator, returns nil if not found
FetchAttestedKey(aggregator gethcommon.Address) *ecdsa.PublicKey
// FetchAttestedKey returns the public key of an attested aggregator
FetchAttestedKey(aggregator gethcommon.Address) (*ecdsa.PublicKey, error)
// StoreAttestedKey - store the public key of an attested aggregator
StoreAttestedKey(aggregator gethcommon.Address, key *ecdsa.PublicKey)
StoreAttestedKey(aggregator gethcommon.Address, key *ecdsa.PublicKey) error
}

// Storage is the enclave's interface for interacting with the enclave's datastore
Expand Down
17 changes: 10 additions & 7 deletions go/enclave/db/rawdb/accessors_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ package rawdb

import (
"crypto/ecdsa"
"fmt"

gethlog "github.com/ethereum/go-ethereum/log"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/obscuronet/go-obscuro/go/common/log"
)

func ReadAttestationKey(db ethdb.KeyValueReader, address gethcommon.Address, logger gethlog.Logger) *ecdsa.PublicKey {
func ReadAttestationKey(db ethdb.KeyValueReader, address gethcommon.Address) (*ecdsa.PublicKey, error) {
key, err := db.Get(attestationPkKey(address))
if err != nil {
logger.Crit("Could not read key from db. ", log.ErrKey, err)
return nil, fmt.Errorf("could not retrieve attestation key for address %s. Cause: %w", address, err)
}

publicKey, err := crypto.DecompressPubkey(key)
if err != nil {
logger.Crit("Could not parse key from db.", log.ErrKey, err)
return nil, fmt.Errorf("could not parse key from db. Cause: %w", err)
}
return publicKey

return publicKey, nil
}

func WriteAttestationKey(db ethdb.KeyValueWriter, address gethcommon.Address, key *ecdsa.PublicKey, logger gethlog.Logger) {
func WriteAttestationKey(db ethdb.KeyValueWriter, address gethcommon.Address, key *ecdsa.PublicKey, logger gethlog.Logger) error {
if err := db.Put(attestationPkKey(address), crypto.CompressPubkey(key)); err != nil {
logger.Crit("Failed to store the attested key. ", log.ErrKey, err)
return fmt.Errorf("could not write attestation key. Cause: %w", err)
}
return nil
}
31 changes: 19 additions & 12 deletions go/enclave/db/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@ import (
// todo - all the function in this file should return an error, which must be handled by the caller
// once that is done, the logger parameter should be removed

func ReadRollup(db ethdb.KeyValueReader, hash gethcommon.Hash, logger gethlog.Logger) *core.Rollup {
height := ReadHeaderNumber(db, hash)
if height == nil {
return nil
func ReadRollup(db ethdb.KeyValueReader, hash gethcommon.Hash, logger gethlog.Logger) (*core.Rollup, error) {
height, err := ReadHeaderNumber(db, hash)
if err != nil {
return nil, err
}
return &core.Rollup{
Header: ReadHeader(db, hash, *height, logger),
Transactions: ReadBody(db, hash, *height, logger),
}
}, nil
}

// ReadHeaderNumber returns the header number assigned to a hash.
func ReadHeaderNumber(db ethdb.KeyValueReader, hash gethcommon.Hash) *uint64 {
data, _ := db.Get(headerNumberKey(hash))
func ReadHeaderNumber(db ethdb.KeyValueReader, hash gethcommon.Hash) (*uint64, error) {
data, err := db.Get(headerNumberKey(hash))
if err != nil {
return nil, err
}
if len(data) != 8 {
return nil
return nil, fmt.Errorf("header number bytes had wrong length")
}
number := binary.BigEndian.Uint64(data)
return &number
return &number, nil
}

func WriteRollup(db ethdb.KeyValueWriter, rollup *core.Rollup, logger gethlog.Logger) {
Expand Down Expand Up @@ -136,13 +139,17 @@ func ReadBodyRLP(db ethdb.KeyValueReader, hash gethcommon.Hash, number uint64, l
return data
}

func ReadRollupsForHeight(db ethdb.Database, number uint64, logger gethlog.Logger) []*core.Rollup {
func ReadRollupsForHeight(db ethdb.Database, number uint64, logger gethlog.Logger) ([]*core.Rollup, error) {
hashes := ReadAllHashes(db, number)
rollups := make([]*core.Rollup, len(hashes))
for i, hash := range hashes {
rollups[i] = ReadRollup(db, hash, logger)
rollup, err := ReadRollup(db, hash, logger)
if err != nil {
return nil, err
}
rollups[i] = rollup
}
return rollups
return rollups, nil
}

// ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
Expand Down
19 changes: 12 additions & 7 deletions go/enclave/db/rawdb/accessors_metadata.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
package rawdb

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/obscuronet/go-obscuro/go/common/errutil"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/obscuronet/go-obscuro/go/enclave/crypto"
)

func ReadSharedSecret(db ethdb.KeyValueReader) (*crypto.SharedEnclaveSecret, bool) {
func ReadSharedSecret(db ethdb.KeyValueReader) (*crypto.SharedEnclaveSecret, error) {
var ss crypto.SharedEnclaveSecret

// TODO - Handle error.
enc, _ := db.Get(sharedSecret)
if len(enc) == 0 {
return nil, false
return nil, errutil.ErrNotFound
}
if err := rlp.DecodeBytes(enc, &ss); err != nil {
return nil, false
return nil, fmt.Errorf("could not decode shared secret")
}

return &ss, true
return &ss, nil
}

func WriteSharedSecret(db ethdb.KeyValueWriter, ss crypto.SharedEnclaveSecret, logger gethlog.Logger) {
func WriteSharedSecret(db ethdb.KeyValueWriter, ss crypto.SharedEnclaveSecret) error {
enc, err := rlp.EncodeToBytes(ss)
if err != nil {
logger.Crit("could not encode shared secret. ", log.ErrKey, err)
return fmt.Errorf("could not encode shared secret. Cause: %w", err)
}
if err = db.Put(sharedSecret, enc); err != nil {
logger.Crit("could not put shared secret in DB. ", log.ErrKey, err)
return fmt.Errorf("could not shared secret in DB. Cause: %w", err)
}
return nil
}

func ReadGenesisHash(db ethdb.KeyValueReader) (*common.Hash, bool) {
Expand Down
7 changes: 4 additions & 3 deletions go/enclave/db/rawdb/accessors_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ func WriteContractCreationTx(db ethdb.KeyValueWriter, receipts types.Receipts, l
}

// ReadContractTransaction - returns the tx that created a contract
func ReadContractTransaction(db ethdb.Reader, address common.Address, logger gethlog.Logger) common.Hash {
func ReadContractTransaction(db ethdb.Reader, address common.Address) (*common.Hash, error) {
value, err := db.Get(contractReceiptKey(address))
if err != nil {
logger.Error("failed to read the contract receipt.", log.ErrKey, err)
return nil, err
}
return common.BytesToHash(value)
hash := common.BytesToHash(value)
return &hash, nil
}

// DeleteReceipts removes all receipt data associated with a block hash.
Expand Down
Loading

0 comments on commit e659879

Please sign in to comment.