Skip to content

Commit

Permalink
refactor core/utils.go and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Mar 10, 2020
1 parent a3b08ff commit 634feef
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 27 deletions.
9 changes: 5 additions & 4 deletions cmd/pdu/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ package main
import (
"errors"
"fmt"
"github.com/pdupub/go-pdu/crypto"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
"github.com/spf13/cobra"

"github.com/howeyc/gopass"
"github.com/pdupub/go-pdu/core"
)

const (
Expand Down Expand Up @@ -84,7 +85,7 @@ func generate() error {
return crypto.ErrSigTypeNotSupport
}

engine, err = core.SelectEngine(accCrypt)
engine, err = utils.SelectEngine(accCrypt)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/pdu/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

"github.com/howeyc/gopass"
"github.com/mitchellh/go-homedir"
"github.com/pdupub/go-pdu/core"
"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
"github.com/pdupub/go-pdu/db"
"github.com/pdupub/go-pdu/db/bolt"
"github.com/pdupub/go-pdu/params"
Expand Down Expand Up @@ -121,7 +121,7 @@ func unlockKeyByCmd() (*crypto.PrivateKey, *crypto.PublicKey, error) {
return nil, nil, err
}

return core.DecryptKey(keyJson, string(passwd))
return utils.DecryptKey(keyJson, string(passwd))
}

func unlockKeyByFile(keyFile, passFile string) (*crypto.PrivateKey, *crypto.PublicKey, error) {
Expand All @@ -135,7 +135,7 @@ func unlockKeyByFile(keyFile, passFile string) (*crypto.PrivateKey, *crypto.Publ
return nil, nil, err
}

return core.DecryptKey(keyJson, strings.TrimSpace(string(passwd)))
return utils.DecryptKey(keyJson, strings.TrimSpace(string(passwd)))
}

