[#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
candidateFeeConfigKey = "InnerRingCandidateFee"
version = 2
version = 3
innerRingKey = "innerring"
voteKey = "ballots"
@ -91,19 +91,22 @@ const (
var (
configPrefix = []byte("config")
ctx storage.Context
)
func Main(op string, args []interface{}) interface{} {
func init() {
// The trigger determines whether this smart-contract is being
// run in 'verification' or 'application' mode.
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":
}
func Init(args []interface{}) bool {
if storage.Get(ctx, innerRingKey) != nil {
panic("neofs: contract already deployed")
}
@ -124,16 +127,17 @@ func Main(op string, args []interface{}) interface{} {
runtime.Log("neofs: contract initialized")
return true
case "InnerRingList":
return getInnerRingNodes(ctx, innerRingKey)
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
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")
}
@ -153,12 +157,9 @@ func Main(op string, args []interface{}) interface{} {
setSerialized(ctx, candidatesKey, nodes)
return true
case "InnerRingCandidateAdd":
if len(args) != 1 {
panic("irCandidateAdd: bad arguments")
}
}
key := args[0].([]byte) // inner ring candidate public key
func InnerRingCandidateAdd(key []byte) bool {
if !runtime.CheckWitness(key) {
panic("irCandidateAdd: you should be the owner of the public key")
}
@ -185,17 +186,18 @@ func Main(op string, args []interface{}) interface{} {
setSerialized(ctx, candidatesKey, list)
return true
case "Deposit":
if len(args) < 2 || len(args) > 3 {
}
func Deposit(from []byte, args []interface{}) bool {
if len(args) < 1 || len(args) > 2 {
panic("deposit: bad arguments")
}
from := args[0].([]byte)
if !runtime.CheckWitness(from) {
panic("deposit: you should be the owner of the wallet")
}
amount := args[1].(int)
amount := args[0].(int)
if amount > 0 {
amount = amount * 100000000
}
@ -211,25 +213,21 @@ func Main(op string, args []interface{}) interface{} {
runtime.Log("deposit: funds have been transferred")
var rcv = from
if len(args) == 3 {
rcv = args[2].([]byte) // todo: check if rcv value is valid
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
case "Withdraw":
if len(args) != 2 {
panic("withdraw: bad arguments")
}
}
user := args[0].([]byte)
func Withdraw(user []byte, amount int) bool {
if !runtime.CheckWitness(user) {
panic("withdraw: you should be the owner of the wallet")
}
amount := args[1].(int)
if amount > 0 {
amount = amount * 100000000
}
@ -238,16 +236,9 @@ func Main(op string, args []interface{}) interface{} {
runtime.Notify("Withdraw", user, amount, tx.Hash)
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
}
func Cheque(id, user []byte, amount int, lockAcc []byte) bool {
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
@ -285,33 +276,44 @@ func Main(op string, args []interface{}) interface{} {
}
return true
case "Bind", "Unbind":
if len(args) < 2 {
panic("binding: bad arguments")
}
}
user := args[0].([]byte)
func Bind(user []byte, keys []interface{}) bool {
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 {
for i := 0; i < len(keys); i++ {
pubKey := keys[i].([]byte)
if len(pubKey) != publicKeySize {
panic("binding: incorrect public key size")
}
keys = append(keys, pub)
}
runtime.Notify(op, user, keys)
runtime.Notify("Bind", user, keys)
return true
case "InnerRingUpdate":
if len(args) < 1+minInnerRingSize {
// cheque id + inner ring public keys
}
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")
}
}
runtime.Notify("Unbind", user, keys)
return true
}
func InnerRingUpdate(chequeID []byte, args [][]byte) bool {
if len(args) < minInnerRingSize {
panic("irUpdate: bad arguments")
}
@ -323,23 +325,22 @@ func Main(op string, args []interface{}) interface{} {
panic("innerRingUpdate: invoked by non inner ring node")
}
id := args[0].([]byte)
c := cheque{id: id}
c := cheque{id: chequeID}
cashedCheques := getCashedCheques(ctx)
chequesList, ok := addCheque(cashedCheques, c)
if !ok {
panic("irUpdate: non unique id")
panic("irUpdate: non unique chequeID")
}
oldNodes := 0
candidates := getInnerRingNodes(ctx, candidatesKey)
newIR := []node{}
loop:
loop:
for i := 1; i < len(args); i++ {
key := args[i].([]byte)
key := args[i]
if len(key) != publicKeySize {
panic("irUpdate: invalid public key in inner ring list")
}
@ -366,7 +367,7 @@ func Main(op string, args []interface{}) interface{} {
panic("irUpdate: inner ring change rate must not be more than 1/3 ")
}
hashID := crypto.SHA256(id)
hashID := crypto.SHA256(chequeID)
n := vote(ctx, hashID, irKey)
if n >= threshold {
@ -381,12 +382,9 @@ func Main(op string, args []interface{}) interface{} {
}
return true
case "IsInnerRing":
if len(args) != 1 {
panic("isInnerRing: wrong arguments")
}
}
key := args[0].([]byte)
func IsInnerRing(key []byte) bool {
if len(key) != publicKeySize {
panic("isInnerRing: incorrect public key")
}
@ -401,19 +399,13 @@ func Main(op string, args []interface{}) interface{} {
}
return false
case "Config":
if len(args) != 1 {
panic("config: bad arguments")
}
key := args[0].([]byte)
}
func Config(key []byte) interface{} {
return getConfig(ctx, key)
case "SetConfig":
if len(args) != 3 {
panic("setConfig: bad arguments")
}
}
func SetConfig(id, key, val []byte) bool {
// check if it is inner ring invocation
irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1
@ -424,7 +416,6 @@ func Main(op string, args []interface{}) interface{} {
}
// check unique id of the operation
id := args[0].([]byte)
c := cheque{id: id}
cashedCheques := getCashedCheques(ctx)
@ -440,9 +431,6 @@ func Main(op string, args []interface{}) interface{} {
if n >= threshold {
removeVotes(ctx, hashID)
key := args[1]
val := args[2]
setConfig(ctx, key, val)
setSerialized(ctx, cashedChequesKey, chequesList)
@ -451,7 +439,9 @@ func Main(op string, args []interface{}) interface{} {
}
return true
case "ListConfig":
}
func ListConfig() []record {
var config []record
it := storage.Find(ctx, configPrefix)
@ -464,7 +454,9 @@ func Main(op string, args []interface{}) interface{} {
}
return config
case "InitConfig":
}
func InitConfig(args []interface{}) bool {
if getConfig(ctx, candidateFeeConfigKey) != nil {
panic("neofs: configuration already installed")
}
@ -486,11 +478,10 @@ func Main(op string, args []interface{}) interface{} {
runtime.Log("neofs: config has been installed")
return true
case "Version":
return version
}
}
panic("unknown operation")
func Version() int {
return version
}
// 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
}
// 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.
func removeVotes(ctx storage.Context, id []byte) {
var (