-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcf3a3b
commit bfac431
Showing
9 changed files
with
438 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/algorand/go-algorand-sdk/v2/protocol/config" | ||
|
||
sdk "github.com/algorand/go-algorand-sdk/v2/types" | ||
) | ||
|
||
// stateSchemaMinBalance computes the MinBalance requirements for a StateSchema | ||
// based on the consensus parameters | ||
func stateSchemaMinBalance(sm sdk.StateSchema, proto *config.ConsensusParams) uint64 { | ||
// Flat cost for each key/value pair | ||
flatCost := proto.SchemaMinBalancePerEntry * (sm.NumUint + sm.NumByteSlice) | ||
|
||
// Cost for uints | ||
uintCost := proto.SchemaUintMinBalance * sm.NumUint | ||
|
||
// Cost for byte slices | ||
bytesCost := proto.SchemaBytesMinBalance * sm.NumByteSlice | ||
|
||
// Sum the separate costs | ||
return flatCost + uintCost + bytesCost | ||
} | ||
|
||
// minBalance computes the minimum balance requirements for an account based on | ||
// some consensus parameters. MinBalance should correspond roughly to how much | ||
// storage the account is allowed to store on disk. | ||
func minBalance( | ||
proto *config.ConsensusParams, | ||
totalAssets uint64, | ||
totalAppSchema sdk.StateSchema, | ||
totalAppParams uint64, totalAppLocalStates uint64, | ||
totalExtraAppPages uint64, | ||
totalBoxes uint64, totalBoxBytes uint64, | ||
) uint64 { | ||
var min uint64 | ||
|
||
// First, base MinBalance | ||
min = proto.MinBalance | ||
|
||
// MinBalance for each Asset | ||
assetCost := proto.MinBalance * totalAssets | ||
min += assetCost | ||
|
||
// Base MinBalance for each created application | ||
appCreationCost := proto.AppFlatParamsMinBalance * totalAppParams | ||
min += appCreationCost | ||
|
||
// Base MinBalance for each opted in application | ||
appOptInCost := proto.AppFlatOptInMinBalance * totalAppLocalStates | ||
min += appOptInCost | ||
|
||
// MinBalance for state usage measured by LocalStateSchemas and | ||
// GlobalStateSchemas | ||
schemaCost := stateSchemaMinBalance(totalAppSchema, proto) | ||
min += schemaCost | ||
|
||
// MinBalance for each extra app program page | ||
extraAppProgramLenCost := proto.AppFlatParamsMinBalance * totalExtraAppPages | ||
min += extraAppProgramLenCost | ||
|
||
// Base MinBalance for each created box | ||
boxBaseCost := proto.BoxFlatMinBalance * totalBoxes | ||
min += boxBaseCost | ||
|
||
// Per byte MinBalance for boxes | ||
boxByteCost := proto.BoxByteMinBalance * totalBoxBytes | ||
min += boxByteCost | ||
|
||
return min | ||
} | ||
|
||
// AccountMinBalance computes the minimum balance requirements for an account | ||
// based on some consensus parameters. MinBalance should correspond roughly to | ||
// how much storage the account is allowed to store on disk. | ||
func AccountMinBalance(account sdk.AccountData, proto *config.ConsensusParams) uint64 { | ||
return minBalance( | ||
proto, | ||
account.TotalAssets, | ||
account.TotalAppSchema, | ||
account.TotalAppParams, account.TotalAppLocalStates, | ||
uint64(account.TotalExtraAppPages), | ||
account.TotalBoxes, account.TotalBoxBytes, | ||
) | ||
} |