Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Lazy Deposit All Module Accounts During EndBlock#58

Merged
BrandonWeng merged 17 commits intomainfrom
bweng-gas-used-fix
Oct 18, 2022
Merged

Lazy Deposit All Module Accounts During EndBlock#58
BrandonWeng merged 17 commits intomainfrom
bweng-gas-used-fix

Conversation

@BrandonWeng
Copy link
Contributor

@BrandonWeng BrandonWeng commented Oct 17, 2022

Describe your changes and provide context

Relevant PR:
sei-protocol/sei-chain#313

We are moving the module deposits to the end of the block and transferring them all at once. This is to prevent the gas fee deposit from being a single source of a bottleneck when it comes to concurrency.

Testing performed to validate your change

Ran 3 iterations of load tests with 5 rounds, and 400 orders per block
image

image

@BrandonWeng BrandonWeng changed the base branch from main to fix-unit-tests October 17, 2022 20:59
@BrandonWeng BrandonWeng changed the base branch from fix-unit-tests to main October 17, 2022 21:00
return nil
}

// Processing in a given TX is sequential so no need for dependency
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we adding this check here? AddEdge is meant to be a helper that solely adds an edge, whereas helpers such as AddNodeBuildDependency will do the business logic of ensuring there arent edges within the same tx? I guess its fine to have this as an invariant for edge creation

// It deducts the balance from an accAddress and stores the balance in a mapping for ModuleAccounts.
// In the EndBlocker, it will then perform one deposit for each module account.
// It will panic if the module account does not exist.
func (k BaseKeeper) LazySendCoinsFromAccountToModule(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling this LazySend, maybe we can call it something like DeferredSend to make more contextual sense that it performs the send at the end block?

}

// Iterates on all the lazy deposits and deposit them into the store
func (k BaseKeeper) WriteLazyDepositsToModuleAccounts(ctx sdk.Context) []abci.Event {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we also need to call this in the bank endblock logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of calling it in the sei-chain app - the baseapp in sei-cosmos doesn't have access to the bank keeper

baseGas := uint64(59142) // baseGas is the gas consumed before tx msg
// check block gas is always consumed - this value may change if we update the logic for
// how gas is consumed
baseGas := uint64(52585) // baseGas is the gas consumed before tx msg
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer charge everytime so the base gas fee goes down too

@BrandonWeng BrandonWeng requested a review from udpatil October 18, 2022 04:37
type basicGasMeter struct {
limit Gas
consumed Gas
lock *sync.Mutex
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockGas can/will be modified concurrently

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to make Gas an atomic by using
atomic.AddUint64 and atomic. AddUint64? This way if we add / remove any functions that touch gas, we know that the locking is already handled at a lower layer. O/w we need to be careful of ensuring every function that accesses any Gas variable handles the mutex \cc @codchen . Not a blocker, though

type basicGasMeter struct {
limit Gas
consumed Gas
lock *sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to make Gas an atomic by using
atomic.AddUint64 and atomic. AddUint64? This way if we add / remove any functions that touch gas, we know that the locking is already handled at a lower layer. O/w we need to be careful of ensuring every function that accesses any Gas variable handles the mutex \cc @codchen . Not a blocker, though

// DeferredSendCoinsFromAccountToModule transfers coins from an AccAddress to a ModuleAccount.
// It deducts the balance from an accAddress and stores the balance in a mapping for ModuleAccounts.
// In the EndBlocker, it will then perform one deposit for each module account.
// It will panic if the module account does not exist.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the entire block get rolled back? IIRC it does, but wanted to double check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would get stuck, but the block will not be committed - in this case we would be able upload a new binary to fix it.

@BrandonWeng BrandonWeng merged commit 07fdb22 into main Oct 18, 2022
@BrandonWeng BrandonWeng deleted the bweng-gas-used-fix branch October 18, 2022 23:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants