OSDN Git Service

Feat(BVM): change op add (#1862)
authorxuexiansong <s.xue.xian.c@gmail.com>
Wed, 7 Apr 2021 08:37:04 +0000 (16:37 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Apr 2021 08:37:04 +0000 (16:37 +0800)
* Feat(BVM): change op add

* Feat(BVM): change op add 1

protocol/vm/numeric.go
protocol/vm/numeric_test.go
protocol/vm/vm_test.go

index 8af30e5..72906a1 100644 (file)
@@ -158,19 +158,22 @@ func opAdd(vm *virtualMachine) error {
        if err != nil {
                return err
        }
-       y, err := vm.popInt64(true)
+
+       y, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       x, err := vm.popInt64(true)
+
+       x, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       res, ok := checked.AddInt64(x, y)
-       if !ok {
+
+       if x.Add(x, y); x.Sign() < 0 {
                return ErrRange
        }
-       return vm.pushInt64(res, true)
+
+       return vm.pushBigInt(x, true)
 }
 
 func opSub(vm *virtualMachine) error {
index 45f5952..7517fbb 100644 (file)
@@ -91,26 +91,6 @@ func TestNumericOps(t *testing.T) {
                        dataStack: [][]byte{{1}},
                },
        }, {
-               op: OP_2DIV,
-               startVM: &virtualMachine{
-                       runLimit:  50000,
-                       dataStack: [][]byte{Int64Bytes(-2)},
-               },
-               wantVM: &virtualMachine{
-                       runLimit:  49998,
-                       dataStack: [][]byte{Int64Bytes(-1)},
-               },
-       }, {
-               op: OP_2DIV,
-               startVM: &virtualMachine{
-                       runLimit:  50000,
-                       dataStack: [][]byte{Int64Bytes(-1)},
-               },
-               wantVM: &virtualMachine{
-                       runLimit:  49998,
-                       dataStack: [][]byte{Int64Bytes(-1)},
-               },
-       }, {
                op: OP_NEGATE,
                startVM: &virtualMachine{
                        runLimit:  50000,
@@ -933,7 +913,6 @@ func Test_op2Div(t *testing.T) {
        type args struct {
                vm *virtualMachine
        }
-
        tests := []struct {
                name    string
                args    args
@@ -988,9 +967,81 @@ func Test_op2Div(t *testing.T) {
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
                        if err := op2Div(tt.args.vm); err != nil {
-                               if !tt.wantErr{
+                               if !tt.wantErr {
                                        t.Errorf("op2Div() error = %v, wantErr %v", err, tt.wantErr)
-                               }else {
+                               } else {
+                                       return
+                               }
+                       }
+                       if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) {
+                               t.Errorf("op1Sub() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want)
+                       }
+               })
+       }
+}
+
+func Test_opAdd(t *testing.T) {
+       type args struct {
+               vm *virtualMachine
+       }
+
+       tests := []struct {
+               name    string
+               args    args
+               want    [][]byte
+               wantErr bool
+       }{
+               {
+                       name: "Test 2 + 2 = 4",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{0x02}, {0x02}},
+                               },
+                       },
+                       want:    [][]byte{{0x04}},
+                       wantErr: false,
+               },
+               {
+                       name: "Test that one bytes number become two byte number",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x01}},
+                               },
+                       },
+                       want:    [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+                       wantErr: false,
+               },
+               {
+                       name: "Test for 0 + -1 got error",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{}, mocks.U256NumNegative1},
+                               },
+                       },
+                       want:    nil,
+                       wantErr: true,
+               },
+               {
+                       name: "Test for -1 + -1 got error",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{mocks.U256NumNegative1, mocks.U256NumNegative1},
+                               },
+                       },
+                       want:    nil,
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if err := opAdd(tt.args.vm); err != nil {
+                               if !tt.wantErr {
+                                       t.Errorf("op1Sub() error = %v, wantErr %v", err, tt.wantErr)
+                               } else {
                                        return
                                }
                        }
index 71b3f56..8fab176 100644 (file)
@@ -64,8 +64,8 @@ func doOKNotOK(t *testing.T, expectOK bool) {
                {"2DIV 1 NUMEQUAL", [][]byte{Int64Bytes(2)}, false},
                {"2DIV 0 NUMEQUAL", [][]byte{Int64Bytes(1)}, false},
                {"2DIV 0 NUMEQUAL", [][]byte{Int64Bytes(0)}, false},
-               {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, false},
-               {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-2)}, false},
+               {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, true},
+               {"2DIV -1 NUMEQUAL", [][]byte{Int64Bytes(-2)}, true},
 
                {"NEGATE -1 NUMEQUAL", [][]byte{Int64Bytes(1)}, false},
                {"NEGATE 1 NUMEQUAL", [][]byte{Int64Bytes(-1)}, false},