github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/private/engine/constellation/constellation.go (about) 1 package constellation 2 3 import ( 4 "fmt" 5 6 "github.com/kisexp/xdchain/private/engine" 7 8 "github.com/kisexp/xdchain/common" 9 "github.com/kisexp/xdchain/private/cache" 10 11 gocache "github.com/patrickmn/go-cache" 12 ) 13 14 type constellation struct { 15 node *Client 16 c *gocache.Cache 17 } 18 19 func Is(ptm interface{}) bool { 20 _, ok := ptm.(*constellation) 21 return ok 22 } 23 24 func New(client *engine.Client) *constellation { 25 return &constellation{ 26 node: &Client{ 27 httpClient: client.HttpClient, 28 }, 29 c: gocache.New(cache.DefaultExpiration, cache.CleanupInterval), 30 } 31 } 32 33 func (g *constellation) Send(data []byte, from string, to []string, extra *engine.ExtraMetadata) (string, []string, common.EncryptedPayloadHash, error) { 34 if extra.PrivacyFlag.IsNotStandardPrivate() { 35 return "", nil, common.EncryptedPayloadHash{}, engine.ErrPrivateTxManagerDoesNotSupportPrivacyEnhancements 36 } 37 out, err := g.node.SendPayload(data, from, to, extra.ACHashes, extra.ACMerkleRoot) 38 if err != nil { 39 return "", nil, common.EncryptedPayloadHash{}, err 40 } 41 cacheKey := string(out.Bytes()) 42 g.c.Set(cacheKey, cache.PrivateCacheItem{ 43 Payload: data, 44 Extra: *extra, 45 }, cache.DefaultExpiration) 46 return "", nil, out, nil 47 } 48 49 func (g *constellation) EncryptPayload(data []byte, from string, to []string, extra *engine.ExtraMetadata) ([]byte, error) { 50 return nil, engine.ErrPrivateTxManagerNotSupported 51 } 52 53 func (g *constellation) DecryptPayload(payload common.DecryptRequest) ([]byte, *engine.ExtraMetadata, error) { 54 return nil, nil, engine.ErrPrivateTxManagerNotSupported 55 } 56 57 func (g *constellation) StoreRaw(data []byte, from string) (common.EncryptedPayloadHash, error) { 58 return common.EncryptedPayloadHash{}, engine.ErrPrivateTxManagerNotSupported 59 } 60 61 func (g *constellation) SendSignedTx(data common.EncryptedPayloadHash, to []string, extra *engine.ExtraMetadata) (string, []string, []byte, error) { 62 return "", nil, nil, engine.ErrPrivateTxManagerNotSupported 63 } 64 65 func (g *constellation) ReceiveRaw(data common.EncryptedPayloadHash) ([]byte, string, *engine.ExtraMetadata, error) { 66 return nil, "", nil, engine.ErrPrivateTxManagerNotSupported 67 } 68 69 func (g *constellation) IsSender(txHash common.EncryptedPayloadHash) (bool, error) { 70 return false, engine.ErrPrivateTxManagerNotSupported 71 } 72 73 func (ptm *constellation) Groups() ([]engine.PrivacyGroup, error) { 74 return nil, engine.ErrPrivateTxManagerNotSupported 75 } 76 77 func (g *constellation) GetParticipants(txHash common.EncryptedPayloadHash) ([]string, error) { 78 return nil, engine.ErrPrivateTxManagerNotSupported 79 } 80 81 func (g *constellation) GetMandatory(txHash common.EncryptedPayloadHash) ([]string, error) { 82 return nil, engine.ErrPrivateTxManagerNotSupported 83 } 84 85 func (g *constellation) Receive(data common.EncryptedPayloadHash) (string, []string, []byte, *engine.ExtraMetadata, error) { 86 if common.EmptyEncryptedPayloadHash(data) { 87 return "", nil, nil, nil, nil 88 } 89 // Ignore this error since not being a recipient of 90 // a payload isn't an error. 91 // TODO: Return an error if it's anything OTHER than 92 // 'you are not a recipient.' 93 cacheKey := string(data.Bytes()) 94 x, found := g.c.Get(cacheKey) 95 if found { 96 cacheItem, ok := x.(cache.PrivateCacheItem) 97 if !ok { 98 return "", nil, nil, nil, fmt.Errorf("unknown cache item. expected type PrivateCacheItem") 99 } 100 return "", nil, cacheItem.Payload, &cacheItem.Extra, nil 101 } 102 privatePayload, acHashes, acMerkleRoot, err := g.node.ReceivePayload(data) 103 if nil != err { 104 return "", nil, nil, nil, err 105 } 106 extra := engine.ExtraMetadata{ 107 ACHashes: acHashes, 108 ACMerkleRoot: acMerkleRoot, 109 } 110 g.c.Set(cacheKey, cache.PrivateCacheItem{ 111 Payload: privatePayload, 112 Extra: extra, 113 }, cache.DefaultExpiration) 114 return "", nil, privatePayload, &extra, nil 115 } 116 117 func (g *constellation) Name() string { 118 return "Constellation" 119 } 120 121 func (g *constellation) HasFeature(f engine.PrivateTransactionManagerFeature) bool { 122 return false 123 }