Skip to content

Commit

Permalink
vm: add PUSHT and PUSHF opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaShaleva committed Oct 28, 2022
1 parent b590d4c commit cfd83a9
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 208 deletions.
2 changes: 1 addition & 1 deletion cli/nep_test/nep17_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestNEP17Balance(t *testing.T) {
}

e.CheckNextLine(t, "^\\s*$")
addr4, err := address.StringToUint160("NfWu6j9KPLQMsWLfHz9iZRy5sNw2bUZWQL") // deployed verify.go contract
addr4, err := address.StringToUint160("NiFxRcC5Anz9pmqQyMHh5vamBUZDbRRRzA") // deployed verify.go contract
require.NoError(t, err)
e.CheckNextLine(t, "^Account "+address.Uint160ToString(addr4))
e.CheckEOF(t)
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/wallet1_solo.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"isDefault": false
},
{
"address": "NfWu6j9KPLQMsWLfHz9iZRy5sNw2bUZWQL",
"address": "NiFxRcC5Anz9pmqQyMHh5vamBUZDbRRRzA",
"key": "6PYSATFztBa3CHjSR6sLAKungUEAbQUCVE16KzmaQQ38gLeYGZ9Knd5mGv",
"label": "verify",
"contract": {
Expand Down
4 changes: 2 additions & 2 deletions cli/vm/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ func TestRunWithHistoricState(t *testing.T) {
e.checkNextLine(t, "READY: loaded 36 instructions")
e.checkStack(t, []byte{1})
e.checkNextLine(t, "READY: loaded 36 instructions")
e.checkNextLineExact(t, "Error: at instruction 31 (SYSCALL): failed to invoke syscall 1381727586: called contract cd583ac7a1a4faef70d6e9f513bc988dde22f672 not found: key not found\n")
e.checkNextLineExact(t, "Error: at instruction 31 (SYSCALL): failed to invoke syscall 1381727586: called contract 73a23e915b66ae406866787f4a6c1c517dc981e2 not found: key not found\n")
}

func TestEvents(t *testing.T) {
Expand All @@ -939,7 +939,7 @@ func TestEvents(t *testing.T) {
}),
}),
}
e.checkNextLine(t, "READY: loaded 43 instructions")
e.checkNextLine(t, "READY: loaded 42 instructions")
e.checkStack(t, stackitem.Null{})
e.checkEvents(t, true, expectedEvent) // automatically printed after `run` command
e.checkEvents(t, false, expectedEvent) // printed after `events` command
Expand Down
2 changes: 2 additions & 0 deletions pkg/core/fee/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var coefficients = [256]uint16{
opcode.PUSHINT64: 1 << 0,
opcode.PUSHINT128: 1 << 2,
opcode.PUSHINT256: 1 << 2,
opcode.PUSHT: 1 << 0,
opcode.PUSHF: 1 << 0,
opcode.PUSHA: 1 << 2,
opcode.PUSHNULL: 1 << 0,
opcode.PUSHDATA1: 1 << 3,
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/rpcsrv/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
t.Run("contract-based verification with parameters", func(t *testing.T) {
verAcc, err := util.Uint160DecodeStringLE(verifyWithArgsContractHash)
require.NoError(t, err)
checkContract(t, verAcc, []byte{}, 490890) // No C# match, but we believe it's OK and it differs from the one above.
checkContract(t, verAcc, []byte{}, 489770) // No C# match, but we believe it's OK and it differs from the one above.
})
t.Run("contract-based verification with invocation script", func(t *testing.T) {
verAcc, err := util.Uint160DecodeStringLE(verifyWithArgsContractHash)
Expand All @@ -2567,7 +2567,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
emit.Int(invocWriter.BinWriter, 5)
emit.String(invocWriter.BinWriter, "")
invocScript := invocWriter.Bytes()
checkContract(t, verAcc, invocScript, 393720) // No C# match, but we believe it's OK and it has a specific invocation script overriding anything server-side.
checkContract(t, verAcc, invocScript, 392600) // No C# match, but we believe it's OK and it has a specific invocation script overriding anything server-side.
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/smartcontract/param_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func TestEncodeDefaultValue(t *testing.T) {
for p, l := range map[ParamType]int{
UnknownType: 0,
AnyType: 66,
BoolType: 2,
BoolType: 1,
IntegerType: 33,
ByteArrayType: 66,
StringType: 66,
Expand Down
6 changes: 3 additions & 3 deletions pkg/vm/emit/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func Opcodes(w *io.BinWriter, ops ...opcode.Opcode) {

// Bool emits a bool type to the given buffer.
func Bool(w *io.BinWriter, ok bool) {
var opVal = opcode.PUSHF
var opVal = opcode.PUSHT
if !ok {
opVal = opcode.PUSHT
opVal = opcode.PUSHF
}
Opcodes(w, opVal, opcode.NOT)
Opcodes(w, opVal)
}

func padRight(s int, buf []byte) []byte {
Expand Down
12 changes: 5 additions & 7 deletions pkg/vm/emit/emit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ func TestEmitArray(t *testing.T) {
veryBig := new(big.Int).SetUint64(math.MaxUint64)
veryBig.Add(veryBig, big.NewInt(1))
Array(buf.BinWriter, p160, p256, &u160, &u256, u160, u256, big.NewInt(0), veryBig,
[]interface{}{int64(1), int64(2)}, nil, int64(1), "str", true, []byte{0xCA, 0xFE})
[]interface{}{int64(1), int64(2)}, nil, int64(1), "str", false, true, []byte{0xCA, 0xFE})
require.NoError(t, buf.Err)

res := buf.Bytes()
assert.EqualValues(t, opcode.PUSHDATA1, res[0])
assert.EqualValues(t, 2, res[1])
assert.EqualValues(t, []byte{0xCA, 0xFE}, res[2:4])
assert.EqualValues(t, opcode.PUSHF, res[4])
assert.EqualValues(t, opcode.NOT, res[5])
assert.EqualValues(t, opcode.PUSHT, res[4])
assert.EqualValues(t, opcode.PUSHF, res[5])
assert.EqualValues(t, opcode.PUSHDATA1, res[6])
assert.EqualValues(t, 3, res[7])
assert.EqualValues(t, []byte("str"), res[8:11])
Expand Down Expand Up @@ -280,10 +280,8 @@ func TestEmitBool(t *testing.T) {
Bool(buf.BinWriter, true)
Bool(buf.BinWriter, false)
result := buf.Bytes()
assert.EqualValues(t, opcode.PUSH0, result[0])
assert.EqualValues(t, opcode.NOT, result[1])
assert.EqualValues(t, opcode.PUSH1, result[2])
assert.EqualValues(t, opcode.NOT, result[3])
assert.EqualValues(t, opcode.PUSHT, result[0])
assert.EqualValues(t, opcode.PUSHF, result[1])
}

func TestEmitOpcode(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/vm/opcode/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
PUSHINT128 Opcode = 0x04
PUSHINT256 Opcode = 0x05

PUSHT Opcode = 0x08
PUSHF Opcode = 0x09

PUSHA Opcode = 0x0A
PUSHNULL Opcode = 0x0B

Expand All @@ -24,9 +27,7 @@ const (

PUSHM1 Opcode = 0x0F
PUSH0 Opcode = 0x10
PUSHF Opcode = PUSH0
PUSH1 Opcode = 0x11
PUSHT Opcode = PUSH1
PUSH2 Opcode = 0x12
PUSH3 Opcode = 0x13
PUSH4 Opcode = 0x14
Expand Down
Loading

0 comments on commit cfd83a9

Please sign in to comment.