Skip to content

Commit

Permalink
Extract storage from blockchain construction (#1318)
Browse files Browse the repository at this point in the history
This change separates storage instance construction from blockchain
construction. This is necessary for having ability to use custom storage
implementation as a backing storage for the blockchain.
  • Loading branch information
tuommaki authored Mar 22, 2023
1 parent 983ef2d commit 985ddb5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
26 changes: 2 additions & 24 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import (
"errors"
"fmt"
"math/big"
"path/filepath"
"sync"
"sync/atomic"

"github.com/0xPolygon/polygon-edge/blockchain/storage"
"github.com/0xPolygon/polygon-edge/blockchain/storage/leveldb"
"github.com/0xPolygon/polygon-edge/blockchain/storage/memory"
"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/state"
Expand Down Expand Up @@ -187,7 +184,7 @@ func (b *Blockchain) GetAvgGasPrice() *big.Int {
// NewBlockchain creates a new blockchain object
func NewBlockchain(
logger hclog.Logger,
dataDir string,
db storage.Storage,
config *chain.Chain,
consensus Verifier,
executor Executor,
Expand All @@ -197,6 +194,7 @@ func NewBlockchain(
logger: logger.Named("blockchain"),
config: config,
consensus: consensus,
db: db,
executor: executor,
txSigner: txSigner,
stream: &eventStream{},
Expand All @@ -206,26 +204,6 @@ func NewBlockchain(
},
}

var (
db storage.Storage
err error
)

if dataDir == "" {
if db, err = memory.NewMemoryStorage(nil); err != nil {
return nil, err
}
} else {
if db, err = leveldb.NewLevelDBStorage(
filepath.Join(dataDir, "blockchain"),
logger,
); err != nil {
return nil, err
}
}

b.db = db

if err := b.initCaches(defaultCacheSize); err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion blockchain/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/0xPolygon/polygon-edge/blockchain/storage"
"github.com/0xPolygon/polygon-edge/blockchain/storage/memory"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/state"
Expand Down Expand Up @@ -343,7 +344,12 @@ func newBlockChain(config *chain.Chain, executor Executor) (*Blockchain, error)
executor = &mockExecutor{}
}

b, err := NewBlockchain(hclog.NewNullLogger(), "", config, &MockVerifier{}, executor, &mockSigner{})
db, err := memory.NewMemoryStorage(nil)
if err != nil {
return nil, err
}

b, err := NewBlockchain(hclog.NewNullLogger(), db, config, &MockVerifier{}, executor, &mockSigner{})
if err != nil {
return nil, err
}
Expand Down
24 changes: 23 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"path/filepath"
"time"

"github.com/0xPolygon/polygon-edge/blockchain/storage"
"github.com/0xPolygon/polygon-edge/blockchain/storage/leveldb"
"github.com/0xPolygon/polygon-edge/blockchain/storage/memory"
consensusPolyBFT "github.com/0xPolygon/polygon-edge/consensus/polybft"

"github.com/0xPolygon/polygon-edge/archive"
Expand Down Expand Up @@ -245,8 +248,27 @@ func NewServer(config *Config) (*Server, error) {
// use the eip155 signer
signer := crypto.NewEIP155Signer(chain.AllForksEnabled.At(0), uint64(m.config.Chain.Params.ChainID))

// create storage instance for blockchain
var db storage.Storage
{
if m.config.DataDir == "" {
db, err = memory.NewMemoryStorage(nil)
if err != nil {
return nil, err
}
} else {
db, err = leveldb.NewLevelDBStorage(
filepath.Join(m.config.DataDir, "blockchain"),
m.logger,
)
if err != nil {
return nil, err
}
}
}

// blockchain object
m.blockchain, err = blockchain.NewBlockchain(logger, m.config.DataDir, config.Chain, nil, m.executor, signer)
m.blockchain, err = blockchain.NewBlockchain(logger, db, config.Chain, nil, m.executor, signer)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 985ddb5

Please sign in to comment.