diff --git a/chaincore/state/state.go b/chaincore/state/state.go new file mode 100644 index 0000000..4f4cc21 --- /dev/null +++ b/chaincore/state/state.go @@ -0,0 +1,4 @@ +package state + +//Balance - any quantity that is represented as an integer in the lowest denomination +type Balance int64 diff --git a/core/datastore/key.go b/core/datastore/key.go new file mode 100644 index 0000000..f61589a --- /dev/null +++ b/core/datastore/key.go @@ -0,0 +1,151 @@ +package datastore + +import ( + "context" + "fmt" + "github.com/0chain/common/core/common" +) + +/*Key - a type for the entity key */ +type Key = string + +/*IDField - Useful to embed this into all the entities and get consistent behavior */ +type IDField struct { + ID Key `json:"id" yaml:"id"` +} + +/*SetKey sets the key */ +func (k *IDField) SetKey(key Key) { + k.ID = key +} + +/*GetKey returns the key for the entity */ +func (k *IDField) GetKey() Key { + return k.ID +} + +/*Validate - just an abstract implementation */ +func (k *IDField) Validate(ctx context.Context) error { + return nil +} + +/*ComputeProperties - default dummy implementation so only entities that need this can implement */ +func (k *IDField) ComputeProperties() { + +} + +/*Read - abstract method for memory store read */ +func (k *IDField) Read(ctx context.Context, key string) error { + return common.NewError("abstract_read", "Calling entity.Read() requires implementing the method") +} + +/*GetScore - abstract method for score when writing*/ +func (k *IDField) GetScore() int64 { + return 0 +} + +/*Write - abstract method for memory store write */ +func (k *IDField) Write(ctx context.Context) error { + return common.NewError("abstract_write", "Calling entity.Write() requires implementing the method") +} + +/*Delete - abstract method for memory store delete */ +func (k *IDField) Delete(ctx context.Context) error { + return common.NewError("abstract_delete", "Calling entity.Delete() requires implementing the method") +} + +/*NOIDFied - used when we just want to create a datastore entity that doesn't +have it's own id (like 1-to-many) that is only required to send it around with the parent key */ +type NOIDField struct { +} + +/*Read - abstract method for memory store read */ +func (nif *NOIDField) Read(ctx context.Context, key string) error { + return common.NewError("abstract_read", "Calling entity.Read() requires implementing the method") +} + +/*GetScore - abstract method for score when writing*/ +func (nif *NOIDField) GetScore() int64 { + return 0 +} + +/*Write - abstract method for memory store write */ +func (nif *NOIDField) Write(ctx context.Context) error { + return common.NewError("abstract_write", "Calling entity.Write() requires implementing the method") +} + +/*Delete - abstract method for memory store delete */ +func (nif *NOIDField) Delete(ctx context.Context) error { + return common.NewError("abstract_delete", "Calling entity.Delete() requires implementing the method") +} + +/*GetKey - implementing the interface */ +func (nif *NOIDField) GetKey() Key { + return EmptyKey +} + +/*SetKey - implementing the interface */ +func (nif *NOIDField) SetKey(key Key) { +} + +/*ComputeProperties - implementing the interface */ +func (nif *NOIDField) ComputeProperties() { +} + +/*Validate - implementing the interface */ +func (nif *NOIDField) Validate(ctx context.Context) error { + return nil +} + +/*ToString - return string representation of the key */ +func ToString(key Key) string { + return string(key) +} + +func IsEmpty(key Key) bool { + return len(key) == 0 +} + +func IsEqual(key1 Key, key2 Key) bool { + return key1 == key2 +} + +/*EmptyKey - Represents an empty key */ +var EmptyKey = Key("") + +/*ToKey - takes an interface and returns a Key */ +func ToKey(key interface{}) Key { + switch v := key.(type) { + case string: + return Key(v) + case []byte: + return Key(v) + default: + return Key(fmt.Sprintf("%v", v)) + } +} + +/*HashIDField - Useful to embed this into all the entities and get consistent behavior */ +type HashIDField struct { + Hash Key `json:"hash" msgpack:"h"` +} + +/*GetKey - Entity implementation */ +func (h *HashIDField) GetKey() Key { + return ToKey(h.Hash) +} + +/*SetKey - Entity implementation */ +func (h *HashIDField) SetKey(key Key) { + h.Hash = ToString(key) +} + +/*ComputeProperties - Entity implementation */ +func (h *HashIDField) ComputeProperties() { + +} + +/*Validate - Entity implementation */ +func (h *HashIDField) Validate(ctx context.Context) error { + return nil +} diff --git a/smartcontract/faucetsc/models.go b/smartcontract/faucetsc/models.go new file mode 100644 index 0000000..dfa2020 --- /dev/null +++ b/smartcontract/faucetsc/models.go @@ -0,0 +1,108 @@ +package faucetsc + +import ( + "encoding/json" + "github.com/0chain/common/chaincore/state" + "github.com/0chain/common/core/datastore" + "github.com/0chain/common/core/encryption" + "github.com/0chain/common/core/util" + "time" +) + +type limitRequest struct { + PourAmount state.Balance `json:"pour_amount"` + MaxPourAmount state.Balance `json:"max_pour_amount"` + PeriodicLimit state.Balance `json:"periodic_limit"` + GlobalLimit state.Balance `json:"global_limit"` + IndividualReset time.Duration `json:"individual_reset"` //in hours + GlobalReset time.Duration `json:"global_rest"` //in hours +} + +func (lr *limitRequest) encode() []byte { + buff, _ := json.Marshal(lr) + return buff +} + +func (lr *limitRequest) decode(input []byte) error { + err := json.Unmarshal(input, lr) + return err +} + +type periodicResponse struct { + Used state.Balance `json:"tokens_poured"` + Start time.Time `json:"start_time"` + Restart string `json:"time_left"` + Allowed state.Balance `json:"tokens_allowed"` +} + +func (pr *periodicResponse) encode() []byte { + buff, _ := json.Marshal(pr) + return buff +} + +func (pr *periodicResponse) decode(input []byte) error { + err := json.Unmarshal(input, pr) + return err +} + +type GlobalNode struct { + ID string `json:"id"` + PourAmount state.Balance `json:"pour_amount"` + MaxPourAmount state.Balance `json:"max_pour_amount"` + PeriodicLimit state.Balance `json:"periodic_limit"` + GlobalLimit state.Balance `json:"global_limit"` + IndividualReset time.Duration `json:"individual_reset"` //in hours + GlobalReset time.Duration `json:"global_rest"` //in hours + Used state.Balance `json:"used"` + StartTime time.Time `json:"start_time"` +} + +func (gn *GlobalNode) GetKey() datastore.Key { + return datastore.Key(gn.ID + gn.ID) +} + +func (gn *GlobalNode) GetHash() string { + return util.ToHex(gn.GetHashBytes()) +} + +func (gn *GlobalNode) GetHashBytes() []byte { + return encryption.RawHash(gn.Encode()) +} + +func (gn *GlobalNode) Encode() []byte { + buff, _ := json.Marshal(gn) + return buff +} + +func (gn *GlobalNode) Decode(input []byte) error { + err := json.Unmarshal(input, gn) + return err +} + +type UserNode struct { + ID string `json:"id"` + StartTime time.Time `json:"start_time"` + Used state.Balance `json:"used"` +} + +func (un *UserNode) GetKey(globalKey string) datastore.Key { + return datastore.Key(globalKey + un.ID) +} + +func (un *UserNode) GetHash() string { + return util.ToHex(un.GetHashBytes()) +} + +func (un *UserNode) GetHashBytes() []byte { + return encryption.RawHash(un.Encode()) +} + +func (un *UserNode) Encode() []byte { + buff, _ := json.Marshal(un) + return buff +} + +func (un *UserNode) Decode(input []byte) error { + err := json.Unmarshal(input, un) + return err +}