core

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QCFmtStringTEXT         = 1
	QCFmtStringURL          = 2
	QCFmtStringJSON         = 3
	QCFmtStringInt          = 4
	QCFmtStringFloat        = 5
	QCFmtStringAddressHex   = 6
	QCFmtStringSignatureHex = 7

	QCFmtBytesSignature = 33
	QCFmtBytesAddress   = 34

	QCFmtImagePNG = 65
	QCFmtImageJPG = 66
	QCFmtImageBMP = 67

	QCFmtAudioWAV = 97
	QCFmtAudioMP3 = 98

	QCFmtVideoMP4 = 129
)
View Source
const (
	AttitudeRejectOnRef   = -2 // reject the quantum which use any quantum from this address as reference
	AttitudeReject        = -1 // reject any quantum from this address
	AttitudeIgnoreContent = 1  // accept the quantum from this address, but not eval the content, such as invite ...
	AttitudeAccept        = 2  // accept the quantum from this address as normal
	AttitudeBroadcast     = 3  // accept the quantum from this address, broadcast them and used them as reference
)

All Attitude State below is used to show my current subjective attitude to that individual for new quantums, not influence the quantums already be accepted before.

View Source
const (
	// QuantumTypeInfo specifies quantum which user just want to share information, such as
	// post articles, reply to others or chat message with others (encrypted by receiver's public key)
	QuantumTypeInfo = 0

	// QuantumTypeProfile specifies the quantum to update user's (signer's) profile.
	// contents = [key1(QCFmtStringTEXT), value1, key2, value2 ...]
	// if user want to update key1, send new QuantumTypeProfile quantum with
	// contents = [key1, newValue ...]
	// if user want to delete key1, send new QuantumTypeProfile quantum with
	// contents = [key1, newValue which content with empty data]
	QuantumTypeProfile = 1

	// QuantumTypeCommunity specifies the quantum of rule to build new community
	// contents[0] is the display information of current community
	// {fmt:QCFmtStringJSON/QCFmtStringTEXT..., data: ...}
	// contents[1] is the number of invitation (co-signature) from users in current community
	// {fmt:QCFmtStringInt, data:1} at least 1. (creater is absolutely same with others)
	// contents[2] is the max number of invitation by one user
	// {fmt:QCFmtStringInt, data:1} -1 means no limit, 0 means not allowed
	// contents[3] ~ contents[15] is the initial users in this community
	// {fmt:QCFmtBytesAddress/QCFmtStringAddressHex, data:0x1232...}
	// signer of this community is also the initial user in this community
	QuantumTypeCommunity = 2

	// QuantumTypeInvitation specifies the quantum of invitation
	// contents[0] is the signature of target community rule quantum
	// {fmt:QCFmtBytesSignature/QCFmtStringSignatureHex, data:signature of target community rule quantum}
	// contents[1] ~ contents[n] is the address of be invited
	// {fmt:QCFmtBytesAddress/QCFmtStringAddressHex, data:0x123...}
	// no matter all invitation send in same quantum of different quantum
	// only first n address (rule.contents[2]) will be accepted
	// user can not quit community, but any user can block any other user (or self) from any community
	// accepted by any community is decided by user in that community feel about u, not opposite.
	// User belong to community, quantum belong to user. (On trandation forum, posts usually belong to
	// one topic and have lots of tag, is just the function easy to implemnt not base struct here)
	QuantumTypeInvitation = 3

	// QuantumTypeEnd specifies the quantum of ending life cycle
	// When receive this quantum, no more quantum from same address should be received or broadcast.
	// The decision of end from any identity should be respected, no matter for security reason or just want to leave.
	QuantumTypeEnd = 4
)

Variables

View Source
var (
	FirstQuantumReference = Hex2Sig("0x00")
	SameQuantumReference  = Hex2Sig("0x01")
)

Functions

func Sig2Hex added in v0.3.0

func Sig2Hex(sig Sig) string

Types

type Attitude added in v0.3.0

type Attitude struct {
	Level    int        `json:"level"`              // Attitude const level
	Judgment string     `json:"judgment,omitempty"` // my subjective judgment
	Evidence []*Quantum `json:"evidence,omitempty"` // evidence of my judgment, can be omit but all quantum should come from current individual
}

Attitude show the subjective attitude to individual & reasons

type Community added in v0.3.0

type Community struct {
	Note         *QContent          `json:"note"`
	Define       Sig                `json:"define"`
	Creator      identity.Address   `json:"creator"`
	MinCosignCnt int                `json:"minCosignCnt"`
	MaxInviteCnt int                `json:"maxInviteCnt"`
	InitMembers  []identity.Address `json:"initMembers"`
}

func NewCommunity added in v0.3.0

func NewCommunity(quantum *Quantum) (*Community, error)

type Individual added in v0.3.0

type Individual struct {
	Address     identity.Address     `json:"address"`
	Profile     map[string]*QContent `json:"profile,omitempty"`
	Communities []*Community         `json:"communities,omitempty"`
	Attitude    *Attitude            `json:"attitude"`
	LastSig     Sig                  `json:"lastSignature,omitempty"`
	LastSeq     int64                `json:"lastSequence,omitempty"`
}

Individual is the user in pdu system

func NewIndividual added in v0.3.0

func NewIndividual(address identity.Address) *Individual

func (Individual) GetAddress added in v0.3.0

func (ind Individual) GetAddress() identity.Address

func (*Individual) UpdateAttitude added in v0.3.0

func (ind *Individual) UpdateAttitude(na *Attitude) error

func (*Individual) UpsertProfile added in v0.3.0

func (ind *Individual) UpsertProfile(cs []*QContent) error

type QContent added in v0.3.0

