diff --git a/pkg/smartcontract/builder.go b/pkg/smartcontract/builder.go index f20789652..369be1b64 100644 --- a/pkg/smartcontract/builder.go +++ b/pkg/smartcontract/builder.go @@ -66,6 +66,13 @@ func (b *Builder) InvokeWithAssert(contract util.Uint160, method string, params b.Assert() } +// Len returns the current length of the script. It's useful to perform script +// length checks (wrt transaction.MaxScriptLength limit) while building the +// script. +func (b *Builder) Len() int { + return b.bw.Len() +} + // Script return current script, you can't use Builder after invoking this method // unless you Reset it. func (b *Builder) Script() ([]byte, error) { diff --git a/pkg/smartcontract/builder_test.go b/pkg/smartcontract/builder_test.go new file mode 100644 index 000000000..d16f7b621 --- /dev/null +++ b/pkg/smartcontract/builder_test.go @@ -0,0 +1,23 @@ +package smartcontract + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +func TestBuilder(t *testing.T) { + b := NewBuilder() + require.Equal(t, 0, b.Len()) + b.InvokeMethod(util.Uint160{1, 2, 3}, "method") + require.Equal(t, 37, b.Len()) + b.InvokeMethod(util.Uint160{1, 2, 3}, "transfer", util.Uint160{3, 2, 1}, util.Uint160{9, 8, 7}, 100500) + require.Equal(t, 126, b.Len()) + s, err := b.Script() + require.NoError(t, err) + require.NotNil(t, s) + require.Equal(t, 126, len(s)) + b.Reset() + require.Equal(t, 0, b.Len()) +}