Skip to content

Commit

Permalink
Renaming before NUM-556 (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored Jun 21, 2022
1 parent d62d8a3 commit 1acf540
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 173 deletions.
2 changes: 1 addition & 1 deletion pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func TestGetAccount(t *testing.T) {
Balances: map[string]int64{
"USD": 100,
},
Volumes: core.Volumes{
Volumes: core.AssetsVolumes{
"USD": {
Input: 100,
},
Expand Down
24 changes: 12 additions & 12 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,21 @@ func TestGetTransaction(t *testing.T) {

txs, _ := internal.DecodeSingleResponse[[]core.Transaction](t, rsp.Body)
tx := txs[0]
assert.EqualValues(t, core.AggregatedVolumes{
"world": core.Volumes{
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {},
},
"central_bank": core.Volumes{
"central_bank": core.AssetsVolumes{
"USD": {},
},
}, tx.PreCommitVolumes)
assert.EqualValues(t, core.AggregatedVolumes{
"world": core.Volumes{
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {
Output: 1000,
},
},
"central_bank": core.Volumes{
"central_bank": core.AssetsVolumes{
"USD": {
Input: 1000,
},
Expand All @@ -232,21 +232,21 @@ func TestGetTransaction(t *testing.T) {
assert.EqualValues(t, core.Metadata{}, ret.Metadata)
assert.EqualValues(t, "ref", ret.Reference)
assert.NotEmpty(t, ret.Timestamp)
assert.EqualValues(t, core.AggregatedVolumes{
"world": core.Volumes{
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {},
},
"central_bank": core.Volumes{
"central_bank": core.AssetsVolumes{
"USD": {},
},
}, ret.PreCommitVolumes)
assert.EqualValues(t, core.AggregatedVolumes{
"world": core.Volumes{
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {
Output: 1000,
},
},
"central_bank": core.Volumes{
"central_bank": core.AssetsVolumes{
"USD": {
Input: 1000,
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/bus/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type baseEvent struct {
type committedTransactions struct {
Transactions []core.Transaction `json:"transactions"`
// Deprecated (use postCommitVolumes)
Volumes core.AggregatedVolumes `json:"volumes"`
PostCommitVolumes core.AggregatedVolumes `json:"postCommitVolumes"`
PreCommitVolumes core.AggregatedVolumes `json:"preCommitVolumes"`
Volumes core.AccountsAssetsVolumes `json:"volumes"`
PostCommitVolumes core.AccountsAssetsVolumes `json:"postCommitVolumes"`
PreCommitVolumes core.AccountsAssetsVolumes `json:"preCommitVolumes"`
}

type savedMetadata struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const (
)

type Account struct {
Address string `json:"address" example:"users:001"`
Type string `json:"type,omitempty" example:"virtual"`
Balances map[string]int64 `json:"balances,omitempty" example:"COIN:100"`
Volumes Volumes `json:"volumes,omitempty"`
Metadata Metadata `json:"metadata" swaggertype:"object"`
Address string `json:"address" example:"users:001"`
Type string `json:"type,omitempty" example:"virtual"`
Balances AssetsBalances `json:"balances,omitempty" example:"COIN:100"`
Volumes AssetsVolumes `json:"volumes,omitempty"`
Metadata Metadata `json:"metadata" swaggertype:"object"`
}
8 changes: 4 additions & 4 deletions pkg/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (t *TransactionData) Reverse() TransactionData {

type Transaction struct {
TransactionData
ID uint64 `json:"txid"`
Timestamp string `json:"timestamp"`
PreCommitVolumes AggregatedVolumes `json:"preCommitVolumes,omitempty"` // Keep omitempty to keep consistent hash
PostCommitVolumes AggregatedVolumes `json:"postCommitVolumes,omitempty"` // Keep omitempty to keep consistent hash
ID uint64 `json:"txid"`
Timestamp string `json:"timestamp"`
PreCommitVolumes AccountsAssetsVolumes `json:"preCommitVolumes,omitempty"` // Keep omitempty to keep consistent hash
PostCommitVolumes AccountsAssetsVolumes `json:"postCommitVolumes,omitempty"` // Keep omitempty to keep consistent hash
}

func (t *Transaction) AppendPosting(p Posting) {
Expand Down
28 changes: 14 additions & 14 deletions pkg/core/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
)

type Volume struct {
type Volumes struct {
Input int64 `json:"input"`
Output int64 `json:"output"`
}
Expand All @@ -16,48 +16,48 @@ type VolumesWithBalance struct {
Balance int64 `json:"balance"`
}

func (v Volume) MarshalJSON() ([]byte, error) {
func (v Volumes) MarshalJSON() ([]byte, error) {
return json.Marshal(VolumesWithBalance{
Input: v.Input,
Output: v.Output,
Balance: v.Input - v.Output,
})
}

func (v Volume) Balance() int64 {
func (v Volumes) Balance() int64 {
return v.Input - v.Output
}

type Balances map[string]int64
type Volumes map[string]Volume
type AssetsBalances map[string]int64
type AssetsVolumes map[string]Volumes

func (v Volumes) Balances() Balances {
balances := Balances{}
func (v AssetsVolumes) Balances() AssetsBalances {
balances := AssetsBalances{}
for asset, vv := range v {
balances[asset] = vv.Input - vv.Output
}
return balances
}

type AggregatedVolumes map[string]Volumes
type AccountsAssetsVolumes map[string]AssetsVolumes

// Scan - Implement the database/sql scanner interface
func (m *AggregatedVolumes) Scan(value interface{}) error {
func (a *AccountsAssetsVolumes) Scan(value interface{}) error {
if value == nil {
return nil
}

v, err := driver.String.ConvertValue(value)
val, err := driver.String.ConvertValue(value)
if err != nil {
return err
}

*m = AggregatedVolumes{}
switch vv := v.(type) {
*a = AccountsAssetsVolumes{}
switch val := val.(type) {
case []uint8:
return json.Unmarshal(vv, m)
return json.Unmarshal(val, a)
case string:
return json.Unmarshal([]byte(vv), m)
return json.Unmarshal([]byte(val), a)
default:
panic("not handled type")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (l *Ledger) Close(ctx context.Context) error {
}

type CommitResult struct {
PreCommitVolumes core.AggregatedVolumes
PostCommitVolumes core.AggregatedVolumes
PreCommitVolumes core.AccountsAssetsVolumes
PostCommitVolumes core.AccountsAssetsVolumes
GeneratedTransactions []core.Transaction
GeneratedLogs []core.Log
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (l *Ledger) GetAccount(ctx context.Context, address string) (core.Account,
return core.Account{}, err
}

volumes, err := l.store.GetAccountVolumes(ctx, address)
volumes, err := l.store.GetAssetsVolumes(ctx, address)
if err != nil {
return account, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ func TestTransactionExpectedVolumes(t *testing.T) {
res, err := l.Commit(context.Background(), batch)
assert.NoError(t, err)

assert.EqualValues(t, core.AggregatedVolumes{
"world": core.Volumes{
assert.EqualValues(t, core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{
"USD": {
Output: 100,
},
"EUR": {
Output: 200,
},
},
"player": core.Volumes{
"player": core.AssetsVolumes{
"USD": {
Input: 100,
},
Expand All @@ -300,7 +300,7 @@ func TestTransactionExpectedVolumes(t *testing.T) {
Output: 50,
},
},
"player2": core.Volumes{
"player2": core.AssetsVolumes{
"EUR": {
Input: 150,
},
Expand Down
92 changes: 46 additions & 46 deletions pkg/ledger/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ func TestLedger_processTx(t *testing.T) {
},
}

expectedPreCommitVol := core.AggregatedVolumes{
"alice": core.Volumes{
expectedPreCommitVol := core.AccountsAssetsVolumes{
"alice": core.AssetsVolumes{
"USD": {},
"EUR": {},
},
"toto": core.Volumes{
"toto": core.AssetsVolumes{
"USD": {},
"EUR": {},
},
"world": core.Volumes{
"world": core.AssetsVolumes{
"USD": {},
"EUR": {},
},
}

expectedPostCommitVol := core.AggregatedVolumes{
"alice": core.Volumes{
expectedPostCommitVol := core.AccountsAssetsVolumes{
"alice": core.AssetsVolumes{
"USD": {
Input: worldAliceUSD,
Output: aliceTotoUSD,
Expand All @@ -85,7 +85,7 @@ func TestLedger_processTx(t *testing.T) {
Input: worldAliceEUR + totoAliceEUR,
},
},
"toto": core.Volumes{
"toto": core.AssetsVolumes{
"USD": {
Input: worldTotoUSD + aliceTotoUSD,
},
Expand All @@ -94,7 +94,7 @@ func TestLedger_processTx(t *testing.T) {
Output: totoAliceEUR,
},
},
"world": core.Volumes{
"world": core.AssetsVolumes{
"USD": {
Output: worldTotoUSD + worldAliceUSD,
},
Expand Down Expand Up @@ -158,76 +158,76 @@ func TestLedger_processTx(t *testing.T) {
TransactionData: core.TransactionData{Postings: core.Postings{postings[0]}},
ID: 0,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"toto": core.Volumes{"USD": core.Volume{Input: 0, Output: 0}},
"world": core.Volumes{"USD": core.Volume{Input: 0, Output: 0}}},
PostCommitVolumes: core.AggregatedVolumes{
"toto": core.Volumes{"USD": core.Volume{Input: worldTotoUSD, Output: 0}},
"world": core.Volumes{"USD": core.Volume{Input: 0, Output: worldTotoUSD}}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"toto": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: 0}},
"world": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: 0}}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"toto": core.AssetsVolumes{"USD": core.Volumes{Input: worldTotoUSD, Output: 0}},
"world": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: worldTotoUSD}}},
},
{
TransactionData: core.TransactionData{Postings: core.Postings{postings[1]}},
ID: 1,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"USD": core.Volume{Input: 0, Output: worldTotoUSD}},
"alice": core.Volumes{"USD": core.Volume{Input: 0, Output: 0}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: worldTotoUSD}},
"alice": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: 0}},
},
PostCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"USD": core.Volume{Input: 0, Output: worldTotoUSD + worldAliceUSD}},
"alice": core.Volumes{"USD": core.Volume{Input: worldAliceUSD, Output: 0}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"USD": core.Volumes{Input: 0, Output: worldTotoUSD + worldAliceUSD}},
"alice": core.AssetsVolumes{"USD": core.Volumes{Input: worldAliceUSD, Output: 0}},
},
},
{
TransactionData: core.TransactionData{Postings: core.Postings{postings[2]}},
ID: 2,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"alice": core.Volumes{"USD": core.Volume{Input: worldAliceUSD, Output: 0}},
"toto": core.Volumes{"USD": core.Volume{Input: worldTotoUSD, Output: 0}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"alice": core.AssetsVolumes{"USD": core.Volumes{Input: worldAliceUSD, Output: 0}},
"toto": core.AssetsVolumes{"USD": core.Volumes{Input: worldTotoUSD, Output: 0}},
},
PostCommitVolumes: core.AggregatedVolumes{
"alice": core.Volumes{"USD": core.Volume{Input: worldAliceUSD, Output: aliceTotoUSD}},
"toto": core.Volumes{"USD": core.Volume{Input: worldTotoUSD + aliceTotoUSD, Output: 0}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"alice": core.AssetsVolumes{"USD": core.Volumes{Input: worldAliceUSD, Output: aliceTotoUSD}},
"toto": core.AssetsVolumes{"USD": core.Volumes{Input: worldTotoUSD + aliceTotoUSD, Output: 0}},
},
},
{
TransactionData: core.TransactionData{Postings: core.Postings{postings[3]}},
ID: 3,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"EUR": core.Volume{Input: 0, Output: 0}},
"toto": core.Volumes{"EUR": core.Volume{Input: 0, Output: 0}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: 0}},
"toto": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: 0}},
},
PostCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"EUR": core.Volume{Input: 0, Output: worldTotoEUR}},
"toto": core.Volumes{"EUR": core.Volume{Input: worldTotoEUR, Output: 0}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: worldTotoEUR}},
"toto": core.AssetsVolumes{"EUR": core.Volumes{Input: worldTotoEUR, Output: 0}},
},
},
{
TransactionData: core.TransactionData{Postings: core.Postings{postings[4]}},
ID: 4,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"EUR": core.Volume{Input: 0, Output: worldTotoEUR}},
"alice": core.Volumes{"EUR": core.Volume{Input: 0, Output: 0}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: worldTotoEUR}},
"alice": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: 0}},
},
PostCommitVolumes: core.AggregatedVolumes{
"world": core.Volumes{"EUR": core.Volume{Input: 0, Output: worldTotoEUR + worldAliceEUR}},
"alice": core.Volumes{"EUR": core.Volume{Input: worldAliceEUR, Output: 0}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"world": core.AssetsVolumes{"EUR": core.Volumes{Input: 0, Output: worldTotoEUR + worldAliceEUR}},
"alice": core.AssetsVolumes{"EUR": core.Volumes{Input: worldAliceEUR, Output: 0}},
},
},
{
TransactionData: core.TransactionData{Postings: core.Postings{postings[5]}},
ID: 5,
Timestamp: time.Now().UTC().Format(time.RFC3339),
PreCommitVolumes: core.AggregatedVolumes{
"toto": core.Volumes{"EUR": core.Volume{Input: worldTotoEUR, Output: 0}},
"alice": core.Volumes{"EUR": core.Volume{Input: worldAliceEUR, Output: 0}},
PreCommitVolumes: core.AccountsAssetsVolumes{
"toto": core.AssetsVolumes{"EUR": core.Volumes{Input: worldTotoEUR, Output: 0}},
"alice": core.AssetsVolumes{"EUR": core.Volumes{Input: worldAliceEUR, Output: 0}},
},
PostCommitVolumes: core.AggregatedVolumes{
"toto": core.Volumes{"EUR": core.Volume{Input: worldTotoEUR, Output: totoAliceEUR}},
"alice": core.Volumes{"EUR": core.Volume{Input: worldAliceEUR + totoAliceEUR, Output: 0}},
PostCommitVolumes: core.AccountsAssetsVolumes{
"toto": core.AssetsVolumes{"EUR": core.Volumes{Input: worldTotoEUR, Output: totoAliceEUR}},
"alice": core.AssetsVolumes{"EUR": core.Volumes{Input: worldAliceEUR + totoAliceEUR, Output: 0}},
},
},
}
Expand Down Expand Up @@ -294,8 +294,8 @@ func TestLedger_processTx(t *testing.T) {
result, err := l.processTx(context.Background(), []core.TransactionData{})
assert.NoError(t, err)
assert.Equal(t, &CommitResult{
PreCommitVolumes: core.AggregatedVolumes{},
PostCommitVolumes: core.AggregatedVolumes{},
PreCommitVolumes: core.AccountsAssetsVolumes{},
PostCommitVolumes: core.AccountsAssetsVolumes{},
GeneratedTransactions: []core.Transaction{},
GeneratedLogs: []core.Log{},
}, result)
Expand Down
Loading

0 comments on commit 1acf540

Please sign in to comment.