From bd45e50480c571ca7d7e08b1bb92110a114a42b6 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 10 Dec 2020 13:41:16 +0300 Subject: [PATCH] [#23] alphabet: Add OnPayment method Without OnPayment method alphabet contracts won't be able to receive NEO and produce GAS. Signed-off-by: Alex Vanin --- alphabet/alphabet.tpl | 9 +++++++++ alphabet/az/az_contract.go | 13 +++++++++++-- alphabet/buky/buky_contract.go | 13 +++++++++++-- alphabet/dobro/dobro_contract.go | 13 +++++++++++-- alphabet/glagoli/glagoli_contract.go | 13 +++++++++++-- alphabet/jest/jest_contract.go | 13 +++++++++++-- alphabet/vedi/vedi_contract.go | 13 +++++++++++-- alphabet/zhivete/zhivete_contract.go | 13 +++++++++++-- 8 files changed, 86 insertions(+), 14 deletions(-) diff --git a/alphabet/alphabet.tpl b/alphabet/alphabet.tpl index 27f6b67..39a2360 100644 --- a/alphabet/alphabet.tpl +++ b/alphabet/alphabet.tpl @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") diff --git a/alphabet/az/az_contract.go b/alphabet/az/az_contract.go index b01f365..8a73d42 100644 --- a/alphabet/az/az_contract.go +++ b/alphabet/az/az_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/buky/buky_contract.go b/alphabet/buky/buky_contract.go index 6c289f4..5f648ac 100644 --- a/alphabet/buky/buky_contract.go +++ b/alphabet/buky/buky_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/dobro/dobro_contract.go b/alphabet/dobro/dobro_contract.go index b0e2957..33ac6b4 100644 --- a/alphabet/dobro/dobro_contract.go +++ b/alphabet/dobro/dobro_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/glagoli/glagoli_contract.go b/alphabet/glagoli/glagoli_contract.go index cb4cb7a..5c92f7f 100644 --- a/alphabet/glagoli/glagoli_contract.go +++ b/alphabet/glagoli/glagoli_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/jest/jest_contract.go b/alphabet/jest/jest_contract.go index 152dc00..d96a7e1 100644 --- a/alphabet/jest/jest_contract.go +++ b/alphabet/jest/jest_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/vedi/vedi_contract.go b/alphabet/vedi/vedi_contract.go index a5f623b..e480d08 100644 --- a/alphabet/vedi/vedi_contract.go +++ b/alphabet/vedi/vedi_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) ) diff --git a/alphabet/zhivete/zhivete_contract.go b/alphabet/zhivete/zhivete_contract.go index 2a76f6f..5c060cd 100644 --- a/alphabet/zhivete/zhivete_contract.go +++ b/alphabet/zhivete/zhivete_contract.go @@ -1,6 +1,7 @@ package alphabetcontract import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/crypto" @@ -33,7 +34,7 @@ const ( netmapContractKey = "netmapScriptHash" - threshold = totalAlphabetContracts * 2 / 3 + 1 + threshold = totalAlphabetContracts*2/3 + 1 voteKey = "ballots" totalAlphabetContracts = 7 @@ -51,6 +52,14 @@ func init() { ctx = storage.GetContext() } +// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts. +func OnPayment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) { + panic("onPayment: alphabet contract accepts GAS and NEO only") + } +} + func Init(addrNetmap []byte) { if storage.Get(ctx, netmapContractKey) != nil { panic("contract already deployed") @@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool { func voteID(epoch interface{}, args [][]byte) []byte { var ( - result []byte + result []byte epochBytes = epoch.([]byte) )