Skip to content

Commit

Permalink
Fix potential negative write pool
Browse files Browse the repository at this point in the history
  • Loading branch information
peterlimg committed Jun 4, 2023
1 parent 6cf4396 commit 6b88afd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions code/go/0chain.net/smartcontract/storagesc/writepool.go
Expand Up @@ -2,6 +2,7 @@ package storagesc

import (
"encoding/json"
"fmt"

cstate "0chain.net/chaincore/chain/state"
"0chain.net/chaincore/state"
Expand Down Expand Up @@ -80,21 +81,27 @@ func (ssc *StorageSmartContract) writePoolLock(
}

if allocation.Finalized || allocation.Canceled {
return "", common.NewError("write_pool_unlock_failed",
return "", common.NewError("write_pool_lock_failed",
"can't lock tokens with a finalized or cancelled allocation")

}

allocation.WritePool, err = currency.AddCoin(allocation.WritePool, txn.Value)
i, _ := txn.Value.Int64()
if err != nil {
return "", common.NewError("write_pool_lock_failed", fmt.Sprintf("write pool token overflow: %v", err))
}

i, err := txn.Value.Int64()
if err != nil {
return "", common.NewError("write_pool_lock_failed", fmt.Sprintf("invalid lock value: %v", err))
}

balances.EmitEvent(event.TypeStats, event.TagLockWritePool, allocation.ID, event.WritePoolLock{
Client: txn.ClientID,
AllocationId: allocation.ID,
Amount: i,
})
if err != nil {
return "", common.NewError("write_pool_unlock_failed", err.Error())
}

if err := allocation.saveUpdatedStakes(balances); err != nil {
return "", common.NewError("write_pool_lock_failed", err.Error())
}
Expand Down

0 comments on commit 6b88afd

Please sign in to comment.