• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revision8a334b1ab65b102e9d417135e57235adbada29db (tree)
Time2021-04-08 16:02:06
Authorxuexiansong <s.xue.xian.c@gmai...>
CommiterGitHub

Log Message

Feat(BVM): change op mod (#1872)

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,
@@ -1257,3 +1246,107 @@ func Test_opAdd(t *testing.T) {
12571246 })
12581247 }
12591248 }
1249+
1250+func Test_opMod(t *testing.T) {
1251+ type args struct {
1252+ vm *virtualMachine
1253+ }
1254+ tests := []struct {
1255+ name string
1256+ args args
1257+ want [][]byte
1258+ wantErr bool
1259+ }{
1260+ {
1261+ name: "Test 2 mod 2 = 0",
1262+ args: args{
1263+ vm: &virtualMachine{
1264+ runLimit: 50000,
1265+ dataStack: [][]byte{{0x02},{0x02}},
1266+ },
1267+ },
1268+ want: [][]byte{{}},
1269+ wantErr: false,
1270+ },
1271+ {
1272+ name: "Test 2 mod 1 = 0",
1273+ args: args{
1274+ vm: &virtualMachine{
1275+ runLimit: 50000,
1276+ dataStack: [][]byte{{0x02},{0x01}},
1277+ },
1278+ },
1279+ want: [][]byte{{}},
1280+ wantErr: false,
1281+ },
1282+ {
1283+ name: "Test 255 mod 4 = 3",
1284+ args: args{
1285+ vm: &virtualMachine{
1286+ runLimit: 50000,
1287+ dataStack: [][]byte{{0xff},{0x04}},
1288+ },
1289+ },
1290+ want: [][]byte{{0x03}},
1291+ wantErr: false,
1292+ },
1293+ {
1294+ name: "Test that two bytes number become one byte number",
1295+ args: args{
1296+ vm: &virtualMachine{
1297+ runLimit: 50000,
1298+ dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},{0x03}},
1299+ },
1300+ },
1301+ want: [][]byte{{0x01}},
1302+ wantErr: false,
1303+ },
1304+ {
1305+ name: "Test for 0 mod 2 got 0",
1306+ args: args{
1307+ vm: &virtualMachine{
1308+ runLimit: 50000,
1309+ dataStack: [][]byte{{},{0x02}},
1310+ },
1311+ },
1312+ want: [][]byte{{}},
1313+ wantErr: false,
1314+ },
1315+ {
1316+ name: "Test for -1 div 2 got error",
1317+ args: args{
1318+ vm: &virtualMachine{
1319+ runLimit: 50000,
1320+ dataStack: [][]byte{mocks.U256NumNegative1,{0x02}},
1321+ },
1322+ },
1323+ want: nil,
1324+ wantErr: true,
1325+ },
1326+ {
1327+ name: "Test for 1 div 0 got error",
1328+ args: args{
1329+ vm: &virtualMachine{
1330+ runLimit: 50000,
1331+ dataStack: [][]byte{{0x01},{}},
1332+ },
1333+ },
1334+ want: nil,
1335+ wantErr: true,
1336+ },
1337+ }
1338+ for _, tt := range tests {
1339+ t.Run(tt.name, func(t *testing.T) {
1340+ if err := opMod(tt.args.vm); err != nil {
1341+ if !tt.wantErr {
1342+ t.Errorf("opMod() error = %v, wantErr %v", err, tt.wantErr)
1343+ } else {
1344+ return
1345+ }
1346+ }
1347+ if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) {
1348+ t.Errorf("opMod() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want)
1349+ }
1350+ })
1351+ }
1352+}
--- 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