type QContent struct {
	Data   []byte `json:"data,omitempty"`
	Format int    `json:"fmt"`
}

QContent is one piece of data in Quantum, all variables should be in alphabetical order.

func CreateEmptyContent added in v0.3.0

func CreateEmptyContent() *QContent

func CreateFloatContent added in v0.3.0

func CreateFloatContent(num float64) *QContent

func CreateIntContent added in v0.3.0

func CreateIntContent(num int64) *QContent

func CreateTextContent added in v0.3.0

func CreateTextContent(t string) *QContent

func NewContent added in v0.3.0

func NewContent(fmt int, data []byte) (*QContent, error)

func (*QContent) GetData added in v0.3.0

func (c *QContent) GetData() (interface{}, error)

type Quantum added in v0.3.0

type Quantum struct {
	UnsignedQuantum
	Signature Sig `json:"sig,omitempty"`
}

Quantum defines the single message signed by user.

func CreateCommunityQuantum added in v0.3.0

func CreateCommunityQuantum(note string, minCosignCnt int, maxInviteCnt int, initAddrsHex []string, refs ...Sig) (*Quantum, error)

func CreateEndQuantum added in v0.3.0

func CreateEndQuantum(refs ...Sig) (*Quantum, error)

func CreateInfoQuantum added in v0.3.0

func CreateInfoQuantum(qcs []*QContent, refs ...Sig) (*Quantum, error)

func CreateInvitationQuantum added in v0.3.0

func CreateInvitationQuantum(target Sig, addrsHex []string, refs ...Sig) (*Quantum, error)

func CreateProfileQuantum added in v0.3.0

func CreateProfileQuantum(profiles map[string]interface{}, refs ...Sig) (*Quantum, error)

func NewQuantum added in v0.3.0

func NewQuantum(t int, cs []*QContent, refs ...Sig) (*Quantum, error)

NewQuantum try to build Quantum without signature

func (*Quantum) Ecrecover added in v0.3.0

func (q *Quantum) Ecrecover() (identity.Address, error)

Ecrecover recover

func (*Quantum) Sign added in v0.3.0

func (q *Quantum) Sign(did *identity.DID) error

Sign try to add signature to Quantum

type Sig added in v0.3.0

type Sig []byte

func Hex2Sig added in v0.3.0

func Hex2Sig(str string) Sig

type Universe

type Universe interface {
	// ReceiveQuantums will proccess quantums, verify the signature of each quantum. reject the quantums
	// from address in blacklist or conflict with quantums already exist. Both accept and wait quantums
	// will be saved. wait quantums usually cause by ref[0] is missing, so can not be broadcast and implement.
	ReceiveQuantums(originQuantums []*Quantum) (accept []Sig, wait []Sig, reject []Sig, err error)

	// ProcessSingleQuantum verify the signature, decide whether to accept or not, process the quantum by QType
	// return err if quantum not accept. casuse of verif-fail, signer in blacklist, conflict or any reason.
	ProcessSingleQuantum(sig Sig) error

	// ProcessQuantums have same function of ReceiveQuantums, but process quantums already in universe, which is
	// quantums from return "wait" of ReceiveQuantums.
	ProcessQuantums(limit, skip int) (accept []Sig, wait []Sig, reject []Sig, err error)

	// JudgeIndividual update the attitude towards Individual and how to process quantums from this signer.
	JudgeIndividual(address identity.Address, level int, judgment string, evidence ...[]Sig) error

	// JudgeCommunity update the attitude towards Community, decide if filter the individual in this community or not
	JudgeCommunity(sig Sig, level int, statement string) error

	// QueryQuantums query quantums from whole accepted quantums if address is nil, not filter by type if qType is 0
	QueryQuantums(address identity.Address, qType int, skip int, limit int, desc bool) ([]*Quantum, error)

	// QueryIndividuals query Individual from whole universe if community sig is nil.
	QueryIndividuals(sig Sig, skip int, limit int, desc bool) ([]*Individual, error)

	// GetCommunity return community by signature of community create signature.
	GetCommunity(sig Sig) (*Community, error)

	// GetIndividual return individual by address.
	GetIndividual(address identity.Address) (*Individual, error)

	// GetQuantum return quantum by signature of quantum.
	GetQuantum(sig Sig) (*Quantum, error)
}

Universe is an interface that describes an PDU Universe. A type that implements Universe contains all quantums which be received and accepted and has ability to process the quantums. The owner of node have right to decide accept or reject any quantums as they wish, so any universe may same or not with other's, usually one universe only contains part of whole exist quantums, which is not self-conflict.

type UnsignedQuantum added in v0.3.0

type UnsignedQuantum struct {
	// Contents contain all data in this quantum
	Contents []*QContent `json:"cs,omitempty"`

	// References must from the exist signature or 0x00
	// References[0] is the last signature by user-self, 0x00 if this quantum is the first quantum by user
	// References[1] is the last signature of public or important quantum by user-self, which quantum user
	// want other user to view most. (refs[1] usually contains article and forward, but not comments, like or chat)
	// if References[1] same with References[0], References[1] can be SameQuantumReference(0x01)
	// References[2] ~ References[n] is optional, recommend to use new & valid quantum
	// If two quantums by same user with same References[0], these two quantums will cause conflict, and
	// this user maybe block by others. The reason to do that punishment is user should act like individual,
	// all proactive event from one user should be sequence should be total order (全序关系). References[1~n]
	// do not need follow this restriction, because all other references show the partial order (偏序关系).
	// all the quantums which be reference as References[1] should be total order too.
	References []Sig `json:"refs"`

	// Type specifies the type of this quantum
	Type int `json:"type"`
}

UnsignedQuantum defines the single message from user without signature, all variables should be in alphabetical order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL