Skip to content

Commit

Permalink
Sort errors (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador authored Apr 22, 2024
1 parent 301648e commit 2772ef3
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 39 deletions.
11 changes: 2 additions & 9 deletions db/base_db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -87,7 +86,7 @@ func MakeDefaultBaseDBFromBaseDB(db BaseDB) BaseDB {
func newBaseDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*baseDB, error) {
b, err := leveldb.OpenFile(path, o)
if err != nil {
return nil, fmt.Errorf("cannot open leveldb; %v", err)
return nil, fmt.Errorf("cannot open leveldb; %w", err)
}
return &baseDB{
backend: b,
Expand Down Expand Up @@ -124,13 +123,7 @@ func (db *baseDB) Has(key []byte) (bool, error) {
}

func (db *baseDB) Get(key []byte) ([]byte, error) {
b, err := db.backend.Get(key, db.ro)
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return nil, nil
}
}
return b, nil
return db.backend.Get(key, db.ro)
}

func (db *baseDB) NewBatch() Batch {
Expand Down
6 changes: 3 additions & 3 deletions db/code_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (db *codeDB) GetCode(codeHash types.Hash) ([]byte, error) {
key := CodeDBKey(codeHash)
code, err := db.Get(key)
if err != nil {
return nil, fmt.Errorf("cannot get code %s: %v", codeHash, err)
return nil, fmt.Errorf("cannot get code %s: %w", codeHash, err)
}
return code, nil
}
Expand All @@ -92,7 +92,7 @@ func (db *codeDB) PutCode(code []byte) error {
key := CodeDBKey(codeHash)
err := db.Put(key, code)
if err != nil {
return fmt.Errorf("cannot put code %s: %v", codeHash, err)
return fmt.Errorf("cannot put code %s: %w", codeHash, err)
}

return nil
Expand All @@ -107,7 +107,7 @@ func (db *codeDB) DeleteCode(codeHash types.Hash) error {
key := CodeDBKey(codeHash)
err := db.Delete(key)
if err != nil {
return fmt.Errorf("cannot get code %s: %v", codeHash, err)
return fmt.Errorf("cannot delete code %s: %w", codeHash, err)
}
return nil
}
Expand Down
13 changes: 6 additions & 7 deletions db/destroyed_account_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package db

import (
"encoding/binary"
"errors"
"fmt"
"log"

"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"

"github.com/Fantom-foundation/Substate/types"
Expand Down Expand Up @@ -35,7 +37,7 @@ func openDestroyedAccountDB(destroyedAccountDir string, o *opt.Options, wo *opt.
log.Println("substate: OpenDestroyedAccountDB")
backend, err := newBaseDB(destroyedAccountDir, o, wo, ro)
if err != nil {
return nil, fmt.Errorf("error opening deletion-db %s: %v", destroyedAccountDir, err)
return nil, fmt.Errorf("error opening deletion-db %s: %w", destroyedAccountDir, err)
}
return NewDestroyedAccountDB(backend), nil
}
Expand All @@ -60,12 +62,9 @@ func (db *DestroyedAccountDB) SetDestroyedAccounts(block uint64, tx int, des []t

func (db *DestroyedAccountDB) GetDestroyedAccounts(block uint64, tx int) ([]types.Address, []types.Address, error) {
data, err := db.backend.Get(encodeDestroyedAccountKey(block, tx))
if err != nil {
if err != nil && !errors.Is(err, leveldb.ErrNotFound) {
return nil, nil, err
}
if data == nil {
return nil, nil, nil
}
list, err := DecodeAddressList(data)
return list.DestroyedAccounts, list.ResurrectedAccounts, err
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func (db *DestroyedAccountDB) GetFirstKey() (uint64, error) {
for iter.Next() {
firstBlock, _, err := DecodeDestroyedAccountKey(iter.Key())
if err != nil {
return 0, fmt.Errorf("cannot decode updateset key; %v", err)
return 0, fmt.Errorf("cannot decode updateset key; %w", err)
}
return firstBlock, nil
}
Expand All @@ -162,7 +161,7 @@ func (db *DestroyedAccountDB) GetLastKey() (uint64, error) {
for iter.Next() {
block, _, err = DecodeDestroyedAccountKey(iter.Key())
if err != nil {
return 0, fmt.Errorf("cannot decode updateset key; %v", err)
return 0, fmt.Errorf("cannot decode updateset key; %w", err)
}
}
iter.Release()
Expand Down
19 changes: 7 additions & 12 deletions db/substate_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,12 @@ func (db *substateDB) HasSubstate(block uint64, tx int) (bool, error) {
func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, error) {
val, err := db.Get(SubstateDBKey(block, tx))
if err != nil {
return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %v", block, tx, err)
}

// not in db
if val == nil {
return nil, nil
return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %w", block, tx, err)
}

rlpSubstate, err := rlp.Decode(val)
if err != nil {
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %v", block, tx, err)
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err)
}

return rlpSubstate.ToSubstate(db.GetCode, block, tx)
Expand All @@ -131,7 +126,7 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat

b, tx, err := DecodeSubstateDBKey(key)
if err != nil {
return nil, fmt.Errorf("record-replay: invalid substate key found for block %v: %v", block, err)
return nil, fmt.Errorf("record-replay: invalid substate key found for block %v: %w", block, err)
}

if block != b {
Expand All @@ -140,12 +135,12 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat

rlpSubstate, err := rlp.Decode(value)
if err != nil {
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %v", block, tx, err)
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err)
}

sbstt, err := rlpSubstate.ToSubstate(db.GetCode, block, tx)
if err != nil {
return nil, fmt.Errorf("cannot decode data into substate: %v", err)
return nil, fmt.Errorf("cannot decode data into substate: %w", err)
}

txSubstate[tx] = sbstt
Expand All @@ -163,14 +158,14 @@ func (db *substateDB) PutSubstate(ss *substate.Substate) error {
for i, account := range ss.InputSubstate {
err := db.PutCode(account.Code)
if err != nil {
return fmt.Errorf("cannot put preState code from substate-account %v block %v, %v tx into db; %v", i, ss.Block, ss.Transaction, err)
return fmt.Errorf("cannot put preState code from substate-account %v block %v, %v tx into db; %w", i, ss.Block, ss.Transaction, err)
}
}

for i, account := range ss.OutputSubstate {
err := db.PutCode(account.Code)
if err != nil {
return fmt.Errorf("cannot put postState code from substate-account %v block %v, %v tx into db; %v", i, ss.Block, ss.Transaction, err)
return fmt.Errorf("cannot put postState code from substate-account %v block %v, %v tx into db; %w", i, ss.Block, ss.Transaction, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion db/substate_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) {

block, tx, err := DecodeSubstateDBKey(data.key)
if err != nil {
return nil, fmt.Errorf("invalid substate key: %v; %v", key, err)
return nil, fmt.Errorf("invalid substate key: %v; %w", key, err)
}

rlpSubstate, err := rlp.Decode(value)
Expand Down
4 changes: 2 additions & 2 deletions db/substate_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (pool *SubstateTaskPool) ExecuteBlock(block uint64) (numTx int64, gas int64
if pool.BlockFunc != nil {
err := pool.BlockFunc(block, transactions, pool)
if err != nil {
return 0, 0, fmt.Errorf("%s: block %v: %v", pool.Name, block, err)
return 0, 0, fmt.Errorf("%s: block %v: %w", pool.Name, block, err)
}
}
if pool.TaskFunc == nil {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (pool *SubstateTaskPool) ExecuteBlock(block uint64) (numTx int64, gas int64
}
err = pool.TaskFunc(block, tx, substate, pool)
if err != nil {
return 0, 0, fmt.Errorf("%s: %v_%v: %v", pool.Name, block, tx, err)
return 0, 0, fmt.Errorf("%s: %v_%v: %w", pool.Name, block, tx, err)
}

numTx++
Expand Down
3 changes: 2 additions & 1 deletion db/update_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"

Expand Down Expand Up @@ -84,7 +85,7 @@ func (db *updateDB) GetFirstKey() (uint64, error) {
}
return firstBlock, nil
}
return 0, errors.New("no updateset found")
return 0, leveldb.ErrNotFound
}

func (db *updateDB) GetLastKey() (uint64, error) {
Expand Down
2 changes: 1 addition & 1 deletion db/update_set_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (i *updateSetIterator) decode(data rawEntry) (*updateset.UpdateSet, error)

block, err := DecodeUpdateSetKey(data.key)
if err != nil {
return nil, fmt.Errorf("substate: invalid update-set key found: %v - issue: %v", key, err)
return nil, fmt.Errorf("substate: invalid update-set key found: %v - issue: %w", key, err)
}

var updateSetRLP updateset.UpdateSetRLP
Expand Down
4 changes: 3 additions & 1 deletion rlp/rlp_message.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rlp

import (
"errors"
"math/big"

"github.com/Fantom-foundation/Substate/substate"
"github.com/Fantom-foundation/Substate/types"
"github.com/syndtr/goleveldb/leveldb"
)

func NewMessage(message *substate.Message) *Message {
Expand Down Expand Up @@ -72,7 +74,7 @@ func (m Message) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, error
if sm.To == nil {
var err error
sm.Data, err = getHashFunc(*m.InitCodeHash)
if err != nil {
if err != nil && !errors.Is(err, leveldb.ErrNotFound) {
return nil, err
}
}
Expand Down
5 changes: 4 additions & 1 deletion rlp/rlp_world_state.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package rlp

import (
"errors"

"github.com/Fantom-foundation/Substate/substate"
"github.com/Fantom-foundation/Substate/types"
"github.com/syndtr/goleveldb/leveldb"
)

func NewWorldState(worldState substate.WorldState) WorldState {
Expand Down Expand Up @@ -35,7 +38,7 @@ func (ws WorldState) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, e
for i, addr := range ws.Addresses {
acc := ws.Accounts[i]
code, err := getHashFunc(acc.CodeHash)
if err != nil {
if err != nil && !errors.Is(err, leveldb.ErrNotFound) {
return nil, err
}
sws[addr] = substate.NewAccount(acc.Nonce, acc.Balance, code)
Expand Down
5 changes: 4 additions & 1 deletion updateset/update_set.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package updateset

import (
"errors"

"github.com/Fantom-foundation/Substate/rlp"
"github.com/Fantom-foundation/Substate/substate"
"github.com/Fantom-foundation/Substate/types"
"github.com/syndtr/goleveldb/leveldb"
)

func NewUpdateSet(alloc substate.WorldState, block uint64) *UpdateSet {
Expand Down Expand Up @@ -54,7 +57,7 @@ func (up UpdateSetRLP) ToWorldState(getCodeFunc func(codeHash types.Hash) ([]byt
worldStateAcc := up.WorldState.Accounts[i]

code, err := getCodeFunc(worldStateAcc.CodeHash)
if err != nil {
if err != nil && !errors.Is(err, leveldb.ErrNotFound) {
return nil, err
}

Expand Down

0 comments on commit 2772ef3

Please sign in to comment.