core: implement Neo.Witness.GetVerificationScript interop

This commit is contained in:
Roman Khimov 2019-12-23 15:19:16 +03:00
parent 9d638d0fee
commit c287a9e93c
3 changed files with 28 additions and 1 deletions

View file

@ -216,6 +216,20 @@ func (ic *interopContext) invocationTxGetScript(v *vm.VM) error {
return nil
}
// witnessGetVerificationScript returns current witness' script.
func (ic *interopContext) witnessGetVerificationScript(v *vm.VM) error {
witInterface := v.Estack().Pop().Value()
wit, ok := witInterface.(*transaction.Witness)
if !ok {
return errors.New("value is not a witness")
}
// It's important not to share wit.VerificationScript slice with the code running in VM.
script := make([]byte, len(wit.VerificationScript))
copy(script, wit.VerificationScript)
v.Estack().PushVal(script)
return nil
}
// bcGetValidators returns validators.
func (ic *interopContext) bcGetValidators(v *vm.VM) error {
validators := ic.dao.GetValidators()

View file

@ -126,6 +126,19 @@ func TestInvocationTxGetScript(t *testing.T) {
require.Equal(t, inv.Script, value)
}
func TestWitnessGetVerificationScript(t *testing.T) {
v := vm.New()
script := []byte{byte(opcode.PUSHM1), byte(opcode.RET)}
witness := transaction.Witness{InvocationScript: nil, VerificationScript: script}
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil)
v.Estack().PushVal(vm.NewInteropItem(&witness))
err := context.witnessGetVerificationScript(v)
require.NoError(t, err)
value := v.Estack().Pop().Value().([]byte)
require.Equal(t, witness.VerificationScript, value)
}
func TestPopInputFromVM(t *testing.T) {
v, tx, _ := createVMAndTX(t)
v.Estack().PushVal(vm.NewInteropItem(&tx.Inputs[0]))

View file

@ -175,6 +175,7 @@ var neoInterops = []interopedFunction{
{Name: "Neo.Transaction.GetType", Func: (*interopContext).txGetType, Price: 1},
{Name: "Neo.Transaction.GetUnspentCoins", Func: (*interopContext).txGetUnspentCoins, Price: 200},
{Name: "Neo.Transaction.GetWitnesses", Func: (*interopContext).txGetWitnesses, Price: 200},
{Name: "Neo.Witness.GetVerificationScript", Func: (*interopContext).witnessGetVerificationScript, Price: 100},
// {Name: "Neo.Enumerator.Concat", Func: (*interopContext).enumeratorConcat, Price: 1},
// {Name: "Neo.Enumerator.Create", Func: (*interopContext).enumeratorCreate, Price: 1},
// {Name: "Neo.Enumerator.Next", Func: (*interopContext).enumeratorNext, Price: 1},
@ -187,7 +188,6 @@ var neoInterops = []interopedFunction{
{Name: "Neo.Runtime.Deserialize", Func: (*interopContext).runtimeDeserialize, Price: 1},
{Name: "Neo.Runtime.Serialize", Func: (*interopContext).runtimeSerialize, Price: 1},
// {Name: "Neo.Storage.Find", Func: (*interopContext).storageFind, Price: 1},
// {Name: "Neo.Witness.GetVerificationScript", Func: (*interopContext).witnessGetVerificationScript, Price: 100},
// Aliases.
// {Name: "Neo.Iterator.Next", Func: (*interopContext).enumeratorNext, Price: 1},