Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
fix vm (#2107)
Browse files Browse the repository at this point in the history
* fix vm

* change import
  • Loading branch information
bitstory authored Sep 14, 2021
1 parent c3d3aaa commit 0a7abbe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
9 changes: 5 additions & 4 deletions protocol/vm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions protocol/vm/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"bytes"
"math/big"
"reflect"
"testing"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -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)
}
})
}
}
9 changes: 8 additions & 1 deletion protocol/vm/vmutil/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vmutil
import (
"crypto/ed25519"
"encoding/hex"
"reflect"
"testing"

"github.com/bytom/bytom/errors"
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 0a7abbe

Please sign in to comment.