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,6 +8,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 (
|
||||
|
@ -19,12 +20,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
|
||||
}
|
||||
|
||||
netmapNode struct {
|
||||
node storageNode
|
||||
state nodeState
|
||||
|
@ -95,7 +90,7 @@ func Init(keys [][]byte) {
|
|||
setSerialized(ctx, netmapKey, []netmapNode{})
|
||||
setSerialized(ctx, snapshot0Key, []netmapNode{})
|
||||
setSerialized(ctx, snapshot1Key, []netmapNode{})
|
||||
setSerialized(ctx, voteKey, []ballot{})
|
||||
setSerialized(ctx, voteKey, []common.Ballot{})
|
||||
|
||||
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 {
|
||||
var (
|
||||
newCandidates []ballot
|
||||
newCandidates []common.Ballot
|
||||
candidates = getBallots(ctx)
|
||||
found = -1
|
||||
blockHeight = blockchain.GetHeight()
|
||||
|
@ -432,12 +427,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) {
|
||||
|
@ -446,7 +441,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)
|
||||
}
|
||||
|
||||
|
@ -455,10 +450,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
|
||||
}
|
||||
|
||||
|
@ -469,13 +464,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)
|
||||
}
|
||||
}
|
||||
|
@ -510,13 +505,13 @@ func getSnapshot(ctx storage.Context, key string) []storageNode {
|
|||
return []storageNode{}
|
||||
}
|
||||
|
||||
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 setSerialized(ctx storage.Context, key interface{}, value interface{}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue