Skip to content

Commit 2fba044

Browse files
committed
core: add System.Runtime.CurrentSigners syscall
Port the neo-project/neo#2827. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
1 parent 90b01bc commit 2fba044

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

pkg/core/interop/interopnames/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
SystemIteratorValue = "System.Iterator.Value"
1616
SystemRuntimeBurnGas = "System.Runtime.BurnGas"
1717
SystemRuntimeCheckWitness = "System.Runtime.CheckWitness"
18+
SystemRuntimeCurrentSigners = "System.Runtime.CurrentSigners"
1819
SystemRuntimeGasLeft = "System.Runtime.GasLeft"
1920
SystemRuntimeGetAddressVersion = "System.Runtime.GetAddressVersion"
2021
SystemRuntimeGetCallingScriptHash = "System.Runtime.GetCallingScriptHash"
@@ -52,6 +53,7 @@ var names = []string{
5253
SystemIteratorValue,
5354
SystemRuntimeBurnGas,
5455
SystemRuntimeCheckWitness,
56+
SystemRuntimeCurrentSigners,
5557
SystemRuntimeGasLeft,
5658
SystemRuntimeGetAddressVersion,
5759
SystemRuntimeGetCallingScriptHash,

pkg/core/interop/runtime/engine.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/nspcc-dev/neo-go/pkg/config"
99
"github.com/nspcc-dev/neo-go/pkg/core/interop"
10+
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
1011
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
1112
"github.com/nspcc-dev/neo-go/pkg/vm"
1213
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@@ -179,3 +180,16 @@ func BurnGas(ic *interop.Context) error {
179180
}
180181
return nil
181182
}
183+
184+
// CurrentSigners returns signers of the currently loaded transaction or stackitem.Null
185+
// if script container is not a transaction.
186+
func CurrentSigners(ic *interop.Context) error {
187+
tx, ok := ic.Container.(*transaction.Transaction)
188+
if ok {
189+
ic.VM.Estack().PushItem(transaction.SignersToStackItem(tx.Signers))
190+
} else {
191+
ic.VM.Estack().PushItem(stackitem.Null{})
192+
}
193+
194+
return nil
195+
}

pkg/core/interop/runtime/engine_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/nspcc-dev/neo-go/internal/random"
1010
"github.com/nspcc-dev/neo-go/pkg/core/block"
1111
"github.com/nspcc-dev/neo-go/pkg/core/interop"
12+
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
1213
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
1314
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
1415
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
@@ -129,3 +130,45 @@ func TestLog(t *testing.T) {
129130
require.Equal(t, h.StringLE(), logMsg["script"])
130131
})
131132
}
133+
134+
func TestCurrentSigners(t *testing.T) {
135+
t.Run("container is block", func(t *testing.T) {
136+
b := block.New(false)
137+
ic := &interop.Context{VM: vm.New(), Container: b}
138+
require.NoError(t, CurrentSigners(ic))
139+
checkStack(t, ic.VM, stackitem.Null{})
140+
})
141+
142+
t.Run("container is transaction", func(t *testing.T) {
143+
tx := &transaction.Transaction{
144+
Signers: []transaction.Signer{
145+
{
146+
Account: util.Uint160{1},
147+
Scopes: transaction.None,
148+
},
149+
{
150+
Account: util.Uint160{2},
151+
Scopes: transaction.CalledByEntry,
152+
},
153+
},
154+
}
155+
ic := &interop.Context{VM: vm.New(), Container: tx}
156+
require.NoError(t, CurrentSigners(ic))
157+
checkStack(t, ic.VM, stackitem.NewArray([]stackitem.Item{
158+
stackitem.NewArray([]stackitem.Item{
159+
stackitem.NewByteArray(util.Uint160{1}.BytesBE()),
160+
stackitem.NewBigInteger(big.NewInt(int64(transaction.None))),
161+
stackitem.NewArray([]stackitem.Item{}),
162+
stackitem.NewArray([]stackitem.Item{}),
163+
stackitem.NewArray([]stackitem.Item{}),
164+
}),
165+
stackitem.NewArray([]stackitem.Item{
166+
stackitem.NewByteArray(util.Uint160{2}.BytesBE()),
167+
stackitem.NewBigInteger(big.NewInt(int64(transaction.CalledByEntry))),
168+
stackitem.NewArray([]stackitem.Item{}),
169+
stackitem.NewArray([]stackitem.Item{}),
170+
stackitem.NewArray([]stackitem.Item{}),
171+
}),
172+
}))
173+
})
174+
}

pkg/core/interops.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var systemInterops = []interop.Function{
4646
{Name: interopnames.SystemRuntimeBurnGas, Func: runtime.BurnGas, Price: 1 << 4, ParamCount: 1},
4747
{Name: interopnames.SystemRuntimeCheckWitness, Func: runtime.CheckWitness, Price: 1 << 10,
4848
RequiredFlags: callflag.NoneFlag, ParamCount: 1},
49+
{Name: interopnames.SystemRuntimeCurrentSigners, Func: runtime.CurrentSigners, Price: 1 << 4,
50+
RequiredFlags: callflag.NoneFlag},
4951
{Name: interopnames.SystemRuntimeGasLeft, Func: runtime.GasLeft, Price: 1 << 4},
5052
{Name: interopnames.SystemRuntimeGetAddressVersion, Func: runtime.GetAddressVersion, Price: 1 << 3},
5153
{Name: interopnames.SystemRuntimeGetCallingScriptHash, Func: runtime.GetCallingScriptHash, Price: 1 << 4},

pkg/interop/runtime/runtime.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime
77
import (
88
"github.com/nspcc-dev/neo-go/pkg/interop"
99
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
10+
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
1011
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
1112
)
1213

@@ -30,6 +31,13 @@ func CheckWitness(hashOrKey []byte) bool {
3031
return neogointernal.Syscall1("System.Runtime.CheckWitness", hashOrKey).(bool)
3132
}
3233

34+
// CurrentSigners returns signers of the currently loaded transaction or nil if
35+
// executing script container is not a transaction. It uses
36+
// `System.Runtime.CurrentSigners` syscall.
37+
func CurrentSigners() []ledger.TransactionSigner {
38+
return neogointernal.Syscall0("System.Runtime.CurrentSigners").([]ledger.TransactionSigner)
39+
}
40+
3341
// LoadScript loads the given bytecode into the VM and executes it with the
3442
// given call flags and arguments. This bytecode is executed as is from byte 0,
3543
// it's not a deployed contract that can have methods. The execution context is

0 commit comments

Comments
 (0)