forked from TrueCloudLab/frostfs-contract
[#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:
parent
da3c75465b
commit
7a275a3c4b
1 changed files with 340 additions and 349 deletions
|
@ -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,19 +91,22 @@ 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":
|
|
||||||
|
func Init(args []interface{}) bool {
|
||||||
if storage.Get(ctx, innerRingKey) != nil {
|
if storage.Get(ctx, innerRingKey) != nil {
|
||||||
panic("neofs: contract already deployed")
|
panic("neofs: contract already deployed")
|
||||||
}
|
}
|
||||||
|
@ -124,16 +127,17 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
runtime.Log("neofs: contract initialized")
|
runtime.Log("neofs: contract initialized")
|
||||||
|
|
||||||
return true
|
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) {
|
if !runtime.CheckWitness(key) {
|
||||||
panic("irCandidateRemove: you should be the owner of the public 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)
|
setSerialized(ctx, candidatesKey, nodes)
|
||||||
|
|
||||||
return true
|
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) {
|
if !runtime.CheckWitness(key) {
|
||||||
panic("irCandidateAdd: you should be the owner of the public 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)
|
setSerialized(ctx, candidatesKey, list)
|
||||||
|
|
||||||
return true
|
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")
|
panic("deposit: bad arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
from := args[0].([]byte)
|
|
||||||
if !runtime.CheckWitness(from) {
|
if !runtime.CheckWitness(from) {
|
||||||
panic("deposit: you should be the owner of the wallet")
|
panic("deposit: you should be the owner of the wallet")
|
||||||
}
|
}
|
||||||
|
|
||||||
amount := args[1].(int)
|
amount := args[0].(int)
|
||||||
if amount > 0 {
|
if amount > 0 {
|
||||||
amount = amount * 100000000
|
amount = amount * 100000000
|
||||||
}
|
}
|
||||||
|
@ -211,25 +213,21 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
runtime.Log("deposit: funds have been transferred")
|
runtime.Log("deposit: funds have been transferred")
|
||||||
|
|
||||||
var rcv = from
|
var rcv = from
|
||||||
if len(args) == 3 {
|
if len(args) == 2 {
|
||||||
rcv = args[2].([]byte) // todo: check if rcv value is valid
|
rcv = args[1].([]byte) // todo: check if rcv value is valid
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := runtime.GetScriptContainer()
|
tx := runtime.GetScriptContainer()
|
||||||
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
|
runtime.Notify("Deposit", from, amount, rcv, tx.Hash)
|
||||||
|
|
||||||
return true
|
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) {
|
if !runtime.CheckWitness(user) {
|
||||||
panic("withdraw: you should be the owner of the wallet")
|
panic("withdraw: you should be the owner of the wallet")
|
||||||
}
|
}
|
||||||
|
|
||||||
amount := args[1].(int)
|
|
||||||
if amount > 0 {
|
if amount > 0 {
|
||||||
amount = amount * 100000000
|
amount = amount * 100000000
|
||||||
}
|
}
|
||||||
|
@ -238,16 +236,9 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
runtime.Notify("Withdraw", user, amount, tx.Hash)
|
runtime.Notify("Withdraw", user, amount, tx.Hash)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
case "Cheque":
|
|
||||||
if len(args) != 4 {
|
|
||||||
panic("cheque: bad arguments")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
id := args[0].([]byte) // unique cheque id
|
func Cheque(id, user []byte, amount int, lockAcc []byte) bool {
|
||||||
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)
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
||||||
threshold := len(irList)/3*2 + 1
|
threshold := len(irList)/3*2 + 1
|
||||||
|
|
||||||
|
@ -285,33 +276,44 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
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) {
|
if !runtime.CheckWitness(user) {
|
||||||
panic("binding: you should be the owner of the wallet")
|
panic("binding: you should be the owner of the wallet")
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys [][]byte
|
for i := 0; i < len(keys); i++ {
|
||||||
|
pubKey := keys[i].([]byte)
|
||||||
for i := 1; i < len(args); i++ {
|
if len(pubKey) != publicKeySize {
|
||||||
pub := args[i].([]byte)
|
|
||||||
if len(pub) != publicKeySize {
|
|
||||||
panic("binding: incorrect public key size")
|
panic("binding: incorrect public key size")
|
||||||
}
|
}
|
||||||
|
|
||||||
keys = append(keys, pub)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.Notify(op, user, keys)
|
runtime.Notify("Bind", user, keys)
|
||||||
|
|
||||||
return true
|
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")
|
panic("irUpdate: bad arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,14 +325,13 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
panic("innerRingUpdate: invoked by non inner ring node")
|
panic("innerRingUpdate: invoked by non inner ring node")
|
||||||
}
|
}
|
||||||
|
|
||||||
id := args[0].([]byte)
|
c := cheque{id: chequeID}
|
||||||
c := cheque{id: id}
|
|
||||||
|
|
||||||
cashedCheques := getCashedCheques(ctx)
|
cashedCheques := getCashedCheques(ctx)
|
||||||
|
|
||||||
chequesList, ok := addCheque(cashedCheques, c)
|
chequesList, ok := addCheque(cashedCheques, c)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("irUpdate: non unique id")
|
panic("irUpdate: non unique chequeID")
|
||||||
}
|
}
|
||||||
|
|
||||||
oldNodes := 0
|
oldNodes := 0
|
||||||
|
@ -339,7 +340,7 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
for i := 1; i < len(args); i++ {
|
for i := 1; i < len(args); i++ {
|
||||||
key := args[i].([]byte)
|
key := args[i]
|
||||||
if len(key) != publicKeySize {
|
if len(key) != publicKeySize {
|
||||||
panic("irUpdate: invalid public key in inner ring list")
|
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 ")
|
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)
|
n := vote(ctx, hashID, irKey)
|
||||||
if n >= threshold {
|
if n >= threshold {
|
||||||
|
@ -381,12 +382,9 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
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 {
|
if len(key) != publicKeySize {
|
||||||
panic("isInnerRing: incorrect public key")
|
panic("isInnerRing: incorrect public key")
|
||||||
}
|
}
|
||||||
|
@ -401,19 +399,13 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
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)
|
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
|
// check if it is inner ring invocation
|
||||||
irList := getInnerRingNodes(ctx, innerRingKey)
|
irList := getInnerRingNodes(ctx, innerRingKey)
|
||||||
threshold := len(irList)/3*2 + 1
|
threshold := len(irList)/3*2 + 1
|
||||||
|
@ -424,7 +416,6 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check unique id of the operation
|
// check unique id of the operation
|
||||||
id := args[0].([]byte)
|
|
||||||
c := cheque{id: id}
|
c := cheque{id: id}
|
||||||
cashedCheques := getCashedCheques(ctx)
|
cashedCheques := getCashedCheques(ctx)
|
||||||
|
|
||||||
|
@ -440,9 +431,6 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
if n >= threshold {
|
if n >= threshold {
|
||||||
removeVotes(ctx, hashID)
|
removeVotes(ctx, hashID)
|
||||||
|
|
||||||
key := args[1]
|
|
||||||
val := args[2]
|
|
||||||
|
|
||||||
setConfig(ctx, key, val)
|
setConfig(ctx, key, val)
|
||||||
setSerialized(ctx, cashedChequesKey, chequesList)
|
setSerialized(ctx, cashedChequesKey, chequesList)
|
||||||
|
|
||||||
|
@ -451,7 +439,9 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
case "ListConfig":
|
}
|
||||||
|
|
||||||
|
func ListConfig() []record {
|
||||||
var config []record
|
var config []record
|
||||||
|
|
||||||
it := storage.Find(ctx, configPrefix)
|
it := storage.Find(ctx, configPrefix)
|
||||||
|
@ -464,7 +454,9 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
case "InitConfig":
|
}
|
||||||
|
|
||||||
|
func InitConfig(args []interface{}) bool {
|
||||||
if getConfig(ctx, candidateFeeConfigKey) != nil {
|
if getConfig(ctx, candidateFeeConfigKey) != nil {
|
||||||
panic("neofs: configuration already installed")
|
panic("neofs: configuration already installed")
|
||||||
}
|
}
|
||||||
|
@ -486,11 +478,10 @@ func Main(op string, args []interface{}) interface{} {
|
||||||
runtime.Log("neofs: config has been installed")
|
runtime.Log("neofs: config has been installed")
|
||||||
|
|
||||||
return true
|
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.
|
// 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 (
|
||||||
|
|
Loading…
Reference in a new issue