forked from lyriarte/blockchain-ssm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.go
70 lines (57 loc) · 1.42 KB
/
state.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
// Copyright Luc Yriarte <luc.yriarte@thingagora.org> 2018
// License: Apache-2.0
package main
import (
"errors"
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type State struct {
StateModel
}
//
// Storable interface implementation
//
func (self *State) Put(stub shim.ChaincodeStubInterface, key string) error {
data, err := self.Serialize()
if (err != nil) {
return err
}
return stub.PutState(key, data)
}
func (self *State) Get(stub shim.ChaincodeStubInterface, key string) error {
data, err := stub.GetState(key);
if (err != nil) {
return err
}
return self.Deserialize(data)
}
//
// Serializable interface implementation
//
func (self *State) Serialize() ([]byte, error) {
return json.Marshal(self.StateModel)
}
func (self *State) Deserialize(data []byte) error {
return json.Unmarshal(data, &self.StateModel)
}
//
// State API implementation
//
func (self *State) Perform(update *State, role string, action string) error {
// Check the proposed update iteration
if self.Iteration != update.Iteration {
return errors.New("Invalid iteration number of proposed update.")
}
// Set origin transition
self.Origin = &Transition{self.Current, update.Current, role, action}
// Increment iteration
self.Iteration++
// Update the current state
self.Current = update.Current
// Update public and private data
self.Public = update.Public
self.Private = update.Private
// All done
return nil
}