OSDN Git Service

Feat(BVM): change op sub (#1869)
authorxuexiansong <s.xue.xian.c@gmail.com>
Wed, 7 Apr 2021 13:47:22 +0000 (21:47 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Apr 2021 13:47:22 +0000 (21:47 +0800)
protocol/vm/numeric.go
protocol/vm/numeric_test.go

index 72906a1..dce00d1 100644 (file)
@@ -181,19 +181,22 @@ func opSub(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.SubInt64(x, y)
-       if !ok {
+
+       if x.Sub(x, y); x.Sign() < 0 {
                return ErrRange
        }
-       return vm.pushInt64(res, true)
+
+       return vm.pushBigInt(x, true)
 }
 
 func opMul(vm *virtualMachine) error {
index 7517fbb..e66df5d 100644 (file)
@@ -909,6 +909,73 @@ func Test_op1Sub(t *testing.T) {
        }
 }
 
+func Test_opSub(t *testing.T) {
+       type args struct {
+               vm *virtualMachine
+       }
+       tests := []struct {
+               name    string
+               args    args
+               want    [][]byte
+               wantErr bool
+       }{
+               {
+                       name: "Test 2 - 1 = 1",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{0x02}, {0x01}},
+                               },
+                       },
+                       want:    [][]byte{{0x01}},
+                       wantErr: false,
+               },
+               {
+                       name: "Test that two bytes number become one byte number",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01}},
+                               },
+                       },
+                       want:    [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
+                       wantErr: false,
+               },
+               {
+                       name: "Test for 0 - 1 got error",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{{}, {0x01}},
+                               },
+                       },
+                       want:    nil,
+                       wantErr: true,
+               },
+               {
+                       name: "Test for -1 - 1 got error",
+                       args: args{
+                               vm: &virtualMachine{
+                                       runLimit:  50000,
+                                       dataStack: [][]byte{mocks.U256NumNegative1, {0x01}},
+                               },
+                       },
+                       want:    nil,
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if err := opSub(tt.args.vm); (err != nil) != tt.wantErr {
+                               t.Errorf("op1Sub() error = %v, wantErr %v", err, tt.wantErr)
+                       }
+                       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_op2Div(t *testing.T) {
        type args struct {
                vm *virtualMachine