[#15] Refactor contract body for neo-go v0.91

In neo-go v0.91.0 manifest file should contain info
about all contract methods. To do that neo-go compiler
uses public function defined in contract file. This
commit splits entire Main function into smaller
independent contract methods.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-08-26 12:15:07 +03:00 committed by Alex Vanin
parent da3c75465b
commit 7a275a3c4b

View file

@ -77,7 +77,7 @@ const (
defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas
candidateFeeConfigKey = "InnerRingCandidateFee" candidateFeeConfigKey = "InnerRingCandidateFee"
version = 2 version = 3
innerRingKey = "innerring" innerRingKey = "innerring"
voteKey = "ballots" voteKey = "ballots"
@ -91,406 +91,397 @@ const (
var ( var (
configPrefix = []byte("config") configPrefix = []byte("config")
ctx storage.Context
) )
func Main(op string, args []interface{}) interface{} { func init() {
// The trigger determines whether this smart-contract is being // The trigger determines whether this smart-contract is being
// run in 'verification' or 'application' mode. // run in 'verification' or 'application' mode.
if runtime.GetTrigger() != runtime.Application { if runtime.GetTrigger() != runtime.Application {
return false panic("contract has not been called in application node")
} }
ctx := storage.GetContext() ctx = storage.GetContext()
switch op { }
case "Init":
if storage.Get(ctx, innerRingKey) != nil { func Init(args []interface{}) bool {
panic("neofs: contract already deployed") if storage.Get(ctx, innerRingKey) != nil {
panic("neofs: contract already deployed")
}
var irList []node
for i := 0; i < len(args); i++ {
pub := args[i].([]byte)
irList = append(irList, node{pub: pub})
}
// initialize all storage slices
setSerialized(ctx, innerRingKey, irList)
setSerialized(ctx, voteKey, []ballot{})
setSerialized(ctx, candidatesKey, []node{})
setSerialized(ctx, cashedChequesKey, []cheque{})
runtime.Log("neofs: contract initialized")
return true
}
func InnerRingList() []node {
return getInnerRingNodes(ctx, innerRingKey)
}
func InnerRingCandidates() []node {
return getInnerRingNodes(ctx, candidatesKey)
}
func InnerRingCandidateRemove(key []byte) bool {
if !runtime.CheckWitness(key) {
panic("irCandidateRemove: you should be the owner of the public key")
}
nodes := []node{} // it is explicit declaration of empty slice, not nil
candidates := getInnerRingNodes(ctx, candidatesKey)
for i := range candidates {
c := candidates[i]
if !bytesEqual(c.pub, key) {
nodes = append(nodes, c)
} else {
runtime.Log("irCandidateRemove: candidate has been removed")
} }
}
var irList []node setSerialized(ctx, candidatesKey, nodes)
for i := 0; i < len(args); i++ { return true
pub := args[i].([]byte) }
irList = append(irList, node{pub: pub})
}
// initialize all storage slices func InnerRingCandidateAdd(key []byte) bool {
setSerialized(ctx, innerRingKey, irList) if !runtime.CheckWitness(key) {
setSerialized(ctx, voteKey, []ballot{}) panic("irCandidateAdd: you should be the owner of the public key")
setSerialized(ctx, candidatesKey, []node{}) }
setSerialized(ctx, cashedChequesKey, []cheque{})
runtime.Log("neofs: contract initialized") c := node{pub: key}
candidates := getInnerRingNodes(ctx, candidatesKey)
return true list, ok := addNode(candidates, c)
case "InnerRingList": if !ok {
return getInnerRingNodes(ctx, innerRingKey) panic("irCandidateAdd: candidate already in the list")
case "InnerRingCandidates": }
return getInnerRingNodes(ctx, candidatesKey)
case "InnerRingCandidateRemove":
if len(args) != 1 {
panic("irCandidateRemove: bad arguments")
}
key := args[0].([]byte) // inner ring candidate public key from := contract.CreateStandardAccount(key)
if !runtime.CheckWitness(key) { to := runtime.GetExecutingScriptHash()
panic("irCandidateRemove: you should be the owner of the public key") fee := getConfig(ctx, candidateFeeConfigKey).(int)
} params := []interface{}{from, to, fee}
nodes := []node{} // it is explicit declaration of empty slice, not nil transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
candidates := getInnerRingNodes(ctx, candidatesKey) if !transferred {
panic("irCandidateAdd: failed to transfer funds, aborting")
}
for i := range candidates { runtime.Log("irCandidateAdd: candidate has been added")
c := candidates[i] setSerialized(ctx, candidatesKey, list)
if !bytesEqual(c.pub, key) {
nodes = append(nodes, c)
} else {
runtime.Log("irCandidateRemove: candidate has been removed")
}
}
setSerialized(ctx, candidatesKey, nodes) return true
}
return true func Deposit(from []byte, args []interface{}) bool {
case "InnerRingCandidateAdd": if len(args) < 1 || len(args) > 2 {
if len(args) != 1 { panic("deposit: bad arguments")
panic("irCandidateAdd: bad arguments") }
}
key := args[0].([]byte) // inner ring candidate public key if !runtime.CheckWitness(from) {
if !runtime.CheckWitness(key) { panic("deposit: you should be the owner of the wallet")
panic("irCandidateAdd: you should be the owner of the public key") }
}
c := node{pub: key} amount := args[0].(int)
candidates := getInnerRingNodes(ctx, candidatesKey) if amount > 0 {
amount = amount * 100000000
}
list, ok := addNode(candidates, c) to := runtime.GetExecutingScriptHash()
if !ok { params := []interface{}{from, to, amount}
panic("irCandidateAdd: candidate already in the list")
}
from := contract.CreateStandardAccount(key) transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
to := runtime.GetExecutingScriptHash() if !transferred {
fee := getConfig(ctx, candidateFeeConfigKey).(int) panic("deposit: failed to transfer funds, aborting")
params := []interface{}{from, to, fee} }
runtime.Log("deposit: funds have been transferred")
var rcv = from
if len(args) == 2 {
rcv = args[1].([]byte) // todo: check if rcv value is valid
}
tx := runtime.GetScriptContainer()
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
return true
}
func Withdraw(user []byte, amount int) bool {
if !runtime.CheckWitness(user) {
panic("withdraw: you should be the owner of the wallet")
}
if amount > 0 {
amount = amount * 100000000
}
tx := runtime.GetScriptContainer()
runtime.Notify("Withdraw", user, amount, tx.Hash)
return true
}
func Cheque(id, user []byte, amount int, lockAcc []byte) bool {
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
cashedCheques := getCashedCheques(ctx)
hashID := crypto.SHA256(id)
irKey := innerRingInvoker(irList)
if len(irKey) == 0 {
panic("cheque: invoked by non inner ring node")
}
c := cheque{id: id}
list, ok := addCheque(cashedCheques, c)
if !ok {
panic("cheque: non unique id")
}
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
from := runtime.GetExecutingScriptHash()
params := []interface{}{from, user, amount}
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool) transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
if !transferred { if !transferred {
panic("irCandidateAdd: failed to transfer funds, aborting") panic("cheque: failed to transfer funds, aborting")
} }
runtime.Log("irCandidateAdd: candidate has been added") runtime.Log("cheque: funds have been transferred")
setSerialized(ctx, candidatesKey, list)
return true setSerialized(ctx, cashedChequesKey, list)
case "Deposit": runtime.Notify("Cheque", id, user, amount, lockAcc)
if len(args) < 2 || len(args) > 3 { }
panic("deposit: bad arguments")
return true
}
func Bind(user []byte, keys []interface{}) bool {
if !runtime.CheckWitness(user) {
panic("binding: you should be the owner of the wallet")
}
for i := 0; i < len(keys); i++ {
pubKey := keys[i].([]byte)
if len(pubKey) != publicKeySize {
panic("binding: incorrect public key size")
} }
}
from := args[0].([]byte) runtime.Notify("Bind", user, keys)
if !runtime.CheckWitness(from) {
panic("deposit: you should be the owner of the wallet") return true
}
func Unbind(user []byte, keys []interface{}) bool {
if !runtime.CheckWitness(user) {
panic("unbinding: you should be the owner of the wallet")
}
for i := 0; i < len(keys); i++ {
pubKey := keys[i].([]byte)
if len(pubKey) != publicKeySize {
panic("unbinding: incorrect public key size")
} }
}
amount := args[1].(int) runtime.Notify("Unbind", user, keys)
if amount > 0 {
amount = amount * 100000000
}
to := runtime.GetExecutingScriptHash() return true
params := []interface{}{from, to, amount} }
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool) func InnerRingUpdate(chequeID []byte, args [][]byte) bool {
if !transferred { if len(args) < minInnerRingSize {
panic("deposit: failed to transfer funds, aborting") panic("irUpdate: bad arguments")
} }
runtime.Log("deposit: funds have been transferred") irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
var rcv = from irKey := innerRingInvoker(irList)
if len(args) == 3 { if len(irKey) == 0 {
rcv = args[2].([]byte) // todo: check if rcv value is valid panic("innerRingUpdate: invoked by non inner ring node")
} }
tx := runtime.GetScriptContainer() c := cheque{id: chequeID}
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
return true cashedCheques := getCashedCheques(ctx)
case "Withdraw":
if len(args) != 2 {
panic("withdraw: bad arguments")
}
user := args[0].([]byte) chequesList, ok := addCheque(cashedCheques, c)
if !runtime.CheckWitness(user) { if !ok {
panic("withdraw: you should be the owner of the wallet") panic("irUpdate: non unique chequeID")
} }
amount := args[1].(int) oldNodes := 0
if amount > 0 { candidates := getInnerRingNodes(ctx, candidatesKey)
amount = amount * 100000000 newIR := []node{}
}
tx := runtime.GetScriptContainer() loop:
runtime.Notify("Withdraw", user, amount, tx.Hash) for i := 1; i < len(args); i++ {
key := args[i]
return true
case "Cheque":
if len(args) != 4 {
panic("cheque: bad arguments")
}
id := args[0].([]byte) // unique cheque id
user := args[1].([]byte) // GAS receiver
amount := args[2].(int) // amount of GAS
lockAcc := args[3].([]byte) // lock account from internal balance contract
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
cashedCheques := getCashedCheques(ctx)
hashID := crypto.SHA256(id)
irKey := innerRingInvoker(irList)
if len(irKey) == 0 {
panic("cheque: invoked by non inner ring node")
}
c := cheque{id: id}
list, ok := addCheque(cashedCheques, c)
if !ok {
panic("cheque: non unique id")
}
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
from := runtime.GetExecutingScriptHash()
params := []interface{}{from, user, amount}
transferred := engine.AppCall([]byte(tokenHash), "transfer", params).(bool)
if !transferred {
panic("cheque: failed to transfer funds, aborting")
}
runtime.Log("cheque: funds have been transferred")
setSerialized(ctx, cashedChequesKey, list)
runtime.Notify("Cheque", id, user, amount, lockAcc)
}
return true
case "Bind", "Unbind":
if len(args) < 2 {
panic("binding: bad arguments")
}
user := args[0].([]byte)
if !runtime.CheckWitness(user) {
panic("binding: you should be the owner of the wallet")
}
var keys [][]byte
for i := 1; i < len(args); i++ {
pub := args[i].([]byte)
if len(pub) != publicKeySize {
panic("binding: incorrect public key size")
}
keys = append(keys, pub)
}
runtime.Notify(op, user, keys)
return true
case "InnerRingUpdate":
if len(args) < 1+minInnerRingSize {
// cheque id + inner ring public keys
panic("irUpdate: bad arguments")
}
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
irKey := innerRingInvoker(irList)
if len(irKey) == 0 {
panic("innerRingUpdate: invoked by non inner ring node")
}
id := args[0].([]byte)
c := cheque{id: id}
cashedCheques := getCashedCheques(ctx)
chequesList, ok := addCheque(cashedCheques, c)
if !ok {
panic("irUpdate: non unique id")
}
oldNodes := 0
candidates := getInnerRingNodes(ctx, candidatesKey)
newIR := []node{}
loop:
for i := 1; i < len(args); i++ {
key := args[i].([]byte)
if len(key) != publicKeySize {
panic("irUpdate: invalid public key in inner ring list")
}
// find key in actual inner ring list
for j := 0; j < len(irList); j++ {
n := irList[j]
if bytesEqual(n.pub, key) {
newIR = append(newIR, n)
oldNodes++
continue loop
}
}
// find key in candidates list
candidates, newIR, ok = rmNodeByKey(candidates, newIR, key)
if !ok {
panic("irUpdate: unknown public key in inner ring list")
}
}
if oldNodes < len(newIR)*2/3+1 {
panic("irUpdate: inner ring change rate must not be more than 1/3 ")
}
hashID := crypto.SHA256(id)
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
setSerialized(ctx, candidatesKey, candidates)
setSerialized(ctx, innerRingKey, newIR)
setSerialized(ctx, cashedChequesKey, chequesList)
runtime.Notify("InnerRingUpdate", c.id, newIR)
runtime.Log("irUpdate: inner ring list has been updated")
}
return true
case "IsInnerRing":
if len(args) != 1 {
panic("isInnerRing: wrong arguments")
}
key := args[0].([]byte)
if len(key) != publicKeySize { if len(key) != publicKeySize {
panic("isInnerRing: incorrect public key") panic("irUpdate: invalid public key in inner ring list")
} }
irList := getInnerRingNodes(ctx, innerRingKey) // find key in actual inner ring list
for i := range irList { for j := 0; j < len(irList); j++ {
node := irList[i] n := irList[j]
if bytesEqual(n.pub, key) {
newIR = append(newIR, n)
oldNodes++
if bytesEqual(node.pub, key) { continue loop
return true
} }
} }
return false // find key in candidates list
case "Config": candidates, newIR, ok = rmNodeByKey(candidates, newIR, key)
if len(args) != 1 {
panic("config: bad arguments")
}
key := args[0].([]byte)
return getConfig(ctx, key)
case "SetConfig":
if len(args) != 3 {
panic("setConfig: bad arguments")
}
// check if it is inner ring invocation
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
irKey := innerRingInvoker(irList)
if len(irKey) == 0 {
panic("setConfig: invoked by non inner ring node")
}
// check unique id of the operation
id := args[0].([]byte)
c := cheque{id: id}
cashedCheques := getCashedCheques(ctx)
chequesList, ok := addCheque(cashedCheques, c)
if !ok { if !ok {
panic("setConfig: non unique id") panic("irUpdate: unknown public key in inner ring list")
} }
// vote for new configuration value
hashID := crypto.SHA256(id)
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
key := args[1]
val := args[2]
setConfig(ctx, key, val)
setSerialized(ctx, cashedChequesKey, chequesList)
runtime.Notify("SetConfig", id, key, val)
runtime.Log("setConfig: configuration has been updated")
}
return true
case "ListConfig":
var config []record
it := storage.Find(ctx, configPrefix)
for iterator.Next(it) {
key := iterator.Key(it).([]byte)
val := iterator.Value(it).([]byte)
r := record{key: key[len(configPrefix):], val: val}
config = append(config, r)
}
return config
case "InitConfig":
if getConfig(ctx, candidateFeeConfigKey) != nil {
panic("neofs: configuration already installed")
}
ln := len(args)
if ln%2 != 0 {
panic("initConfig: bad arguments")
}
setConfig(ctx, candidateFeeConfigKey, defaultCandidateFee)
for i := 0; i < ln/2; i++ {
key := args[i*2]
val := args[i*2+1]
setConfig(ctx, key, val)
}
runtime.Log("neofs: config has been installed")
return true
case "Version":
return version
} }
panic("unknown operation") if oldNodes < len(newIR)*2/3+1 {
panic("irUpdate: inner ring change rate must not be more than 1/3 ")
}
hashID := crypto.SHA256(chequeID)
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
setSerialized(ctx, candidatesKey, candidates)
setSerialized(ctx, innerRingKey, newIR)
setSerialized(ctx, cashedChequesKey, chequesList)
runtime.Notify("InnerRingUpdate", c.id, newIR)
runtime.Log("irUpdate: inner ring list has been updated")
}
return true
}
func IsInnerRing(key []byte) bool {
if len(key) != publicKeySize {
panic("isInnerRing: incorrect public key")
}
irList := getInnerRingNodes(ctx, innerRingKey)
for i := range irList {
node := irList[i]
if bytesEqual(node.pub, key) {
return true
}
}
return false
}
func Config(key []byte) interface{} {
return getConfig(ctx, key)
}
func SetConfig(id, key, val []byte) bool {
// check if it is inner ring invocation
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
irKey := innerRingInvoker(irList)
if len(irKey) == 0 {
panic("setConfig: invoked by non inner ring node")
}
// check unique id of the operation
c := cheque{id: id}
cashedCheques := getCashedCheques(ctx)
chequesList, ok := addCheque(cashedCheques, c)
if !ok {
panic("setConfig: non unique id")
}
// vote for new configuration value
hashID := crypto.SHA256(id)
n := vote(ctx, hashID, irKey)
if n >= threshold {
removeVotes(ctx, hashID)
setConfig(ctx, key, val)
setSerialized(ctx, cashedChequesKey, chequesList)
runtime.Notify("SetConfig", id, key, val)
runtime.Log("setConfig: configuration has been updated")
}
return true
}
func ListConfig() []record {
var config []record
it := storage.Find(ctx, configPrefix)
for iterator.Next(it) {
key := iterator.Key(it).([]byte)
val := iterator.Value(it).([]byte)
r := record{key: key[len(configPrefix):], val: val}
config = append(config, r)
}
return config
}
func InitConfig(args []interface{}) bool {
if getConfig(ctx, candidateFeeConfigKey) != nil {
panic("neofs: configuration already installed")
}
ln := len(args)
if ln%2 != 0 {
panic("initConfig: bad arguments")
}
setConfig(ctx, candidateFeeConfigKey, defaultCandidateFee)
for i := 0; i < ln/2; i++ {
key := args[i*2]
val := args[i*2+1]
setConfig(ctx, key, val)
}
runtime.Log("neofs: config has been installed")
return true
}
func Version() int {
return version
} }
// innerRingInvoker returns public key of inner ring node that invoked contract. // innerRingInvoker returns public key of inner ring node that invoked contract.
@ -552,7 +543,7 @@ func vote(ctx storage.Context, id, from []byte) int {
return found return found
} }
// removeVotes clears ballots of the decision that has benn aceepted by // removeVotes clears ballots of the decision that has been accepted by
// inner ring nodes. // inner ring nodes.
func removeVotes(ctx storage.Context, id []byte) { func removeVotes(ctx storage.Context, id []byte) {
var ( var (