forked from TrueCloudLab/neoneo-go
Merge pull request #356 from nspcc-dev/add-vm-string-instructions
vm: implement CAT, SUBSTR, LEFT and RIGHT
This commit is contained in:
commit
0feaaabe61
2 changed files with 171 additions and 2 deletions
21
pkg/vm/vm.go
21
pkg/vm/vm.go
|
@ -299,7 +299,24 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
panic("can't TUCK with a one-element stack")
|
panic("can't TUCK with a one-element stack")
|
||||||
}
|
}
|
||||||
v.estack.InsertAt(a, 2)
|
v.estack.InsertAt(a, 2)
|
||||||
|
case CAT:
|
||||||
|
b := v.estack.Pop().Bytes()
|
||||||
|
a := v.estack.Pop().Bytes()
|
||||||
|
ab := append(a, b...)
|
||||||
|
v.estack.PushVal(ab)
|
||||||
|
case SUBSTR:
|
||||||
|
l := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
o := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
s := v.estack.Pop().Bytes()
|
||||||
|
v.estack.PushVal(s[o:o+l])
|
||||||
|
case LEFT:
|
||||||
|
l := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
s := v.estack.Pop().Bytes()
|
||||||
|
v.estack.PushVal(s[:l])
|
||||||
|
case RIGHT:
|
||||||
|
l := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
s := v.estack.Pop().Bytes()
|
||||||
|
v.estack.PushVal(s[len(s)-l:])
|
||||||
case XDROP:
|
case XDROP:
|
||||||
n := int(v.estack.Pop().BigInt().Int64())
|
n := int(v.estack.Pop().BigInt().Int64())
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
|
@ -712,7 +729,7 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
v.state = haltState
|
v.state = haltState
|
||||||
}
|
}
|
||||||
|
|
||||||
case CAT, SUBSTR, LEFT, RIGHT, CHECKSIG, CHECKMULTISIG,
|
case CHECKSIG, CHECKMULTISIG,
|
||||||
UNPACK, REVERSE, REMOVE:
|
UNPACK, REVERSE, REMOVE:
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
|
|
||||||
|
|
|
@ -481,6 +481,158 @@ func TestINVERTgood3(t *testing.T) {
|
||||||
assert.Equal(t, int64(-0x6A), vm.estack.Peek(0).BigInt().Int64())
|
assert.Equal(t, int64(-0x6A), vm.estack.Peek(0).BigInt().Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCATBadNoArgs(t *testing.T) {
|
||||||
|
prog := makeProgram(CAT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCATBadOneArg(t *testing.T) {
|
||||||
|
prog := makeProgram(CAT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abc"))
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCATGood(t *testing.T) {
|
||||||
|
prog := makeProgram(CAT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abc"))
|
||||||
|
vm.estack.PushVal([]byte("def"))
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, []byte("abcdef"), vm.estack.Peek(0).Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRBadNoArgs(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRBadOneArg(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRBadTwoArgs(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(0)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRGood(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, []byte("bc"), vm.estack.Peek(0).Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRBadOffset(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(6)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSUBSTRBadLen(t *testing.T) {
|
||||||
|
prog := makeProgram(SUBSTR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(6)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLEFTBadNoArgs(t *testing.T) {
|
||||||
|
prog := makeProgram(LEFT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLEFTBadNoString(t *testing.T) {
|
||||||
|
prog := makeProgram(LEFT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLEFTGood(t *testing.T) {
|
||||||
|
prog := makeProgram(LEFT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, []byte("ab"), vm.estack.Peek(0).Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLEFTBadLen(t *testing.T) {
|
||||||
|
prog := makeProgram(LEFT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(8)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRIGHTBadNoArgs(t *testing.T) {
|
||||||
|
prog := makeProgram(RIGHT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRIGHTBadNoString(t *testing.T) {
|
||||||
|
prog := makeProgram(RIGHT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRIGHTGood(t *testing.T) {
|
||||||
|
prog := makeProgram(RIGHT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, false, vm.state.HasFlag(faultState))
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, []byte("ef"), vm.estack.Peek(0).Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRIGHTBadLen(t *testing.T) {
|
||||||
|
prog := makeProgram(RIGHT)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte("abcdef"))
|
||||||
|
vm.estack.PushVal(8)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
func makeProgram(opcodes ...Instruction) []byte {
|
func makeProgram(opcodes ...Instruction) []byte {
|
||||||
prog := make([]byte, len(opcodes)+1) // RET
|
prog := make([]byte, len(opcodes)+1) // RET
|
||||||
for i := 0; i < len(opcodes); i++ {
|
for i := 0; i < len(opcodes); i++ {
|
||||||
|
|
Loading…
Reference in a new issue