forked from TrueCloudLab/frostfs-contract
[#42] Share ballot structure between contracts
Create common package. Define Ballot struct in common package. Use new type in all contracts with ballots. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
e87765b733
commit
f9f2a03078
7 changed files with 105 additions and 123 deletions
|
@ -8,18 +8,13 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
irNode struct {
|
irNode struct {
|
||||||
key []byte
|
key []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
ballot struct {
|
|
||||||
id []byte // hash of validators list
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
height int // height is an neofs epoch when ballot was registered
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -70,7 +65,7 @@ func Init(addrNetmap []byte, name string, index, total int) {
|
||||||
storage.Put(ctx, indexKey, index)
|
storage.Put(ctx, indexKey, index)
|
||||||
storage.Put(ctx, totalKey, total)
|
storage.Put(ctx, totalKey, total)
|
||||||
|
|
||||||
setSerialized(ctx, voteKey, []ballot{})
|
setSerialized(ctx, voteKey, []common.Ballot{})
|
||||||
|
|
||||||
runtime.Log(name + " contract initialized")
|
runtime.Log(name + " contract initialized")
|
||||||
}
|
}
|
||||||
|
@ -213,15 +208,15 @@ func Name() string {
|
||||||
|
|
||||||
func vote(ctx storage.Context, epoch int, id, from []byte) int {
|
func vote(ctx storage.Context, epoch int, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -230,22 +225,22 @@ func vote(ctx storage.Context, epoch int, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, height: epoch}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: epoch}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add only valid ballots with current epochs
|
// add only valid ballots with current epochs
|
||||||
if cnd.height == epoch {
|
if cnd.Height == epoch {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
height: epoch})
|
Height: epoch})
|
||||||
found = 1
|
found = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,13 +251,13 @@ func vote(ctx storage.Context, epoch int, id, from []byte) int {
|
||||||
|
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,13 +265,13 @@ func removeVotes(ctx storage.Context, id []byte) {
|
||||||
setSerialized(ctx, voteKey, newCandidates)
|
setSerialized(ctx, voteKey, newCandidates)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -17,12 +18,6 @@ type (
|
||||||
key []byte
|
key []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
ballot struct {
|
|
||||||
id []byte // id of the voting decision
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
block int // block with the last vote
|
|
||||||
}
|
|
||||||
|
|
||||||
// Token holds all token info.
|
// Token holds all token info.
|
||||||
Token struct {
|
Token struct {
|
||||||
// Ticker symbol
|
// Ticker symbol
|
||||||
|
@ -405,7 +400,7 @@ func innerRingInvoker(ir []irNode) []byte {
|
||||||
|
|
||||||
func vote(ctx storage.Context, id, from []byte) int {
|
func vote(ctx storage.Context, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
blockHeight = blockchain.GetHeight()
|
blockHeight = blockchain.GetHeight()
|
||||||
|
@ -414,12 +409,12 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
|
|
||||||
if blockHeight-cnd.block > blockDiff {
|
if blockHeight-cnd.Height > blockDiff {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -428,7 +423,7 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, block: blockHeight}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -437,10 +432,10 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
block: blockHeight})
|
Height: blockHeight})
|
||||||
found = 1
|
found = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,13 +446,13 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -465,13 +460,13 @@ func removeVotes(ctx storage.Context, id []byte) {
|
||||||
setSerialized(ctx, voteKey, newCandidates)
|
setSerialized(ctx, voteKey, newCandidates)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
||||||
|
|
12
common/ballot.go
Normal file
12
common/ballot.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
type Ballot struct {
|
||||||
|
// ID of the voting decision.
|
||||||
|
ID []byte
|
||||||
|
|
||||||
|
// Public keys of already voted inner ring nodes.
|
||||||
|
Voters [][]byte
|
||||||
|
|
||||||
|
// Height of block with the last vote.
|
||||||
|
Height int
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -21,12 +22,6 @@ type (
|
||||||
info []byte
|
info []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
ballot struct {
|
|
||||||
id []byte // id of the voting decision
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
block int // block with the last vote
|
|
||||||
}
|
|
||||||
|
|
||||||
extendedACL struct {
|
extendedACL struct {
|
||||||
val []byte
|
val []byte
|
||||||
sig []byte
|
sig []byte
|
||||||
|
@ -494,7 +489,7 @@ func innerRingInvoker(ir []irNode) []byte {
|
||||||
|
|
||||||
func vote(ctx storage.Context, id, from []byte) int {
|
func vote(ctx storage.Context, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
blockHeight = blockchain.GetHeight()
|
blockHeight = blockchain.GetHeight()
|
||||||
|
@ -503,12 +498,12 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
|
|
||||||
if blockHeight-cnd.block > blockDiff {
|
if blockHeight-cnd.Height > blockDiff {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -517,7 +512,7 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, block: blockHeight}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -526,10 +521,10 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
block: blockHeight})
|
Height: blockHeight})
|
||||||
found = 1
|
found = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -540,13 +535,13 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -577,13 +572,13 @@ func getAllContainers(ctx storage.Context) [][]byte {
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEACL(ctx storage.Context, cid []byte) extendedACL {
|
func getEACL(ctx storage.Context, cid []byte) extendedACL {
|
||||||
|
|
|
@ -41,15 +41,10 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
ballot struct {
|
|
||||||
id []byte // id of the voting decision
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
block int // block with the last vote
|
|
||||||
}
|
|
||||||
|
|
||||||
node struct {
|
node struct {
|
||||||
pub []byte
|
pub []byte
|
||||||
}
|
}
|
||||||
|
@ -127,7 +122,7 @@ func Init(args [][]byte) bool {
|
||||||
|
|
||||||
// initialize all storage slices
|
// initialize all storage slices
|
||||||
setSerialized(ctx, innerRingKey, irList)
|
setSerialized(ctx, innerRingKey, irList)
|
||||||
setSerialized(ctx, voteKey, []ballot{})
|
setSerialized(ctx, voteKey, []common.Ballot{})
|
||||||
setSerialized(ctx, candidatesKey, []node{})
|
setSerialized(ctx, candidatesKey, []node{})
|
||||||
setSerialized(ctx, cashedChequesKey, []cheque{})
|
setSerialized(ctx, cashedChequesKey, []cheque{})
|
||||||
|
|
||||||
|
@ -548,7 +543,7 @@ func innerRingInvoker(ir []node) []byte {
|
||||||
// on unique voters for that decision.
|
// on unique voters for that decision.
|
||||||
func vote(ctx storage.Context, id, from []byte) int {
|
func vote(ctx storage.Context, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates = []ballot{} // it is explicit declaration of empty slice, not nil
|
newCandidates = []common.Ballot{} // it is explicit declaration of empty slice, not nil
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
blockHeight = blockchain.GetHeight()
|
blockHeight = blockchain.GetHeight()
|
||||||
|
@ -557,12 +552,12 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
|
|
||||||
if blockHeight-cnd.block > blockDiff {
|
if blockHeight-cnd.Height > blockDiff {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -571,7 +566,7 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, block: blockHeight}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,10 +577,10 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
found = 1
|
found = 1
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
|
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
block: blockHeight})
|
Height: blockHeight})
|
||||||
}
|
}
|
||||||
|
|
||||||
setSerialized(ctx, voteKey, newCandidates)
|
setSerialized(ctx, voteKey, newCandidates)
|
||||||
|
@ -597,13 +592,13 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
// inner ring nodes.
|
// inner ring nodes.
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates = []ballot{} // it is explicit declaration of empty slice, not nil
|
newCandidates = []common.Ballot{} // it is explicit declaration of empty slice, not nil
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -638,13 +633,13 @@ func getCashedCheques(ctx storage.Context) []cheque {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getInnerRingNodes returns deserialized slice of vote ballots.
|
// getInnerRingNodes returns deserialized slice of vote ballots.
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getConfig returns installed neofs configuration value or nil if it is not set.
|
// getConfig returns installed neofs configuration value or nil if it is not set.
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -15,12 +16,6 @@ type (
|
||||||
key []byte
|
key []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
ballot struct {
|
|
||||||
id []byte // id of the voting decision
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
block int // block with the last vote
|
|
||||||
}
|
|
||||||
|
|
||||||
UserInfo struct {
|
UserInfo struct {
|
||||||
Keys [][]byte
|
Keys [][]byte
|
||||||
}
|
}
|
||||||
|
@ -196,13 +191,13 @@ func getUserInfo(ctx storage.Context, key interface{}) UserInfo {
|
||||||
return UserInfo{Keys: [][]byte{}}
|
return UserInfo{Keys: [][]byte{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
||||||
|
@ -223,7 +218,7 @@ func innerRingInvoker(ir []irNode) []byte {
|
||||||
|
|
||||||
func vote(ctx storage.Context, id, from []byte) int {
|
func vote(ctx storage.Context, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
blockHeight = blockchain.GetHeight()
|
blockHeight = blockchain.GetHeight()
|
||||||
|
@ -232,12 +227,12 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
|
|
||||||
if blockHeight-cnd.block > blockDiff {
|
if blockHeight-cnd.Height > blockDiff {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -246,7 +241,7 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, block: blockHeight}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,10 +250,10 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
block: blockHeight})
|
Height: blockHeight})
|
||||||
found = 1
|
found = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,13 +264,13 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||||
|
"github.com/nspcc-dev/neofs-contract/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -19,12 +20,6 @@ type (
|
||||||
info []byte
|
info []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
ballot struct {
|
|
||||||
id []byte // id of the voting decision
|
|
||||||
n [][]byte // already voted inner ring nodes
|
|
||||||
block int // block with the last vote
|
|
||||||
}
|
|
||||||
|
|
||||||
netmapNode struct {
|
netmapNode struct {
|
||||||
node storageNode
|
node storageNode
|
||||||
state nodeState
|
state nodeState
|
||||||
|
@ -95,7 +90,7 @@ func Init(keys [][]byte) {
|
||||||
setSerialized(ctx, netmapKey, []netmapNode{})
|
setSerialized(ctx, netmapKey, []netmapNode{})
|
||||||
setSerialized(ctx, snapshot0Key, []netmapNode{})
|
setSerialized(ctx, snapshot0Key, []netmapNode{})
|
||||||
setSerialized(ctx, snapshot1Key, []netmapNode{})
|
setSerialized(ctx, snapshot1Key, []netmapNode{})
|
||||||
setSerialized(ctx, voteKey, []ballot{})
|
setSerialized(ctx, voteKey, []common.Ballot{})
|
||||||
|
|
||||||
runtime.Log("netmap contract initialized")
|
runtime.Log("netmap contract initialized")
|
||||||
}
|
}
|
||||||
|
@ -423,7 +418,7 @@ func filterNetmap(ctx storage.Context, st nodeState) []storageNode {
|
||||||
|
|
||||||
func vote(ctx storage.Context, id, from []byte) int {
|
func vote(ctx storage.Context, id, from []byte) int {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
found = -1
|
found = -1
|
||||||
blockHeight = blockchain.GetHeight()
|
blockHeight = blockchain.GetHeight()
|
||||||
|
@ -432,12 +427,12 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
|
|
||||||
if blockHeight-cnd.block > blockDiff {
|
if blockHeight-cnd.Height > blockDiff {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytesEqual(cnd.id, id) {
|
if bytesEqual(cnd.ID, id) {
|
||||||
voters := cnd.n
|
voters := cnd.Voters
|
||||||
|
|
||||||
for j := range voters {
|
for j := range voters {
|
||||||
if bytesEqual(voters[j], from) {
|
if bytesEqual(voters[j], from) {
|
||||||
|
@ -446,7 +441,7 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
voters = append(voters, from)
|
voters = append(voters, from)
|
||||||
cnd = ballot{id: id, n: voters, block: blockHeight}
|
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
|
||||||
found = len(voters)
|
found = len(voters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -455,10 +450,10 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
voters := [][]byte{from}
|
voters := [][]byte{from}
|
||||||
newCandidates = append(newCandidates, ballot{
|
newCandidates = append(newCandidates, common.Ballot{
|
||||||
id: id,
|
ID: id,
|
||||||
n: voters,
|
Voters: voters,
|
||||||
block: blockHeight})
|
Height: blockHeight})
|
||||||
found = 1
|
found = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,13 +464,13 @@ func vote(ctx storage.Context, id, from []byte) int {
|
||||||
|
|
||||||
func removeVotes(ctx storage.Context, id []byte) {
|
func removeVotes(ctx storage.Context, id []byte) {
|
||||||
var (
|
var (
|
||||||
newCandidates []ballot
|
newCandidates []common.Ballot
|
||||||
candidates = getBallots(ctx)
|
candidates = getBallots(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(candidates); i++ {
|
for i := 0; i < len(candidates); i++ {
|
||||||
cnd := candidates[i]
|
cnd := candidates[i]
|
||||||
if !bytesEqual(cnd.id, id) {
|
if !bytesEqual(cnd.ID, id) {
|
||||||
newCandidates = append(newCandidates, cnd)
|
newCandidates = append(newCandidates, cnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -510,13 +505,13 @@ func getSnapshot(ctx storage.Context, key string) []storageNode {
|
||||||
return []storageNode{}
|
return []storageNode{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBallots(ctx storage.Context) []ballot {
|
func getBallots(ctx storage.Context) []common.Ballot {
|
||||||
data := storage.Get(ctx, voteKey)
|
data := storage.Get(ctx, voteKey)
|
||||||
if data != nil {
|
if data != nil {
|
||||||
return binary.Deserialize(data.([]byte)).([]ballot)
|
return binary.Deserialize(data.([]byte)).([]common.Ballot)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []ballot{}
|
return []common.Ballot{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
func setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
||||||
|
|
Loading…
Reference in a new issue