Rename "Deploy" method to "Init"

There was a bit of confusion between contract deploying
and calling "Deploy" method. Also new "Init" method
uses generic setter to initialize contract storage.
This commit is contained in:
alexvanin 2020-07-15 19:26:24 +03:00
parent 209740f060
commit 62d5d863f4

View file

@ -30,7 +30,10 @@ const (
tokenHash = "\x3b\x7d\x37\x11\xc6\xf0\xcc\xf9\xb1\xdc\xa9\x03\xd1\xbf\xa1\xd8\x96\xf1\x23\x8c"
innerRingCandidateFee = 100 * 1000 * 1000 // 10^8
version = 2
innerRingKey = "innerring"
voteKey = "ballots"
candidatesKey = "candidates"
cashedChequesKey = "cheques"
blockDiff = 20 // change base on performance evaluation
)
@ -65,34 +68,31 @@ func Main(op string, args []interface{}) interface{} {
*/
ctx := storage.GetContext()
switch op {
case "Deploy":
irList := getSerialized(ctx, "InnerRingList").([]node)
if len(irList) >= 3 {
panic("contract already deployed")
case "Init":
if storage.Get(ctx, innerRingKey) != nil {
panic("neofs: contract already deployed")
}
irList = []node{}
var irList []node
for i := 0; i < len(args); i++ {
pub := args[i].([]byte)
irList = append(irList, node{pub: pub})
}
data := binary.Serialize(irList)
storage.Put(ctx, "InnerRingList", data)
// initialize all storage slices
setSerialized(ctx, innerRingKey, irList)
setSerialized(ctx, voteKey, []ballot{})
setSerialized(ctx, candidatesKey, []node{})
setSerialized(ctx, cashedChequesKey, []cheque{})
data = binary.Serialize([]interface{}{})
storage.Put(ctx, "UsedVerifCheckList", data)
storage.Put(ctx, "InnerRingCandidates", data)
data = binary.Serialize([]ballot{})
storage.Put(ctx, voteKey, data)
runtime.Log("neofs: contract initialized")
return true
case "InnerRingList":
irList := getSerialized(ctx, "InnerRingList").([]node)
return irList
return getInnerRingNodes(ctx)
case "InnerRingCandidateRemove":
data := args[0].([]byte) // public key
if !runtime.CheckWitness(data) {
@ -308,11 +308,11 @@ func Main(op string, args []interface{}) interface{} {
panic("isInnerRing: incorrect public key")
}
irList := getSerialized(ctx, "InnerRingList").([]node)
irList := getInnerRingNodes(ctx)
for i := range irList {
node := irList[i]
if util.Equals(node.pub, key) {
if bytesEqual(node.pub, key) {
return true
}
}
@ -517,6 +517,16 @@ func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
storage.Put(ctx, key, data)
}
// getInnerRingNodes returns deserialized slice of inner ring nodes from storage.
func getInnerRingNodes(ctx storage.Context) []node {
data := storage.Get(ctx, innerRingKey)
if data != nil {
return binary.Deserialize(data.([]byte)).([]node)
}
return []node{}
}
// getInnerRingNodes returns deserialized slice of vote ballots.
func getBallots(ctx storage.Context) []ballot {
data := storage.Get(ctx, voteKey)