2021-07-04 11:00:56 +00:00
|
|
|
package audit
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-17 15:58:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-06-30 10:59:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-12-17 15:58:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 17:36:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-12-17 15:58:12 +00:00
|
|
|
auditHeader struct {
|
|
|
|
epoch int
|
|
|
|
cid []byte
|
|
|
|
from interop.PublicKey
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
// Audit key is a combination of epoch, container ID and public key of node that
|
|
|
|
// executed audit. Together it should be no more than 64 bytes. We can't shrink
|
|
|
|
// epoch and container ID since we iterate over these values. But we can shrink
|
|
|
|
// public key by using first bytes of the hashed value.
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-12-17 15:58:12 +00:00
|
|
|
const maxKeySize = 24 // 24 + 32 (container ID length) + 8 (epoch length) = 64
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
func (a auditHeader) ID() []byte {
|
|
|
|
var buf interface{} = a.epoch
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-12 12:16:36 +00:00
|
|
|
hashedKey := crypto.Sha256(a.from)
|
2020-12-17 15:58:12 +00:00
|
|
|
shortedKey := hashedKey[:maxKeySize]
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return append(buf.([]byte), append(a.cid, shortedKey...)...)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
const (
|
2021-02-11 15:55:32 +00:00
|
|
|
netmapContractKey = "netmapScriptHash"
|
2021-04-27 10:48:40 +00:00
|
|
|
|
|
|
|
notaryDisabledKey = "notary"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
addrNetmap interop.Hash160
|
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
if len(args.addrNetmap) != 20 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, args.addrNetmap)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-05-04 14:02:05 +00:00
|
|
|
runtime.Log("audit contract notary disabled")
|
|
|
|
}
|
2021-04-27 10:48:40 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("audit contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("audit contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// Put method stores stable marshalled `DataAuditResult` structure. Can be
|
|
|
|
// invoked only by Inner Ring nodes.
|
|
|
|
//
|
|
|
|
// Inner Ring nodes perform audit of the containers and produce `DataAuditResult`
|
|
|
|
// structures. They are being stored in audit contract and used for settlements
|
|
|
|
// in later epochs.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Put(rawAuditResult []byte) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:07:08 +00:00
|
|
|
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()
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
hdr := newAuditHeader(rawAuditResult)
|
|
|
|
presented := false
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
for i := range innerRing {
|
|
|
|
ir := innerRing[i]
|
2021-02-02 19:20:31 +00:00
|
|
|
if common.BytesEqual(ir.PublicKey, hdr.from) {
|
2020-10-27 12:14:06 +00:00
|
|
|
presented = true
|
2020-12-17 15:58:12 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
if !runtime.CheckWitness(hdr.from) || !presented {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("put access denied")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
storage.Put(ctx, hdr.ID(), rawAuditResult)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
runtime.Log("audit: result has been saved")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// Get method returns stable marshaled DataAuditResult structure.
|
|
|
|
//
|
|
|
|
// ID of the DataAuditResult can be obtained from listing methods.
|
2020-12-17 15:58:12 +00:00
|
|
|
func Get(id []byte) []byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-12-17 15:58:12 +00:00
|
|
|
return storage.Get(ctx, id).([]byte)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// List method returns list of all available DataAuditResult IDs from
|
|
|
|
// contract storage.
|
2020-12-17 15:58:12 +00:00
|
|
|
func List() [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, []byte{}, storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return list(it)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// ListByEpoch method returns list of DataAuditResult IDs generated in
|
|
|
|
// specified epoch.
|
2020-12-17 15:58:12 +00:00
|
|
|
func ListByEpoch(epoch int) [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-04-01 06:36:29 +00:00
|
|
|
var buf interface{} = epoch
|
|
|
|
it := storage.Find(ctx, buf.([]byte), storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return list(it)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// ListByCID method returns list of DataAuditResult IDs generated in
|
|
|
|
// specified epoch for specified container.
|
2020-12-17 15:58:12 +00:00
|
|
|
func ListByCID(epoch int, cid []byte) [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
var buf interface{} = epoch
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
prefix := append(buf.([]byte), cid...)
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, prefix, storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return list(it)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// ListByNode method returns list of DataAuditResult IDs generated in
|
|
|
|
// specified epoch for specified container by specified Inner Ring node.
|
2020-12-17 15:58:12 +00:00
|
|
|
func ListByNode(epoch int, cid []byte, key interop.PublicKey) [][]byte {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-12-17 15:58:12 +00:00
|
|
|
hdr := auditHeader{
|
|
|
|
epoch: epoch,
|
|
|
|
cid: cid,
|
|
|
|
from: key,
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, hdr.ID(), storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return list(it)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
func list(it iterator.Iterator) [][]byte {
|
|
|
|
var result [][]byte
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
ignore := [][]byte{
|
|
|
|
[]byte(netmapContractKey),
|
2021-04-29 13:07:08 +00:00
|
|
|
[]byte(notaryDisabledKey),
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop:
|
2020-12-17 15:58:12 +00:00
|
|
|
for iterator.Next(it) {
|
2021-02-08 15:23:08 +00:00
|
|
|
key := iterator.Value(it).([]byte) // iterator MUST BE `storage.KeysOnly`
|
2021-02-11 15:55:32 +00:00
|
|
|
for _, ignoreKey := range ignore {
|
|
|
|
if common.BytesEqual(key, ignoreKey) {
|
|
|
|
continue loop
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
2020-12-17 15:58:12 +00:00
|
|
|
|
|
|
|
result = append(result, key)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
return result
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:25:29 +00:00
|
|
|
// Version returns version of the contract.
|
2020-12-17 15:58:12 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
// readNext reads length from first byte and then reads data (max 127 bytes).
|
|
|
|
func readNext(input []byte) ([]byte, int) {
|
|
|
|
var buf interface{} = input[0]
|
|
|
|
ln := buf.(int)
|
|
|
|
|
|
|
|
return input[1 : 1+ln], 1 + ln
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
func newAuditHeader(input []byte) auditHeader {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-12-21 14:17:17 +00:00
|
|
|
offset := int(input[1])
|
|
|
|
offset = 2 + offset + 1 // version prefix + version len + epoch prefix
|
|
|
|
|
|
|
|
var buf interface{} = input[offset : offset+8] // [ 8 integer bytes ]
|
2020-12-17 15:58:12 +00:00
|
|
|
epoch := buf.(int)
|
|
|
|
|
2020-12-21 14:17:17 +00:00
|
|
|
offset = offset + 8
|
|
|
|
|
2020-12-17 15:58:12 +00:00
|
|
|
// cid is a nested structure with raw bytes
|
|
|
|
// [ cid struct prefix (wireType + len = 2 bytes), cid value wireType (1 byte), ... ]
|
2020-12-21 14:17:17 +00:00
|
|
|
cid, cidOffset := readNext(input[offset+2+1:])
|
2020-12-17 15:58:12 +00:00
|
|
|
|
|
|
|
// key is a raw byte
|
|
|
|
// [ public key wireType (1 byte), ... ]
|
2020-12-21 14:17:17 +00:00
|
|
|
key, _ := readNext(input[offset+2+1+cidOffset+1:])
|
2020-12-17 15:58:12 +00:00
|
|
|
|
|
|
|
return auditHeader{
|
|
|
|
epoch,
|
|
|
|
cid,
|
|
|
|
key,
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|