• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revisiondad707da5462c9505bca14308c0536f4dcf17cd3 (tree)
Time2021-04-08 15:11:11
Authorxuexiansong <s.xue.xian.c@gmai...>
CommiterGitHub

Log Message

Feat(BVM): change op div (#1871)

Change Summary

Incremental Difference

--- a/protocol/vm/numeric.go
+++ b/protocol/vm/numeric.go
@@ -215,7 +215,6 @@ func opMul(vm *virtualMachine) error {
215215 return err
216216 }
217217
218-
219218 if overflow := x.MulOverflow(x, y); overflow || x.Sign() < 0 {
220219 return ErrRange
221220 }
@@ -228,22 +227,22 @@ func opDiv(vm *virtualMachine) error {
228227 if err != nil {
229228 return err
230229 }
231- y, err := vm.popInt64(true)
230+
231+ y, err := vm.popBigInt(true)
232232 if err != nil {
233233 return err
234234 }
235- x, err := vm.popInt64(true)
235+
236+ x, err := vm.popBigInt(true)
236237 if err != nil {
237238 return err
238239 }
239- if y == 0 {
240+
241+ if y.IsZero() {
240242 return ErrDivZero
241243 }
242- res, ok := checked.DivInt64(x, y)
243- if !ok {
244- return ErrRange
245- }
246- return vm.pushInt64(res, true)
244+
245+ return vm.pushBigInt(x.Div(x, y), true)
247246 }
248247
249248 func opMod(vm *virtualMachine) error {
--- a/protocol/vm/numeric_test.go
+++ b/protocol/vm/numeric_test.go
@@ -191,39 +191,6 @@ func TestNumericOps(t *testing.T) {
191191 op: OP_DIV,
192192 startVM: &virtualMachine{
193193 runLimit: 50000,
194- dataStack: [][]byte{Int64Bytes(-2), {1}},
195- },
196- wantVM: &virtualMachine{
197- runLimit: 49992,
198- deferredCost: -9,
199- dataStack: [][]byte{Int64Bytes(-2)},
200- },
201- }, {
202- op: OP_DIV,
203- startVM: &virtualMachine{
204- runLimit: 50000,
205- dataStack: [][]byte{Int64Bytes(-2), Int64Bytes(-1)},
206- },
207- wantVM: &virtualMachine{
208- runLimit: 49992,
209- deferredCost: -23,
210- dataStack: [][]byte{{2}},
211- },
212- }, {
213- op: OP_DIV,
214- startVM: &virtualMachine{
215- runLimit: 50000,
216- dataStack: [][]byte{Int64Bytes(-3), Int64Bytes(2)},
217- },
218- wantVM: &virtualMachine{
219- runLimit: 49992,
220- deferredCost: -9,
221- dataStack: [][]byte{Int64Bytes(-1)},
222- },
223- }, {
224- op: OP_DIV,
225- startVM: &virtualMachine{
226- runLimit: 50000,
227194 dataStack: [][]byte{{2}, {}},
228195 },
229196 wantErr: ErrDivZero,
@@ -1126,6 +1093,99 @@ func Test_op2Div(t *testing.T) {
11261093 }
11271094 }
11281095
1096+func Test_opDiv(t *testing.T) {
1097+ type args struct {
1098+ vm *virtualMachine
1099+ }
1100+ tests := []struct {
1101+ name string
1102+ args args
1103+ want [][]byte
1104+ wantErr bool
1105+ }{
1106+ {
1107+ name: "Test 2 div 2 = 1",
1108+ args: args{
1109+ vm: &virtualMachine{
1110+ runLimit: 50000,
1111+ dataStack: [][]byte{{0x02},{0x02}},
1112+ },
1113+ },
1114+ want: [][]byte{{0x01}},
1115+ wantErr: false,
1116+ },
1117+ {
1118+ name: "Test 2 div 1 = 2",
1119+ args: args{
1120+ vm: &virtualMachine{
1121+ runLimit: 50000,
1122+ dataStack: [][]byte{{0x02},{0x01}},
1123+ },
1124+ },
1125+ want: [][]byte{{0x02}},
1126+ wantErr: false,
1127+ },
1128+ {
1129+ name: "Test that two bytes number become one byte number after op div",
1130+ args: args{
1131+ vm: &virtualMachine{
1132+ runLimit: 50000,
1133+ dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},{0x02}},
1134+ },
1135+ },
1136+ want: [][]byte{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
1137+ wantErr: false,
1138+ },
1139+ {
1140+ name: "Test for 0 div 2 got 0",
1141+ args: args{
1142+ vm: &virtualMachine{
1143+ runLimit: 50000,
1144+ dataStack: [][]byte{{},{0x02}},
1145+ },
1146+ },
1147+ want: [][]byte{{}},
1148+ wantErr: false,
1149+ },
1150+ {
1151+ name: "Test for -1 div 2 got error",
1152+ args: args{
1153+ vm: &virtualMachine{
1154+ runLimit: 50000,
1155+ dataStack: [][]byte{mocks.U256NumNegative1,{0x02}},
1156+ },
1157+ },
1158+ want: nil,
1159+ wantErr: true,
1160+ },
1161+ {
1162+ name: "Test for 1 div 0 got error",
1163+ args: args{
1164+ vm: &virtualMachine{
1165+ runLimit: 50000,
1166+ dataStack: [][]byte{{0x01},{}},
1167+ },
1168+ },
1169+ want: nil,
1170+ wantErr: true,
1171+ },
1172+ }
1173+ for _, tt := range tests {
1174+ t.Run(tt.name, func(t *testing.T) {
1175+ if err := opDiv(tt.args.vm); err != nil {
1176+ if !tt.wantErr {
1177+ t.Errorf("opDiv() error = %v, wantErr %v", err, tt.wantErr)
1178+ } else {
1179+ return
1180+ }
1181+ }
1182+ if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) {
1183+ t.Errorf("opDiv() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want)
1184+ }
1185+ })
1186+ }
1187+}
1188+
11291189 func Test_opAdd(t *testing.T) {
11301190 type args struct {
11311191 vm *virtualMachine
Show on old repository browser