OSDN Git Service

fix vm (#2107)
authorxuexiansong <s.xue.xian.c@gmail.com>
Tue, 14 Sep 2021 08:53:24 +0000 (16:53 +0800)
committerGitHub <noreply@github.com>
Tue, 14 Sep 2021 08:53:24 +0000 (16:53 +0800)
* fix vm

* change import

protocol/vm/types.go
protocol/vm/types_test.go
protocol/vm/vmutil/script_test.go

index 99b0c98..771d71a 100644 (file)
@@ -62,9 +62,10 @@ func bigIntInt64(n *uint256.Int) (int64, error) {
 
 // reverse []byte.
 func reverse(b []byte) []byte {
-       for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
-               b[i], b[j] = b[j], b[i]
+       r := make([]byte, len(b))
+       copy(r, b)
+       for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
+               r[i], r[j] = r[j], r[i]
        }
-
-       return b
+       return r
 }
index 626a7ca..1e75f80 100644 (file)
@@ -3,6 +3,7 @@ package vm
 import (
        "bytes"
        "math/big"
+       "reflect"
        "testing"
 
        "github.com/holiman/uint256"
@@ -136,3 +137,49 @@ func TestInt64BigIntConvert(t *testing.T) {
                }
        }
 }
+
+func Test_reverse(t *testing.T) {
+       type args struct {
+               b []byte
+       }
+       type wants struct {
+               origin []byte
+               want   []byte
+       }
+       tests := []struct {
+               name  string
+               args  args
+               wants wants
+       }{
+               {
+                       name: "test reverse",
+                       args: args{
+                               b: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
+                       },
+                       wants: wants{
+                               origin: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
+                               want:   []byte{0x01, 0x00, 0x00, 0x00, 0x00},
+                       },
+               },
+               {
+                       name: "test reverse 1",
+                       args: args{
+                               b: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
+                       },
+                       wants: wants{
+                               origin: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
+                               want:   []byte{0x01, 0x03, 0x20, 0x02, 0x01},
+                       },
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if got := reverse(tt.args.b); !reflect.DeepEqual(got, tt.wants.want) {
+                               t.Errorf("reverse() = %v, want %v", got, tt.wants.want)
+                       }
+                       if !reflect.DeepEqual(tt.args.b, tt.wants.origin) {
+                               t.Errorf("after reverse args = %v, origin %v", tt.args.b, tt.wants.origin)
+                       }
+               })
+       }
+}
index db794d7..eefb9f7 100644 (file)
@@ -3,6 +3,7 @@ package vmutil
 import (
        "crypto/ed25519"
        "encoding/hex"
+       "reflect"
        "testing"
 
        "github.com/bytom/bytom/errors"
@@ -211,10 +212,16 @@ func TestGetIssuanceProgramRestrictHeight(t *testing.T) {
                if err != nil {
                        t.Fatal(err)
                }
-
+               originProgram := make([]byte, len(program))
+               copy(originProgram, program)
                gotHeight := GetIssuanceProgramRestrictHeight(program)
                if gotHeight != test.wantHeight {
                        t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight)
+                       return
+               }
+
+               if !reflect.DeepEqual(originProgram, program) {
+                       t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: after got %v before %v", i, program, originProgram)
                }
        }
 }