From 508369be75c8abde9b323f34a02b7cb622b94963 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 4 Mar 2021 22:21:49 +0300 Subject: [PATCH] [#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 --- alphabet/alphabet_contract.go | 4 ++-- balance/balance_contract.go | 4 ++-- common/ir.go | 8 ++++---- common/vote.go | 5 +++-- container/container_contract.go | 28 ++++++++++++++-------------- neofs/neofs_contract.go | 16 ++++++++-------- neofsid/neofsid_contract.go | 6 +++--- netmap/netmap_contract.go | 8 ++++---- proxy/proxy_contract.go | 2 +- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/alphabet/alphabet_contract.go b/alphabet/alphabet_contract.go index f80c541..8b16e56 100644 --- a/alphabet/alphabet_contract.go +++ b/alphabet/alphabet_contract.go @@ -80,7 +80,7 @@ func irList() []common.IRNode { } func currentEpoch() int { - netmapContractAddr := storage.Get(ctx, netmapKey).([]byte) + netmapContractAddr := storage.Get(ctx, netmapKey).(interop.Hash160) return contract.Call(netmapContractAddr, "epoch", contract.ReadOnly).(int) } @@ -143,7 +143,7 @@ func Emit() bool { return true } -func Vote(epoch int, candidates [][]byte) { +func Vote(epoch int, candidates []interop.PublicKey) { index := index() name := name() diff --git a/balance/balance_contract.go b/balance/balance_contract.go index 3cff1db..50b0b77 100644 --- a/balance/balance_contract.go +++ b/balance/balance_contract.go @@ -64,7 +64,7 @@ func init() { token = CreateToken() } -func Init(owner interop.Hash160, addrNetmap, addrContainer []byte) { +func Init(owner, addrNetmap, addrContainer interop.Hash160) { if !common.HasUpdateAccess(ctx) { panic("only owner can reinitialize contract") } @@ -162,7 +162,7 @@ func NewEpoch(epochNum int) bool { it := storage.Find(ctx, []byte{}, storage.KeysOnly) 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 { continue } diff --git a/common/ir.go b/common/ir.go index 98c3d0a..acb0877 100644 --- a/common/ir.go +++ b/common/ir.go @@ -13,11 +13,11 @@ const ( ) type IRNode struct { - PublicKey []byte + PublicKey interop.PublicKey } // 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++ { node := ir[i] if runtime.CheckWitness(node.PublicKey) { @@ -33,7 +33,7 @@ func InnerRingInvoker(ir []IRNode) []byte { // // Address of smart contract is received from storage by key. func InnerRingListViaStorage(ctx storage.Context, key interface{}) []IRNode { - sc := storage.Get(ctx, key).([]byte) + sc := storage.Get(ctx, key).(interop.Hash160) return InnerRingList(sc) } @@ -47,7 +47,7 @@ func InnerRingList(sc interop.Hash160) []IRNode { // keys by invoking netmap contract, which scripthash stored in the contract // storage by the key `key`. func InnerRingMultiAddressViaStorage(ctx storage.Context, key interface{}) []byte { - sc := storage.Get(ctx, key).([]byte) + sc := storage.Get(ctx, key).(interop.Hash160) return InnerRingMultiAddress(sc) } diff --git a/common/vote.go b/common/vote.go index 7491006..4adfbd2 100644 --- a/common/vote.go +++ b/common/vote.go @@ -1,6 +1,7 @@ package common 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/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger" @@ -13,7 +14,7 @@ type Ballot struct { ID []byte // Public keys of already voted inner ring nodes. - Voters [][]byte + Voters []interop.PublicKey // Height of block with the last vote. Height int @@ -62,7 +63,7 @@ func Vote(ctx storage.Context, id, from []byte) int { } if found < 0 { - voters := [][]byte{from} + voters := []interop.PublicKey{from} newCandidates = append(newCandidates, Ballot{ ID: id, Voters: voters, diff --git a/container/container_contract.go b/container/container_contract.go index 3b47467..122969e 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -60,7 +60,7 @@ func init() { ctx = storage.GetContext() } -func Init(owner interop.Hash160, addrNetmap, addrBalance, addrID []byte) { +func Init(owner, addrNetmap, addrBalance, addrID interop.Hash160) { if !common.HasUpdateAccess(ctx) { panic("only owner can reinitialize contract") } @@ -89,18 +89,18 @@ func Migrate(script []byte, manifest []byte) bool { 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 = 2 + offset + 4 // version prefix + version size + owner prefix ownerID := container[offset : offset+25] // offset + size of owner containerID := crypto.SHA256(container) - neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) + neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160) multiaddr := common.InnerRingMultiAddressViaStorage(ctx, netmapContractKey) if !runtime.CheckWitness(multiaddr) { if !isSignedByOwnerKey(container, signature, ownerID, publicKey) { // 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) { panic("put: invalid owner signature") } @@ -153,7 +153,7 @@ func Delete(containerID, signature []byte) bool { if !runtime.CheckWitness(multiaddr) { // check provided key 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) { panic("delete: invalid owner signature") @@ -213,8 +213,8 @@ func SetEACL(eACL, signature []byte) bool { panic("setEACL: container does not exists") } - neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) - keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) + neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160) + keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey) if !verifySignature(eACL, signature, keys) { 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 - neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).([]byte) - keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([][]byte) + neofsIDContractAddr := storage.Get(ctx, neofsIDContractKey).(interop.Hash160) + keys := contract.Call(neofsIDContractAddr, "key", contract.ReadOnly, ownerID).([]interop.PublicKey) for i := range keys { key := keys[i] @@ -434,14 +434,14 @@ func getEACL(ctx storage.Context, cid []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 { 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 { key := keys[i] if crypto.ECDsaSecp256r1Verify(msg, key, sig) { @@ -469,7 +469,7 @@ func getOwnerByID(ctx storage.Context, id []byte) []byte { 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) { return false } @@ -477,7 +477,7 @@ func isSignedByOwnerKey(msg, sig, owner, key []byte) bool { return crypto.ECDsaSecp256r1Verify(msg, key, sig) } -func isOwnerFromKey(owner []byte, key []byte) bool { +func isOwnerFromKey(owner []byte, key interop.PublicKey) bool { ownerSH := walletToScripHash(owner) keySH := contract.CreateStandardAccount(key) @@ -508,7 +508,7 @@ func getContainerSizeEstimation(key, cid []byte) containerSizes { // isStorageNode looks into _previous_ epoch network map, because storage node // announce container size estimation of previous epoch. 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) for i := range snapshot { diff --git a/neofs/neofs_contract.go b/neofs/neofs_contract.go index 1e8df89..5def62b 100644 --- a/neofs/neofs_contract.go +++ b/neofs/neofs_contract.go @@ -85,7 +85,7 @@ func init() { } // 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) { panic("only owner can reinitialize contract") } @@ -141,7 +141,7 @@ func InnerRingCandidates() []common.IRNode { } // 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) { 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. -func InnerRingCandidateAdd(key []byte) bool { +func InnerRingCandidateAdd(key interop.PublicKey) bool { if !runtime.CheckWitness(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() - if !common.BytesEqual(caller, []byte(gas.Hash)) { + if !common.BytesEqual(caller, interop.Hash160(gas.Hash)) { 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 // 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) 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. -func Bind(user []byte, keys [][]byte) bool { +func Bind(user []byte, keys []interop.PublicKey) bool { if !runtime.CheckWitness(user) { 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 -func Unbind(user []byte, keys [][]byte) bool { +func Unbind(user []byte, keys []interop.PublicKey) bool { if !runtime.CheckWitness(user) { 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 // public keys. -func InnerRingUpdate(chequeID []byte, args [][]byte) bool { +func InnerRingUpdate(chequeID []byte, args []interop.PublicKey) bool { if len(args) < minInnerRingSize { panic("irUpdate: bad arguments") } diff --git a/neofsid/neofsid_contract.go b/neofsid/neofsid_contract.go index 2d5a2f6..8e65750 100644 --- a/neofsid/neofsid_contract.go +++ b/neofsid/neofsid_contract.go @@ -29,7 +29,7 @@ func init() { ctx = storage.GetContext() } -func Init(owner interop.Hash160, addrNetmap, addrContainer []byte) { +func Init(owner, addrNetmap, addrContainer interop.Hash160) { if !common.HasUpdateAccess(ctx) { panic("only owner can reinitialize contract") } @@ -57,7 +57,7 @@ func Migrate(script []byte, manifest []byte) bool { return true } -func AddKey(owner []byte, keys [][]byte) bool { +func AddKey(owner []byte, keys []interop.PublicKey) bool { if len(owner) != 25 { panic("addKey: incorrect owner") } @@ -92,7 +92,7 @@ addLoop: return true } -func RemoveKey(owner []byte, keys [][]byte) bool { +func RemoveKey(owner []byte, keys []interop.PublicKey) bool { if len(owner) != 25 { panic("removeKey: incorrect owner") } diff --git a/netmap/netmap_contract.go b/netmap/netmap_contract.go index 90f1792..1b0ea5a 100644 --- a/netmap/netmap_contract.go +++ b/netmap/netmap_contract.go @@ -59,7 +59,7 @@ func init() { // Init function sets up initial list of inner ring public keys and should // 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) { panic("only owner can reinitialize contract") } @@ -106,7 +106,7 @@ func Multiaddress() []byte { return multiaddress(getIRNodes(ctx)) } -func UpdateInnerRing(keys [][]byte) bool { +func UpdateInnerRing(keys []interop.PublicKey) bool { multiaddr := Multiaddress() if !runtime.CheckWitness(multiaddr) { panic("updateInnerRing: this method must be invoked by inner ring nodes") @@ -152,7 +152,7 @@ func AddPeer(nodeInfo []byte) bool { return true } -func UpdateState(state int, publicKey []byte) bool { +func UpdateState(state int, publicKey interop.PublicKey) bool { if len(publicKey) != 33 { panic("updateState: incorrect public key") } @@ -324,7 +324,7 @@ func addToNetmap(ctx storage.Context, n storageNode) []netmapNode { return netmap } -func removeFromNetmap(ctx storage.Context, key []byte) []netmapNode { +func removeFromNetmap(ctx storage.Context, key interop.PublicKey) []netmapNode { var ( netmap = getNetmapNodes(ctx) newNetmap = []netmapNode{} diff --git a/proxy/proxy_contract.go b/proxy/proxy_contract.go index 9bceb58..c7d4d79 100644 --- a/proxy/proxy_contract.go +++ b/proxy/proxy_contract.go @@ -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) { panic("only owner can reinitialize contract") }