func scanLine(input *string) {
Expand Down
6 changes: 4 additions & 2 deletions core/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package core

import (
"encoding/json"

"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
)

// Auth contain public key
Expand All @@ -35,7 +37,7 @@ func (a *Auth) UnmarshalJSON(input []byte) error {
}
a.Source = aMap["source"].(string)
a.SigType = aMap["sigType"].(string)
engine, err := SelectEngine(a.Source)
engine, err := utils.SelectEngine(a.Source)
if err != nil {
return err
}
Expand All @@ -49,7 +51,7 @@ func (a *Auth) UnmarshalJSON(input []byte) error {

// MarshalJSON marshal public key to json
func (a Auth) MarshalJSON() ([]byte, error) {
engine, err := SelectEngine(a.Source)
engine, err := utils.SelectEngine(a.Source)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions core/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"

"github.com/pdupub/go-pdu/common"
"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
)

// Message is valid msg in pdu
Expand Down Expand Up @@ -57,7 +59,7 @@ func CreateMsg(user *User, value *MsgValue, priKey *crypto.PrivateKey, refs ...*
Signature: nil,
}

engine, err := SelectEngine(priKey.Source)
engine, err := utils.SelectEngine(priKey.Source)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +81,7 @@ func CreateMsg(user *User, value *MsgValue, priKey *crypto.PrivateKey, refs ...*
func VerifyMsg(msg Message) (bool, error) {
signature := msg.Signature
msg.Signature = nil
engine, err := SelectEngine(signature.Source)
engine, err := utils.SelectEngine(signature.Source)
if err != nil {
return false, err
}
Expand Down
4 changes: 3 additions & 1 deletion core/msg_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package core

import (
"encoding/json"

"github.com/pdupub/go-pdu/common"
"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
)

const (
Expand Down Expand Up @@ -62,7 +64,7 @@ func (mv *DOBMsgContent) SignByParent(user *User, privKey crypto.PrivateKey) err
return err
}
var signature *crypto.Signature
engine, err := SelectEngine(privKey.Source)
engine, err := utils.SelectEngine(privKey.Source)
if err != nil {
return err
}
Expand Down
33 changes: 21 additions & 12 deletions core/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ import (
"github.com/pdupub/go-pdu/dag"
)

// Universe contain many space time on different time line
// Universe is the struct contain all the information can be received and validated.
// It is built with two precreate users as root users. Any message should be validated before
// add into msgD, which is a DAG used to store message. If new user create from valid message,
// the user should add into userD, a DAG used to store all users. The universe contain at least
// one spacetime, each spacetime base on one user msg as time line. validation of message mean
// this message should be valid at least in one of spacetime in stD. Infomation in local universe
// is only part of infomation in whole decentralized system.
type Universe struct {
msgD *dag.DAG // contain all messages valid in any universe (time proof)
userD *dag.DAG // contain all users valid in any universe (time proof) (strict)
stD *dag.DAG // contain all space time, which is the origin thought of PDU (strict)
msgD *dag.DAG // contain all messages valid in at least one spacetime
userD *dag.DAG // contain all users valid in at least one spacetime (strict)
stD *dag.DAG // contain all spacetime, which could be diff by selecting (strict)
}

// NewUniverse create Universe from two user with diff gender
// NewUniverse create Universe with two user with diff gender as root users
func NewUniverse(Eve, Adam *User) (*Universe, error) {
if Eve.Gender() == Adam.Gender() {
return nil, ErrNotSupportYet
Expand All @@ -51,9 +57,9 @@ func NewUniverse(Eve, Adam *User) (*Universe, error) {
return &Universe{userD: userD}, nil
}

// AddMsg will check if the msg from valid user,
// add new msg into Universe, and update time proof if
// msg.SenderID is belong to time proof
// AddMsg will check if the message from valid user, who is validated in at least one spacetime
// (in stD). Then new message will be added into Universe and update time proof if msg.SenderID
// is any spacetime based on.
func (u *Universe) AddMsg(msg *Message) error {
if !u.CheckUserExist(msg.SenderID) {
return ErrUserNotExist
Expand All @@ -66,7 +72,6 @@ func (u *Universe) AddMsg(msg *Message) error {
return err
}
} else {

// check
if u.GetMsgByID(msg.ID()) != nil {
return ErrMsgAlreadyExist
Expand Down Expand Up @@ -98,7 +103,7 @@ func (u *Universe) AddMsg(msg *Message) error {
return nil
}

// GetSpaceTimeIDs get ids in this space time
// GetSpaceTimeIDs get ids in of spacetime (list of msg.SenderID of each spacetime)
func (u *Universe) GetSpaceTimeIDs() []common.Hash {
var ids []common.Hash
if u.stD != nil {
Expand All @@ -109,7 +114,8 @@ func (u *Universe) GetSpaceTimeIDs() []common.Hash {
return ids
}

// AddSpaceTime will get all messages save in Universe with same msg.SenderID
// AddSpaceTime will add spacetime in Universe with msg.SenderID, and follow the
// time sequence from ref.
func (u *Universe) AddSpaceTime(msg *Message, ref *MsgReference) error {
if u.GetMsgByID(msg.ID()) == nil {
return ErrMsgNotFound
Expand Down Expand Up @@ -171,7 +177,7 @@ func (u *Universe) initializeSpaceTime(msgSpaceTime *Message, ref *MsgReference)
return nil
}

// CheckUserExist check if the user valid in this Universe
// CheckUserExist check if the user valid in the Universe
func (u Universe) CheckUserExist(userID common.Hash) bool {
if nil != u.GetUserByID(userID) {
return true
Expand Down Expand Up @@ -227,6 +233,9 @@ func (u Universe) GetMsgByID(msgID interface{}) *Message {
return nil
}

// initializeMsgD only run once to create u.msgD by initial message, and the DAG
// will remove strict rule, so msgD can accept new message if at least one of
// reference exist in whole universe.
func (u *Universe) initializeMsgD(msg *Message) error {
// build msg dag
msgVertex, err := dag.NewVertex(msg.ID(), msg)
Expand Down
3 changes: 2 additions & 1 deletion core/universe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pdupub/go-pdu/common/log"
"github.com/pdupub/go-pdu/core/rule"
"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/utils"
)

var (
Expand All @@ -40,7 +41,7 @@ var (
)

func TestNewUniverse(t *testing.T) {
universeEngine, _ = SelectEngine(crypto.ETH)
universeEngine, _ = utils.SelectEngine(crypto.ETH)

// Test 1: Create root users, Adam and Eve , create universe
// The gender of user relate to public key (random), so createRootUser
Expand Down
5 changes: 3 additions & 2 deletions core/utils.go → crypto/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PDU library. If not, see <http://www.gnu.org/licenses/>.

package core
package utils

import (
"encoding/json"
"strings"

"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/crypto/bitcoin"
"github.com/pdupub/go-pdu/crypto/ethereum"
"github.com/pdupub/go-pdu/crypto/pdu"
"strings"
)

// SelectEngine return a new engine by source type
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
Expand Down Expand Up @@ -133,6 +134,7 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pierrec/lz4 v0.0.0-20190327172049-315a67e90e41/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand Down Expand Up @@ -178,6 +180,7 @@ github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUW
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
Expand Down

0 comments on commit 634feef

Please sign in to comment.