[#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:
Leonard Lyubich 2021-02-02 19:39:16 +03:00 committed by Alex Vanin
parent e87765b733
commit f9f2a03078
7 changed files with 105 additions and 123 deletions

View file

@ -10,6 +10,7 @@ import (
"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/util"
"github.com/nspcc-dev/neofs-contract/common"
)
type (
@ -21,12 +22,6 @@ type (
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 {
val []byte
sig []byte
@ -494,7 +489,7 @@ func innerRingInvoker(ir []irNode) []byte {
func vote(ctx storage.Context, id, from []byte) int {
var (
newCandidates []ballot
newCandidates []common.Ballot
candidates = getBallots(ctx)
found = -1
blockHeight = blockchain.GetHeight()
@ -503,12 +498,12 @@ func vote(ctx storage.Context, id, from []byte) int {
for i := 0; i < len(candidates); i++ {
cnd := candidates[i]
if blockHeight-cnd.block > blockDiff {
if blockHeight-cnd.Height > blockDiff {
continue
}
if bytesEqual(cnd.id, id) {
voters := cnd.n
if bytesEqual(cnd.ID, id) {
voters := cnd.Voters
for j := range voters {
if bytesEqual(voters[j], from) {
@ -517,7 +512,7 @@ func vote(ctx storage.Context, id, from []byte) int {
}
voters = append(voters, from)
cnd = ballot{id: id, n: voters, block: blockHeight}
cnd = common.Ballot{ID: id, Voters: voters, Height: blockHeight}
found = len(voters)
}
@ -526,10 +521,10 @@ func vote(ctx storage.Context, id, from []byte) int {
if found < 0 {
voters := [][]byte{from}
newCandidates = append(newCandidates, ballot{
id: id,
n: voters,
block: blockHeight})
newCandidates = append(newCandidates, common.Ballot{
ID: id,
Voters: voters,
Height: blockHeight})
found = 1
}
@ -540,13 +535,13 @@ func vote(ctx storage.Context, id, from []byte) int {
func removeVotes(ctx storage.Context, id []byte) {
var (
newCandidates []ballot
newCandidates []common.Ballot
candidates = getBallots(ctx)
)
for i := 0; i < len(candidates); i++ {
cnd := candidates[i]
if !bytesEqual(cnd.id, id) {
if !bytesEqual(cnd.ID, id) {
newCandidates = append(newCandidates, cnd)
}
}
@ -577,13 +572,13 @@ func getAllContainers(ctx storage.Context) [][]byte {
return list
}
func getBallots(ctx storage.Context) []ballot {
func getBallots(ctx storage.Context) []common.Ballot {
data := storage.Get(ctx, voteKey)
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 {