diff --git a/service/api/service.go b/service/api/service.go deleted file mode 100644 index 1b9f9c17..00000000 --- a/service/api/service.go +++ /dev/null @@ -1,137 +0,0 @@ -package api - -import ( - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/document" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/treecache" - "go.uber.org/zap" - "io" - "net/http" - "time" -) - -const CName = "APIService" - -var log = logger.NewNamed("api") - -func New() app.Component { - return &service{} -} - -type service struct { - treeCache treecache.Service - documentService document.Service - srv *http.Server - cfg *config.Config -} - -func (s *service) Init(a *app.App) (err error) { - s.treeCache = a.MustComponent(treecache.CName).(treecache.Service) - s.documentService = a.MustComponent(document.CName).(document.Service) - s.cfg = a.MustComponent(config.CName).(*config.Config) - return nil -} - -func (s *service) Name() (name string) { - return CName -} - -func (s *service) Run(ctx context.Context) (err error) { - defer func() { - if err == nil { - log.With(zap.String("port", s.cfg.APIServer.Port)).Info("api server started running") - } - }() - - s.srv = &http.Server{ - Addr: fmt.Sprintf(":%s", s.cfg.APIServer.Port), - } - mux := http.NewServeMux() - mux.HandleFunc("/treeDump", s.treeDump) - mux.HandleFunc("/createDocumentTree", s.createDocumentTree) - mux.HandleFunc("/appendDocument", s.appendDocument) - s.srv.Handler = mux - - go s.runServer() - return nil -} - -func (s *service) runServer() { - err := s.srv.ListenAndServe() - if err != nil { - log.With(zap.Error(err)).Error("could not run api server") - } -} - -func (s *service) Close(ctx context.Context) (err error) { - return s.srv.Shutdown(ctx) -} - -func (s *service) treeDump(w http.ResponseWriter, req *http.Request) { - var ( - query = req.URL.Query() - treeId = query.Get("treeId") - dump string - err error - ) - err = s.treeCache.Do(context.Background(), treeId, func(obj interface{}) error { - t := obj.(tree.ObjectTree) - dump, err = t.DebugDump() - if err != nil { - return err - } - return nil - }) - if err != nil { - sendText(w, http.StatusInternalServerError, err.Error()) - return - } - sendText(w, http.StatusOK, dump) -} - -func (s *service) createDocumentTree(w http.ResponseWriter, req *http.Request) { - var ( - query = req.URL.Query() - text = query.Get("text") - aclListId = query.Get("aclListId") - ) - timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Second*30) - treeId, err := s.documentService.CreateDocumentTree(timeoutCtx, aclListId, text) - cancel() - if err != nil { - sendText(w, http.StatusInternalServerError, err.Error()) - return - } - sendText(w, http.StatusOK, treeId) -} - -func (s *service) appendDocument(w http.ResponseWriter, req *http.Request) { - var ( - query = req.URL.Query() - text = query.Get("text") - treeId = query.Get("treeId") - ) - timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Second*30) - err := s.documentService.UpdateDocumentTree(timeoutCtx, treeId, text) - cancel() - if err != nil { - sendText(w, http.StatusInternalServerError, err.Error()) - return - } - sendText(w, http.StatusOK, fmt.Sprintf("updated document with id: %s with text: %s", treeId, text)) -} - -func sendText(r http.ResponseWriter, code int, body string) { - r.Header().Set("Content-Type", "text/plain") - r.WriteHeader(code) - - _, err := io.WriteString(r, fmt.Sprintf("%s\n", body)) - if err != nil { - log.Error("writing response failed", zap.Error(err)) - } -} diff --git a/service/document/service.go b/service/document/service.go deleted file mode 100644 index 6d1192c2..00000000 --- a/service/document/service.go +++ /dev/null @@ -1,219 +0,0 @@ -package document - -import ( - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto/aclpb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - testchanges "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/testchanges/proto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/node" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/sync/message" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/treecache" - "github.com/anytypeio/go-anytype-infrastructure-experiments/syncproto" - "github.com/gogo/protobuf/proto" - "go.uber.org/zap" -) - -var CName = "DocumentService" - -var log = logger.NewNamed("documentservice") - -type service struct { - messageService message.Service - treeCache treecache.Service - account account.Service - storage storage.Service - // to create new documents we need to know all nodes - nodes []*node.Node -} - -type Service interface { - UpdateDocumentTree(ctx context.Context, id, text string) error - CreateDocumentTree(ctx context.Context, aclTreeId string, text string) (id string, err error) -} - -func New() app.Component { - return &service{} -} - -func (s *service) Init(a *app.App) (err error) { - s.account = a.MustComponent(account.CName).(account.Service) - s.messageService = a.MustComponent(message.CName).(message.Service) - s.treeCache = a.MustComponent(treecache.CName).(treecache.Service) - s.storage = a.MustComponent(storage.CName).(storage.Service) - - nodesService := a.MustComponent(node.CName).(node.Service) - s.nodes = nodesService.Nodes() - - return nil -} - -func (s *service) Name() (name string) { - return CName -} - -func (s *service) Run(ctx context.Context) (err error) { - syncData := s.storage.ImportedACLSyncData() - - // we could have added a timeout or some additional logic, - // but let's just use the ACL id of the latest started node :-) - return s.messageService.SendToSpaceAsync("", syncproto.WrapACLList( - &syncproto.SyncACLList{Records: syncData.Records}, - syncData.Header, - syncData.Id, - )) -} - -func (s *service) Close(ctx context.Context) (err error) { - return nil -} - -func (s *service) UpdateDocumentTree(ctx context.Context, id, text string) (err error) { - var ( - ch *aclpb.RawChange - header *aclpb.Header - snapshotPath []string - heads []string - ) - log.With(zap.String("id", id), zap.String("text", text)). - Debug("updating document") - - err = s.treeCache.Do(ctx, id, func(obj interface{}) error { - docTree, ok := obj.(tree.ObjectTree) - if !ok { - return fmt.Errorf("can't update acl trees with text") - } - - docTree.Lock() - defer docTree.Unlock() - err = s.treeCache.Do(ctx, docTree.Header().AclListId, func(obj interface{}) error { - aclTree := obj.(list.ACLList) - aclTree.RLock() - defer aclTree.RUnlock() - - content := createAppendTextChange(text) - signable := tree.SignableChangeContent{ - Proto: content, - Key: s.account.Account().SignKey, - Identity: s.account.Account().Identity, - IsSnapshot: false, - } - ch, err = docTree.AddContent(ctx, signable) - if err != nil { - return err - } - return nil - }) - if err != nil { - return err - } - - id = docTree.ID() - heads = docTree.Heads() - header = docTree.Header() - snapshotPath = docTree.SnapshotPath() - return nil - }) - if err != nil { - return err - } - log.With( - zap.String("id", id), - zap.Strings("heads", heads), - zap.String("header", header.String())). - Debug("document updated in the database") - - return s.messageService.SendToSpaceAsync("", syncproto.WrapHeadUpdate(&syncproto.SyncHeadUpdate{ - Heads: heads, - Changes: []*aclpb.RawChange{ch}, - SnapshotPath: snapshotPath, - }, header, id)) -} - -func (s *service) CreateDocumentTree(ctx context.Context, aclListId string, text string) (id string, err error) { - acc := s.account.Account() - var ( - ch *aclpb.RawChange - header *aclpb.Header - snapshotPath []string - heads []string - ) - err = s.treeCache.Do(ctx, aclListId, func(obj interface{}) error { - t := obj.(list.ACLList) - t.RLock() - defer t.RUnlock() - - content := createInitialTextChange(text) - doc, err := tree.CreateNewTreeStorage(acc, t, content, s.storage.CreateTreeStorage) - if err != nil { - return err - } - - id, err = doc.ID() - if err != nil { - return err - } - - header, err = doc.Header() - if err != nil { - return err - } - - heads = []string{header.FirstId} - snapshotPath = []string{header.FirstId} - ch, err = doc.GetRawChange(ctx, header.FirstId) - if err != nil { - return err - } - - return nil - }) - if err != nil { - return "", err - } - - log.With(zap.String("id", id), zap.String("text", text)). - Debug("creating document") - - err = s.messageService.SendToSpaceAsync("", syncproto.WrapHeadUpdate(&syncproto.SyncHeadUpdate{ - Heads: heads, - Changes: []*aclpb.RawChange{ch}, - SnapshotPath: snapshotPath, - }, header, id)) - if err != nil { - return "", err - } - return id, err -} - -func createInitialTextChange(text string) proto.Marshaler { - return &testchanges.PlainTextChangeData{ - Content: []*testchanges.PlainTextChangeContent{ - createAppendTextChangeContent(text), - }, - Snapshot: &testchanges.PlainTextChangeSnapshot{Text: text}, - } -} - -func createAppendTextChange(text string) proto.Marshaler { - return &testchanges.PlainTextChangeData{ - Content: []*testchanges.PlainTextChangeContent{ - createAppendTextChangeContent(text), - }, - } -} - -func createAppendTextChangeContent(text string) *testchanges.PlainTextChangeContent { - return &testchanges.PlainTextChangeContent{ - Value: &testchanges.PlainTextChangeContentValueOfTextAppend{ - TextAppend: &testchanges.PlainTextChangeTextAppend{ - Text: text, - }, - }, - } -} diff --git a/service/storage/service.go b/service/storage/service.go deleted file mode 100644 index 627627c0..00000000 --- a/service/storage/service.go +++ /dev/null @@ -1,71 +0,0 @@ -package storage - -import ( - "context" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto/aclpb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" -) - -var CName = "storage" - -var log = logger.NewNamed("storage").Sugar() - -type ImportedACLSyncData struct { - Id string - Header *aclpb.Header - Records []*aclpb.RawACLRecord -} - -type Service interface { - storage.Provider - ImportedACLSyncData() ImportedACLSyncData -} - -func New() app.Component { - return &service{} -} - -type service struct { - storageProvider storage.Provider - importedACLSyncData ImportedACLSyncData -} - -func (s *service) Init(a *app.App) (err error) { - s.storageProvider = storage.NewInMemoryTreeStorageProvider() - // importing hardcoded acl list, check that the keys there are correct - return nil -} - -func (s *service) Storage(treeId string) (storage.Storage, error) { - return s.storageProvider.Storage(treeId) -} - -func (s *service) AddStorage(id string, st storage.Storage) error { - return s.storageProvider.AddStorage(id, st) -} - -func (s *service) CreateTreeStorage(payload storage.TreeStorageCreatePayload) (storage.TreeStorage, error) { - return s.storageProvider.CreateTreeStorage(payload) -} - -func (s *service) CreateACLListStorage(payload storage.ACLListStorageCreatePayload) (storage.ListStorage, error) { - return s.storageProvider.CreateACLListStorage(payload) -} - -func (s *service) Name() (name string) { - return CName -} - -func (s *service) ImportedACLSyncData() ImportedACLSyncData { - return s.importedACLSyncData -} - -func (s *service) Run(ctx context.Context) (err error) { - return nil -} - -func (s service) Close(ctx context.Context) (err error) { - return nil -} diff --git a/service/treecache/service.go b/service/treecache/service.go deleted file mode 100644 index 90e91b98..00000000 --- a/service/treecache/service.go +++ /dev/null @@ -1,127 +0,0 @@ -package treecache - -import ( - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/account" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclrecordproto/aclpb" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list" - aclstorage "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/storage" - "go.uber.org/zap" -) - -const CName = "treecache" - -type ObjFunc = func(obj interface{}) error - -var log = logger.NewNamed("treecache") - -type Service interface { - Do(ctx context.Context, id string, f ObjFunc) error - Add(ctx context.Context, id string, payload any) error -} - -type service struct { - storage storage.Service - account account.Service - cache ocache.OCache -} - -func New() app.ComponentRunnable { - return &service{} -} - -func (s *service) Do(ctx context.Context, treeId string, f ObjFunc) error { - log. - With(zap.String("treeId", treeId)). - Debug("requesting tree from cache to perform operation") - - t, err := s.cache.Get(ctx, treeId) - defer s.cache.Release(treeId) - if err != nil { - return err - } - return f(t) -} - -func (s *service) Add(ctx context.Context, treeId string, payload any) error { - switch pl := payload.(type) { - case aclstorage.TreeStorageCreatePayload: - log. - With(zap.String("treeId", treeId), zap.Int("len(changes)", len(pl.Changes))). - Debug("adding Tree with changes") - - _, err := s.storage.CreateTreeStorage(payload.(aclstorage.TreeStorageCreatePayload)) - if err != nil { - return err - } - case aclstorage.ACLListStorageCreatePayload: - log. - With(zap.String("treeId", treeId), zap.Int("len(changes)", len(pl.Records))). - Debug("adding ACLList with records") - - _, err := s.storage.CreateACLListStorage(payload.(aclstorage.ACLListStorageCreatePayload)) - if err != nil { - return err - } - - } - return nil -} - -func (s *service) Init(a *app.App) (err error) { - s.cache = ocache.New(s.loadTree) - s.account = a.MustComponent(account.CName).(account.Service) - s.storage = a.MustComponent(storage.CName).(storage.Service) - // TODO: for test we should load some predefined keys - return nil -} - -func (s *service) Name() (name string) { - return CName -} - -func (s *service) Run(ctx context.Context) (err error) { - return nil -} - -func (s *service) Close(ctx context.Context) (err error) { - return s.cache.Close() -} - -func (s *service) loadTree(ctx context.Context, id string) (ocache.Object, error) { - t, err := s.storage.Storage(id) - if err != nil { - return nil, err - } - header, err := t.Header() - if err != nil { - return nil, err - } - - switch header.DocType { // handler - case aclpb.Header_ACL: - return list.BuildACLListWithIdentity(s.account.Account(), t.(aclstorage.ListStorage)) - case aclpb.Header_DocTree: - break - default: - return nil, fmt.Errorf("incorrect type") - } - log.Info("got header", zap.String("header", header.String())) - var objTree tree.ObjectTree - err = s.Do(ctx, header.AclListId, func(obj interface{}) error { - aclList := obj.(list.ACLList) - objTree, err = tree.BuildObjectTree(t.(aclstorage.TreeStorage), nil, aclList) - if err != nil { - return err - } - return nil - }) - - return objTree, err -} diff --git a/syncproto/helpers.go b/syncproto/helpers.go deleted file mode 100644 index fbbf11dc..00000000 --- a/syncproto/helpers.go +++ /dev/null @@ -1,45 +0,0 @@ -package syncproto - -import ( - "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" -) - -func WrapHeadUpdate(update *SyncHeadUpdate, header *aclpb.Header, treeId string) *Sync { - return &Sync{ - Message: &SyncContentValue{ - Value: &SyncContentValueValueOfHeadUpdate{HeadUpdate: update}, - }, - TreeHeader: header, - TreeId: treeId, - } -} - -func WrapFullRequest(request *SyncFullRequest, header *aclpb.Header, treeId string) *Sync { - return &Sync{ - Message: &SyncContentValue{ - Value: &SyncContentValueValueOfFullSyncRequest{FullSyncRequest: request}, - }, - TreeHeader: header, - TreeId: treeId, - } -} - -func WrapFullResponse(response *SyncFullResponse, header *aclpb.Header, treeId string) *Sync { - return &Sync{ - Message: &SyncContentValue{ - Value: &SyncContentValueValueOfFullSyncResponse{FullSyncResponse: response}, - }, - TreeHeader: header, - TreeId: treeId, - } -} - -func WrapACLList(aclList *SyncACLList, header *aclpb.Header, id string) *Sync { - return &Sync{ - Message: &SyncContentValue{ - Value: &SyncContentValueValueOfAclList{AclList: aclList}, - }, - TreeHeader: header, - TreeId: id, - } -} diff --git a/syncproto/proto/sync.proto b/syncproto/proto/sync.proto deleted file mode 100644 index d1c60d7d..00000000 --- a/syncproto/proto/sync.proto +++ /dev/null @@ -1,49 +0,0 @@ -syntax = "proto3"; -package anytype; -option go_package = "/syncproto"; - -import "pkg/acl/aclrecordproto/aclpb/protos/aclrecordproto.proto"; - -message Message { - Header header = 1; - bytes data = 2; -} - -message Header { - bytes traceId = 1; - uint64 requestId = 2; - uint64 replyId = 3; - MessageType type = 4; - string debugInfo = 5; -} - -enum MessageType { - MessageTypeSystem = 0; - MessageTypeSpace = 1; - MessageTypeSync = 2; -} - -message System { - Handshake handshake = 1; - Ping ping = 2; - Ack ack = 3; - - message Handshake { - string protocolVersion = 1; - } - message Ping { - uint64 unixTime = 1; - } - message Ack { - Error error = 2; - } - message Error { - Code code = 1; - string description = 2; - - enum Code { - UNKNOWN = 0; - UNSUPPORTED_PROTOCOL_VERSION = 10; - } - } -} diff --git a/syncproto/sync.pb.go b/syncproto/sync.pb.go deleted file mode 100644 index 20a189a1..00000000 --- a/syncproto/sync.pb.go +++ /dev/null @@ -1,1791 +0,0 @@ -// Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: syncproto/proto/sync.proto - -package syncproto - -import ( - fmt "fmt" - _ "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb" - proto "github.com/gogo/protobuf/proto" - io "io" - math "math" - math_bits "math/bits" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package - -type MessageType int32 - -const ( - MessageType_MessageTypeSystem MessageType = 0 - MessageType_MessageTypeSpace MessageType = 1 - MessageType_MessageTypeSync MessageType = 2 -) - -var MessageType_name = map[int32]string{ - 0: "MessageTypeSystem", - 1: "MessageTypeSpace", - 2: "MessageTypeSync", -} - -var MessageType_value = map[string]int32{ - "MessageTypeSystem": 0, - "MessageTypeSpace": 1, - "MessageTypeSync": 2, -} - -func (x MessageType) String() string { - return proto.EnumName(MessageType_name, int32(x)) -} - -func (MessageType) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{0} -} - -type System_Error_Code int32 - -const ( - System_Error_UNKNOWN System_Error_Code = 0 - System_Error_UNSUPPORTED_PROTOCOL_VERSION System_Error_Code = 10 -) - -var System_Error_Code_name = map[int32]string{ - 0: "UNKNOWN", - 10: "UNSUPPORTED_PROTOCOL_VERSION", -} - -var System_Error_Code_value = map[string]int32{ - "UNKNOWN": 0, - "UNSUPPORTED_PROTOCOL_VERSION": 10, -} - -func (x System_Error_Code) String() string { - return proto.EnumName(System_Error_Code_name, int32(x)) -} - -func (System_Error_Code) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2, 3, 0} -} - -type Message struct { - Header *Header `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` - Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` -} - -func (m *Message) Reset() { *m = Message{} } -func (m *Message) String() string { return proto.CompactTextString(m) } -func (*Message) ProtoMessage() {} -func (*Message) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{0} -} -func (m *Message) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Message) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Message.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *Message) XXX_Merge(src proto.Message) { - xxx_messageInfo_Message.Merge(m, src) -} -func (m *Message) XXX_Size() int { - return m.Size() -} -func (m *Message) XXX_DiscardUnknown() { - xxx_messageInfo_Message.DiscardUnknown(m) -} - -var xxx_messageInfo_Message proto.InternalMessageInfo - -func (m *Message) GetHeader() *Header { - if m != nil { - return m.Header - } - return nil -} - -func (m *Message) GetData() []byte { - if m != nil { - return m.Data - } - return nil -} - -type Header struct { - TraceId []byte `protobuf:"bytes,1,opt,name=traceId,proto3" json:"traceId,omitempty"` - RequestId uint64 `protobuf:"varint,2,opt,name=requestId,proto3" json:"requestId,omitempty"` - ReplyId uint64 `protobuf:"varint,3,opt,name=replyId,proto3" json:"replyId,omitempty"` - Type MessageType `protobuf:"varint,4,opt,name=type,proto3,enum=anytype.MessageType" json:"type,omitempty"` - DebugInfo string `protobuf:"bytes,5,opt,name=debugInfo,proto3" json:"debugInfo,omitempty"` -} - -func (m *Header) Reset() { *m = Header{} } -func (m *Header) String() string { return proto.CompactTextString(m) } -func (*Header) ProtoMessage() {} -func (*Header) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{1} -} -func (m *Header) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Header) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Header.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *Header) XXX_Merge(src proto.Message) { - xxx_messageInfo_Header.Merge(m, src) -} -func (m *Header) XXX_Size() int { - return m.Size() -} -func (m *Header) XXX_DiscardUnknown() { - xxx_messageInfo_Header.DiscardUnknown(m) -} - -var xxx_messageInfo_Header proto.InternalMessageInfo - -func (m *Header) GetTraceId() []byte { - if m != nil { - return m.TraceId - } - return nil -} - -func (m *Header) GetRequestId() uint64 { - if m != nil { - return m.RequestId - } - return 0 -} - -func (m *Header) GetReplyId() uint64 { - if m != nil { - return m.ReplyId - } - return 0 -} - -func (m *Header) GetType() MessageType { - if m != nil { - return m.Type - } - return MessageType_MessageTypeSystem -} - -func (m *Header) GetDebugInfo() string { - if m != nil { - return m.DebugInfo - } - return "" -} - -type System struct { - Handshake *System_Handshake `protobuf:"bytes,1,opt,name=handshake,proto3" json:"handshake,omitempty"` - Ping *System_Ping `protobuf:"bytes,2,opt,name=ping,proto3" json:"ping,omitempty"` - Ack *System_Ack `protobuf:"bytes,3,opt,name=ack,proto3" json:"ack,omitempty"` -} - -func (m *System) Reset() { *m = System{} } -func (m *System) String() string { return proto.CompactTextString(m) } -func (*System) ProtoMessage() {} -func (*System) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2} -} -func (m *System) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *System) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_System.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *System) XXX_Merge(src proto.Message) { - xxx_messageInfo_System.Merge(m, src) -} -func (m *System) XXX_Size() int { - return m.Size() -} -func (m *System) XXX_DiscardUnknown() { - xxx_messageInfo_System.DiscardUnknown(m) -} - -var xxx_messageInfo_System proto.InternalMessageInfo - -func (m *System) GetHandshake() *System_Handshake { - if m != nil { - return m.Handshake - } - return nil -} - -func (m *System) GetPing() *System_Ping { - if m != nil { - return m.Ping - } - return nil -} - -func (m *System) GetAck() *System_Ack { - if m != nil { - return m.Ack - } - return nil -} - -type System_Handshake struct { - ProtocolVersion string `protobuf:"bytes,1,opt,name=protocolVersion,proto3" json:"protocolVersion,omitempty"` -} - -func (m *System_Handshake) Reset() { *m = System_Handshake{} } -func (m *System_Handshake) String() string { return proto.CompactTextString(m) } -func (*System_Handshake) ProtoMessage() {} -func (*System_Handshake) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2, 0} -} -func (m *System_Handshake) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *System_Handshake) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_System_Handshake.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *System_Handshake) XXX_Merge(src proto.Message) { - xxx_messageInfo_System_Handshake.Merge(m, src) -} -func (m *System_Handshake) XXX_Size() int { - return m.Size() -} -func (m *System_Handshake) XXX_DiscardUnknown() { - xxx_messageInfo_System_Handshake.DiscardUnknown(m) -} - -var xxx_messageInfo_System_Handshake proto.InternalMessageInfo - -func (m *System_Handshake) GetProtocolVersion() string { - if m != nil { - return m.ProtocolVersion - } - return "" -} - -type System_Ping struct { - UnixTime uint64 `protobuf:"varint,1,opt,name=unixTime,proto3" json:"unixTime,omitempty"` -} - -func (m *System_Ping) Reset() { *m = System_Ping{} } -func (m *System_Ping) String() string { return proto.CompactTextString(m) } -func (*System_Ping) ProtoMessage() {} -func (*System_Ping) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2, 1} -} -func (m *System_Ping) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *System_Ping) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_System_Ping.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *System_Ping) XXX_Merge(src proto.Message) { - xxx_messageInfo_System_Ping.Merge(m, src) -} -func (m *System_Ping) XXX_Size() int { - return m.Size() -} -func (m *System_Ping) XXX_DiscardUnknown() { - xxx_messageInfo_System_Ping.DiscardUnknown(m) -} - -var xxx_messageInfo_System_Ping proto.InternalMessageInfo - -func (m *System_Ping) GetUnixTime() uint64 { - if m != nil { - return m.UnixTime - } - return 0 -} - -type System_Ack struct { - Error *System_Error `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"` -} - -func (m *System_Ack) Reset() { *m = System_Ack{} } -func (m *System_Ack) String() string { return proto.CompactTextString(m) } -func (*System_Ack) ProtoMessage() {} -func (*System_Ack) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2, 2} -} -func (m *System_Ack) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *System_Ack) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_System_Ack.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *System_Ack) XXX_Merge(src proto.Message) { - xxx_messageInfo_System_Ack.Merge(m, src) -} -func (m *System_Ack) XXX_Size() int { - return m.Size() -} -func (m *System_Ack) XXX_DiscardUnknown() { - xxx_messageInfo_System_Ack.DiscardUnknown(m) -} - -var xxx_messageInfo_System_Ack proto.InternalMessageInfo - -func (m *System_Ack) GetError() *System_Error { - if m != nil { - return m.Error - } - return nil -} - -type System_Error struct { - Code System_Error_Code `protobuf:"varint,1,opt,name=code,proto3,enum=anytype.System_Error_Code" json:"code,omitempty"` - Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` -} - -func (m *System_Error) Reset() { *m = System_Error{} } -func (m *System_Error) String() string { return proto.CompactTextString(m) } -func (*System_Error) ProtoMessage() {} -func (*System_Error) Descriptor() ([]byte, []int) { - return fileDescriptor_4b28dfdd48a89166, []int{2, 3} -} -func (m *System_Error) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *System_Error) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_System_Error.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *System_Error) XXX_Merge(src proto.Message) { - xxx_messageInfo_System_Error.Merge(m, src) -} -func (m *System_Error) XXX_Size() int { - return m.Size() -} -func (m *System_Error) XXX_DiscardUnknown() { - xxx_messageInfo_System_Error.DiscardUnknown(m) -} - -var xxx_messageInfo_System_Error proto.InternalMessageInfo - -func (m *System_Error) GetCode() System_Error_Code { - if m != nil { - return m.Code - } - return System_Error_UNKNOWN -} - -func (m *System_Error) GetDescription() string { - if m != nil { - return m.Description - } - return "" -} - -func init() { - proto.RegisterEnum("anytype.MessageType", MessageType_name, MessageType_value) - proto.RegisterEnum("anytype.System_Error_Code", System_Error_Code_name, System_Error_Code_value) - proto.RegisterType((*Message)(nil), "anytype.Message") - proto.RegisterType((*Header)(nil), "anytype.Header") - proto.RegisterType((*System)(nil), "anytype.System") - proto.RegisterType((*System_Handshake)(nil), "anytype.System.Handshake") - proto.RegisterType((*System_Ping)(nil), "anytype.System.Ping") - proto.RegisterType((*System_Ack)(nil), "anytype.System.Ack") - proto.RegisterType((*System_Error)(nil), "anytype.System.Error") -} - -func init() { proto.RegisterFile("syncproto/proto/sync.proto", fileDescriptor_4b28dfdd48a89166) } - -var fileDescriptor_4b28dfdd48a89166 = []byte{ - // 536 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x93, 0xd1, 0x8a, 0xd3, 0x40, - 0x14, 0x86, 0x3b, 0xdb, 0x6c, 0x6b, 0x4f, 0x96, 0xdd, 0x38, 0xbb, 0x0b, 0x31, 0x2c, 0x21, 0x14, - 0xc5, 0xa2, 0x90, 0x4a, 0x65, 0xf1, 0x7a, 0x5d, 0x2b, 0x2d, 0x6a, 0x53, 0xa6, 0xed, 0x0a, 0xde, - 0x2c, 0xd3, 0xc9, 0x98, 0x86, 0x76, 0x93, 0x98, 0xa4, 0x60, 0xde, 0x62, 0x9f, 0xc1, 0x67, 0xf0, - 0x21, 0xbc, 0xdc, 0x4b, 0x2f, 0xa5, 0x7d, 0x11, 0x99, 0x49, 0xda, 0xc6, 0xe2, 0x45, 0x92, 0x39, - 0xff, 0xf9, 0xff, 0x33, 0xdf, 0x30, 0x04, 0x8c, 0x24, 0x0b, 0x58, 0x14, 0x87, 0x69, 0xd8, 0xce, - 0xdf, 0xa2, 0xb6, 0xe5, 0x12, 0xd7, 0x69, 0x90, 0xa5, 0x59, 0xc4, 0x8d, 0x57, 0xd1, 0xdc, 0x6b, - 0x53, 0xb6, 0x10, 0x0f, 0x9b, 0xd1, 0xc0, 0xe3, 0x89, 0x58, 0x46, 0xd3, 0x3c, 0x93, 0x94, 0xf4, - 0x3c, 0xda, 0x7c, 0x0f, 0xf5, 0x4f, 0x3c, 0x49, 0xa8, 0xc7, 0xf1, 0x73, 0xa8, 0xcd, 0x38, 0x75, - 0x79, 0xac, 0x23, 0x0b, 0xb5, 0xd4, 0xce, 0x89, 0x5d, 0x8c, 0xb5, 0x7b, 0x52, 0x26, 0x45, 0x1b, - 0x63, 0x50, 0x5c, 0x9a, 0x52, 0xfd, 0xc0, 0x42, 0xad, 0x23, 0x22, 0xd7, 0xcd, 0x1f, 0x08, 0x6a, - 0xb9, 0x0d, 0xeb, 0x50, 0x4f, 0x63, 0xca, 0x78, 0xdf, 0x95, 0x83, 0x8e, 0xc8, 0xa6, 0xc4, 0x17, - 0xd0, 0x88, 0xf9, 0xb7, 0x25, 0x4f, 0xd2, 0xbe, 0x2b, 0xd3, 0x0a, 0xd9, 0x09, 0x22, 0x17, 0xf3, - 0x68, 0x91, 0xf5, 0x5d, 0xbd, 0x2a, 0x7b, 0x9b, 0x12, 0xb7, 0x40, 0x11, 0x1c, 0xba, 0x62, 0xa1, - 0xd6, 0x71, 0xe7, 0x6c, 0xcb, 0x55, 0x90, 0x8f, 0xb3, 0x88, 0x13, 0xe9, 0x10, 0x3b, 0xb8, 0x7c, - 0xba, 0xf4, 0xfa, 0xc1, 0xd7, 0x50, 0x3f, 0xb4, 0x50, 0xab, 0x41, 0x76, 0x42, 0xf3, 0x67, 0x15, - 0x6a, 0xa3, 0x2c, 0x49, 0xf9, 0x1d, 0x7e, 0x03, 0x8d, 0x19, 0x0d, 0xdc, 0x64, 0x46, 0xe7, 0xbc, - 0x38, 0xef, 0x93, 0xed, 0xdc, 0xdc, 0x63, 0xf7, 0x36, 0x06, 0xb2, 0xf3, 0x0a, 0x96, 0xc8, 0x0f, - 0x3c, 0x89, 0xaf, 0x96, 0x58, 0x8a, 0xcc, 0xd0, 0x0f, 0x3c, 0x22, 0x1d, 0xf8, 0x19, 0x54, 0x29, - 0x9b, 0xcb, 0xb3, 0xa8, 0x9d, 0xd3, 0x7d, 0xe3, 0x15, 0x9b, 0x13, 0xd1, 0x37, 0x2e, 0xa1, 0xd1, - 0x2b, 0x4d, 0x3f, 0x91, 0xf7, 0xc2, 0xc2, 0xc5, 0x0d, 0x8f, 0x13, 0x3f, 0x0c, 0x24, 0x5c, 0x83, - 0xec, 0xcb, 0x46, 0x13, 0x14, 0xb1, 0x17, 0x36, 0xe0, 0xd1, 0x32, 0xf0, 0xbf, 0x8f, 0xfd, 0xbb, - 0xfc, 0x1c, 0x0a, 0xd9, 0xd6, 0x46, 0x07, 0xaa, 0x57, 0x6c, 0x8e, 0x5f, 0xc2, 0x21, 0x8f, 0xe3, - 0x30, 0x2e, 0x98, 0xcf, 0xf7, 0x51, 0xba, 0xa2, 0x49, 0x72, 0x8f, 0x71, 0x8f, 0xe0, 0x50, 0x0a, - 0xd8, 0x06, 0x85, 0x85, 0x6e, 0x3e, 0xf5, 0xb8, 0x63, 0xfc, 0x37, 0x65, 0x5f, 0x87, 0x2e, 0x27, - 0xd2, 0x87, 0x2d, 0x50, 0x5d, 0x9e, 0xb0, 0xd8, 0x8f, 0x52, 0xc1, 0x7d, 0x20, 0xb9, 0xcb, 0x52, - 0xf3, 0x12, 0x14, 0xe1, 0xc7, 0x2a, 0xd4, 0x27, 0x83, 0x0f, 0x03, 0xe7, 0xf3, 0x40, 0xab, 0x60, - 0x0b, 0x2e, 0x26, 0x83, 0xd1, 0x64, 0x38, 0x74, 0xc8, 0xb8, 0xfb, 0xee, 0x76, 0x48, 0x9c, 0xb1, - 0x73, 0xed, 0x7c, 0xbc, 0xbd, 0xe9, 0x92, 0x51, 0xdf, 0x19, 0x68, 0xf0, 0xc2, 0x01, 0xb5, 0x74, - 0xd3, 0xf8, 0x1c, 0x1e, 0x97, 0xca, 0x9c, 0x46, 0xab, 0xe0, 0x33, 0xd0, 0xca, 0x72, 0x44, 0x19, - 0xd7, 0x10, 0x3e, 0x85, 0x93, 0x7f, 0xcc, 0x01, 0xd3, 0x0e, 0xde, 0x3e, 0xfd, 0xb5, 0x32, 0xd1, - 0xc3, 0xca, 0x44, 0x7f, 0x56, 0x26, 0xba, 0x5f, 0x9b, 0x95, 0x87, 0xb5, 0x59, 0xf9, 0xbd, 0x36, - 0x2b, 0x5f, 0xa0, 0xbd, 0xfd, 0xcd, 0xa6, 0x35, 0xf9, 0x79, 0xfd, 0x37, 0x00, 0x00, 0xff, 0xff, - 0x5f, 0xee, 0x92, 0xca, 0x7a, 0x03, 0x00, 0x00, -} - -func (m *Message) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Message) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Message) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Data) > 0 { - i -= len(m.Data) - copy(dAtA[i:], m.Data) - i = encodeVarintSync(dAtA, i, uint64(len(m.Data))) - i-- - dAtA[i] = 0x12 - } - if m.Header != nil { - { - size, err := m.Header.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintSync(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *Header) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Header) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Header) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.DebugInfo) > 0 { - i -= len(m.DebugInfo) - copy(dAtA[i:], m.DebugInfo) - i = encodeVarintSync(dAtA, i, uint64(len(m.DebugInfo))) - i-- - dAtA[i] = 0x2a - } - if m.Type != 0 { - i = encodeVarintSync(dAtA, i, uint64(m.Type)) - i-- - dAtA[i] = 0x20 - } - if m.ReplyId != 0 { - i = encodeVarintSync(dAtA, i, uint64(m.ReplyId)) - i-- - dAtA[i] = 0x18 - } - if m.RequestId != 0 { - i = encodeVarintSync(dAtA, i, uint64(m.RequestId)) - i-- - dAtA[i] = 0x10 - } - if len(m.TraceId) > 0 { - i -= len(m.TraceId) - copy(dAtA[i:], m.TraceId) - i = encodeVarintSync(dAtA, i, uint64(len(m.TraceId))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *System) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *System) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *System) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.Ack != nil { - { - size, err := m.Ack.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintSync(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a - } - if m.Ping != nil { - { - size, err := m.Ping.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintSync(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - if m.Handshake != nil { - { - size, err := m.Handshake.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintSync(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *System_Handshake) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *System_Handshake) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *System_Handshake) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.ProtocolVersion) > 0 { - i -= len(m.ProtocolVersion) - copy(dAtA[i:], m.ProtocolVersion) - i = encodeVarintSync(dAtA, i, uint64(len(m.ProtocolVersion))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *System_Ping) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *System_Ping) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *System_Ping) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.UnixTime != 0 { - i = encodeVarintSync(dAtA, i, uint64(m.UnixTime)) - i-- - dAtA[i] = 0x8 - } - return len(dAtA) - i, nil -} - -func (m *System_Ack) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *System_Ack) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *System_Ack) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.Error != nil { - { - size, err := m.Error.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintSync(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - return len(dAtA) - i, nil -} - -func (m *System_Error) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *System_Error) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *System_Error) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Description) > 0 { - i -= len(m.Description) - copy(dAtA[i:], m.Description) - i = encodeVarintSync(dAtA, i, uint64(len(m.Description))) - i-- - dAtA[i] = 0x12 - } - if m.Code != 0 { - i = encodeVarintSync(dAtA, i, uint64(m.Code)) - i-- - dAtA[i] = 0x8 - } - return len(dAtA) - i, nil -} - -func encodeVarintSync(dAtA []byte, offset int, v uint64) int { - offset -= sovSync(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return base -} -func (m *Message) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Header != nil { - l = m.Header.Size() - n += 1 + l + sovSync(uint64(l)) - } - l = len(m.Data) - if l > 0 { - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func (m *Header) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.TraceId) - if l > 0 { - n += 1 + l + sovSync(uint64(l)) - } - if m.RequestId != 0 { - n += 1 + sovSync(uint64(m.RequestId)) - } - if m.ReplyId != 0 { - n += 1 + sovSync(uint64(m.ReplyId)) - } - if m.Type != 0 { - n += 1 + sovSync(uint64(m.Type)) - } - l = len(m.DebugInfo) - if l > 0 { - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func (m *System) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Handshake != nil { - l = m.Handshake.Size() - n += 1 + l + sovSync(uint64(l)) - } - if m.Ping != nil { - l = m.Ping.Size() - n += 1 + l + sovSync(uint64(l)) - } - if m.Ack != nil { - l = m.Ack.Size() - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func (m *System_Handshake) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.ProtocolVersion) - if l > 0 { - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func (m *System_Ping) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.UnixTime != 0 { - n += 1 + sovSync(uint64(m.UnixTime)) - } - return n -} - -func (m *System_Ack) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Error != nil { - l = m.Error.Size() - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func (m *System_Error) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Code != 0 { - n += 1 + sovSync(uint64(m.Code)) - } - l = len(m.Description) - if l > 0 { - n += 1 + l + sovSync(uint64(l)) - } - return n -} - -func sovSync(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 -} -func sozSync(x uint64) (n int) { - return sovSync(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *Message) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Message: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Message: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Header", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Header == nil { - m.Header = &Header{} - } - if err := m.Header.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Data = append(m.Data[:0], dAtA[iNdEx:postIndex]...) - if m.Data == nil { - m.Data = []byte{} - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *Header) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Header: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Header: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TraceId", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.TraceId = append(m.TraceId[:0], dAtA[iNdEx:postIndex]...) - if m.TraceId == nil { - m.TraceId = []byte{} - } - iNdEx = postIndex - case 2: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field RequestId", wireType) - } - m.RequestId = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.RequestId |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 3: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field ReplyId", wireType) - } - m.ReplyId = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.ReplyId |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 4: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) - } - m.Type = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Type |= MessageType(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 5: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field DebugInfo", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.DebugInfo = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *System) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: System: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: System: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Handshake", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Handshake == nil { - m.Handshake = &System_Handshake{} - } - if err := m.Handshake.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Ping", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Ping == nil { - m.Ping = &System_Ping{} - } - if err := m.Ping.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Ack", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Ack == nil { - m.Ack = &System_Ack{} - } - if err := m.Ack.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *System_Handshake) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Handshake: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Handshake: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ProtocolVersion", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.ProtocolVersion = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *System_Ping) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Ping: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Ping: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field UnixTime", wireType) - } - m.UnixTime = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.UnixTime |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *System_Ack) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Ack: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Ack: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Error == nil { - m.Error = &System_Error{} - } - if err := m.Error.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *System_Error) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Error: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Error: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Code", wireType) - } - m.Code = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Code |= System_Error_Code(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Description", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowSync - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthSync - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthSync - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Description = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipSync(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthSync - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipSync(dAtA []byte) (n int, err error) { - l := len(dAtA) - iNdEx := 0 - depth := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowSync - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowSync - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if dAtA[iNdEx-1] < 0x80 { - break - } - } - case 1: - iNdEx += 8 - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowSync - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if length < 0 { - return 0, ErrInvalidLengthSync - } - iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupSync - } - depth-- - case 5: - iNdEx += 4 - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - if iNdEx < 0 { - return 0, ErrInvalidLengthSync - } - if depth == 0 { - return iNdEx, nil - } - } - return 0, io.ErrUnexpectedEOF -} - -var ( - ErrInvalidLengthSync = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowSync = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupSync = fmt.Errorf("proto: unexpected end of group") -) diff --git a/util/crc16/crc16.go b/util/crc16/crc16.go deleted file mode 100644 index 810f4bed..00000000 --- a/util/crc16/crc16.go +++ /dev/null @@ -1,122 +0,0 @@ -// Package crc16 is implementation according to CCITT standards. -// -// Note by @antirez: this is actually the XMODEM CRC 16 algorithm, using the -// following parameters: -// -// Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN" -// Width : 16 bit -// Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) -// Initialization : 0000 -// Reflect Input byte : False -// Reflect Output CRC : False -// Xor constant to output CRC : 0000 -// Output for "123456789" : 31C3 -// -// ported from the c++ code in the stellar-core codebase -// (https://github.com/stellar/stellar-core). The code is licensed -// as: -/* - * Copyright 2001-2010 Georges Menie (www.menie.org) - * Copyright 2010-2012 Salvatore Sanfilippo (adapted to Redis coding style) - * Copyright 2015 Stellar Development Foundation (ported to go) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the University of California, Berkeley nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package crc16 - -import ( - "bytes" - "encoding/binary" - "fmt" -) - -// ErrInvalidChecksum is returned when Validate determines either the checksum -// or the payload has been corrupted -var ErrInvalidChecksum = fmt.Errorf("invalid checksum") - -var crc16tab = [256]uint16{ - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, - 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, - 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, - 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, - 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, - 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, - 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, - 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, - 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, - 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, - 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, - 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, - 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, - 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, - 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, - 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, - 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, - 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, - 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, - 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, - 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, - 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, - 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, -} - -// Checksum returns the 2-byte checksum for the provided data -func Checksum(data []byte) []byte { - var crc uint16 - var out bytes.Buffer - for _, b := range data { - crc = ((crc << 8) & 0xffff) ^ crc16tab[((crc>>8)^uint16(b))&0x00FF] - } - - err := binary.Write(&out, binary.LittleEndian, crc) - if err != nil { - panic(err) - } - - return out.Bytes() -} - -// Validate returns an error if the provided checksum does not match -// the calculated checksum of the provided data -func Validate(data []byte, expected []byte) error { - - actual := Checksum(data) - - // validate the provided checksum against the calculated - if !bytes.Equal(actual, expected) { - return ErrInvalidChecksum - } - - return nil -} diff --git a/util/strkey/strkey.go b/util/strkey/strkey.go deleted file mode 100644 index 0e766f12..00000000 --- a/util/strkey/strkey.go +++ /dev/null @@ -1,118 +0,0 @@ -package strkey - -import ( - "bytes" - "encoding/binary" - "fmt" - - "github.com/mr-tron/base58/base58" - - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/crc16" -) - -// ErrInvalidVersionByte is returned when the version byte from a provided -// strkey-encoded string is not one of the valid values. -var ErrInvalidVersionByte = fmt.Errorf("invalid version byte") - -// VersionByte represents one of the possible prefix values for a StrKey base -// string--the string the when encoded using base58 yields a final StrKey. -type VersionByte byte - -// Decode decodes the provided StrKey into a raw value, checking the checksum -// and ensuring the expected VersionByte (the version parameter) is the value -// actually encoded into the provided src string. -func Decode(expected VersionByte, src string) ([]byte, error) { - raw, err := decodeString(src) - if err != nil { - return nil, err - } - - // decode into components - version := VersionByte(raw[0]) - vp := raw[0 : len(raw)-2] - payload := raw[1 : len(raw)-2] - checksum := raw[len(raw)-2:] - - // ensure version byte is expected - if version != expected { - return nil, ErrInvalidVersionByte - } - - // ensure checksum is valid - if err := crc16.Validate(vp, checksum); err != nil { - return nil, err - } - - // if we made it through the gaunlet, return the decoded value - return payload, nil -} - -// MustDecode is like Decode, but panics on error -func MustDecode(expected VersionByte, src string) []byte { - d, err := Decode(expected, src) - if err != nil { - panic(err) - } - return d -} - -// Encode encodes the provided data to a StrKey, using the provided version -// byte. -func Encode(version VersionByte, src []byte) (string, error) { - var raw bytes.Buffer - - // write version byte - if err := binary.Write(&raw, binary.LittleEndian, version); err != nil { - return "", err - } - - // write payload - if _, err := raw.Write(src); err != nil { - return "", err - } - - // calculate and write checksum - checksum := crc16.Checksum(raw.Bytes()) - if _, err := raw.Write(checksum); err != nil { - return "", err - } - - result := base58.FastBase58Encoding(raw.Bytes()) - return result, nil -} - -// MustEncode is like Encode, but panics on error -func MustEncode(version VersionByte, src []byte) string { - e, err := Encode(version, src) - if err != nil { - panic(err) - } - return e -} - -// Version extracts and returns the version byte from the provided source -// string. -func Version(src string) (VersionByte, error) { - raw, err := decodeString(src) - if err != nil { - return VersionByte(0), err - } - - return VersionByte(raw[0]), nil -} - -// decodeString decodes a base58 string into the raw bytes, and ensures it could -// potentially be strkey encoded (i.e. it has both a version byte and a -// checksum, neither of which are explicitly checked by this func) -func decodeString(src string) ([]byte, error) { - raw, err := base58.FastBase58Decoding(src) - if err != nil { - return nil, fmt.Errorf("base58 decode failed: %s", err) - } - - if len(raw) < 3 { - return nil, fmt.Errorf("encoded value is %d bytes; minimum valid length is 3", len(raw)) - } - - return raw, nil -}