Skip to content

Commit

Permalink
fix getongbalance bug (#1379)
Browse files Browse the repository at this point in the history

Co-authored-by: lucas <[email protected]>
  • Loading branch information
lucas7788 and lucas authored Nov 4, 2021
1 parent e120178 commit cf47852
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 42 deletions.
4 changes: 2 additions & 2 deletions smartcontract/service/native/ong/ong.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func RegisterOngContract(native *native.NativeService) {

func OngInit(native *native.NativeService) ([]byte, error) {
contract := native.ContextRef.CurrentContext().ContractAddress
amount, err := utils.GetStorageUInt64(native.CacheDB, ont.GenTotalSupplyKey(contract))
amount, err := utils.GetNativeTokenBalance(native.CacheDB, ont.GenTotalSupplyKey(contract))
if err != nil {
return utils.BYTE_FALSE, err
}

if amount > 0 {
if !amount.IsZero() {
return utils.BYTE_FALSE, errors.NewErr("Init ong has been completed!")
}
addr := common.Address{}
Expand Down
65 changes: 45 additions & 20 deletions smartcontract/service/native/ont/ont.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,21 @@ func grantOng(native *native.NativeService, contract, address common.Address, ba
if balance != 0 {
value := utils.CalcUnbindOng(balance, startOffset, endOffset)

args, amount, err := getApproveArgs(native, contract, utils.OngContractAddress, address, value)
args, amount, method, err := getApproveArgs(native, contract, utils.OngContractAddress, address, value)
if err != nil {
return err
}
if _, err := native.NativeCall(utils.OngContractAddress, "approve", args); err != nil {
if _, err := native.NativeCall(utils.OngContractAddress, method, args); err != nil {
return err
}

if endOffset > config.GetOntHolderUnboundDeadline() {
if address != utils.GovernanceContractAddress {
args, err := getTransferFromArgs(address, contract, address, amount)
args, method, err := getTransferFromArgs(address, contract, address, amount)
if err != nil {
return err
}
if _, err := native.NativeCall(utils.OngContractAddress, "transferFrom", args); err != nil {
if _, err := native.NativeCall(utils.OngContractAddress, method, args); err != nil {
return err
}
}
Expand Down Expand Up @@ -472,22 +473,32 @@ func unboundOngToGovernance(native *native.NativeService) error {
return nil
}

func getApproveArgs(native *native.NativeService, contract, ongContract, address common.Address, value uint64) ([]byte, uint64, error) {
func getApproveArgs(native *native.NativeService, contract, ongContract, address common.Address, value uint64) ([]byte, cstates.NativeTokenBalance, string, error) {
bf := common.NewZeroCopySink(nil)
approve := TransferState{
approve := TransferStateV2{
From: contract,
To: address,
Value: value,
Value: cstates.NativeTokenBalanceFromInteger(value),
}

stateValue, err := utils.GetStorageUInt64(native.CacheDB, GenApproveKey(ongContract, approve.From, approve.To))
stateValue, err := utils.GetNativeTokenBalance(native.CacheDB, GenApproveKey(ongContract, approve.From, approve.To))
if err != nil {
return nil, 0, err
return nil, cstates.NativeTokenBalance{}, "", err
}
approve.Value = approve.Value.Add(stateValue)
var method string
if approve.Value.IsFloat() {
approve.Serialization(bf)
method = "approveV2"
} else {
approveOld := TransferState{
From: contract,
To: address,
Value: approve.Value.MustToInteger64(),
}
approveOld.Serialization(bf)
method = "approve"
}

approve.Value += stateValue
approve.Serialization(bf)
return bf.Bytes(), approve.Value, nil
return bf.Bytes(), approve.Value, method, nil
}

func getTransferArgs(contract, address common.Address, value uint64) ([]byte, error) {
Expand All @@ -503,17 +514,31 @@ func getTransferArgs(contract, address common.Address, value uint64) ([]byte, er
return bf.Bytes(), nil
}

func getTransferFromArgs(sender, from, to common.Address, value uint64) ([]byte, error) {
func getTransferFromArgs(sender, from, to common.Address, value cstates.NativeTokenBalance) ([]byte, string, error) {
sink := common.NewZeroCopySink(nil)
param := TransferFrom{
param := TransferFromStateV2{
Sender: sender,
TransferState: TransferState{
TransferStateV2: TransferStateV2{
From: from,
To: to,
Value: value,
},
}

param.Serialization(sink)
return sink.Bytes(), nil
var method string
if value.IsFloat() {
param.Serialization(sink)
method = "transferFromV2"
} else {
paramOld := TransferFrom{
Sender: sender,
TransferState: TransferState{
From: from,
To: to,
Value: value.MustToInteger64(),
},
}
paramOld.Serialization(sink)
method = "transferFrom"
}
return sink.Bytes(), method, nil
}
4 changes: 2 additions & 2 deletions smartcontract/service/native/ont/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func reduceFromBalance(native *native.NativeService, fromKey []byte, value cstat
newFromBalance, err := fromBalance.Sub(value)
if err != nil {
addr, _ := common.AddressParseFromBytes(fromKey[20:])
return cstates.NativeTokenBalance{}, fmt.Errorf("[Transfer] balance insufficient. contract:%s, account:%s, err: %v",
native.ContextRef.CurrentContext().ContractAddress.ToHexString(), addr.ToBase58(), err)
return cstates.NativeTokenBalance{}, fmt.Errorf("[Transfer] balance insufficient. contract:%s, account:%s, fromBalance:%s,value:%s, err: %v",
native.ContextRef.CurrentContext().ContractAddress.ToHexString(), addr.ToBase58(), fromBalance.String(), value.String(), err)
}

if newFromBalance.IsZero() {
Expand Down
37 changes: 36 additions & 1 deletion smartcontract/service/native/testsuite/ont_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func setOngBalance(db *storage.CacheDB, addr common.Address, value uint64) {
}

func ongBalanceOf(native *native.NativeService, addr common.Address) uint64 {
origin := native.ContextRef.CurrentContext().ContractAddress
native.ContextRef.CurrentContext().ContractAddress = utils.OngContractAddress
defer func() {
native.ContextRef.CurrentContext().ContractAddress = origin
}()
sink := common.NewZeroCopySink(nil)
utils.EncodeAddress(sink, addr)
native.Input = sink.Bytes()
Expand All @@ -76,7 +80,11 @@ func ongBalanceOf(native *native.NativeService, addr common.Address) uint64 {
}

func ongBalanceOfV2(native *native.NativeService, addr common.Address) bigint.Int {
origin := native.ContextRef.CurrentContext().ContractAddress
native.ContextRef.CurrentContext().ContractAddress = utils.OngContractAddress
defer func() {
native.ContextRef.CurrentContext().ContractAddress = origin
}()
sink := common.NewZeroCopySink(nil)
utils.EncodeAddress(sink, addr)
native.Input = sink.Bytes()
Expand All @@ -86,7 +94,11 @@ func ongBalanceOfV2(native *native.NativeService, addr common.Address) bigint.In
}

func ongAllowance(native *native.NativeService, from, to common.Address) uint64 {
origin := native.ContextRef.CurrentContext().ContractAddress
native.ContextRef.CurrentContext().ContractAddress = utils.OngContractAddress
defer func() {
native.ContextRef.CurrentContext().ContractAddress = origin
}()
sink := common.NewZeroCopySink(nil)
utils.EncodeAddress(sink, from)
utils.EncodeAddress(sink, to)
Expand All @@ -97,7 +109,11 @@ func ongAllowance(native *native.NativeService, from, to common.Address) uint64
}

func ongAllowanceV2(native *native.NativeService, from, to common.Address) bigint.Int {
origin := native.ContextRef.CurrentContext().ContractAddress
native.ContextRef.CurrentContext().ContractAddress = utils.OngContractAddress
defer func() {
native.ContextRef.CurrentContext().ContractAddress = origin
}()
sink := common.NewZeroCopySink(nil)
utils.EncodeAddress(sink, from)
utils.EncodeAddress(sink, to)
Expand All @@ -107,6 +123,20 @@ func ongAllowanceV2(native *native.NativeService, from, to common.Address) bigin
return bigint.New(val)
}

func ongTransferFromV2(native *native.NativeService, spender, from, to common.Address, amt states.NativeTokenBalance) error {
origin := native.ContextRef.CurrentContext().ContractAddress
native.ContextRef.CurrentContext().ContractAddress = utils.OngContractAddress
defer func() {
native.ContextRef.CurrentContext().ContractAddress = origin
}()
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)
state := &ont.TransferFromStateV2{Sender: spender, TransferStateV2: ont.TransferStateV2{From: from, To: to, Value: amt}}
native.Input = common.SerializeToBytes(state)

_, err := ong.OngTransferFromV2(native)
return err
}

func ontTotalAllowance(native *native.NativeService, addr common.Address) int {
sink := common.NewZeroCopySink(nil)
utils.EncodeAddress(sink, addr)
Expand Down Expand Up @@ -350,10 +380,15 @@ func TestGovernanceUnboundV2(t *testing.T) {
setOngBalance(native.CacheDB, utils.OntContractAddress, constants.ONG_TOTAL_SUPPLY)

native.Time = constants.GENESIS_BLOCK_TIMESTAMP + 1

assert.Nil(t, ontTransferV2(native, testAddr, testAddr, 1))
assert.Equal(t, ongAllowanceV2(native, utils.OntContractAddress, testAddr).String(), big.NewInt(5000000000*states.ScaleFactor).String())
native.Time = native.Time + 100000
native.Height = config.GetAddDecimalsHeight()
assert.Nil(t, ontTransferV2(native, testAddr, testAddr, constants.ONT_TOTAL_SUPPLY_V2/2))

assert.Nil(t, ongTransferFromV2(native, testAddr, utils.OntContractAddress, testAddr, states.NativeTokenBalance{Balance: bigint.New(1)}))
native.Time = native.Time + 100000
assert.Nil(t, ontTransferV2(native, testAddr, testAddr, 1))
return nil, nil
})

Expand Down
15 changes: 0 additions & 15 deletions smartcontract/service/native/utils/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,6 @@ func GetNativeTokenBalance(cacheDB *storage.CacheDB, key []byte) (cstates.Native
return cstates.NativeTokenBalanceFromStorageItem(item)
}

func GetStorageUInt64(cacheDB *storage.CacheDB, key []byte) (uint64, error) {
item, err := GetStorageItem(cacheDB, key)
if err != nil {
return 0, err
}
if item == nil {
return 0, nil
}
v, err := serialization.ReadUint64(bytes.NewBuffer(item.Value))
if err != nil {
return 0, err
}
return v, nil
}

func GetStorageUInt32(cacheDB *storage.CacheDB, key []byte) (uint32, error) {
item, err := GetStorageItem(cacheDB, key)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions txnpool/common/txnpool_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ func (n OrderByNetWorkFee) Less(i, j int) bool { return n[j].Tx.GasPrice < n[i].
func GetOngBalance(account common.Address) (*big.Int, error) {
cache := ledger.DefLedger.GetStore().GetCacheDB()
balanceKey := ont.GenBalanceKey(utils.OngContractAddress, account)
amount, err := utils.GetStorageUInt64(cache, balanceKey)
amount, err := utils.GetNativeTokenBalance(cache, balanceKey)
if err != nil {
return nil, err
}

return big.NewInt(0).SetUint64(amount), nil
return amount.ToBigInt(), nil
}

func ShowTraceLog(format string, a ...interface{}) {
Expand Down

0 comments on commit cf47852

Please sign in to comment.