From b276f63892813eb6c4cbd0945437e13afe439e17 Mon Sep 17 00:00:00 2001 From: xuexiansong Date: Wed, 7 Apr 2021 21:47:22 +0800 Subject: [PATCH] Feat(BVM): change op sub (#1869) --- protocol/vm/numeric.go | 13 +++++---- protocol/vm/numeric_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/protocol/vm/numeric.go b/protocol/vm/numeric.go index 72906a16..dce00d10 100644 --- a/protocol/vm/numeric.go +++ b/protocol/vm/numeric.go @@ -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 { diff --git a/protocol/vm/numeric_test.go b/protocol/vm/numeric_test.go index 7517fbba..e66df5dd 100644 --- a/protocol/vm/numeric_test.go +++ b/protocol/vm/numeric_test.go @@ -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 -- 2.11.0