Merge pull request #2691 from nspcc-dev/sc-builder-len

smartcontract: add Len to Builder
This commit is contained in:
Roman Khimov 2022-09-14 10:36:01 +03:00 committed by GitHub
commit 8ecafaaadf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -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) {

View file

@ -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())
}