• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版4f9a9929b19fc12ae65ffbcf33dd29aa51b96a60 (tree)
时间2019-11-27 16:28:52
作者Paladz <yzhu101@uott...>
CommiterGitHub

Log Message

Merge branch 'mov' into dust_tx

更改概述

差异

--- a/application/mov/match/match.go
+++ b/application/mov/match/match.go
@@ -3,6 +3,7 @@ package match
33 import (
44 "encoding/hex"
55 "math"
6+ "math/big"
67
78 "github.com/vapor/application/mov/common"
89 "github.com/vapor/application/mov/contract"
@@ -221,11 +222,21 @@ func addMatchTxOutput(txData *types.TxData, txInput *types.TxInput, order *commo
221222 }
222223
223224 func CalcRequestAmount(fromAmount uint64, contractArg *vmutil.MagneticContractArgs) uint64 {
224- return uint64(int64(fromAmount) * contractArg.RatioNumerator / contractArg.RatioDenominator)
225+ res := big.NewInt(0).SetUint64(fromAmount)
226+ res.Mul(res, big.NewInt(contractArg.RatioNumerator)).Quo(res, big.NewInt(contractArg.RatioDenominator))
227+ if !res.IsUint64() {
228+ return 0
229+ }
230+ return res.Uint64()
225231 }
226232
227233 func calcShouldPayAmount(receiveAmount uint64, contractArg *vmutil.MagneticContractArgs) uint64 {
228- return uint64(math.Floor(float64(receiveAmount) * float64(contractArg.RatioDenominator) / float64(contractArg.RatioNumerator)))
234+ res := big.NewInt(0).SetUint64(receiveAmount)
235+ res.Mul(res, big.NewInt(contractArg.RatioDenominator)).Quo(res, big.NewInt(contractArg.RatioNumerator))
236+ if !res.IsUint64() {
237+ return 0
238+ }
239+ return res.Uint64()
229240 }
230241
231242 func calcMaxFeeAmount(shouldPayAmount uint64, maxFeeRate float64) int64 {
@@ -238,7 +249,7 @@ func calcOppositeIndex(size int, selfIdx int) int {
238249
239250 func isMatched(orders []*common.Order) bool {
240251 for i, order := range orders {
241- if opposisteOrder := orders[calcOppositeIndex(len(orders), i)]; 1/order.Rate < opposisteOrder.Rate {
252+ if oppositeOrder := orders[calcOppositeIndex(len(orders), i)]; 1/order.Rate < oppositeOrder.Rate {
242253 return false
243254 }
244255 }
--- a/application/mov/mov_core.go
+++ b/application/mov/mov_core.go
@@ -8,7 +8,6 @@ import (
88 "github.com/vapor/consensus/segwit"
99 dbm "github.com/vapor/database/leveldb"
1010 "github.com/vapor/errors"
11- "github.com/vapor/math/checked"
1211 "github.com/vapor/protocol/bc"
1312 "github.com/vapor/protocol/bc/types"
1413 )
@@ -27,7 +26,7 @@ var (
2726 errNumeratorOfRatioIsOverflow = errors.New("ratio numerator of contract args product input amount is overflow")
2827 errLengthOfInputIsIncorrect = errors.New("length of matched tx input is not equals to actual matched tx input")
2928 errSpendOutputIDIsIncorrect = errors.New("spend output id of matched tx is not equals to actual matched tx")
30- errRequestAmountLessThenOne = errors.New("request amount of order less than one")
29+ errRequestAmountMath = errors.New("request amount of order less than one or big than max of int64")
3130 )
3231
3332 // MovCore represent the core logic of the match module, which include generate match transactions before packing the block,
@@ -233,12 +232,8 @@ func validateMagneticContractArgs(fromAmount uint64, program []byte) error {
233232 return errRatioOfTradeLessThanZero
234233 }
235234
236- if _, ok := checked.MulInt64(int64(fromAmount), contractArgs.RatioNumerator); !ok {
237- return errNumeratorOfRatioIsOverflow
238- }
239-
240235 if match.CalcRequestAmount(fromAmount, contractArgs) < 1 {
241- return errRequestAmountLessThenOne
236+ return errRequestAmountMath
242237 }
243238 return nil
244239 }
--- a/application/mov/mov_core_test.go
+++ b/application/mov/mov_core_test.go
@@ -389,17 +389,17 @@ func TestValidateBlock(t *testing.T) {
389389 wantError: errRatioOfTradeLessThanZero,
390390 },
391391 {
392- desc: "ratio numerator product input amount is overflow",
392+ desc: "want amount is overflow",
393393 block: &types.Block{
394394 Transactions: []*types.Tx{
395395 types.NewTx(types.TxData{
396396 Inputs: []*types.TxInput{types.NewSpendInput(nil, *mock.Btc2EthOrders[0].Utxo.SourceID, *mock.Btc2EthOrders[0].FromAssetID, mock.Btc2EthOrders[0].Utxo.Amount, mock.Btc2EthOrders[0].Utxo.SourcePos, []byte{0x51})},
397- Outputs: []*types.TxOutput{types.NewIntraChainOutput(*mock.Btc2EthOrders[0].FromAssetID, mock.Btc2EthOrders[0].Utxo.Amount, mock.MustCreateP2WMCProgram(mock.ETH, testutil.MustDecodeHexString("51"), math.MaxInt64, 10))},
397+ Outputs: []*types.TxOutput{types.NewIntraChainOutput(*mock.Btc2EthOrders[0].FromAssetID, mock.Btc2EthOrders[0].Utxo.Amount, mock.MustCreateP2WMCProgram(mock.ETH, testutil.MustDecodeHexString("51"), math.MaxInt64, 1))},
398398 }),
399399 },
400400 },
401401 verifyResults: []*bc.TxVerifyResult{{StatusFail: false}},
402- wantError: errNumeratorOfRatioIsOverflow,
402+ wantError: errRequestAmountMath,
403403 },
404404 }
405405
--- a/protocol/vm/numeric.go
+++ b/protocol/vm/numeric.go
@@ -2,6 +2,7 @@ package vm
22
33 import (
44 "math"
5+ "math/big"
56
67 "github.com/vapor/math/checked"
78 )
@@ -457,3 +458,36 @@ func opWithin(vm *virtualMachine) error {
457458 }
458459 return vm.pushBool(x >= min && x < max, true)
459460 }
461+
462+func opMulFraction(vm *virtualMachine) error {
463+ if err := vm.applyCost(8); err != nil {
464+ return err
465+ }
466+
467+ z, err := vm.popInt64(true)
468+ if err != nil {
469+ return err
470+ }
471+
472+ if z == 0 {
473+ return ErrDivZero
474+ }
475+
476+ y, err := vm.popInt64(true)
477+ if err != nil {
478+ return err
479+ }
480+
481+ x, err := vm.popInt64(true)
482+ if err != nil {
483+ return err
484+ }
485+
486+ res := big.NewInt(x)
487+ res.Mul(res, big.NewInt(y)).Quo(res, big.NewInt(z))
488+ if !res.IsInt64() {
489+ return ErrRange
490+ }
491+
492+ return vm.pushInt64(res.Int64(), true)
493+}
--- a/protocol/vm/numeric_test.go
+++ b/protocol/vm/numeric_test.go
@@ -446,6 +446,40 @@ func TestNumericOps(t *testing.T) {
446446 deferredCost: -18,
447447 dataStack: [][]byte{{1}},
448448 },
449+ }, {
450+ op: OP_MULFRACTION,
451+ startVM: &virtualMachine{
452+ runLimit: 50000,
453+ dataStack: [][]byte{{15}, {2}, {3}},
454+ },
455+ wantVM: &virtualMachine{
456+ runLimit: 49992,
457+ deferredCost: -18,
458+ dataStack: [][]byte{{10}},
459+ },
460+ }, {
461+ op: OP_MULFRACTION,
462+ startVM: &virtualMachine{
463+ runLimit: 50000,
464+ dataStack: [][]byte{Int64Bytes(-654), {3}, {2}},
465+ },
466+ wantVM: &virtualMachine{
467+ runLimit: 49992,
468+ deferredCost: -18,
469+ dataStack: [][]byte{Int64Bytes(-981)},
470+ },
471+ }, {
472+ op: OP_MULFRACTION,
473+ startVM: &virtualMachine{
474+ runLimit: 50000,
475+ dataStack: [][]byte{Int64Bytes(-654), {3}, {}},
476+ },
477+ wantVM: &virtualMachine{
478+ runLimit: 49992,
479+ deferredCost: -18,
480+ dataStack: [][]byte{},
481+ },
482+ wantErr: ErrDivZero,
449483 }}
450484
451485 numops := []Op{
@@ -453,6 +487,7 @@ func TestNumericOps(t *testing.T) {
453487 OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_LSHIFT, OP_RSHIFT, OP_BOOLAND,
454488 OP_BOOLOR, OP_NUMEQUAL, OP_NUMEQUALVERIFY, OP_NUMNOTEQUAL, OP_LESSTHAN,
455489 OP_LESSTHANOREQUAL, OP_GREATERTHAN, OP_GREATERTHANOREQUAL, OP_MIN, OP_MAX, OP_WITHIN,
490+ OP_MULFRACTION,
456491 }
457492
458493 for _, op := range numops {
@@ -536,6 +571,14 @@ func TestRangeErrs(t *testing.T) {
536571 {fmt.Sprintf("%d 1 LSHIFT", int64(math.MinInt64)), true},
537572 {fmt.Sprintf("%d 1 LSHIFT", int64(math.MinInt64)/2), false},
538573 {fmt.Sprintf("%d 2 LSHIFT", int64(math.MinInt64)/2), true},
574+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MaxInt64)/2-1, 2, 1), false},
575+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MaxInt64)/2+1, 2, 1), true},
576+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MinInt64)/2+1, 2, 1), false},
577+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MinInt64)/2-1, 2, 1), true},
578+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MaxInt64), 3, 2), true},
579+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MaxInt64), 2, 3), false},
580+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MinInt64), 3, 2), true},
581+ {fmt.Sprintf("%d %d %d MULFRACTION", int64(math.MinInt64), 2, 3), false},
539582 }
540583
541584 for i, c := range cases {
--- a/protocol/vm/ops.go
+++ b/protocol/vm/ops.go
@@ -194,6 +194,7 @@ const (
194194 OP_MIN Op = 0xa3
195195 OP_MAX Op = 0xa4
196196 OP_WITHIN Op = 0xa5
197+ OP_MULFRACTION Op = 0xa6
197198
198199 OP_SHA256 Op = 0xa8
199200 OP_SHA3 Op = 0xaa
@@ -300,6 +301,7 @@ var (
300301 OP_MIN: {OP_MIN, "MIN", opMin},
301302 OP_MAX: {OP_MAX, "MAX", opMax},
302303 OP_WITHIN: {OP_WITHIN, "WITHIN", opWithin},
304+ OP_MULFRACTION: {OP_MULFRACTION, "MULFRACTION", opMulFraction},
303305
304306 OP_SHA256: {OP_SHA256, "SHA256", opSha256},
305307 OP_SHA3: {OP_SHA3, "SHA3", opSha3},
--- a/protocol/vm/vmutil/script.go
+++ b/protocol/vm/vmutil/script.go
@@ -200,10 +200,9 @@ func P2WMCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error) {
200200 // PICK [... exchangeAmount sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset exchangeAmount]
201201 // 3 [... exchangeAmount sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset exchangeAmount 3]
202202 // ROLL [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset exchangeAmount ratioDenominator]
203-// MUL [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset (exchangeAmount * ratioDenominator)]
204-// 2 [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset (exchangeAmount * ratioDenominator) 2]
205-// ROLL [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset (exchangeAmount * ratioDenominator) ratioNumerator]
206-// DIV [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount]
203+// 3 [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset exchangeAmount ratioDenominator 3]
204+// ROLL [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset exchangeAmount ratioDenominator ratioNumerator]
205+// MULFRACTION [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount]
207206 // AMOUNT [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount]
208207 // OVER [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount actualAmount]
209208 // 0 [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount actualAmount 0]
@@ -244,10 +243,9 @@ func P2WMCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error) {
244243 // AMOUNT [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset valueAmount]
245244 // 2 [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset valueAmount 2]
246245 // ROLL [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset valueAmount ratioNumerator]
247-// MUL [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset (valueAmount * ratioNumerator)]
248-// 2 [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset (valueAmount * ratioNumerator) 2]
249-// ROLL [... sellerKey standardProgram sellerProgram requestedAsset (valueAmount * ratioNumerator) ratioDenominator]
250-// DIV [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount]
246+// 3 [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset valueAmount ratioNumerator 3]
247+// ROLL [... sellerKey standardProgram sellerProgram requestedAsset valueAmount ratioNumerator ratioDenominator]
248+// MULFRACTION [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount]
251249 // DUP [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount requestedAmount]
252250 // 0 [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount requestedAmount 0]
253251 // GREATERTHAN [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount (requestedAmount > 0)]
@@ -299,9 +297,9 @@ func P2MCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error) {
299297 builder.AddOp(vm.OP_PICK)
300298 builder.AddOp(vm.OP_3)
301299 builder.AddOp(vm.OP_ROLL)
302- builder.AddOp(vm.OP_MUL)
303- builder.AddOp(vm.OP_ROT)
304- builder.AddOp(vm.OP_DIV)
300+ builder.AddOp(vm.OP_3)
301+ builder.AddOp(vm.OP_ROLL)
302+ builder.AddOp(vm.OP_MULFRACTION)
305303 builder.AddOp(vm.OP_AMOUNT)
306304 builder.AddOp(vm.OP_OVER)
307305 builder.AddOp(vm.OP_0)
@@ -339,9 +337,9 @@ func P2MCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error) {
339337 builder.SetJumpTarget(1)
340338 builder.AddOp(vm.OP_AMOUNT)
341339 builder.AddOp(vm.OP_ROT)
342- builder.AddOp(vm.OP_MUL)
343- builder.AddOp(vm.OP_ROT)
344- builder.AddOp(vm.OP_DIV)
340+ builder.AddOp(vm.OP_3)
341+ builder.AddOp(vm.OP_ROLL)
342+ builder.AddOp(vm.OP_MULFRACTION)
345343 builder.AddOp(vm.OP_DUP)
346344 builder.AddOp(vm.OP_0)
347345 builder.AddOp(vm.OP_GREATERTHAN)
Show on old repository browser