Skip to content
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
23 changes: 23 additions & 0 deletions solana/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,33 @@ func (node *Node) Boot(ctx context.Context, version string) {
}
go node.bootObserver(ctx, version)
go node.bootSigner(ctx)
go node.mtgBalanceCheckLoop(ctx)

logger.Printf("node.Boot(%s, %d)", node.id, node.Index())
}

func (node *Node) mtgBalanceCheckLoop(ctx context.Context) {
for {
as, err := node.store.ListDeployedAssets(ctx)
if err != nil {
panic(err)
}
for _, a := range as {
node.checkMintBalance(ctx, a)
time.Sleep(100 * time.Millisecond)
}
time.Sleep(time.Minute)
}
}

func (node *Node) checkMintBalance(ctx context.Context, a *solanaApp.DeployedAsset) {
supply := node.RPCMintSupply(ctx, a.Address)
balance := node.getMtgAssetBalance(ctx, a.AssetId)
if balance.Cmp(supply) < 0 {
panic(fmt.Errorf("invalid balance of mtg asset %s %s: %s %s", a.AssetId, a.Address, balance, supply))
}
}

func (node *Node) Index() int {
index := node.findMember(string(node.id))
if index < 0 {
Expand Down
6 changes: 3 additions & 3 deletions solana/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ func (node *Node) checkSufficientBalanceForBurnSystemCall(ctx context.Context, c
panic(err)
}
amount := decimal.New(int64(*burn.Amount), -int32(da.Decimals))
balance := node.getAssetBalanceAt(ctx, math.MaxInt64, da.AssetId)
balance := node.getMtgAssetBalance(ctx, da.AssetId)
if balance.Cmp(amount) < 0 {
logger.Printf("insufficient balance to confirm burn system call: %s %s %s %s", call.RequestId, da.AssetId, amount.String(), balance.String())
return false
Expand All @@ -1025,8 +1025,8 @@ func (node *Node) checkSufficientBalanceForBurnSystemCall(ctx context.Context, c
return true
}

func (node *Node) getAssetBalanceAt(ctx context.Context, sequence uint64, assetId string) decimal.Decimal {
os := node.group.ListOutputsForAsset(ctx, node.conf.AppId, assetId, node.conf.MTG.Genesis.Epoch, sequence, mtg.SafeUtxoStateUnspent, 0)
func (node *Node) getMtgAssetBalance(ctx context.Context, assetId string) decimal.Decimal {
os := node.group.ListOutputsForAsset(ctx, node.conf.AppId, assetId, node.conf.MTG.Genesis.Epoch, math.MaxInt64, mtg.SafeUtxoStateUnspent, 0)
total := decimal.NewFromInt(0)
for _, o := range os {
total = total.Add(o.Amount)
Expand Down
10 changes: 10 additions & 0 deletions solana/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/token"
"github.com/gagliardetto/solana-go/rpc"
"github.com/shopspring/decimal"
)

func (node *Node) checkCreatedAtaUntilSufficient(ctx context.Context, tx *solana.Transaction) error {
Expand Down Expand Up @@ -253,6 +254,15 @@ func (node *Node) RPCCheckNFT(ctx context.Context, account string) (bool, error)
return tm.Supply == 1 && tm.Decimals == 0, nil
}

func (node *Node) RPCMintSupply(ctx context.Context, account string) decimal.Decimal {
mint, err := node.solana.GetMint(ctx, solana.MPK(account))
if err != nil || mint == nil {
panic(fmt.Errorf("solana.GetMint(%s) => %v %v", account, mint, err))
}
supply := decimal.New(int64(mint.Supply), -int32(mint.Decimals))
return supply
}

func (node *Node) RPCGetMinimumBalanceForRentExemption(ctx context.Context, dataSize uint64) (uint64, error) {
key := fmt.Sprintf("getMinimumBalanceForRentExemption:%d", dataSize)
value, err := node.store.ReadCache(ctx, key)
Expand Down
14 changes: 14 additions & 0 deletions solana/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ func (node *Node) checkTransaction(ctx context.Context, act *mtg.Action, assetId
return ""
}

if !common.CheckTestEnvironment(ctx) {
da, err := node.store.ReadDeployedAsset(ctx, assetId)
if err != nil {
panic(err)
}
if da != nil {
supply := node.RPCMintSupply(ctx, da.Address)
balance = node.getMtgAssetBalance(ctx, da.AssetId)
if balance.Sub(amt).Cmp(supply) < 0 {
panic(fmt.Errorf("invalid balance of mtg asset %s %s: %s %s %s", da.AssetId, da.Address, balance, amt, supply))
}
}
}

nextId := common.UniqueId(node.group.GenesisId(), traceId)
logger.Printf("node.checkTransaction(%s) => %s", traceId, nextId)
return nextId
Expand Down
Loading