namereg cleanup, tests

This commit is contained in:
Ethan Buchman
2015-05-24 14:41:42 -04:00
parent 02aedaaefb
commit 77ff09e173
5 changed files with 242 additions and 41 deletions

View File

@ -1,5 +1,40 @@
package types
import (
"regexp"
)
var (
MinNameRegistrationPeriod uint64 = 5
// cost for storing a name for a block is
// CostPerBlock*CostPerByte*(len(data) + 32)
NameCostPerByte uint64 = 1
NameCostPerBlock uint64 = 1
MaxNameLength = 32
MaxDataLength = 1 << 16
// Name should be alphanum, underscore, slash
// Data should be anything permitted in JSON
regexpAlphaNum = regexp.MustCompile("^[a-zA-Z0-9_/]*$")
regexpJSON = regexp.MustCompile(`^[a-zA-Z0-9_/ \-"':,\n\t.{}()\[\]]*$`)
)
// filter strings
func validateNameRegEntryName(name string) bool {
return regexpAlphaNum.Match([]byte(name))
}
func validateNameRegEntryData(data string) bool {
return regexpJSON.Match([]byte(data))
}
// base cost is "effective" number of bytes
func BaseEntryCost(name, data string) uint64 {
return uint64(len(data) + 32)
}
type NameRegEntry struct {
Name string `json:"name"` // registered name for the entry
Owner []byte `json:"owner"` // address that created the entry

View File

@ -3,7 +3,6 @@ package types
import (
"errors"
"io"
"regexp"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
@ -200,15 +199,30 @@ func (tx *NameTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
binary.WriteTo([]byte(`}]}`), w, n, err)
}
// alphanum, underscore, forward slash
var regexpAlphaNum, _ = regexp.Compile("^[a-zA-Z0-9_/]*$")
func (tx *NameTx) ValidateStrings() error {
if len(tx.Name) == 0 {
return errors.New("Name must not be empty")
}
if len(tx.Name) > MaxNameLength {
return errors.New(Fmt("Name is too long. Max %d bytes", MaxNameLength))
}
if len(tx.Data) > MaxDataLength {
return errors.New(Fmt("Data is too long. Max %d bytes", MaxDataLength))
}
// anything you might find in a json
var regexpJSON, err = regexp.Compile(`^[a-zA-Z0-9_/ \-"':,\n\t.{}()\[\]]*$`)
if !validateNameRegEntryName(tx.Name) {
return errors.New(Fmt("Invalid characters found in NameTx.Name (%s). Only alphanumeric, underscores, and forward slashes allowed", tx.Name))
}
func (tx *NameTx) Validate() bool {
// Name should be alphanum and Data should be like JSON
return regexpAlphaNum.Match([]byte(tx.Name)) && regexpJSON.Match([]byte(tx.Data))
if !validateNameRegEntryData(tx.Data) {
return errors.New(Fmt("Invalid characters found in NameTx.Data (%s). Only the kind of things found in a JSON file are allowed", tx.Data))
}
return nil
}
func (tx *NameTx) BaseEntryCost() uint64 {
return BaseEntryCost(tx.Name, tx.Data)
}
func (tx *NameTx) String() string {