Skip to content

Commit

Permalink
Comments fix - remove DBTransaction interface
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Oct 13, 2023
1 parent 12cd8d0 commit c997b86
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 75 deletions.
5 changes: 3 additions & 2 deletions consensus/polybft/checkpoint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xPolygon/polygon-edge/types"
hclog "github.com/hashicorp/go-hclog"
"github.com/umbracle/ethgo"
bolt "go.etcd.io/bbolt"
)

var (
Expand Down Expand Up @@ -50,7 +51,7 @@ func (d *dummyCheckpointManager) GetLogFilters() map[types.Address][]types.Hash
return make(map[types.Address][]types.Hash)
}
func (d *dummyCheckpointManager) AddLog(header *types.Header,
log *ethgo.Log, dbTx DBTransaction) error {
log *ethgo.Log, dbTx *bolt.Tx) error {
return nil
}

Expand Down Expand Up @@ -428,7 +429,7 @@ func (c *checkpointManager) GetLogFilters() map[types.Address][]types.Hash {

// AddLog is the implementation of EventSubscriber interface,
// used to handle a log defined in GetLogFilters, provided by event provider
func (c *checkpointManager) AddLog(header *types.Header, log *ethgo.Log, dbTx DBTransaction) error {
func (c *checkpointManager) AddLog(header *types.Header, log *ethgo.Log, dbTx *bolt.Tx) error {
exitEvent, doesMatch, err := parseExitEvent(header, log)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
bolt "go.etcd.io/bbolt"

"github.com/0xPolygon/go-ibft/messages"
"github.com/0xPolygon/go-ibft/messages/proto"
Expand Down Expand Up @@ -238,7 +239,7 @@ func (c *consensusRuntime) initCheckpointManager(logger hcf.Logger) error {
}

// initStakeManager initializes stake manager
func (c *consensusRuntime) initStakeManager(logger hcf.Logger, dbTx DBTransaction) error {
func (c *consensusRuntime) initStakeManager(logger hcf.Logger, dbTx *bolt.Tx) error {
rootRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint))
if err != nil {
return err
Expand Down Expand Up @@ -495,7 +496,7 @@ func (c *consensusRuntime) FSM() error {

// restartEpoch resets the previously run epoch and moves to the next one
// returns *epochMetadata different from nil if the lastEpoch is not the current one and everything was successful
func (c *consensusRuntime) restartEpoch(header *types.Header, dbTx DBTransaction) (*epochMetadata, error) {
func (c *consensusRuntime) restartEpoch(header *types.Header, dbTx *bolt.Tx) (*epochMetadata, error) {
lastEpoch := c.epoch

systemState, err := c.getSystemState(header)
Expand Down
5 changes: 3 additions & 2 deletions consensus/polybft/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package polybft
import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/types"
bolt "go.etcd.io/bbolt"
)

type PostBlockRequest struct {
Expand All @@ -14,7 +15,7 @@ type PostBlockRequest struct {
IsEpochEndingBlock bool
// DBTx is the opened transaction on state store (in our case boltDB)
// used to save necessary data on PostBlock
DBTx DBTransaction
DBTx *bolt.Tx
}

type PostEpochRequest struct {
Expand All @@ -33,5 +34,5 @@ type PostEpochRequest struct {

// DBTx is the opened transaction on state store (in our case boltDB)
// used to save necessary data on PostEpoch
DBTx DBTransaction
DBTx *bolt.Tx
}
3 changes: 2 additions & 1 deletion consensus/polybft/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/contract"
bolt "go.etcd.io/bbolt"
)

var _ blockchainBackend = (*blockchainMock)(nil)
Expand Down Expand Up @@ -148,7 +149,7 @@ func (p *polybftBackendMock) GetValidators(blockNumber uint64, parents []*types.
}

func (p *polybftBackendMock) GetValidatorsWithTx(blockNumber uint64, parents []*types.Header,
dbTx DBTransaction) (validator.AccountSet, error) {
dbTx *bolt.Tx) (validator.AccountSet, error) {
args := p.Called(blockNumber, parents, dbTx)
if len(args) == 1 {
accountSet, _ := args.Get(0).(validator.AccountSet)
Expand Down
5 changes: 3 additions & 2 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/hashicorp/go-hclog"
bolt "go.etcd.io/bbolt"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus"
Expand Down Expand Up @@ -46,7 +47,7 @@ type polybftBackend interface {
// GetValidators retrieves validator set for the given block
// Function expects that db tx is already open
GetValidatorsWithTx(blockNumber uint64, parents []*types.Header,
dbTx DBTransaction) (validator.AccountSet, error)
dbTx *bolt.Tx) (validator.AccountSet, error)
}

// Factory is the factory function to create a discovery consensus
Expand Down Expand Up @@ -729,7 +730,7 @@ func (p *Polybft) GetValidators(blockNumber uint64, parents []*types.Header) (va
}

func (p *Polybft) GetValidatorsWithTx(blockNumber uint64, parents []*types.Header,
dbTx DBTransaction) (validator.AccountSet, error) {
dbTx *bolt.Tx) (validator.AccountSet, error) {
return p.validatorsCache.GetSnapshot(blockNumber, parents, dbTx)
}

Expand Down
9 changes: 5 additions & 4 deletions consensus/polybft/proposer_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
bolt "go.etcd.io/bbolt"
)

var (
Expand All @@ -33,7 +34,7 @@ type ProposerSnapshot struct {
}

// NewProposerSnapshotFromState create ProposerSnapshot from state if possible or from genesis block
func NewProposerSnapshotFromState(config *runtimeConfig, dbTx DBTransaction) (*ProposerSnapshot, error) {
func NewProposerSnapshotFromState(config *runtimeConfig, dbTx *bolt.Tx) (*ProposerSnapshot, error) {
snapshot, err := config.State.ProposerSnapshotStore.getProposerSnapshot(dbTx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -161,7 +162,7 @@ type ProposerCalculator struct {

// NewProposerCalculator creates a new proposer calculator object
func NewProposerCalculator(config *runtimeConfig, logger hclog.Logger,
dbTx DBTransaction) (*ProposerCalculator, error) {
dbTx *bolt.Tx) (*ProposerCalculator, error) {
snap, err := NewProposerSnapshotFromState(config, dbTx)

if err != nil {
Expand Down Expand Up @@ -215,7 +216,7 @@ func (pc *ProposerCalculator) PostBlock(req *PostBlockRequest) error {
return pc.update(blockNumber, req.DBTx)
}

func (pc *ProposerCalculator) update(blockNumber uint64, dbTx DBTransaction) error {
func (pc *ProposerCalculator) update(blockNumber uint64, dbTx *bolt.Tx) error {
pc.logger.Debug("Update proposers snapshot started", "target block", blockNumber)

from := pc.snapshot.Height
Expand All @@ -242,7 +243,7 @@ func (pc *ProposerCalculator) update(blockNumber uint64, dbTx DBTransaction) err
}

// Updates ProposerSnapshot to block block with number `blockNumber`
func (pc *ProposerCalculator) updatePerBlock(blockNumber uint64, dbTx DBTransaction) error {
func (pc *ProposerCalculator) updatePerBlock(blockNumber uint64, dbTx *bolt.Tx) error {
if pc.snapshot.Height != blockNumber {
return fmt.Errorf("proposers snapshot update called for wrong block. block number=%d, snapshot block number=%d",
blockNumber, pc.snapshot.Height)
Expand Down
11 changes: 6 additions & 5 deletions consensus/polybft/stake_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
bolt "go.etcd.io/bbolt"
)

var (
Expand Down Expand Up @@ -50,7 +51,7 @@ func (d *dummyStakeManager) UpdateValidatorSet(epoch uint64,
func (d *dummyStakeManager) GetLogFilters() map[types.Address][]types.Hash {
return make(map[types.Address][]types.Hash)
}
func (d *dummyStakeManager) AddLog(header *types.Header, log *ethgo.Log, dbTx DBTransaction) error {
func (d *dummyStakeManager) AddLog(header *types.Header, log *ethgo.Log, dbTx *bolt.Tx) error {
return nil
}

Expand Down Expand Up @@ -79,7 +80,7 @@ func newStakeManager(
blockchain blockchainBackend,
polybftBackend polybftBackend,
maxValidatorSetSize int,
dbTx DBTransaction,
dbTx *bolt.Tx,
) (*stakeManager, error) {
sm := &stakeManager{
logger: logger,
Expand Down Expand Up @@ -123,7 +124,7 @@ func (s *stakeManager) PostBlock(req *PostBlockRequest) error {
return s.state.StakeStore.insertFullValidatorSet(fullValidatorSet, req.DBTx)
}

func (s *stakeManager) init(blockchain blockchainBackend, dbTx DBTransaction) error {
func (s *stakeManager) init(blockchain blockchainBackend, dbTx *bolt.Tx) error {
currentHeader := blockchain.CurrentHeader()
currentBlockNumber := currentHeader.Number

Expand Down Expand Up @@ -180,7 +181,7 @@ func (s *stakeManager) init(blockchain blockchainBackend, dbTx DBTransaction) er
return s.state.StakeStore.insertFullValidatorSet(validatorSet, dbTx)
}

func (s *stakeManager) getOrInitValidatorSet(dbTx DBTransaction) (validatorSetState, error) {
func (s *stakeManager) getOrInitValidatorSet(dbTx *bolt.Tx) (validatorSetState, error) {
validatorSet, err := s.state.StakeStore.getFullValidatorSet(dbTx)
if err != nil {
if !errors.Is(err, errNoFullValidatorSet) {
Expand Down Expand Up @@ -392,7 +393,7 @@ func (s *stakeManager) GetLogFilters() map[types.Address][]types.Hash {

// AddLog is the implementation of EventSubscriber interface,
// used to handle a log defined in GetLogFilters, provided by event provider
func (s *stakeManager) AddLog(header *types.Header, log *ethgo.Log, dbTx DBTransaction) error {
func (s *stakeManager) AddLog(header *types.Header, log *ethgo.Log, dbTx *bolt.Tx) error {
var transferEvent contractsapi.TransferEvent

doesMatch, err := transferEvent.ParseLog(log)
Expand Down
19 changes: 5 additions & 14 deletions consensus/polybft/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ var (
edgeEventsLastProcessedBlockKey = []byte("EdgeEventsLastProcessedBlockKey")
)

type DBTransaction interface {
Commit() error
Rollback() error
Bucket(bucket []byte) *bolt.Bucket
DeleteBucket(bucket []byte) error
CreateBucket(bucket []byte) (*bolt.Bucket, error)
CreateBucketIfNotExists(bucket []byte) (*bolt.Bucket, error)
}

// MessageSignature encapsulates sender identifier and its signature
type MessageSignature struct {
// Signer of the vote
Expand Down Expand Up @@ -126,8 +117,8 @@ func (s *State) initStorages() error {
}

// insertLastProcessedEventsBlock inserts the last processed block for events on Edge
func (s *State) insertLastProcessedEventsBlock(block uint64, dbTx DBTransaction) error {
insertFn := func(tx DBTransaction) error {
func (s *State) insertLastProcessedEventsBlock(block uint64, dbTx *bolt.Tx) error {
insertFn := func(tx *bolt.Tx) error {
return tx.Bucket(edgeEventsLastProcessedBlockBucket).Put(
edgeEventsLastProcessedBlockKey, common.EncodeUint64ToBytes(block))
}
Expand All @@ -142,13 +133,13 @@ func (s *State) insertLastProcessedEventsBlock(block uint64, dbTx DBTransaction)
}

// getLastProcessedEventsBlock gets the last processed block for events on Edge
func (s *State) getLastProcessedEventsBlock(dbTx DBTransaction) (uint64, error) {
func (s *State) getLastProcessedEventsBlock(dbTx *bolt.Tx) (uint64, error) {
var (
lastProcessed uint64
err error
)

getFn := func(tx DBTransaction) {
getFn := func(tx *bolt.Tx) {
value := tx.Bucket(edgeEventsLastProcessedBlockBucket).Get(edgeEventsLastProcessedBlockKey)
if value != nil {
lastProcessed = common.EncodeBytesToUint64(value)
Expand All @@ -170,7 +161,7 @@ func (s *State) getLastProcessedEventsBlock(dbTx DBTransaction) (uint64, error)

// beginDBTransaction creates and begins a transaction on BoltDB
// Note that transaction needs to be manually rollback or committed
func (s *State) beginDBTransaction(isWriteTx bool) (DBTransaction, error) {
func (s *State) beginDBTransaction(isWriteTx bool) (*bolt.Tx, error) {
return s.db.Begin(isWriteTx)
}

Expand Down
9 changes: 5 additions & 4 deletions consensus/polybft/state_event_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo"
bolt "go.etcd.io/bbolt"
)

// EventSubscriber specifies functions needed for a component to subscribe to eventProvider
Expand All @@ -15,7 +16,7 @@ type EventSubscriber interface {
GetLogFilters() map[types.Address][]types.Hash

// AddLog is used to handle a log defined in GetLogFilters, provided by event provider
AddLog(header *types.Header, log *ethgo.Log, dbTx DBTransaction) error
AddLog(header *types.Header, log *ethgo.Log, dbTx *bolt.Tx) error
}

// EventProvider represents an event provider in a blockchain system
Expand Down Expand Up @@ -74,7 +75,7 @@ func (e *EventProvider) Subscribe(subscriber EventSubscriber) {
// - error - if a block or its receipts could not be retrieved from blockchain
func (e *EventProvider) GetEventsFromBlocks(lastProcessedBlock uint64,
latestBlock *types.FullBlock,
dbTx DBTransaction) error {
dbTx *bolt.Tx) error {
if err := e.getEventsFromBlocksRange(lastProcessedBlock+1, latestBlock.Block.Number()-1, dbTx); err != nil {
return err
}
Expand All @@ -92,7 +93,7 @@ func (e *EventProvider) GetEventsFromBlocks(lastProcessedBlock uint64,
// Returns:
// - nil - if getting events finished successfully
// - error - if a block or its receipts could not be retrieved from blockchain
func (e *EventProvider) getEventsFromBlocksRange(from, to uint64, dbTx DBTransaction) error {
func (e *EventProvider) getEventsFromBlocksRange(from, to uint64, dbTx *bolt.Tx) error {
for i := from; i <= to; i++ {
blockHeader, found := e.blockchain.GetHeaderByNumber(i)
if !found {
Expand Down Expand Up @@ -124,7 +125,7 @@ func (e *EventProvider) getEventsFromBlocksRange(from, to uint64, dbTx DBTransac
// - error - if a subscriber for a certain log (event) returns an error on log (event) handling
func (e *EventProvider) getEventsFromReceipts(blockHeader *types.Header,
receipts []*types.Receipt,
dbTx DBTransaction) error {
dbTx *bolt.Tx) error {
for _, receipt := range receipts {
if receipt.Status == nil || *receipt.Status != types.ReceiptSuccess {
continue
Expand Down
8 changes: 4 additions & 4 deletions consensus/polybft/state_store_checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (s *CheckpointStore) initialize(tx *bolt.Tx) error {
}

// insertExitEventWithTx inserts an exit event to db
func (s *CheckpointStore) insertExitEvent(exitEvent *ExitEvent, dbTx DBTransaction) error {
insertFn := func(tx DBTransaction) error {
func (s *CheckpointStore) insertExitEvent(exitEvent *ExitEvent, dbTx *bolt.Tx) error {
insertFn := func(tx *bolt.Tx) error {
raw, err := json.Marshal(exitEvent)
if err != nil {
return err
Expand Down Expand Up @@ -179,13 +179,13 @@ func (s *CheckpointStore) getExitEvents(epoch uint64, filter func(exitEvent *Exi
}

// updateLastSaved saves the last block processed for exit events
func (s *CheckpointStore) getLastSaved(dbTx DBTransaction) (uint64, error) {
func (s *CheckpointStore) getLastSaved(dbTx *bolt.Tx) (uint64, error) {
var (
lastSavedBlock uint64
err error
)

getFn := func(tx DBTransaction) error {
getFn := func(tx *bolt.Tx) error {
v := tx.Bucket(exitEventLastProcessedBlockBucket).Get(lastProcessedBlockKey)
if v == nil {
return errNoLastSavedEntry
Expand Down
Loading

0 comments on commit c997b86

Please sign in to comment.