Skip to content

Commit

Permalink
feat: deterministic order for all output files
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Sep 3, 2024
1 parent a2cb86b commit a7a22f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
8 changes: 5 additions & 3 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"encoding/json"
"maps"
"slices"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -131,10 +133,10 @@ func getAccounts(
}
}
}
// Map to slice
// Map to slice with deterministic order
var accounts []Account
for _, a := range accountsByAddr {
accounts = append(accounts, a)
for _, addr := range slices.Sorted(maps.Keys(accountsByAddr)) {
accounts = append(accounts, accountsByAddr[addr])
}
return accounts
}
18 changes: 10 additions & 8 deletions distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type airdrop struct {
params distriParams
// addresses contains the airdrop amount per address.
addresses map[string]sdk.Int
addressesDetail map[string]airdropDetail
addressesDetail []addrAmtDetail
// nonVotersMultiplier ensures that non-voters don't hold more than 1/3 of
// the supply
nonVotersMultiplier sdk.Dec
Expand All @@ -50,7 +50,8 @@ type airdrop struct {
reservedAddr sdk.Dec
}

type airdropDetail struct {
type addrAmtDetail struct {
Address string `json:"address"`
YesDetail amtDetail `json:"yesDetail"`
NoDetail amtDetail `json:"noDetail"`
NWVDetail amtDetail `json:"nwvDetail"`
Expand Down Expand Up @@ -112,10 +113,9 @@ func (d distrib) votePercentages() map[govtypes.VoteOption]sdk.Dec {

func distribution(accounts []Account, params distriParams, prefix string) (airdrop, error) {
airdrop := airdrop{
params: params,
addresses: make(map[string]sdk.Int),
addressesDetail: make(map[string]airdropDetail),
icfSlash: sdk.ZeroDec(),
params: params,
addresses: make(map[string]sdk.Int),
icfSlash: sdk.ZeroDec(),
atom: distrib{
supply: sdk.ZeroDec(),
votes: newVoteMap(),
Expand Down Expand Up @@ -219,7 +219,8 @@ func distribution(accounts []Account, params distriParams, prefix string) (airdr
}
// Fill with "cosmos" prefixed address
airdrop.addresses[addr] = amtInt
airdrop.addressesDetail[addr] = airdropDetail{
ad := addrAmtDetail{
Address: addr,
YesDetail: amtDetail{
AtomAmt: yesAtomAmt,
Multiplier: params.yesVotesMultiplier,
Expand Down Expand Up @@ -264,9 +265,10 @@ func distribution(accounts []Account, params distriParams, prefix string) (airdr
},
Total: airdropAmt,
}
airdrop.addressesDetail = append(airdrop.addressesDetail, ad)
amt := yesAirdropAmt.Add(noAirdropAmt).Add(noWithVetoAirdropAmt).Add(abstainAirdropAmt).Add(noVoteAirdropAmt).Add(liquidAirdropAmt)
if !amt.Equal(airdropAmt) {
panic(fmt.Sprintf("WRONG %+v\n", airdrop.addressesDetail[addr]))
panic(fmt.Sprintf("WRONG %+v\n", ad))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/atomone-hub/govbox

go 1.21.3
go 1.23

require (
github.com/cosmos/cosmos-sdk v0.45.16
github.com/cosmos/ibc-go/v4 v4.5.1
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.1
github.com/go-echarts/go-echarts/v2 v2.3.3
github.com/gogo/protobuf v1.3.3
Expand Down Expand Up @@ -46,7 +47,6 @@ require (
github.com/cosmos/iavl v0.19.5 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ func distributionCmd() *ffcli.Command {
"liquidAtomAmt", "liquidMultiplier", "liquidBonusMalus", "liquidAtoneAmt",
"totalAtoneAmt",
})
for k, v := range airdrops[0].addressesDetail {
for _, v := range airdrops[0].addressesDetail {
w.Write([]string{
k, v.YesDetail.Factor.String(),
v.Address, v.YesDetail.Factor.String(),
v.YesDetail.AtomAmt.String(), v.YesDetail.Multiplier.String(), v.YesDetail.BonusMalus.String(), v.YesDetail.AtoneAmt.String(),
v.NoDetail.AtomAmt.String(), v.NoDetail.Multiplier.String(), v.NoDetail.BonusMalus.String(), v.NoDetail.AtoneAmt.String(),
v.NWVDetail.AtomAmt.String(), v.NWVDetail.Multiplier.String(), v.NWVDetail.BonusMalus.String(), v.NWVDetail.AtoneAmt.String(),
Expand Down

0 comments on commit a7a22f4

Please sign in to comment.