Skip to content

Commit

Permalink
*: use well-known constants where possible
Browse files Browse the repository at this point in the history
Part 2 from #307.
  • Loading branch information
AnnaShaleva committed Jan 31, 2023
1 parent 15fbab2 commit 044c786
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions balance/balance_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (t Token) transfer(ctx storage.Context, from, to interop.Hash160, amount in
return false
}

if len(from) == 20 {
if len(from) == interop.Hash160Len {
if amountFrom.Balance == amount {
storage.Delete(ctx, from)
} else {
Expand All @@ -448,7 +448,7 @@ func (t Token) transfer(ctx storage.Context, from, to interop.Hash160, amount in
}
}

if len(to) == 20 {
if len(to) == interop.Hash160Len {
amountTo := getAccount(ctx, to)
amountTo.Balance = amountTo.Balance + amount // neo-go#953
common.SetSerialized(ctx, to, amountTo)
Expand Down Expand Up @@ -487,7 +487,7 @@ func (t Token) canTransfer(ctx storage.Context, from, to interop.Hash160, amount

// isUsableAddress checks if the sender is either a correct NEO address or SC address.
func isUsableAddress(addr interop.Hash160) bool {
if len(addr) == 20 {
if len(addr) == interop.Hash160Len {
if runtime.CheckWitness(addr) {
return true
}
Expand Down
7 changes: 4 additions & 3 deletions container/container_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const (
AliasFeeKey = "ContainerAliasFee"

// V2 format
containerIDSize = 32 // SHA256 size
containerIDSize = interop.Hash256Len // SHA256 size

singleEstimatePrefix = "est"
estimateKeyPrefix = "cnr"
Expand Down Expand Up @@ -471,7 +471,7 @@ func SetEACL(eACL []byte, signature interop.Signature, publicKey interop.PublicK
// get container ID
offset := int(eACL[1])
offset = 2 + offset + 4
containerID := eACL[offset : offset+32]
containerID := eACL[offset : offset+containerIDSize]

ownerID := getOwnerByID(ctx, containerID)
if ownerID == nil {
Expand Down Expand Up @@ -828,10 +828,11 @@ func isStorageNode(ctx storage.Context, key interop.PublicKey) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).(interop.Hash160)
snapshot := contract.Call(netmapContractAddr, "snapshot", contract.ReadOnly, 1).([]storageNode)

nodeKeyOffset := 2
for i := range snapshot {
// V2 format
nodeInfo := snapshot[i].info
nodeKey := nodeInfo[2:35] // offset:2, len:33
nodeKey := nodeInfo[nodeKeyOffset : nodeKeyOffset+interop.PublicKeyCompressedLen] // offset:2, len:33

if common.BytesEqual(key, nodeKey) {
return true
Expand Down
2 changes: 1 addition & 1 deletion neofs/neofs_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
}

switch len(rcv) {
case 20:
case interop.Hash160Len:
case 0:
rcv = from
default:
Expand Down
6 changes: 4 additions & 2 deletions netmap/netmap_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ func AddPeerIR(nodeInfo []byte) {

common.CheckAlphabetWitness(common.AlphabetAddress())

publicKey := nodeInfo[2:35] // V2 format: offset:2, len:33
keyOffset := 2
publicKey := nodeInfo[keyOffset : keyOffset+interop.PublicKeyCompressedLen] // V2 format: offset:2, len:33

addToNetmap(ctx, publicKey, Node{
BLOB: nodeInfo,
Expand Down Expand Up @@ -312,7 +313,8 @@ func AddPeer(nodeInfo []byte) {
}

// V2 format
publicKey := nodeInfo[2:35] // offset:2, len:33
keyOffset := 2
publicKey := nodeInfo[keyOffset : keyOffset+interop.PublicKeyCompressedLen] // V2 format: offset:2, len:33

// If notary is enabled or caller is not an alphabet node,
// just emit the notification for alphabet.
Expand Down

0 comments on commit 044c786

Please sign in to comment.