Skip to content

Commit

Permalink
truncate too long descriptions in SCs
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed Oct 2, 2020
1 parent 85eba8b commit 2dd69a7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/util/strutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

const (
ending = "[..]"
)

func GentleCut(s string, length int) string {
if len(s) <= length {
return s
}
if length <= len(ending) {
return ending
}
return s[:length-len(ending)] + ending
}
21 changes: 21 additions & 0 deletions packages/util/strutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import "testing"

func TestCutGently(t *testing.T) {
t.Log(GentleCut("kukukuku", 10))
t.Log(GentleCut("kukukuku", 8))
t.Log(GentleCut("kukukuku", 5))
t.Log(GentleCut("kukukukukuku", 5))
t.Log(GentleCut("kukukukukuku", 6))
t.Log(GentleCut("kukukukukuku", 7))
t.Log(GentleCut("kukukukukuku", 8))
t.Log(GentleCut("kukukukukuku", 9))
t.Log(GentleCut("kukukukukuku", 10))
t.Log(GentleCut("kukukukukuku", 11))
t.Log(GentleCut("kukukukukuku", 12))
t.Log(GentleCut("ku", 1))
t.Log(GentleCut("ku", 5))
t.Log(GentleCut("kuku", 1))
t.Log(GentleCut("kuku", 4))
}
4 changes: 4 additions & 0 deletions packages/vm/examples/donatewithfeedback/dwfimpl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package dwfimpl
import (
"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/balance"
"github.com/iotaledger/wasp/packages/sctransaction"
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm/examples/donatewithfeedback"
"github.com/iotaledger/wasp/packages/vm/vmtypes"
"strings"
Expand Down Expand Up @@ -51,6 +52,8 @@ func (ep dwfEntryPoint) WithGasLimit(_ int) vmtypes.EntryPoint {
return ep
}

const maxComment = 150

// donate implements request 'donate'. It takes feedback text from the request
// and adds it into the log of feedback messages
func donate(ctx vmtypes.Sandbox) {
Expand All @@ -61,6 +64,7 @@ func donate(ctx vmtypes.Sandbox) {
donated := ctx.AccessSCAccount().AvailableBalanceFromRequest(&balance.ColorIOTA)
// take feedback text contained in the request
feedback, ok, err := ctx.AccessRequest().Args().GetString(donatewithfeedback.VarReqFeedback)
feedback = util.GentleCut(feedback, maxComment)

stateAccess := ctx.AccessState()
tlog := stateAccess.GetTimestampedLog(donatewithfeedback.VarStateTheLog)
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/examples/fairauction/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
OwnerMarginDefault = 50 // 5%
OwnerMarginMin = 5 // minimum 0.5%
OwnerMarginMax = 100 // max 10%
MaxDescription = 150
)

// validating constants at node boot
Expand Down Expand Up @@ -289,6 +290,7 @@ func startAuction(ctx vmtypes.Sandbox) {
if !ok {
description = "N/A"
}
description = util.GentleCut(description, MaxDescription)

// find out if auction for this color already exist in the dictionary
auctions := ctx.AccessState().GetDictionary(VarStateAuctions)
Expand Down
4 changes: 4 additions & 0 deletions packages/vm/examples/tokenregistry/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func initSC(ctx vmtypes.Sandbox) {
ctx.Publishf("TokenRegistry: initSC")
}

const maxDescription = 150

// mintSupply implements 'mint supply' request
func mintSupply(ctx vmtypes.Sandbox) {
ctx.Publish("TokenRegistry: mintSupply")
Expand Down Expand Up @@ -116,6 +118,8 @@ func mintSupply(ctx vmtypes.Sandbox) {
if !ok {
description = "no dscr"
}
description = util.GentleCut(description, maxDescription)

// get the additional arbitrary deta attached to the supply record
uddata, err := reqAccess.Args().Get(VarReqUserDefinedMetadata)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/cluster/tests/wasptest2/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/testutil"
_ "github.com/iotaledger/wasp/packages/vm/examples"
//_ "github.com/iotaledger/wasp/packages/vm/examples"
"github.com/iotaledger/wasp/packages/vm/examples/inccounter"
"github.com/iotaledger/wasp/packages/vm/examples/tokenregistry"
"github.com/iotaledger/wasp/packages/vm/vmconst"
Expand Down

0 comments on commit 2dd69a7

Please sign in to comment.