github.com/datachainlab/burrow@v0.25.0/execution/names/names.go (about) 1 // Copyright 2017 Monax Industries Limited 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package names 16 17 import ( 18 "fmt" 19 20 "github.com/hyperledger/burrow/event/query" 21 amino "github.com/tendermint/go-amino" 22 ) 23 24 var MinNameRegistrationPeriod uint64 = 5 25 26 const ( 27 28 // NOTE: base costs and validity checks are here so clients 29 // can use them without importing state 30 31 // cost for storing a name for a block is 32 // CostPerBlock*CostPerByte*(len(data) + 32) 33 NameByteCostMultiplier uint64 = 1 34 NameBlockCostMultiplier uint64 = 1 35 36 MaxNameLength = 64 37 MaxDataLength = 1 << 16 38 ) 39 40 var cdc = amino.NewCodec() 41 42 func (e *Entry) Encode() ([]byte, error) { 43 return cdc.MarshalBinaryBare(e) 44 } 45 46 func (e *Entry) String() string { 47 return fmt.Sprintf("NameEntry{%v -> %v; Expires: %v, Owner: %v}", e.Name, e.Data, e.Expires, e.Owner) 48 } 49 50 type TaggedEntry struct { 51 *Entry 52 query.Tagged 53 } 54 55 func (e *Entry) Tagged() *TaggedEntry { 56 return &TaggedEntry{ 57 Entry: e, 58 Tagged: query.MustReflectTags(e), 59 } 60 } 61 62 func DecodeEntry(entryBytes []byte) (*Entry, error) { 63 entry := new(Entry) 64 err := cdc.UnmarshalBinaryBare(entryBytes, entry) 65 if err != nil { 66 return nil, err 67 } 68 return entry, nil 69 } 70 71 type Reader interface { 72 GetName(name string) (*Entry, error) 73 } 74 75 type Writer interface { 76 // Updates the name entry creating it if it does not exist 77 UpdateName(entry *Entry) error 78 // Remove the name entry 79 RemoveName(name string) error 80 } 81 82 type ReaderWriter interface { 83 Reader 84 Writer 85 } 86 87 type Iterable interface { 88 IterateNames(consumer func(*Entry) error) (err error) 89 } 90 91 type IterableReader interface { 92 Iterable 93 Reader 94 } 95 96 type IterableReaderWriter interface { 97 Iterable 98 ReaderWriter 99 } 100 101 // base cost is "effective" number of bytes 102 func NameBaseCost(name, data string) uint64 { 103 return uint64(len(data) + 32) 104 } 105 106 func NameCostPerBlock(baseCost uint64) uint64 { 107 return NameBlockCostMultiplier * NameByteCostMultiplier * baseCost 108 } 109 110 func NameCostForExpiryIn(name, data string, expiresIn uint64) uint64 { 111 return NameCostPerBlock(NameBaseCost(name, data)) * expiresIn 112 }