2021-07-04 11:00:56 +00:00
|
|
|
package netmap
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-03-17 08:10:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-07-21 11:45:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// NodeState is an enumeration for node states.
|
|
|
|
type NodeState int
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// Various Node states
|
|
|
|
const (
|
|
|
|
_ NodeState = iota
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// NodeStateOnline stands for nodes that are in full network and
|
|
|
|
// operational availability.
|
|
|
|
NodeStateOnline
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// NodeStateOffline stands for nodes that are in network unavailability.
|
|
|
|
NodeStateOffline
|
|
|
|
|
|
|
|
// NodeStateMaintenance stands for nodes under maintenance with partial
|
|
|
|
// network availability.
|
|
|
|
NodeStateMaintenance
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2023-01-11 07:50:07 +00:00
|
|
|
// Node groups data related to FrostFS storage nodes registered in the FrostFS
|
2022-09-28 07:41:51 +00:00
|
|
|
// network. The information is stored in the current contract.
|
|
|
|
type Node struct {
|
2023-01-11 07:50:07 +00:00
|
|
|
// Information about the node encoded according to the FrostFS binary
|
2022-09-28 07:41:51 +00:00
|
|
|
// protocol.
|
|
|
|
BLOB []byte
|
|
|
|
|
|
|
|
// Current node state.
|
|
|
|
State NodeState
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
const (
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
2021-04-27 10:56:02 +00:00
|
|
|
innerRingKey = "innerring"
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
// DefaultSnapshotCount contains the number of previous snapshots stored by this contract.
|
2022-03-10 10:53:36 +00:00
|
|
|
// Must be less than 255.
|
2022-03-28 06:39:22 +00:00
|
|
|
DefaultSnapshotCount = 10
|
|
|
|
snapshotCountKey = "snapshotCount"
|
2022-03-10 10:53:36 +00:00
|
|
|
snapshotKeyPrefix = "snapshot_"
|
|
|
|
snapshotCurrentIDKey = "snapshotCurrent"
|
|
|
|
snapshotEpoch = "snapshotEpoch"
|
|
|
|
snapshotBlockKey = "snapshotBlock"
|
2021-03-17 13:58:56 +00:00
|
|
|
|
|
|
|
containerContractKey = "containerScriptHash"
|
|
|
|
balanceContractKey = "balanceScriptHash"
|
2021-04-27 10:48:40 +00:00
|
|
|
|
|
|
|
cleanupEpochMethod = "newEpoch"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-29 18:15:53 +00:00
|
|
|
configPrefix = []byte("config")
|
|
|
|
candidatePrefix = []byte("candidate")
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-29 20:30:42 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
common.RmAndCheckNotaryDisabledKey(data, notaryDisabledKey)
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
var args = data.(struct {
|
2023-03-07 08:21:11 +00:00
|
|
|
//TODO(@acid-ant): #9 remove notaryDisabled in future version
|
2021-11-29 16:34:11 +00:00
|
|
|
notaryDisabled bool
|
|
|
|
addrBalance interop.Hash160
|
|
|
|
addrContainer interop.Hash160
|
|
|
|
keys []interop.PublicKey
|
|
|
|
config [][]byte
|
2021-12-27 08:49:30 +00:00
|
|
|
version int
|
2021-11-29 16:34:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ln := len(args.config)
|
2021-10-19 10:04:10 +00:00
|
|
|
if ln%2 != 0 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("bad configuration")
|
2021-10-19 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
2021-11-29 16:34:11 +00:00
|
|
|
key := args.config[i*2]
|
|
|
|
val := args.config[i*2+1]
|
2021-10-19 10:04:10 +00:00
|
|
|
|
|
|
|
setConfig(ctx, key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isUpdate {
|
2021-12-27 08:49:30 +00:00
|
|
|
common.CheckVersion(args.version)
|
2021-10-19 10:04:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(args.addrBalance) != interop.Hash160Len || len(args.addrContainer) != interop.Hash160Len {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2021-03-17 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
// epoch number is a little endian int, it doesn't need to be serialized
|
2022-03-28 06:39:22 +00:00
|
|
|
storage.Put(ctx, snapshotCountKey, DefaultSnapshotCount)
|
2020-10-27 12:14:06 +00:00
|
|
|
storage.Put(ctx, snapshotEpoch, 0)
|
2021-07-21 11:45:32 +00:00
|
|
|
storage.Put(ctx, snapshotBlockKey, 0)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
prefix := []byte(snapshotKeyPrefix)
|
2022-03-28 06:39:22 +00:00
|
|
|
for i := 0; i < DefaultSnapshotCount; i++ {
|
2022-09-28 07:41:51 +00:00
|
|
|
common.SetSerialized(ctx, append(prefix, byte(i)), []Node{})
|
2022-03-10 10:53:36 +00:00
|
|
|
}
|
2022-03-28 06:39:22 +00:00
|
|
|
storage.Put(ctx, snapshotCurrentIDKey, 0)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, balanceContractKey, args.addrBalance)
|
|
|
|
storage.Put(ctx, containerContractKey, args.addrContainer)
|
2021-03-17 13:58:56 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("netmap contract initialized")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It 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("netmap contract updated")
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// AddPeerIR accepts Alphabet calls in the notary-enabled contract setting and
|
|
|
|
// behaves similar to AddPeer in the notary-disabled one.
|
|
|
|
//
|
|
|
|
// AddPeerIR MUST NOT be called in notary-disabled contract setting.
|
|
|
|
// AddPeerIR MUST be called by the Alphabet member only.
|
2022-03-11 09:19:35 +00:00
|
|
|
func AddPeerIR(nodeInfo []byte) {
|
2021-12-02 15:59:29 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
|
|
|
|
2022-04-01 05:52:54 +00:00
|
|
|
publicKey := nodeInfo[2:35] // V2 format: offset:2, len:33
|
2022-09-28 07:41:51 +00:00
|
|
|
|
|
|
|
addToNetmap(ctx, publicKey, Node{
|
|
|
|
BLOB: nodeInfo,
|
|
|
|
State: NodeStateOnline,
|
|
|
|
})
|
2021-12-02 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 07:50:07 +00:00
|
|
|
// AddPeer accepts information about the network map candidate in the FrostFS
|
2023-03-07 08:21:11 +00:00
|
|
|
// binary protocol format and does nothing. Keep method because storage node
|
|
|
|
// creates a notary transaction with this method, which produces a notary
|
|
|
|
// notification (implicit here).
|
2021-05-21 11:37:31 +00:00
|
|
|
func AddPeer(nodeInfo []byte) {
|
2023-03-07 08:21:11 +00:00
|
|
|
// V2 format - offset:2, len:33
|
|
|
|
common.CheckWitness(nodeInfo[2:35])
|
|
|
|
return
|
2022-09-28 07:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updates state of the network map candidate by its public key in the contract
|
|
|
|
// storage, and throws UpdateStateSuccess notification after this.
|
|
|
|
//
|
|
|
|
// State MUST be from the NodeState enum.
|
|
|
|
func updateCandidateState(ctx storage.Context, publicKey interop.PublicKey, state NodeState) {
|
|
|
|
switch state {
|
|
|
|
case NodeStateOffline:
|
|
|
|
removeFromNetmap(ctx, publicKey)
|
|
|
|
runtime.Log("remove storage node from the network map")
|
|
|
|
case NodeStateOnline, NodeStateMaintenance:
|
|
|
|
updateNetmapState(ctx, publicKey, state)
|
|
|
|
runtime.Log("update state of the network map candidate")
|
|
|
|
default:
|
|
|
|
panic("unsupported state")
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Notify("UpdateStateSuccess", publicKey, state)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// UpdateState accepts new state to be assigned to network map candidate
|
2023-03-07 08:21:11 +00:00
|
|
|
// identified by the given public key, identifies the signer.
|
|
|
|
// Applicable only for notary-enabled environment.
|
2022-09-28 07:41:51 +00:00
|
|
|
//
|
|
|
|
// Signers:
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2022-09-28 07:41:51 +00:00
|
|
|
// (a) candidate himself only, if provided public key corresponds to the signer
|
|
|
|
// (b) Alphabet member only
|
|
|
|
// (ab) both candidate and Alphabet member
|
|
|
|
// (c) others
|
2022-03-22 07:50:36 +00:00
|
|
|
//
|
2022-09-28 07:41:51 +00:00
|
|
|
// UpdateState case-by-case behavior:
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2023-03-07 08:21:11 +00:00
|
|
|
// (a) panics
|
|
|
|
// (b) panics
|
|
|
|
// (ab) updates candidate's state in the contract storage (*), and throws
|
2022-09-28 07:41:51 +00:00
|
|
|
// UpdateStateSuccess with the provided key and new state
|
|
|
|
// (c) panics
|
|
|
|
//
|
|
|
|
// (*) Candidate is removed from the candidate set if state is NodeStateOffline.
|
|
|
|
// Any other state is written into candidate's descriptor in the contract storage.
|
|
|
|
// If requested candidate is missing, panic occurs. Throws UpdateStateSuccess
|
|
|
|
// notification on success.
|
|
|
|
//
|
|
|
|
// State MUST be from the NodeState enum. Public key MUST be
|
|
|
|
// interop.PublicKeyCompressedLen bytes.
|
|
|
|
func UpdateState(state NodeState, publicKey interop.PublicKey) {
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(publicKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect public key")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
common.CheckWitness(publicKey)
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
2021-04-29 13:20:00 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
updateCandidateState(ctx, publicKey, state)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// UpdateStateIR accepts Alphabet calls in the notary-enabled contract setting
|
|
|
|
// and behaves similar to UpdateState, but does not require candidate's
|
|
|
|
// signature presence.
|
|
|
|
//
|
|
|
|
// UpdateStateIR MUST NOT be called in notary-disabled contract setting.
|
|
|
|
// UpdateStateIR MUST be called by the Alphabet member only.
|
2022-09-20 07:34:27 +00:00
|
|
|
func UpdateStateIR(state NodeState, publicKey interop.PublicKey) {
|
2022-03-10 12:01:10 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
updateCandidateState(ctx, publicKey, state)
|
2022-03-10 12:01:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// NewEpoch method changes the epoch number up to the provided epochNum argument. It can
|
|
|
|
// be invoked only by Alphabet nodes. If provided epoch number is less than the
|
|
|
|
// current epoch number or equals it, the method throws panic.
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// When epoch number is updated, the contract sets storage node candidates as the current
|
|
|
|
// network map. The contract also invokes NewEpoch method on Balance and Container
|
2021-07-22 11:37:52 +00:00
|
|
|
// contracts.
|
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces NewEpoch notification.
|
2021-05-21 11:37:31 +00:00
|
|
|
func NewEpoch(epochNum int) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:20:00 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2020-12-25 11:45:24 +00:00
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
if epochNum <= currentEpoch {
|
2021-05-21 11:37:31 +00:00
|
|
|
panic("invalid epoch") // ignore invocations with invalid epoch
|
2020-12-25 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
dataOnlineState := filterNetmap(ctx)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("process new epoch")
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// todo: check if provided epoch number is bigger than current
|
|
|
|
storage.Put(ctx, snapshotEpoch, epochNum)
|
2021-07-21 11:45:32 +00:00
|
|
|
storage.Put(ctx, snapshotBlockKey, ledger.CurrentIndex())
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
2022-03-28 06:39:22 +00:00
|
|
|
id = (id + 1) % getSnapshotCount(ctx)
|
2022-03-10 10:53:36 +00:00
|
|
|
storage.Put(ctx, snapshotCurrentIDKey, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
// put netmap into actual snapshot
|
2022-03-10 10:53:36 +00:00
|
|
|
common.SetSerialized(ctx, snapshotKeyPrefix+string([]byte{byte(id)}), dataOnlineState)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
// make clean up routines in other contracts
|
|
|
|
cleanup(ctx, epochNum)
|
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
runtime.Notify("NewEpoch", epochNum)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Epoch method returns the current epoch number.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Epoch() int {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// LastEpochBlock method returns the block number when the current epoch was applied.
|
2021-07-21 11:45:32 +00:00
|
|
|
func LastEpochBlock() int {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Get(ctx, snapshotBlockKey).(int)
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// Netmap returns set of information about the storage nodes representing a network
|
|
|
|
// map in the current epoch.
|
|
|
|
//
|
|
|
|
// Current state of each node is represented in the State field. It MAY differ
|
|
|
|
// with the state encoded into BLOB field, in this case binary encoded state
|
|
|
|
// MUST NOT be processed.
|
|
|
|
func Netmap() []Node {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
|
|
|
return getSnapshot(ctx, snapshotKeyPrefix+string([]byte{byte(id)}))
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// NetmapCandidates returns set of information about the storage nodes
|
|
|
|
// representing candidates for the network map in the coming epoch.
|
|
|
|
//
|
|
|
|
// Current state of each node is represented in the State field. It MAY differ
|
|
|
|
// with the state encoded into BLOB field, in this case binary encoded state
|
|
|
|
// MUST NOT be processed.
|
|
|
|
func NetmapCandidates() []Node {
|
2021-06-02 10:39:00 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getNetmapNodes(ctx)
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// Snapshot returns set of information about the storage nodes representing a network
|
|
|
|
// map in (current-diff)-th epoch.
|
|
|
|
//
|
|
|
|
// Diff MUST NOT be negative. Diff MUST be less than maximum number of network
|
|
|
|
// map snapshots stored in the contract. The limit is a contract setting,
|
|
|
|
// DefaultSnapshotCount by default. See UpdateSnapshotCount for details.
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2022-09-28 07:41:51 +00:00
|
|
|
// Current state of each node is represented in the State field. It MAY differ
|
|
|
|
// with the state encoded into BLOB field, in this case binary encoded state
|
|
|
|
// MUST NOT be processed.
|
|
|
|
func Snapshot(diff int) []Node {
|
2022-03-28 06:39:22 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
count := getSnapshotCount(ctx)
|
|
|
|
if diff < 0 || count <= diff {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect diff")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:53:36 +00:00
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
2022-03-28 06:39:22 +00:00
|
|
|
needID := (id - diff + count) % count
|
2022-03-10 10:53:36 +00:00
|
|
|
key := snapshotKeyPrefix + string([]byte{byte(needID)})
|
2020-10-27 12:14:06 +00:00
|
|
|
return getSnapshot(ctx, key)
|
|
|
|
}
|
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
func getSnapshotCount(ctx storage.Context) int {
|
|
|
|
return storage.Get(ctx, snapshotCountKey).(int)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// UpdateSnapshotCount updates the number of the stored snapshots.
|
|
|
|
// If a new number is less than the old one, old snapshots are removed.
|
|
|
|
// Otherwise, history is extended with empty snapshots, so
|
2022-03-28 06:39:22 +00:00
|
|
|
// `Snapshot` method can return invalid results for `diff = new-old` epochs
|
|
|
|
// until `diff` epochs have passed.
|
2022-09-28 07:41:51 +00:00
|
|
|
//
|
|
|
|
// Count MUST NOT be negative.
|
2022-03-28 06:39:22 +00:00
|
|
|
func UpdateSnapshotCount(count int) {
|
|
|
|
common.CheckAlphabetWitness(common.AlphabetAddress())
|
|
|
|
if count < 0 {
|
|
|
|
panic("count must be positive")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
curr := getSnapshotCount(ctx)
|
|
|
|
if curr == count {
|
|
|
|
panic("count has not changed")
|
|
|
|
}
|
|
|
|
storage.Put(ctx, snapshotCountKey, count)
|
|
|
|
|
|
|
|
id := storage.Get(ctx, snapshotCurrentIDKey).(int)
|
|
|
|
var delStart, delFinish int
|
|
|
|
if curr < count {
|
|
|
|
// Increase history size.
|
|
|
|
//
|
|
|
|
// Old state (N = count, K = curr, E = current index, C = current epoch)
|
|
|
|
// KEY INDEX: 0 | 1 | ... | E | E+1 | ... | K-1 | ... | N-1
|
|
|
|
// EPOCH : C-E | C-E+1 | ... | C | C-K+1 | ... | C-E-1 |
|
|
|
|
//
|
|
|
|
// New state:
|
|
|
|
// KEY INDEX: 0 | 1 | ... | E | E+1 | ... | K-1 | ... | N-1
|
|
|
|
// EPOCH : C-E | C-E+1 | ... | C | nil | ... | . | ... | C-E-1
|
|
|
|
//
|
|
|
|
// So we need to move tail snapshots N-K keys forward,
|
|
|
|
// i.e. from E+1 .. K to N-K+E+1 .. N
|
|
|
|
diff := count - curr
|
|
|
|
lower := diff + id + 1
|
|
|
|
for k := count - 1; k >= lower; k-- {
|
|
|
|
moveSnapshot(ctx, k-diff, k)
|
|
|
|
}
|
|
|
|
delStart, delFinish = id+1, id+1+diff
|
|
|
|
if curr < delFinish {
|
|
|
|
delFinish = curr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Decrease history size.
|
|
|
|
//
|
|
|
|
// Old state (N = curr, K = count)
|
|
|
|
// KEY INDEX: 0 | 1 | ... K1 ... | E | E+1 | ... K2-1 ... | N-1
|
|
|
|
// EPOCH : C-E | C-E+1 | ... .. ... | C | C-N+1 | ... ... ... | C-E-1
|
|
|
|
var step, start int
|
|
|
|
if id < count {
|
|
|
|
// K2 case, move snapshots from E+1+N-K .. N-1 range to E+1 .. K-1
|
|
|
|
// New state:
|
|
|
|
// KEY INDEX: 0 | 1 | ... | E | E+1 | ... | K-1
|
|
|
|
// EPOCH : C-E | C-E+1 | ... | C | C-K+1 | ... | C-E-1
|
|
|
|
step = curr - count
|
|
|
|
start = id + 1
|
|
|
|
} else {
|
|
|
|
// New state:
|
|
|
|
// KEY INDEX: 0 | 1 | ... | K-1
|
|
|
|
// EPOCH : C-K+1 | C-K+2 | ... | C
|
|
|
|
// K1 case, move snapshots from E-K+1 .. E range to 0 .. K-1
|
|
|
|
// AND replace current id with K-1
|
|
|
|
step = id - count + 1
|
|
|
|
storage.Put(ctx, snapshotCurrentIDKey, count-1)
|
|
|
|
}
|
|
|
|
for k := start; k < count; k++ {
|
|
|
|
moveSnapshot(ctx, k+step, k)
|
|
|
|
}
|
|
|
|
delStart, delFinish = count, curr
|
|
|
|
}
|
|
|
|
for k := delStart; k < delFinish; k++ {
|
|
|
|
key := snapshotKeyPrefix + string([]byte{byte(k)})
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveSnapshot(ctx storage.Context, from, to int) {
|
|
|
|
keyFrom := snapshotKeyPrefix + string([]byte{byte(from)})
|
|
|
|
keyTo := snapshotKeyPrefix + string([]byte{byte(to)})
|
|
|
|
data := storage.Get(ctx, keyFrom)
|
|
|
|
storage.Put(ctx, keyTo, data)
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// SnapshotByEpoch returns set of information about the storage nodes representing
|
|
|
|
// a network map in the given epoch.
|
2021-07-22 11:37:52 +00:00
|
|
|
//
|
2022-09-28 07:41:51 +00:00
|
|
|
// Behaves like Snapshot: it is called after difference with the current epoch is
|
|
|
|
// calculated.
|
|
|
|
func SnapshotByEpoch(epoch int) []Node {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-01-12 08:09:30 +00:00
|
|
|
currentEpoch := storage.Get(ctx, snapshotEpoch).(int)
|
|
|
|
|
|
|
|
return Snapshot(currentEpoch - epoch)
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:50:07 +00:00
|
|
|
// Config returns configuration value of FrostFS configuration. If key does
|
2021-07-22 11:37:52 +00:00
|
|
|
// not exists, returns nil.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Config(key []byte) interface{} {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return getConfig(ctx, key)
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:50:07 +00:00
|
|
|
// SetConfig key-value pair as a FrostFS runtime configuration value. It can be invoked
|
2021-07-22 11:37:52 +00:00
|
|
|
// only by Alphabet nodes.
|
2021-05-21 11:37:31 +00:00
|
|
|
func SetConfig(id, key, val []byte) {
|
2021-04-29 13:20:00 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-02-19 15:52:27 +00:00
|
|
|
setConfig(ctx, key, val)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("configuration has been updated")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
type record struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// ListConfig returns an array of structures that contain key and value of all
|
2023-01-11 07:50:07 +00:00
|
|
|
// FrostFS configuration records. Key and value are both byte arrays.
|
2020-10-27 12:14:06 +00:00
|
|
|
func ListConfig() []record {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
var config []record
|
|
|
|
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, configPrefix, storage.None)
|
2020-10-27 12:14:06 +00:00
|
|
|
for iterator.Next(it) {
|
2021-12-01 17:28:58 +00:00
|
|
|
pair := iterator.Value(it).(struct {
|
|
|
|
key []byte
|
|
|
|
val []byte
|
|
|
|
})
|
|
|
|
r := record{key: pair.key[len(configPrefix):], val: pair.val}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
config = append(config, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the version of the contract.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
// serializes and stores the given Node by its public key in the contract storage,
|
|
|
|
// and throws AddPeerSuccess notification after this.
|
|
|
|
//
|
|
|
|
// Public key MUST match the one encoded in BLOB field.
|
|
|
|
func addToNetmap(ctx storage.Context, publicKey []byte, node Node) {
|
|
|
|
storageKey := append(candidatePrefix, publicKey...)
|
2021-06-29 18:15:53 +00:00
|
|
|
storage.Put(ctx, storageKey, std.Serialize(node))
|
2022-09-28 07:41:51 +00:00
|
|
|
|
|
|
|
runtime.Notify("AddPeerSuccess", interop.PublicKey(publicKey))
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
func removeFromNetmap(ctx storage.Context, key interop.PublicKey) {
|
|
|
|
storageKey := append(candidatePrefix, key...)
|
|
|
|
storage.Delete(ctx, storageKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 07:42:36 +00:00
|
|
|
func updateNetmapState(ctx storage.Context, key interop.PublicKey, state NodeState) {
|
|
|
|
storageKey := append(candidatePrefix, key...)
|
|
|
|
raw := storage.Get(ctx, storageKey).([]byte)
|
2022-09-20 09:47:14 +00:00
|
|
|
if raw == nil {
|
|
|
|
panic("peer is missing")
|
|
|
|
}
|
2022-09-28 07:41:51 +00:00
|
|
|
node := std.Deserialize(raw).(Node)
|
|
|
|
node.State = state
|
2022-09-20 07:42:36 +00:00
|
|
|
storage.Put(ctx, storageKey, std.Serialize(node))
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
func filterNetmap(ctx storage.Context) []Node {
|
2020-10-27 12:14:06 +00:00
|
|
|
var (
|
|
|
|
netmap = getNetmapNodes(ctx)
|
2022-09-28 07:41:51 +00:00
|
|
|
result = []Node{}
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < len(netmap); i++ {
|
|
|
|
item := netmap[i]
|
2022-09-28 07:41:51 +00:00
|
|
|
if item.State != NodeStateOffline {
|
|
|
|
result = append(result, item)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
func getNetmapNodes(ctx storage.Context) []Node {
|
|
|
|
result := []Node{}
|
2021-06-29 18:15:53 +00:00
|
|
|
|
2021-11-29 16:56:19 +00:00
|
|
|
it := storage.Find(ctx, candidatePrefix, storage.ValuesOnly|storage.DeserializeValues)
|
2021-06-29 18:15:53 +00:00
|
|
|
for iterator.Next(it) {
|
2022-09-28 07:41:51 +00:00
|
|
|
node := iterator.Value(it).(Node)
|
2021-06-29 18:15:53 +00:00
|
|
|
result = append(result, node)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 18:15:53 +00:00
|
|
|
return result
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
func getSnapshot(ctx storage.Context, key string) []Node {
|
2020-10-27 12:14:06 +00:00
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2022-09-28 07:41:51 +00:00
|
|
|
return std.Deserialize(data.([]byte)).([]Node)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
return []Node{}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getConfig(ctx storage.Context, key interface{}) interface{} {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
return storage.Get(ctx, storageKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setConfig(ctx storage.Context, key, val interface{}) {
|
|
|
|
postfix := key.([]byte)
|
|
|
|
storageKey := append(configPrefix, postfix...)
|
|
|
|
|
|
|
|
storage.Put(ctx, storageKey, val)
|
|
|
|
}
|
2021-02-19 14:24:03 +00:00
|
|
|
|
2021-03-17 13:58:56 +00:00
|
|
|
func cleanup(ctx storage.Context, epoch int) {
|
|
|
|
balanceContractAddr := storage.Get(ctx, balanceContractKey).(interop.Hash160)
|
|
|
|
contract.Call(balanceContractAddr, cleanupEpochMethod, contract.All, epoch)
|
|
|
|
|
|
|
|
containerContractAddr := storage.Get(ctx, containerContractKey).(interop.Hash160)
|
|
|
|
contract.Call(containerContractAddr, cleanupEpochMethod, contract.All, epoch)
|
|
|
|
}
|