Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't generate Mana from storage deposit #245

Merged
merged 7 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230802095618-99821a09a868
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230801163503-b55ade45411b
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230801163327-a72de1469de7
github.com/iotaledger/iota.go/v4 v4.0.0-20230801163034-70beca23c22c
github.com/iotaledger/iota.go/v4 v4.0.0-20230802140830-5a196b94e5f8
github.com/labstack/echo/v4 v4.11.1
github.com/libp2p/go-libp2p v0.29.0
github.com/multiformats/go-multiaddr v0.10.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230801163503-b55ade45411b h1:MeK4C
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230801163503-b55ade45411b/go.mod h1:zoeXg8m9SfQTQY/2713pkocWg0Fi8U5fnoffcqar2J8=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230801163327-a72de1469de7 h1:AN2pAMyIhsfxMUWm3GX/FgastZFP5Q7lzfYboURzgus=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230801163327-a72de1469de7/go.mod h1:Dg0D7hAtieN4EUc7duZk6iWSikv5egQisl/XXY7zRBI=
github.com/iotaledger/iota.go/v4 v4.0.0-20230801163034-70beca23c22c h1:LtUGM6yb/l++o65Gtaj6cGPH5TSuygY6O5HUshnjKqM=
github.com/iotaledger/iota.go/v4 v4.0.0-20230801163034-70beca23c22c/go.mod h1:BGmYQp0ZX6OidxsNzH+GbwF2oc4wbsCSNHVnt0P+vl0=
github.com/iotaledger/iota.go/v4 v4.0.0-20230802140830-5a196b94e5f8 h1:ggLlxvd95Nto1G/djk+KQmPP0Gwj9W9U+OLo8iiVPXk=
github.com/iotaledger/iota.go/v4 v4.0.0-20230802140830-5a196b94e5f8/go.mod h1:BGmYQp0ZX6OidxsNzH+GbwF2oc4wbsCSNHVnt0P+vl0=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
Expand Down
22 changes: 11 additions & 11 deletions pkg/protocol/engine/accounts/mana.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (

// Mana is the stored and potential mana value of an account collected on the UTXO layer - used by the Scheduler.
type Mana struct {
value iotago.Mana `serix:"0"`
deposit iotago.BaseToken `serix:"1"`
updateTime iotago.SlotIndex `serix:"2"`
value iotago.Mana `serix:"0"`
excessBaseTokens iotago.BaseToken `serix:"1"`
updateTime iotago.SlotIndex `serix:"2"`

mutex syncutils.RWMutex
}

func NewMana(value iotago.Mana, deposit iotago.BaseToken, updateTime iotago.SlotIndex) *Mana {
func NewMana(value iotago.Mana, excessBaseTokens iotago.BaseToken, updateTime iotago.SlotIndex) *Mana {
return &Mana{
value: value,
deposit: deposit,
updateTime: updateTime,
value: value,
excessBaseTokens: excessBaseTokens,
updateTime: updateTime,
}
}

func (m *Mana) Update(value iotago.Mana, deposit iotago.BaseToken, updateTime iotago.SlotIndex) {
func (m *Mana) Update(value iotago.Mana, excessBaseTokens iotago.BaseToken, updateTime iotago.SlotIndex) {
m.mutex.Lock()
defer m.mutex.Unlock()

m.value = value
m.deposit = deposit
m.excessBaseTokens = excessBaseTokens
m.updateTime = updateTime
}

Expand All @@ -46,11 +46,11 @@ func (m *Mana) Value() iotago.Mana {
return m.value
}

func (m *Mana) Deposit() iotago.BaseToken {
func (m *Mana) ExcessBaseTokens() iotago.BaseToken {
m.mutex.RLock()
defer m.mutex.RUnlock()

return m.deposit
return m.excessBaseTokens
}

func (m *Mana) UpdateTime() iotago.SlotIndex {
Expand Down
22 changes: 17 additions & 5 deletions pkg/protocol/engine/accounts/mana/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
type Manager struct {
manaDecayProvider *iotago.ManaDecayProvider

rentStructure *iotago.RentStructure

manaVectorCache *cache.Cache[iotago.AccountID, *accounts.Mana]

accountOutputResolveFunc func(iotago.AccountID, iotago.SlotIndex) (*utxoledger.Output, error)
Expand All @@ -27,9 +29,10 @@ type Manager struct {
module.Module
}

func NewManager(manaDecayProvider *iotago.ManaDecayProvider, accountOutputResolveFunc func(iotago.AccountID, iotago.SlotIndex) (*utxoledger.Output, error)) *Manager {
func NewManager(manaDecayProvider *iotago.ManaDecayProvider, rentStructure *iotago.RentStructure, accountOutputResolveFunc func(iotago.AccountID, iotago.SlotIndex) (*utxoledger.Output, error)) *Manager {
return &Manager{
manaDecayProvider: manaDecayProvider,
rentStructure: rentStructure,
accountOutputResolveFunc: accountOutputResolveFunc,
manaVectorCache: cache.New[iotago.AccountID, *accounts.Mana](10000),
}
Expand All @@ -45,8 +48,12 @@ func (m *Manager) GetManaOnAccount(accountID iotago.AccountID, currentSlot iotag
if err != nil {
return 0, ierrors.Errorf("failed to resolve AccountOutput for %s in slot %s: %w", accountID, currentSlot, err)
}

mana = accounts.NewMana(output.StoredMana(), output.Deposit(), output.CreationTime())
minDeposit := m.rentStructure.MinDeposit(output.Output())
if output.BaseTokenAmount() <= minDeposit {
mana = accounts.NewMana(output.StoredMana(), 0, output.CreationTime())
} else {
mana = accounts.NewMana(output.StoredMana(), output.BaseTokenAmount()-minDeposit, output.CreationTime())
}

m.manaVectorCache.Put(accountID, mana)
}
Expand All @@ -63,7 +70,7 @@ func (m *Manager) GetManaOnAccount(accountID iotago.AccountID, currentSlot iotag
updatedValue := manaStored

// get newly generated potential since last update and apply decay
manaPotential, err := m.manaDecayProvider.PotentialManaWithDecay(mana.Deposit(), mana.UpdateTime(), currentSlot)
manaPotential, err := m.manaDecayProvider.PotentialManaWithDecay(mana.ExcessBaseTokens(), mana.UpdateTime(), currentSlot)
if err != nil {
return 0, err
}
Expand All @@ -85,7 +92,12 @@ func (m *Manager) ApplyDiff(slotIndex iotago.SlotIndex, destroyedAccounts ds.Set
for accountID, output := range accountOutputs {
mana, exists := m.manaVectorCache.Get(accountID)
if exists {
mana.Update(output.Output().StoredMana(), output.Output().Deposit(), slotIndex)
minDeposit := m.rentStructure.MinDeposit(output.Output())
if output.BaseTokenAmount() <= minDeposit {
mana.Update(output.StoredMana(), 0, slotIndex)
} else {
mana.Update(output.StoredMana(), output.BaseTokenAmount()-minDeposit, slotIndex)
}
}
}
}
3 changes: 2 additions & 1 deletion pkg/protocol/engine/ledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewProvider() module.Provider[*engine.Engine, ledger.Ledger] {

// TODO: how do we want to handle changing API here?
iotagoAPI := l.apiProvider.CurrentAPI()
l.manaManager = mana.NewManager(iotagoAPI.ManaDecayProvider(), l.resolveAccountOutput)
l.manaManager = mana.NewManager(iotagoAPI.ManaDecayProvider(), iotagoAPI.ProtocolParameters().RentStructure(), l.resolveAccountOutput)
l.accountsLedger.SetCommitmentEvictionAge(iotagoAPI.ProtocolParameters().MaxCommittableAge())
l.accountsLedger.SetLatestCommittedSlot(e.Storage.Settings().LatestCommitment().Index())

Expand Down Expand Up @@ -643,6 +643,7 @@ func (l *Ledger) resolveState(stateRef iotago.Input) *promise.Promise[mempool.St

switch stateRef.Type() {
case iotago.InputUTXO:
//nolint:forcetypeassert // we can safely assume that this is an UTXOInput
concreteStateRef := stateRef.(*iotago.UTXOInput)
isUnspent, err := l.utxoLedger.IsOutputIDUnspentWithoutLocking(concreteStateRef.Ref())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/ledger/tests/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (m *MockedOutput) VBytes(_ *iotago.RentStructure, _ iotago.VBytesFunc) iota
panic("implement me")
}

func (m *MockedOutput) Deposit() iotago.BaseToken {
func (m *MockedOutput) BaseTokenAmount() iotago.BaseToken {
//TODO implement me
panic("implement me")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/utxoledger/iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (m *Manager) ComputeLedgerBalance(options ...IterateOption) (balance iotago
count = 0
consumerFunc := func(output *Output) bool {
count++
balance += output.Deposit()
balance += output.BaseTokenAmount()

return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/utxoledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/iotaledger/iota.go/v4/api"
)

// ErrOutputsSumNotEqualTotalSupply is returned if the sum of the output deposits is not equal the total supply of tokens.
// ErrOutputsSumNotEqualTotalSupply is returned if the sum of the output base token amounts is not equal the total supply of tokens.
var ErrOutputsSumNotEqualTotalSupply = ierrors.New("accumulated output balance is not equal to total supply")

type Manager struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/utxoledger/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (o *Output) Bytes() []byte {
return o.encodedOutput
}

func (o *Output) Deposit() iotago.BaseToken {
return o.Output().Deposit()
func (o *Output) BaseTokenAmount() iotago.BaseToken {
return o.Output().BaseTokenAmount()
}

func (o *Output) StoredMana() iotago.Mana {
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/utxoledger/spent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (s *Spent) OutputType() iotago.OutputType {
return s.output.OutputType()
}

func (s *Spent) Deposit() iotago.BaseToken {
return s.output.Deposit()
func (s *Spent) BaseTokenAmount() iotago.BaseToken {
return s.output.BaseTokenAmount()
}

// TransactionIDSpent returns the ID of the transaction that spent the output.
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/utxoledger/tpkg/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func EqualOutput(t *testing.T, expected *utxoledger.Output, actual *utxoledger.O
require.NotNil(t, expectedIdent)
require.NotNil(t, actualIdent)
require.True(t, expectedIdent.Equal(actualIdent))
require.Equal(t, expected.Deposit(), actual.Deposit())
require.Equal(t, expected.BaseTokenAmount(), actual.BaseTokenAmount())
require.EqualValues(t, expected.Output(), actual.Output())
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/protocol/snapshotcreator/snapshotcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func CreateSnapshot(opts ...options.Option[Options]) error {
engineInstance.EvictionState.AddRootBlock(blockID, commitmentID)
}

totalAccountDeposit := lo.Reduce(opt.Accounts, func(accumulator iotago.BaseToken, details AccountDetails) iotago.BaseToken {
totalAccountAmount := lo.Reduce(opt.Accounts, func(accumulator iotago.BaseToken, details AccountDetails) iotago.BaseToken {
return accumulator + details.Amount
}, iotago.BaseToken(0))
if err := createGenesisOutput(opt.ProtocolParameters.TokenSupply()-totalAccountDeposit, opt.GenesisSeed, engineInstance); err != nil {
if err := createGenesisOutput(opt.ProtocolParameters.TokenSupply()-totalAccountAmount, opt.GenesisSeed, engineInstance); err != nil {
return ierrors.Wrap(err, "failed to create genesis outputs")
}

Expand All @@ -122,7 +122,7 @@ func createGenesisOutput(genesisTokenAmount iotago.BaseToken, genesisSeed []byte
genesisWallet := mock.NewHDWallet("genesis", genesisSeed, 0)
output := createOutput(genesisWallet.Address(), genesisTokenAmount)

if _, err = engineInstance.CurrentAPI().ProtocolParameters().RentStructure().CoversStateRent(output, genesisTokenAmount); err != nil {
if _, err = engineInstance.CurrentAPI().ProtocolParameters().RentStructure().CoversMinDeposit(output, genesisTokenAmount); err != nil {
return ierrors.Wrap(err, "min rent not covered by Genesis output with index 0")
}

Expand All @@ -141,7 +141,7 @@ func createGenesisAccounts(accounts []AccountDetails, engineInstance *engine.Eng
for idx, genesisAccount := range accounts {
output := createAccount(genesisAccount.AccountID, genesisAccount.Address, genesisAccount.Amount, genesisAccount.Mana, genesisAccount.IssuerKey, genesisAccount.ExpirySlot, genesisAccount.StakedAmount, genesisAccount.StakingEpochEnd, genesisAccount.FixedCost)

if _, err := engineInstance.CurrentAPI().ProtocolParameters().RentStructure().CoversStateRent(output, genesisAccount.Amount); err != nil {
if _, err := engineInstance.CurrentAPI().ProtocolParameters().RentStructure().CoversMinDeposit(output, genesisAccount.Amount); err != nil {
return ierrors.Wrapf(err, "min rent not covered by account output with index %d", idx+1)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_TransitionAccount(t *testing.T) {
Address: nil,
// Set 3 time min amount to cover the rent. If it's too little, then the snapshot creation will fail.
// We need more to cover an additional key that is added in the test.
Amount: testsuite.MinIssuerAccountDeposit * 3,
Amount: testsuite.MinIssuerAccountAmount * 3,
Mana: 0,
// AccountID is derived from this field, so this must be set uniquely for each account.
IssuerKey: oldGenesisOutputKey,
Expand Down
4 changes: 2 additions & 2 deletions pkg/testsuite/mock/hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (hd *HDWallet) Name() string {
func (hd *HDWallet) Balance() iotago.BaseToken {
var balance iotago.BaseToken
for _, u := range hd.utxo {
balance += u.Deposit()
balance += u.BaseTokenAmount()
}

return balance
Expand Down Expand Up @@ -124,7 +124,7 @@ func (hd *HDWallet) PrintStatus() {
}
nativeTokenDescription += "]"
}
status += fmt.Sprintf("\t%s [%s] = %d %v\n", u.OutputID().ToHex(), u.OutputType(), u.Deposit(), nativeTokenDescription)
status += fmt.Sprintf("\t%s [%s] = %d %v\n", u.OutputID().ToHex(), u.OutputType(), u.BaseTokenAmount(), nativeTokenDescription)
}
fmt.Printf("%s\n", status)
}
34 changes: 17 additions & 17 deletions pkg/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"github.com/iotaledger/iota.go/v4/tpkg"
)

const MinIssuerAccountDeposit = iotago.BaseToken(84400)
const MinValidatorAccountDeposit = iotago.BaseToken(88200)
const MinIssuerAccountAmount = iotago.BaseToken(84400)
const MinValidatorAccountAmount = iotago.BaseToken(88200)

type TestSuite struct {
Testing *testing.T
Expand Down Expand Up @@ -402,7 +402,7 @@ func (t *TestSuite) Shutdown() {
})
}

func (t *TestSuite) addNodeToPartition(name string, partition string, validator bool, optDeposit ...iotago.BaseToken) *mock.Node {
func (t *TestSuite) addNodeToPartition(name string, partition string, validator bool, optAmount ...iotago.BaseToken) *mock.Node {
t.mutex.Lock()
defer t.mutex.Unlock()

Expand All @@ -413,15 +413,15 @@ func (t *TestSuite) addNodeToPartition(name string, partition string, validator
node := mock.NewNode(t.Testing, t.Network, partition, name, validator)
t.nodes.Set(name, node)

deposit := MinValidatorAccountDeposit
if len(optDeposit) > 0 {
deposit = optDeposit[0]
amount := MinValidatorAccountAmount
if len(optAmount) > 0 {
amount = optAmount[0]
}
if deposit > 0 {
if amount > 0 {
accountDetails := snapshotcreator.AccountDetails{
Address: iotago.Ed25519AddressFromPubKey(node.PubKey),
Amount: deposit,
Mana: iotago.Mana(deposit),
Amount: amount,
Mana: iotago.Mana(amount),
IssuerKey: ed25519.PublicKey(node.PubKey),
ExpirySlot: math.MaxUint64,
BlockIssuanceCredits: iotago.BlockIssuanceCredits(math.MaxInt64),
Expand All @@ -438,20 +438,20 @@ func (t *TestSuite) addNodeToPartition(name string, partition string, validator
return node
}

func (t *TestSuite) AddValidatorNodeToPartition(name string, partition string, optDeposit ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, partition, true, optDeposit...)
func (t *TestSuite) AddValidatorNodeToPartition(name string, partition string, optAmount ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, partition, true, optAmount...)
}

func (t *TestSuite) AddValidatorNode(name string, optDeposit ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, mock.NetworkMainPartition, true, optDeposit...)
func (t *TestSuite) AddValidatorNode(name string, optAmount ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, mock.NetworkMainPartition, true, optAmount...)
}

func (t *TestSuite) AddNodeToPartition(name string, partition string, optDeposit ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, partition, false, optDeposit...)
func (t *TestSuite) AddNodeToPartition(name string, partition string, optAmount ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, partition, false, optAmount...)
}

func (t *TestSuite) AddNode(name string, optDeposit ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, mock.NetworkMainPartition, false, optDeposit...)
func (t *TestSuite) AddNode(name string, optAmount ...iotago.BaseToken) *mock.Node {
return t.addNodeToPartition(name, mock.NetworkMainPartition, false, optAmount...)
}

func (t *TestSuite) RemoveNode(name string) {
Expand Down
Loading