-
Notifications
You must be signed in to change notification settings - Fork 29
/
acknowledgment.go
122 lines (99 loc) · 3.49 KB
/
acknowledgment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package magmasc
import (
"encoding/json"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/zmagmacore/errors"
"github.com/0chain/gosdk/zmagmacore/storage"
)
type (
// Acknowledgment contains the necessary data obtained when the consumer
// accepts the provider terms and stores in the state of the blockchain
// as a result of performing the consumerAcceptTerms MagmaSmartContract function.
Acknowledgment struct {
SessionID string `json:"session_id"`
AccessPointID string `json:"access_point_id"`
Billing Billing `json:"billing"`
Consumer *Consumer `json:"consumer,omitempty"`
Provider *Provider `json:"provider,omitempty"`
Terms ProviderTerms `json:"terms"`
TokenPool *TokenPool `json:"token_pool,omitempty"`
}
)
var (
// Make sure Acknowledgment implements PoolConfigurator interface.
_ PoolConfigurator = (*Acknowledgment)(nil)
// Make sure Acknowledgment implements Value interface.
_ storage.Value = (*Acknowledgment)(nil)
// Make sure Acknowledgment implements Serializable interface.
_ util.Serializable = (*Acknowledgment)(nil)
)
// ActiveKey returns key used for operations with storage.Storage
// AcknowledgmentPrefix + AcknowledgmentActivePrefixPart + Acknowledgment.SessionID.
func (m *Acknowledgment) ActiveKey() []byte {
return []byte(AcknowledgmentPrefix + AcknowledgmentActivePrefixPart + m.SessionID)
}
// Decode implements util.Serializable interface.
func (m *Acknowledgment) Decode(blob []byte) error {
var ackn Acknowledgment
if err := json.Unmarshal(blob, &ackn); err != nil {
return errDecodeData.Wrap(err)
}
if err := ackn.Validate(); err != nil {
return err
}
m.SessionID = ackn.SessionID
m.AccessPointID = ackn.AccessPointID
m.Billing = ackn.Billing
m.Consumer = ackn.Consumer
m.Provider = ackn.Provider
m.Terms = ackn.Terms
m.TokenPool = ackn.TokenPool
return nil
}
// Encode implements util.Serializable interface.
func (m *Acknowledgment) Encode() []byte {
blob, _ := json.Marshal(m)
return blob
}
// Key returns key with AcknowledgmentPrefix.
// Used for operations with storage.Storage.
func (m *Acknowledgment) Key() []byte {
return []byte(AcknowledgmentPrefix + m.SessionID)
}
// PoolBalance implements PoolConfigurator interface.
func (m *Acknowledgment) PoolBalance() uint64 {
return m.Terms.GetAmount()
}
// PoolID implements PoolConfigurator interface.
func (m *Acknowledgment) PoolID() string {
return m.SessionID
}
// PoolHolderID implements PoolConfigurator interface.
func (m *Acknowledgment) PoolHolderID() string {
return Address
}
// PoolPayerID implements PoolConfigurator interface.
func (m *Acknowledgment) PoolPayerID() string {
return m.Consumer.ID
}
// PoolPayeeID implements PoolConfigurator interface.
func (m *Acknowledgment) PoolPayeeID() string {
return m.Provider.ID
}
// Validate checks Acknowledgment for correctness.
// If it is not return errInvalidAcknowledgment.
func (m *Acknowledgment) Validate() (err error) {
switch { // is invalid
case m.SessionID == "":
err = errors.New(errCodeBadRequest, "session id is required")
case m.AccessPointID == "":
err = errors.New(errCodeBadRequest, "access point id is required")
case m.Consumer == nil || m.Consumer.ExtID == "":
err = errors.New(errCodeBadRequest, "consumer external id is required")
case m.Provider == nil || m.Provider.ExtID == "":
err = errors.New(errCodeBadRequest, "provider external id is required")
default:
return nil // is valid
}
return errInvalidAcknowledgment.Wrap(err)
}