mirror of https://github.com/lightninglabs/loop
multi: add swap label to SwapContract and store under separate key
This commits adds an optional label to our swaps, and writes it to disk under a separate key in our swap bucket. This approach is chosen rather than an on-the-fly addition to our existing swap contract field so that we do not need to deal with EOF checking in the future. To allow creation of unique internal labels, we add a reserved prefix which can be used by the daemon to set labels that are distinct from client set ones.pull/263/head
parent
e2c54bd052
commit
9678c7817d
@ -0,0 +1,46 @@
|
|||||||
|
package labels
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MaxLength is the maximum length we allow for labels.
|
||||||
|
MaxLength = 500
|
||||||
|
|
||||||
|
// Reserved is used as a prefix to separate labels that are created by
|
||||||
|
// loopd from those created by users.
|
||||||
|
Reserved = "[reserved]"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrLabelTooLong is returned when a label exceeds our length limit.
|
||||||
|
ErrLabelTooLong = errors.New("label exceeds maximum length")
|
||||||
|
|
||||||
|
// ErrReservedPrefix is returned when a label contains the prefix
|
||||||
|
// which is reserved for internally produced labels.
|
||||||
|
ErrReservedPrefix = errors.New("label contains reserved prefix")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Validate checks that a label is of appropriate length and is not in our list
|
||||||
|
// of reserved labels.
|
||||||
|
func Validate(label string) error {
|
||||||
|
if len(label) > MaxLength {
|
||||||
|
return ErrLabelTooLong
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the label is shorter than our reserved prefix, it cannot contain
|
||||||
|
// it.
|
||||||
|
if len(label) < len(Reserved) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if our label begins with our reserved prefix. We don't mind if
|
||||||
|
// it has our reserved prefix in another case, we just need to be able
|
||||||
|
// to reserve a subset of labels with this prefix.
|
||||||
|
if label[0:len(Reserved)] == Reserved {
|
||||||
|
return ErrReservedPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package labels
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestValidate tests validation of labels.
|
||||||
|
func TestValidate(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
label string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "label ok",
|
||||||
|
label: "label",
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "exceeds limit",
|
||||||
|
label: strings.Repeat(" ", MaxLength+1),
|
||||||
|
err: ErrLabelTooLong,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "exactly reserved prefix",
|
||||||
|
label: Reserved,
|
||||||
|
err: ErrReservedPrefix,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "starts with reserved prefix",
|
||||||
|
label: fmt.Sprintf("%v test", Reserved),
|
||||||
|
err: ErrReservedPrefix,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ends with reserved prefix",
|
||||||
|
label: fmt.Sprintf("test %v", Reserved),
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
require.Equal(t, test.err, Validate(test.label))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue