types: Less/GreaterThan for bigints

This commit is contained in:
Łukasz Magiera 2019-09-23 19:11:44 +02:00
parent 182b754aa2
commit 5ce15bfaf4
10 changed files with 26 additions and 15 deletions

View File

@ -247,7 +247,7 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
futurePower := types.BigAdd(self.Power, mi.SectorSize)
collateralRequired := CollateralForPower(futurePower)
if types.BigCmp(act.Balance, collateralRequired) < 0 {
if act.Balance.LessThan(collateralRequired) {
return nil, aerrors.New(3, "not enough collateral")
}
@ -328,11 +328,11 @@ func (sma StorageMinerActor) SubmitPoSt(act *types.Actor, vmctx types.VMContext,
//TODO temporary sector failure fees
msgVal := vmctx.Message().Value
if types.BigCmp(msgVal, feesRequired) < 0 {
if msgVal.LessThan(feesRequired) {
return nil, aerrors.New(2, "not enough funds to pay post submission fees")
}
if types.BigCmp(msgVal, feesRequired) > 0 {
if msgVal.GreaterThan(feesRequired) {
_, err := vmctx.Send(vmctx.Message().From, 0,
types.BigSub(msgVal, feesRequired), nil)
if err != nil {
@ -719,7 +719,7 @@ func (sma StorageMinerActor) SlashConsensusFault(act *types.Actor, vmctx types.V
}
slashedCollateral := params.SlashedCollateral
if types.BigCmp(slashedCollateral, act.Balance) < 0 {
if slashedCollateral.LessThan(act.Balance) {
slashedCollateral = act.Balance
}

View File

@ -173,12 +173,12 @@ func (pca PaymentChannelActor) UpdateChannelState(act *types.Actor, vmctx types.
ls.Redeemed = sv.Amount
newSendBalance := types.BigAdd(self.ToSend, balanceDelta)
if types.BigCmp(newSendBalance, types.NewInt(0)) < 0 {
if newSendBalance.LessThan(types.NewInt(0)) {
// TODO: is this impossible?
return nil, aerrors.New(9, "voucher would leave channel balance negative")
}
if types.BigCmp(newSendBalance, act.Balance) > 0 {
if newSendBalance.GreaterThan(act.Balance) {
return nil, aerrors.New(10, "not enough funds in channel to cover voucher")
}

View File

@ -74,7 +74,7 @@ func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.V
return nil, err
}
if types.BigCmp(vmctx.Message().Value, reqColl) < 0 {
if vmctx.Message().Value.LessThan(reqColl) {
return nil, aerrors.Newf(1, "not enough funds passed to cover required miner collateral (needed %s, got %s)", reqColl, vmctx.Message().Value)
}

View File

@ -91,7 +91,7 @@ func (h *Handler) validateVouchers(ctx context.Context, deal MinerDeal) error {
// TODO: make sure that current laneStatus.Amount == 0
if types.BigCmp(voucher.Amount, deal.Proposal.TotalPrice) < 0 {
if voucher.Amount.LessThan(deal.Proposal.TotalPrice) {
return xerrors.Errorf("validating payment voucher %d: not enough funds in the voucher", i)
}

View File

@ -85,6 +85,16 @@ func (bi *BigInt) Nil() bool {
return bi.Int == nil
}
// LessThan returns true if bi < o
func (bi *BigInt) LessThan(o BigInt) bool {
return BigCmp(*bi, o) < 0
}
// LessThan returns true if bi > o
func (bi *BigInt) GreaterThan(o BigInt) bool {
return BigCmp(*bi, o) > 0
}
func (bi *BigInt) MarshalJSON() ([]byte, error) {
return json.Marshal(bi.String())
}

View File

@ -171,5 +171,6 @@ func PowerCmp(eproof ElectionProof, mpow, totpow BigInt) bool {
top := BigMul(rden, mpow)
out := BigDiv(top, totpow)
return BigCmp(BigFromBytes(h[:]), out) < 0
hp := BigFromBytes(h[:])
return hp.LessThan(out)
}

View File

@ -167,7 +167,7 @@ func (vmc *VMContext) GasUsed() types.BigInt {
func (vmc *VMContext) ChargeGas(amount uint64) aerrors.ActorError {
toUse := types.NewInt(amount)
vmc.gasUsed = types.BigAdd(vmc.gasUsed, toUse)
if types.BigCmp(vmc.gasUsed, vmc.gasAvailable) > 0 {
if vmc.gasUsed.GreaterThan(vmc.gasAvailable) {
return aerrors.Newf(outOfGasErrCode, "not enough gas: used=%s, available=%s", vmc.gasUsed, vmc.gasAvailable)
}
return nil
@ -451,7 +451,7 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
totalCost := types.BigAdd(gascost, msg.Value)
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
if fromActor.Balance.LessThan(totalCost) {
return nil, xerrors.Errorf("not enough funds (%s < %s)", fromActor.Balance, totalCost)
}
if err := DeductFunds(fromActor, gascost); err != nil {
@ -619,7 +619,7 @@ func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params [
}
func DeductFunds(act *types.Actor, amt types.BigInt) error {
if types.BigCmp(act.Balance, amt) < 0 {
if act.Balance.LessThan(amt) {
return fmt.Errorf("not enough funds")
}

View File

@ -291,7 +291,7 @@ var paychVoucherBestSpendableCmd = &cli.Command{
return err
}
if spendable {
if best == nil || types.BigCmp(v.Amount, best.Amount) > 0 {
if best == nil || v.Amount.GreaterThan(best.Amount) {
best = v
}
}

View File

@ -159,7 +159,7 @@ func (pm *Manager) CheckVoucherValid(ctx context.Context, ch address.Address, sv
// TODO: also account for vouchers on other lanes we've received
newTotal := types.BigAdd(sendAmount, pca.ToSend)
if types.BigCmp(act.Balance, newTotal) < 0 {
if act.Balance.LessThan(newTotal) {
return fmt.Errorf("not enough funds in channel to cover voucher")
}

View File

@ -226,7 +226,7 @@ func (ps *Store) AddVoucher(ch address.Address, sv *types.SignedVoucher, proof [
}
delta := types.BigSub(sv.Amount, bestAmount)
if types.BigCmp(minDelta, delta) > 0 {
if minDelta.GreaterThan(delta) {
return delta, xerrors.Errorf("addVoucher: supplied token amount too low; minD=%s, D=%s; bestAmt=%s; v.Amt=%s", minDelta, delta, bestAmount, sv.Amount)
}