Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support notary and w/o notary work flows in contracts #74

Merged
merged 19 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sidechain: alphabet morph

alphabet_sc = alphabet
morph_sc = audit balance container neofsid netmap proxy reputation
mainnet_sc = neofs
mainnet_sc = neofs processing

define sc_template
$(2)$(1)/$(1)_contract.nef: $(2)$(1)/$(1)_contract.go
Expand Down
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

NeoFS-Contract contains all NeoFS related contracts written for
[neo-go](https://github.com/nspcc-dev/neo-go) compiler. These contracts
are deployed both in mainnet and sidechain.
are deployed both in main chain and side chain.

Mainnet contract:
Main chain contracts:

- neofs
- processing

Sidechain contracts:
Side chain contracts:

- alphabet
- audit
Expand All @@ -34,7 +35,7 @@ Sidechain contracts:

To compile smart contracts you need:

- [neo-go](https://github.com/nspcc-dev/neo-go) >= 0.94.0
- [neo-go](https://github.com/nspcc-dev/neo-go) >= 0.94.1

## Compilation

Expand All @@ -44,15 +45,16 @@ corresponding directories.

```
$ make all
neo-go contract compile -i alphabet/alphabet_contract.go -c alphabet/config.yml -m alphabet/config.json
neo-go contract compile -i audit/audit_contract.go -c audit/config.yml -m audit/config.json
neo-go contract compile -i balance/balance_contract.go -c balance/config.yml -m balance/config.json
neo-go contract compile -i container/container_contract.go -c container/config.yml -m container/config.json
neo-go contract compile -i neofsid/neofsid_contract.go -c neofsid/config.yml -m neofsid/config.json
neo-go contract compile -i netmap/netmap_contract.go -c netmap/config.yml -m netmap/config.json
neo-go contract compile -i proxy/proxy_contract.go -c proxy/config.yml -m proxy/config.json
neo-go contract compile -i alphabet/alphabet_contract.go -c alphabet/config.yml -m alphabet/config.json
neo-go contract compile -i audit/audit_contract.go -c audit/config.yml -m audit/config.json
neo-go contract compile -i balance/balance_contract.go -c balance/config.yml -m balance/config.json
neo-go contract compile -i container/container_contract.go -c container/config.yml -m container/config.json
neo-go contract compile -i neofsid/neofsid_contract.go -c neofsid/config.yml -m neofsid/config.json
neo-go contract compile -i netmap/netmap_contract.go -c netmap/config.yml -m netmap/config.json
neo-go contract compile -i proxy/proxy_contract.go -c proxy/config.yml -m proxy/config.json
neo-go contract compile -i reputation/reputation_contract.go -c reputation/config.yml -m reputation/config.json
neo-go contract compile -i neofs/neofs_contract.go -c neofs/config.yml -m neofs/config.json
neo-go contract compile -i processing/processing_contract.go -c processing/config.yml -m processing/config.json
```

You can specify path to the `neo-go` binary with `NEOGO` environment variable:
Expand Down
72 changes: 66 additions & 6 deletions alphabet/alphabet_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package alphabetcontract
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/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"
Expand All @@ -19,6 +20,8 @@ const (
totalKey = "threshold"
nameKey = "name"

notaryDisabledKey = "notary"

version = 1
)

Expand All @@ -30,7 +33,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
}
}

func Init(owner interop.Hash160, addrNetmap, addrProxy interop.Hash160, name string, index, total int) {
func Init(notaryDisabled bool, owner interop.Hash160, addrNetmap, addrProxy interop.Hash160, name string, index, total int) {
ctx := storage.GetContext()

if !common.HasUpdateAccess(ctx) {
Expand All @@ -48,6 +51,13 @@ func Init(owner interop.Hash160, addrNetmap, addrProxy interop.Hash160, name str
storage.Put(ctx, indexKey, index)
storage.Put(ctx, totalKey, total)

// initialize the way to collect signatures
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
if notaryDisabled {
common.InitVote(ctx)
runtime.Log(name + " notary disabled")
}

runtime.Log(name + " contract initialized")
}

Expand Down Expand Up @@ -104,6 +114,7 @@ func checkPermission(ir []common.IRNode) bool {

func Emit() bool {
ctx := storage.GetReadOnlyContext()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)

alphabet := common.AlphabetNodes()
if !checkPermission(alphabet) {
Expand All @@ -126,7 +137,15 @@ func Emit() bool {
gas.Transfer(contractHash, proxyAddr, proxyGas, nil)
runtime.Log("utility token has been emitted to proxy contract")

innerRing := common.InnerRingNodes()
var innerRing []common.IRNode

if notaryDisabled {
netmapContract := storage.Get(ctx, netmapKey).(interop.Hash160)
innerRing = common.InnerRingNodesFromNetmap(netmapContract)
} else {
innerRing = common.InnerRingNodes()
}

gasPerNode := gasBalance / 2 * 7 / 8 / len(innerRing)

if gasPerNode != 0 {
Expand All @@ -142,13 +161,27 @@ func Emit() bool {
}

func Vote(epoch int, candidates []interop.PublicKey) {
ctx := storage.GetReadOnlyContext()
ctx := storage.GetContext()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
index := index(ctx)
name := name(ctx)

multiaddr := common.AlphabetAddress()
if !runtime.CheckWitness(multiaddr) {
panic("invalid invoker")
var ( // for invocation collection without notary
alphabet []common.IRNode
nodeKey []byte
)

if notaryDisabled {
alphabet = common.AlphabetNodes()
nodeKey = common.InnerRingInvoker(alphabet)
if len(nodeKey) == 0 {
panic("invalid invoker")
}
} else {
multiaddr := common.AlphabetAddress()
if !runtime.CheckWitness(multiaddr) {
panic("invalid invoker")
}
}

curEpoch := currentEpoch(ctx)
Expand All @@ -159,6 +192,18 @@ func Vote(epoch int, candidates []interop.PublicKey) {
candidate := candidates[index%len(candidates)]
address := runtime.GetExecutingScriptHash()

if notaryDisabled {
threshold := len(alphabet)*2/3 + 1
id := voteID(epoch, candidates)

n := common.Vote(ctx, id, nodeKey)
if n < threshold {
return
}

common.RemoveVotes(ctx, id)
}

ok := neo.Vote(address, candidate)
if ok {
runtime.Log(name + ": successfully voted for validator")
Expand All @@ -169,6 +214,21 @@ func Vote(epoch int, candidates []interop.PublicKey) {
return
}

func voteID(epoch interface{}, args []interop.PublicKey) []byte {
var (
result []byte
epochBytes = epoch.([]byte)
)

result = append(result, epochBytes...)

for i := range args {
result = append(result, args[i]...)
}

return crypto.Sha256(result)
}

func Name() string {
ctx := storage.GetReadOnlyContext()
return name(ctx)
Expand Down
22 changes: 20 additions & 2 deletions audit/audit_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const (
version = 1

netmapContractKey = "netmapScriptHash"

notaryDisabledKey = "notary"
)

func Init(owner interop.Hash160, addrNetmap interop.Hash160) {
func Init(notaryDisabled bool, owner interop.Hash160, addrNetmap interop.Hash160) {
ctx := storage.GetContext()

if !common.HasUpdateAccess(ctx) {
Expand All @@ -54,6 +56,12 @@ func Init(owner interop.Hash160, addrNetmap interop.Hash160) {
storage.Put(ctx, common.OwnerKey, owner)
storage.Put(ctx, netmapContractKey, addrNetmap)

// initialize the way to collect signatures
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
if notaryDisabled {
runtime.Log("audit contract notary disabled")
}

runtime.Log("audit contract initialized")
}

Expand All @@ -73,7 +81,16 @@ func Migrate(script []byte, manifest []byte) bool {

func Put(rawAuditResult []byte) bool {
ctx := storage.GetContext()
innerRing := common.InnerRingNodes()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)

var innerRing []common.IRNode

if notaryDisabled {
netmapContract := storage.Get(ctx, netmapContractKey).(interop.Hash160)
innerRing = common.InnerRingNodesFromNetmap(netmapContract)
} else {
innerRing = common.InnerRingNodes()
}

hdr := newAuditHeader(rawAuditResult)
presented := false
Expand Down Expand Up @@ -148,6 +165,7 @@ func list(it iterator.Iterator) [][]byte {
ignore := [][]byte{
[]byte(netmapContractKey),
[]byte(common.OwnerKey),
[]byte(notaryDisabledKey),
}

loop:
Expand Down
Loading