Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
  • Loading branch information
cthulhu-rider committed Feb 22, 2023
1 parent bce47f4 commit 46c46be
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
83 changes: 55 additions & 28 deletions alphabet/alphabet_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package alphabet
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/native/notary"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neofs-contract/common"
Expand Down Expand Up @@ -68,7 +69,7 @@ func _deploy(data interface{}, isUpdate bool) {
panic("non-notary mode is not supported anymore")
}

if len(args.addrNetmap) != interop.Hash160Len || !args.notaryDisabled && len(args.addrProxy) != interop.Hash160Len {
if len(args.addrNetmap) != interop.Hash160Len || len(args.addrProxy) != interop.Hash160Len {
panic("incorrect length of contract script hash")
}

Expand Down Expand Up @@ -120,15 +121,22 @@ func switchToNotary(ctx storage.Context, args []interface{}) {
records := resResolve.([]string)
if len(records) == 0 {
panic("did not find a record of the Proxy contract in the NNS")
} else if records[0] != std.Base58Encode(proxyContract) { // FIXME: how do we know exact encoding?
panic("wrong address of the Proxy contract")
} else if records[0] != address.FromHash160(proxyContract) {
const hexTable = "0123456789abcdef"
j := 0
for _, v := range proxyContract {
if records[0][j] != hexTable[v>>4] || records[0][j+1] != hexTable[v&0x0f] {
panic("wrong address of the Proxy contract")
}
j += 2
}
}

if !common.TryPurgeVotes(ctx) {
panic("pending vote detected")
}

// distribute GAS:
// distribute 75% of available GAS:
// - 50% to Proxy contract
// - the rest is evenly distributed between Inner Ring and storage nodes
netmapContract := args[1].(interop.Hash160)
Expand All @@ -146,37 +154,56 @@ func switchToNotary(ctx storage.Context, args []interface{}) {
innerRingNodes := common.InnerRingNodesFromNetmap(netmapContract)

currentContact := runtime.GetExecutingScriptHash()
currentGAS := gas.BalanceOf(currentContact)
currentGAS := gas.BalanceOf(currentContact) * 3 / 4

if currentGAS > 0 { // need panic otherwise?
toTransfer := currentGAS / 2
if !gas.Transfer(currentContact, proxyContract, toTransfer, nil) {
panic("failed to transfer half of GAS to Proxy contract")
}
if currentGAS == 0 {
panic("no GAS in the contract")
}

toTransfer = currentGAS - toTransfer
toTransfer := currentGAS / 2
if !gas.Transfer(currentContact, proxyContract, toTransfer, nil) {
panic("failed to transfer half of GAS to Proxy contract")
}

nNodes := len(storageNodes) + len(innerRingNodes)
if nNodes > 0 { // need panic otherwise?
perNodeGAS := toTransfer / nNodes
toTransfer = currentGAS - toTransfer

for i := 0; toTransfer > 0 && i < len(innerRingNodes); i++ {
if !gas.Transfer(currentContact, contract.CreateStandardAccount(innerRingNodes[i]), perNodeGAS, nil) {
panic("failed to transfer [art of GAS to the Inner Ring node") // or continue?
}
nNodes := len(storageNodes) + len(innerRingNodes)
perNodeGAS := toTransfer / nNodes

toTransfer -= perNodeGAS
}
// see https://github.com/nspcc-dev/neo-go/blob/v0.101.0/docs/notary.md#1-notary-deposit
notaryTransferData := []interface{}{
nil, // receiver account (set in loop)
0, // till (defaults to current chain height + 5760)
}
// TODO: is till OK?

for i := 0; toTransfer > 0 && i < len(innerRingNodes); i++ {
addr := contract.CreateStandardAccount(innerRingNodes[i])
if !gas.Transfer(currentContact, addr, perNodeGAS, nil) {
panic("failed to transfer part of GAS to the Inner Ring node")
}

for i := 0; toTransfer > 0 && i < len(storageNodes); i++ {
publicKey := storageNodes[i].blob[2:35] // hardcoded cause there was no other way
if !gas.Transfer(currentContact, contract.CreateStandardAccount(publicKey), perNodeGAS, nil) {
panic("failed to transfer [art of GAS to the Inner Ring node") // or continue?
}
notaryTransferData[0] = addr
if !gas.Transfer(currentContact, interop.Hash160(notary.Hash), perNodeGAS, notaryTransferData) {
panic("failed to make notary deposit for the Inner Ring node")
}

toTransfer -= perNodeGAS
}
toTransfer -= perNodeGAS
}

for i := 0; toTransfer > 0 && i < len(storageNodes); i++ {
publicKey := storageNodes[i].blob[2:35] // hardcoded cause there was no other way
addr := contract.CreateStandardAccount(publicKey)
if !gas.Transfer(currentContact, addr, perNodeGAS, nil) {
panic("failed to transfer part of GAS to the storage node")
}

notaryTransferData[0] = addr
if !gas.Transfer(currentContact, interop.Hash160(notary.Hash), perNodeGAS, notaryTransferData) {
panic("failed to make notary deposit for the storage node")
}

toTransfer -= perNodeGAS
}

storage.Put(ctx, proxyKey, proxyContract)
Expand Down
9 changes: 0 additions & 9 deletions balance/balance_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ func _deploy(data interface{}, isUpdate bool) {
return
}

args := data.(struct {
notaryDisabled bool
_, _ interop.Hash160 // Netmap and Container contract from non-notary legacy
})

if args.notaryDisabled {
panic("non-notary mode is not supported anymore")
}

runtime.Log("balance contract initialized")
}

Expand Down

0 comments on commit 46c46be

Please sign in to comment.