[#25] Use go aliases

Change []byte type to interop.*aliasName*
types in contracts where it is suitable to
improve the readability of the contracts.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
enable-notary-in-public-chains
Pavel Karpy 2021-03-04 22:21:49 +03:00 committed by Leonard Lyubich
parent 91c2612d08
commit 508369be75
9 changed files with 41 additions and 40 deletions

View File

@ -80,7 +80,7 @@ func irList() []common.IRNode {
} }
func currentEpoch() int { func currentEpoch() int {
netmapContractAddr := storage.Get(ctx, netmapKey).([]byte) netmapContractAddr := storage.Get(ctx, netmapKey).(interop.Hash160)
return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int) return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int)
} }
@ -143,7 +143,7 @@ func Emit() bool {
return true return true
} }
func Vote(epoch int, candidates [][]byte) { func Vote(epoch int, candidates []interop.PublicKey) {
index := index() index := index()
name := name() name := name()

View File

@ -64,7 +64,7 @@ func init() {
token = CreateToken() token = CreateToken()
} }
func Init(owner interop.Hash160, addrNetmap, addrContainer []byte) { func Init(owner, addrNetmap, addrContainer interop.Hash160) {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -162,7 +162,7 @@ func NewEpoch(epochNum int) bool {
it := storage.Find(ctx, []byte{}, storage.KeysOnly) it := storage.Find(ctx, []byte{}, storage.KeysOnly)
for iterator.Next(it) { for iterator.Next(it) {
addr := iterator.Value(it).([]byte) // it MUST BE `storage.KeysOnly` addr := iterator.Value(it).(interop.Hash160) // it MUST BE `storage.KeysOnly`
if len(addr) != 20 { if len(addr) != 20 {
continue continue
} }

View File

@ -13,11 +13,11 @@ const (
) )
type IRNode struct { type IRNode struct {
PublicKey []byte PublicKey interop.PublicKey
} }
// InnerRingInvoker returns public key of inner ring node that invoked contract. // InnerRingInvoker returns public key of inner ring node that invoked contract.
func InnerRingInvoker(ir []IRNode) []byte { func InnerRingInvoker(ir []IRNode) interop.PublicKey {
for i := 0; i < len(ir); i++ { for i := 0; i < len(ir); i++ {
node := ir[i] node := ir[i]
if runtime.CheckWitness(node.PublicKey) { if runtime.CheckWitness(node.PublicKey) {
@ -33,7 +33,7 @@ func InnerRingInvoker(ir []IRNode) []byte {
// //
// Address of smart contract is received from storage by key. // Address of smart contract is received from storage by key.
func InnerRingListViaStorage(ctx storage.Context, key interface{}) []IRNode { func InnerRingListViaStorage(ctx storage.Context, key interface{}) []IRNode {
sc := storage.Get(ctx, key).([]byte) sc := storage.Get(ctx, key).(interop.Hash160)
return InnerRingList(sc) return InnerRingList(sc)
} }
@ -47,7 +47,7 @@ func InnerRingList(sc interop.Hash160) []IRNode {
// keys by invoking netmap contract, which scripthash stored in the contract // keys by invoking netmap contract, which scripthash stored in the contract
// storage by the key `key`. // storage by the key `key`.
func InnerRingMultiAddressViaStorage(ctx storage.Context, key interface{}) []byte { func InnerRingMultiAddressViaStorage(ctx storage.Context, key interface{}) []byte {
sc := storage.Get(ctx, key).([]byte) sc := storage.Get(ctx, key).(interop.Hash160)
return InnerRingMultiAddress(sc) return InnerRingMultiAddress(sc)
} }

View File

@ -1,6 +1,7 @@
package common package common
import ( 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/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger" "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
@ -13,7 +14,7 @@ type Ballot struct {
ID []byte ID []byte
// Public keys of already voted inner ring nodes. // Public keys of already voted inner ring nodes.
Voters [][]byte Voters []interop.PublicKey
// Height of block with the last vote. // Height of block with the last vote.
Height int Height int
@ -62,7 +63,7 @@ func Vote(ctx storage.Context, id, from []byte) int {
} }
if found < 0 { if found < 0 {
voters := [][]byte{from} voters := []interop.PublicKey{from}
newCandidates = append(newCandidates, Ballot{ newCandidates = append(newCandidates, Ballot{
ID: id, ID: id,
Voters: voters, Voters: voters,

View File

@ -60,7 +60,7 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
func Init(owner interop.Hash160, addrNetmap, addrBalance, addrID []byte) { func Init(owner, addrNetmap, addrBalance, addrID interop.Hash160) {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -89,18 +89,18 @@ func Migrate(script []byte, manifest []byte) bool {
return true return true
} }
func Put(container, signature, publicKey []byte) bool { func Put(container []byte, signature interop.Signature, publicKey interop.PublicKey) bool {
offset := int(container[1]) offset := int(container[1])
offset = 2 + offset + 4 // version prefix + version size + owner prefix offset = 2 + offset + 4 // version prefix + version size + owner prefix
ownerID := container[offset : offset+25] // offset + size of owner ownerID := container[offset : offset+25] // offset + size of owner
containerID := crypto.SHA256(container) containerID := crypto.SHA256(container)
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160)
multiaddr := common.InnerRingMultiAddressViaStorage(ctx, netmapContractKey) multiaddr := common.InnerRingMultiAddressViaStorage(ctx, netmapContractKey)
if !runtime.CheckWitness(multiaddr) { if !runtime.CheckWitness(multiaddr) {
if !isSignedByOwnerKey(container, signature, ownerID, publicKey) { if !isSignedByOwnerKey(container, signature, ownerID, publicKey) {
// check keys from NeoFSID // check keys from NeoFSID
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey)
if !verifySignature(container, signature, keys) { if !verifySignature(container, signature, keys) {
panic("put: invalid owner signature") panic("put: invalid owner signature")
} }
@ -153,7 +153,7 @@ func Delete(containerID, signature []byte) bool {
if !runtime.CheckWitness(multiaddr) { if !runtime.CheckWitness(multiaddr) {
// check provided key // check provided key
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte)
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey)
if !verifySignature(containerID, signature, keys) { if !verifySignature(containerID, signature, keys) {
panic("delete: invalid owner signature") panic("delete: invalid owner signature")
@ -213,8 +213,8 @@ func SetEACL(eACL, signature []byte) bool {
panic("setEACL: container does not exists") panic("setEACL: container does not exists")
} }
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160)
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey)
if !verifySignature(eACL, signature, keys) { if !verifySignature(eACL, signature, keys) {
panic("setEACL: invalid eACL signature") panic("setEACL: invalid eACL signature")
@ -247,8 +247,8 @@ func EACL(containerID []byte) extendedACL {
// attach corresponding public key if it was not revoked from neofs id // attach corresponding public key if it was not revoked from neofs id
neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160)
keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey)
for i := range keys { for i := range keys {
key := keys[i] key := keys[i]
@ -434,14 +434,14 @@ func getEACL(ctx storage.Context, cid []byte) extendedACL {
return binary.Deserialize(data.([]byte)).(extendedACL) return binary.Deserialize(data.([]byte)).(extendedACL)
} }
return extendedACL{val: []byte{}, sig: []byte{}, pub: []byte{}} return extendedACL{val: []byte{}, sig: interop.Signature{}, pub: interop.PublicKey{}}
} }
func walletToScripHash(wallet []byte) []byte { func walletToScripHash(wallet []byte) []byte {
return wallet[1 : len(wallet)-4] return wallet[1 : len(wallet)-4]
} }
func verifySignature(msg, sig []byte, keys [][]byte) bool { func verifySignature(msg []byte, sig interop.Signature, keys []interop.PublicKey) bool {
for i := range keys { for i := range keys {
key := keys[i] key := keys[i]
if crypto.ECDsaSecp256r1Verify(msg, key, sig) { if crypto.ECDsaSecp256r1Verify(msg, key, sig) {
@ -469,7 +469,7 @@ func getOwnerByID(ctx storage.Context, id []byte) []byte {
return nil return nil
} }
func isSignedByOwnerKey(msg, sig, owner, key []byte) bool { func isSignedByOwnerKey(msg []byte, sig interop.Signature, owner []byte, key interop.PublicKey) bool {
if !isOwnerFromKey(owner, key) { if !isOwnerFromKey(owner, key) {
return false return false
} }
@ -477,7 +477,7 @@ func isSignedByOwnerKey(msg, sig, owner, key []byte) bool {
return crypto.ECDsaSecp256r1Verify(msg, key, sig) return crypto.ECDsaSecp256r1Verify(msg, key, sig)
} }
func isOwnerFromKey(owner []byte, key []byte) bool { func isOwnerFromKey(owner []byte, key interop.PublicKey) bool {
ownerSH := walletToScripHash(owner) ownerSH := walletToScripHash(owner)
keySH := contract.CreateStandardAccount(key) keySH := contract.CreateStandardAccount(key)
@ -508,7 +508,7 @@ func getContainerSizeEstimation(key, cid []byte) containerSizes {
// isStorageNode looks into _previous_ epoch network map, because storage node // isStorageNode looks into _previous_ epoch network map, because storage node
// announce container size estimation of previous epoch. // announce container size estimation of previous epoch.
func isStorageNode(key interop.PublicKey) bool { func isStorageNode(key interop.PublicKey) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160)
snapshot := contract.Call(netmapContractAddr, "snapshot", contract.ReadOnly, 1).([]storageNode) snapshot := contract.Call(netmapContractAddr, "snapshot", contract.ReadOnly, 1).([]storageNode)
for i := range snapshot { for i := range snapshot {

View File

@ -85,7 +85,7 @@ func init() {
} }
// Init set up initial inner ring node keys. // Init set up initial inner ring node keys.
func Init(owner interop.PublicKey, args [][]byte) bool { func Init(owner interop.PublicKey, args []interop.PublicKey) bool {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -141,7 +141,7 @@ func InnerRingCandidates() []common.IRNode {
} }
// InnerRingCandidateRemove removes key from the list of inner ring candidates. // InnerRingCandidateRemove removes key from the list of inner ring candidates.
func InnerRingCandidateRemove(key []byte) bool { func InnerRingCandidateRemove(key interop.PublicKey) 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")
} }
@ -164,7 +164,7 @@ func InnerRingCandidateRemove(key []byte) bool {
} }
// InnerRingCandidateAdd adds key to the list of inner ring candidates. // InnerRingCandidateAdd adds key to the list of inner ring candidates.
func InnerRingCandidateAdd(key []byte) bool { func InnerRingCandidateAdd(key interop.PublicKey) 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")
} }
@ -200,7 +200,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
} }
caller := runtime.GetCallingScriptHash() caller := runtime.GetCallingScriptHash()
if !common.BytesEqual(caller, []byte(gas.Hash)) { if !common.BytesEqual(caller, interop.Hash160(gas.Hash)) {
panic("onNEP17Payment: only GAS can be accepted for deposit") panic("onNEP17Payment: only GAS can be accepted for deposit")
} }
@ -267,7 +267,7 @@ func Withdraw(user []byte, amount int) bool {
// Cheque sends gas assets back to the user if they were successfully // Cheque sends gas assets back to the user if they were successfully
// locked in NeoFS balance contract. // locked in NeoFS balance contract.
func Cheque(id, user []byte, amount int, lockAcc []byte) bool { func Cheque(id []byte, user interop.Hash160, amount int, lockAcc []byte) bool {
irList := getInnerRingNodes(ctx, innerRingKey) irList := getInnerRingNodes(ctx, innerRingKey)
threshold := len(irList)/3*2 + 1 threshold := len(irList)/3*2 + 1
@ -307,7 +307,7 @@ func Cheque(id, user []byte, amount int, lockAcc []byte) bool {
} }
// Bind public key with user's account to use it in NeoFS requests. // Bind public key with user's account to use it in NeoFS requests.
func Bind(user []byte, keys [][]byte) bool { func Bind(user []byte, keys []interop.PublicKey) 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")
} }
@ -325,7 +325,7 @@ func Bind(user []byte, keys [][]byte) bool {
} }
// Unbind public key from user's account // Unbind public key from user's account
func Unbind(user []byte, keys [][]byte) bool { func Unbind(user []byte, keys []interop.PublicKey) bool {
if !runtime.CheckWitness(user) { if !runtime.CheckWitness(user) {
panic("unbinding: you should be the owner of the wallet") panic("unbinding: you should be the owner of the wallet")
} }
@ -344,7 +344,7 @@ func Unbind(user []byte, keys [][]byte) bool {
// InnerRingUpdate updates list of inner ring nodes with provided list of // InnerRingUpdate updates list of inner ring nodes with provided list of
// public keys. // public keys.
func InnerRingUpdate(chequeID []byte, args [][]byte) bool { func InnerRingUpdate(chequeID []byte, args []interop.PublicKey) bool {
if len(args) < minInnerRingSize { if len(args) < minInnerRingSize {
panic("irUpdate: bad arguments") panic("irUpdate: bad arguments")
} }

View File

@ -29,7 +29,7 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
func Init(owner interop.Hash160, addrNetmap, addrContainer []byte) { func Init(owner, addrNetmap, addrContainer interop.Hash160) {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -57,7 +57,7 @@ func Migrate(script []byte, manifest []byte) bool {
return true return true
} }
func AddKey(owner []byte, keys [][]byte) bool { func AddKey(owner []byte, keys []interop.PublicKey) bool {
if len(owner) != 25 { if len(owner) != 25 {
panic("addKey: incorrect owner") panic("addKey: incorrect owner")
} }
@ -92,7 +92,7 @@ addLoop:
return true return true
} }
func RemoveKey(owner []byte, keys [][]byte) bool { func RemoveKey(owner []byte, keys []interop.PublicKey) bool {
if len(owner) != 25 { if len(owner) != 25 {
panic("removeKey: incorrect owner") panic("removeKey: incorrect owner")
} }

View File

@ -59,7 +59,7 @@ func init() {
// Init function sets up initial list of inner ring public keys and should // Init function sets up initial list of inner ring public keys and should
// be invoked once at neofs infrastructure setup. // be invoked once at neofs infrastructure setup.
func Init(owner interop.Hash160, keys [][]byte) { func Init(owner interop.Hash160, keys []interop.PublicKey) {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }
@ -106,7 +106,7 @@ func Multiaddress() []byte {
return multiaddress(getIRNodes(ctx)) return multiaddress(getIRNodes(ctx))
} }
func UpdateInnerRing(keys [][]byte) bool { func UpdateInnerRing(keys []interop.PublicKey) bool {
multiaddr := Multiaddress() multiaddr := Multiaddress()
if !runtime.CheckWitness(multiaddr) { if !runtime.CheckWitness(multiaddr) {
panic("updateInnerRing: this method must be invoked by inner ring nodes") panic("updateInnerRing: this method must be invoked by inner ring nodes")
@ -152,7 +152,7 @@ func AddPeer(nodeInfo []byte) bool {
return true return true
} }
func UpdateState(state int, publicKey []byte) bool { func UpdateState(state int, publicKey interop.PublicKey) bool {
if len(publicKey) != 33 { if len(publicKey) != 33 {
panic("updateState: incorrect public key") panic("updateState: incorrect public key")
} }
@ -324,7 +324,7 @@ func addToNetmap(ctx storage.Context, n storageNode) []netmapNode {
return netmap return netmap
} }
func removeFromNetmap(ctx storage.Context, key []byte) []netmapNode { func removeFromNetmap(ctx storage.Context, key interop.PublicKey) []netmapNode {
var ( var (
netmap = getNetmapNodes(ctx) netmap = getNetmapNodes(ctx)
newNetmap = []netmapNode{} newNetmap = []netmapNode{}

View File

@ -28,7 +28,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
} }
} }
func Init(owner interop.Hash160, addrNetmap []byte) { func Init(owner, addrNetmap interop.Hash160) {
if !common.HasUpdateAccess(ctx) { if !common.HasUpdateAccess(ctx) {
panic("only owner can reinitialize contract") panic("only owner can reinitialize contract")
} }