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

Cap the domain deposit amount for certain assets #157

Merged
merged 8 commits into from
Oct 12, 2023
Merged
71 changes: 71 additions & 0 deletions storage/badger_asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package storage

import (
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/domains/bitcoin"
"github.com/MixinNetwork/mixin/domains/ethereum"
"github.com/dgraph-io/badger/v4"
)

func assetCapAt(id crypto.Hash) common.Integer {
switch id {
case bitcoin.BitcoinChainId:
return common.NewIntegerFromString("10000")
case ethereum.EthereumChainId:
return common.NewIntegerFromString("100000")
case common.XINAssetId:
return common.NewIntegerFromString("1000000")
default: // TODO more assets and better default value
return common.NewIntegerFromString("115792089237316195423570985008687907853269984665640564039457.58400791")
}
}

func readTotalInAsset(txn *badger.Txn, hash crypto.Hash) (common.Integer, error) {
key := graphAssetTotalKey(hash)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return common.Zero, nil
} else if err != nil {
return common.Zero, err
}
val, err := item.ValueCopy(nil)
if err != nil {
return common.Zero, err
}
return common.NewIntegerFromString(string(val)), nil
}

func writeTotalInAsset(txn *badger.Txn, ver *common.VersionedTransaction) error {
total, err := readTotalInAsset(txn, ver.Asset)
if err != nil {
return err
}

typ := ver.TransactionType()
switch { // TODO needs full test code for all kind of transactions
case typ == common.TransactionTypeWithdrawalSubmit:
total = total.Sub(ver.Outputs[0].Amount)
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to fix the old data before the deployment, otherwise this integer sub would panic.

case typ == common.TransactionTypeDeposit:
total = total.Add(ver.DepositData().Amount)
case typ == common.TransactionTypeMint:
total = total.Add(ver.Inputs[0].Mint.Amount)
case len(ver.Inputs[0].Genesis) > 0:
for _, out := range ver.Outputs {
total = total.Add(out.Amount)
}
default:
return nil
}

max := assetCapAt(ver.Asset)
if total.Cmp(max) > 0 {
panic(total.String())
}
key := graphAssetTotalKey(ver.Asset)
return txn.Set(key, []byte(total.String()))
}

func graphAssetTotalKey(id crypto.Hash) []byte {
return append([]byte(graphPrefixAssetTotal), id[:]...)
}
1 change: 1 addition & 0 deletions storage/badger_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
graphPrefixWorkSnapshot = "WORKSNAPSHOT"
graphPrefixSpaceCheckpoint = "SPACECHECKPOINT"
graphPrefixSpaceQueue = "SPACEQUEUE"
graphPrefixAssetTotal = "ASSETTOTAL"
graphPrefixCustodianUpdate = "CUSTODIANUPDATE"
)

Expand Down
3 changes: 2 additions & 1 deletion storage/badger_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func finalizeTransaction(txn *badger.Txn, ver *common.VersionedTransaction, snap
return err
}
}
return nil

return writeTotalInAsset(txn, ver)
}

func writeUTXO(txn *badger.Txn, utxo *common.UTXOWithLock, extra []byte, timestamp uint64, genesis bool) error {
Expand Down
Loading