• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revision095d46e44f6934f081c1993e8355dab1e98f665f (tree)
Time2021-04-08 16:24:53
Authorsongxuexian <s.xue.xian.c@gmai...>
Commitersongxuexian

Log Message

Merge branch 'btm2.0' into btm2.0_vm_numeric_cmp

Change Summary

Incremental Difference

--- a/protocol/vm/numeric.go
+++ b/protocol/vm/numeric.go
@@ -250,30 +250,22 @@ func opMod(vm *virtualMachine) error {
250250 if err != nil {
251251 return err
252252 }
253- y, err := vm.popInt64(true)
253+
254+ y, err := vm.popBigInt(true)
254255 if err != nil {
255256 return err
256257 }
257- x, err := vm.popInt64(true)
258+
259+ x, err := vm.popBigInt(true)
258260 if err != nil {
259261 return err
260262 }
261- if y == 0 {
262- return ErrDivZero
263- }
264263
265- res, ok := checked.ModInt64(x, y)
266- if !ok {
267- return ErrRange
268- }
269-
270- // Go's modulus operator produces the wrong result for mixed-sign
271- // operands
272- if res != 0 && (x >= 0) != (y >= 0) {
273- res += y
264+ if y.IsZero() {
265+ return ErrDivZero
274266 }
275267
276- return vm.pushInt64(res, true)
268+ return vm.pushBigInt(x.Mod(x, y), true)
277269 }
278270
279271 func opLshift(vm *virtualMachine) error {
--- a/protocol/vm/numeric_test.go
+++ b/protocol/vm/numeric_test.go
@@ -209,17 +209,6 @@ func TestNumericOps(t *testing.T) {
209209 op: OP_MOD,
210210 startVM: &virtualMachine{
211211 runLimit: 50000,
212- dataStack: [][]byte{Int64Bytes(-12), {10}},
213- },
214- wantVM: &virtualMachine{
215- runLimit: 49992,
216- deferredCost: -16,
217- dataStack: [][]byte{{8}},
218- },
219- }, {
220- op: OP_MOD,
221- startVM: &virtualMachine{
222- runLimit: 50000,
223212 dataStack: [][]byte{{2}, {0}},
224213 },
225214 wantErr: ErrDivZero,
@@ -1347,3 +1336,107 @@ func Test_opAdd(t *testing.T) {
13471336 })
13481337 }
13491338 }
1339+
1340+func Test_opMod(t *testing.T) {
1341+ type args struct {
1342+ vm *virtualMachine
1343+ }
1344+ tests := []struct {
1345+ name string
1346+ args args
1347+ want [][]byte
1348+ wantErr bool
1349+ }{
1350+ {
1351+ name: "Test 2 mod 2 = 0",
1352+ args: args{
1353+ vm: &virtualMachine{
1354+ runLimit: 50000,
1355+ dataStack: [][]byte{{0x02},{0x02}},
1356+ },
1357+ },
1358+ want: [][]byte{{}},
1359+ wantErr: false,
1360+ },
1361+ {
1362+ name: "Test 2 mod 1 = 0",
1363+ args: args{
1364+ vm: &virtualMachine{
1365+ runLimit: 50000,
1366+ dataStack: [][]byte{{0x02},{0x01}},
1367+ },
1368+ },
1369+ want: [][]byte{{}},
1370+ wantErr: false,
1371+ },
1372+ {
1373+ name: "Test 255 mod 4 = 3",
1374+ args: args{
1375+ vm: &virtualMachine{
1376+ runLimit: 50000,
1377+ dataStack: [][]byte{{0xff},{0x04}},
1378+ },
1379+ },
1380+ want: [][]byte{{0x03}},
1381+ wantErr: false,
1382+ },
1383+ {
1384+ name: "Test that two bytes number become one byte number",
1385+ args: args{
1386+ vm: &virtualMachine{
1387+ runLimit: 50000,
1388+ dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},{0x03}},
1389+ },
1390+ },
1391+ want: [][]byte{{0x01}},
1392+ wantErr: false,
1393+ },
1394+ {
1395+ name: "Test for 0 mod 2 got 0",
1396+ args: args{
1397+ vm: &virtualMachine{
1398+ runLimit: 50000,
1399+ dataStack: [][]byte{{},{0x02}},
1400+ },
1401+ },
1402+ want: [][]byte{{}},
1403+ wantErr: false,
1404+ },
1405+ {
1406+ name: "Test for -1 div 2 got error",
1407+ args: args{
1408+ vm: &virtualMachine{
1409+ runLimit: 50000,
1410+ dataStack: [][]byte{mocks.U256NumNegative1,{0x02}},
1411+ },
1412+ },
1413+ want: nil,
1414+ wantErr: true,
1415+ },
1416+ {
1417+ name: "Test for 1 div 0 got error",
1418+ args: args{
1419+ vm: &virtualMachine{
1420+ runLimit: 50000,
1421+ dataStack: [][]byte{{0x01},{}},
1422+ },
1423+ },
1424+ want: nil,
1425+ wantErr: true,
1426+ },
1427+ }
1428+ for _, tt := range tests {
1429+ t.Run(tt.name, func(t *testing.T) {
1430+ if err := opMod(tt.args.vm); err != nil {
1431+ if !tt.wantErr {
1432+ t.Errorf("opMod() error = %v, wantErr %v", err, tt.wantErr)
1433+ } else {
1434+ return
1435+ }
1436+ }
1437+ if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) {
1438+ t.Errorf("opMod() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want)
1439+ }
1440+ })
1441+ }
1442+}
--- a/protocol/vm/vm_test.go
+++ b/protocol/vm/vm_test.go
@@ -67,10 +67,6 @@ func doOKNotOK(t *testing.T, expectOK bool) {
6767 {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, true},
6868 {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-2)}, true},
6969
70- {"NEGATE -1 NUMEQUAL", [][]byte{Int64Bytes(1)}, false},
71- {"NEGATE 1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, false},
72- {"NEGATE 0 NUMEQUAL", [][]byte{Int64Bytes(0)}, false},
73-
7470 {"ABS 1 NUMEQUAL", [][]byte{Int64Bytes(1)}, false},
7571 {"ABS 1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, false},
7672 {"ABS 0 NUMEQUAL", [][]byte{Int64Bytes(0)}, false},
@@ -87,13 +83,7 @@ func doOKNotOK(t *testing.T, expectOK bool) {
8783 {"DIV 2 NUMEQUAL", [][]byte{Int64Bytes(6), Int64Bytes(3)}, false},
8884
8985 {"MOD 0 NUMEQUAL", [][]byte{Int64Bytes(6), Int64Bytes(2)}, false},
90- {"MOD 0 NUMEQUAL", [][]byte{Int64Bytes(-6), Int64Bytes(2)}, false},
91- {"MOD 0 NUMEQUAL", [][]byte{Int64Bytes(6), Int64Bytes(-2)}, false},
92- {"MOD 0 NUMEQUAL", [][]byte{Int64Bytes(-6), Int64Bytes(-2)}, false},
9386 {"MOD 2 NUMEQUAL", [][]byte{Int64Bytes(12), Int64Bytes(10)}, false},
94- {"MOD 8 NUMEQUAL", [][]byte{Int64Bytes(-12), Int64Bytes(10)}, false},
95- {"MOD -8 NUMEQUAL", [][]byte{Int64Bytes(12), Int64Bytes(-10)}, false},
96- {"MOD -2 NUMEQUAL", [][]byte{Int64Bytes(-12), Int64Bytes(-10)}, false},
9787
9888 {"LSHIFT 2 NUMEQUAL", [][]byte{Int64Bytes(1), Int64Bytes(1)}, false},
9989 {"LSHIFT 4 NUMEQUAL", [][]byte{Int64Bytes(1), Int64Bytes(2)}, false},
Show on old repository browser