diff --git a/packages/mana/balance.go b/packages/mana/balance.go index b3fc2c5abadcfa94e32ea328540b6d9a7c0dadd0..9d365216e3618a3836d5b22dd10700b117198e14 100644 --- a/packages/mana/balance.go +++ b/packages/mana/balance.go @@ -3,20 +3,24 @@ package mana import ( "sync" + "github.com/golang/protobuf/proto" + "github.com/iotaledger/goshimmer/packages/datastructure" "github.com/iotaledger/goshimmer/packages/errors" + manaproto "github.com/iotaledger/goshimmer/packages/mana/proto" + "github.com/iotaledger/goshimmer/packages/marshaling" ) type Balance struct { - calculator *Calculator - transferHistory *datastructure.DoublyLinkedList - mutex sync.RWMutex + calculator *Calculator + balanceHistory *datastructure.DoublyLinkedList + mutex sync.RWMutex } func NewBalance(calculator *Calculator) *Balance { return &Balance{ - calculator: calculator, - transferHistory: &datastructure.DoublyLinkedList{}, + calculator: calculator, + balanceHistory: &datastructure.DoublyLinkedList{}, } } @@ -25,7 +29,7 @@ func (balance *Balance) GetValue(now ...uint64) (result uint64, err errors.Ident balance.mutex.RLock() defer balance.mutex.RUnlock() - if lastBalanceHistoryEntry, historyErr := balance.transferHistory.GetLast(); historyErr != nil { + if lastBalanceHistoryEntry, historyErr := balance.balanceHistory.GetLast(); historyErr != nil { if !datastructure.ErrNoSuchElement.Equals(historyErr) { err = historyErr } @@ -55,7 +59,7 @@ func (balance *Balance) GetLastErosion() uint64 { balance.mutex.RLock() defer balance.mutex.RUnlock() - if lastBalanceHistoryEntry, err := balance.transferHistory.GetLast(); datastructure.ErrNoSuchElement.Equals(err) { + if lastBalanceHistoryEntry, err := balance.balanceHistory.GetLast(); datastructure.ErrNoSuchElement.Equals(err) { return 0 } else { return lastBalanceHistoryEntry.(*BalanceHistoryEntry).transfer.spentTime @@ -84,7 +88,7 @@ func (balance *Balance) CleanupTransferHistory(referenceTime uint64) (err errors balance.mutex.Lock() defer balance.mutex.Unlock() - if currentTransferHistoryEntry, historyErr := balance.transferHistory.GetFirstEntry(); historyErr != nil { + if currentTransferHistoryEntry, historyErr := balance.balanceHistory.GetFirstEntry(); historyErr != nil { if !datastructure.ErrNoSuchElement.Equals(historyErr) { err = historyErr } @@ -92,7 +96,7 @@ func (balance *Balance) CleanupTransferHistory(referenceTime uint64) (err errors for currentTransferHistoryEntry.GetNext() != nil && currentTransferHistoryEntry.GetValue().(*BalanceHistoryEntry).transfer.spentTime < referenceTime { nextTransferHistoryEntry := currentTransferHistoryEntry.GetNext() - if historyErr := balance.transferHistory.RemoveEntry(currentTransferHistoryEntry); historyErr != nil { + if historyErr := balance.balanceHistory.RemoveEntry(currentTransferHistoryEntry); historyErr != nil { err = historyErr break @@ -105,6 +109,105 @@ func (balance *Balance) CleanupTransferHistory(referenceTime uint64) (err errors return } +func (balance *Balance) ToProto() proto.Message { + balance.mutex.RLock() + defer balance.mutex.RUnlock() + + balance.mutex.RLock() + defer balance.mutex.RUnlock() + + balanceHistorySize := balance.balanceHistory.GetSize() + protoBalance := &manaproto.Balance{ + BalanceHistory: make([]*manaproto.BalanceHistoryEntry, balanceHistorySize), + } + + if balanceHistorySize >= 1 { + if currentHistoryEntry, err := balance.balanceHistory.GetFirstEntry(); err != nil { + panic(err) + } else { + for i := 0; i < balanceHistorySize; i++ { + protoBalance.BalanceHistory[i] = currentHistoryEntry.GetValue().(*BalanceHistoryEntry).ToProto().(*manaproto.BalanceHistoryEntry) + + currentHistoryEntry = currentHistoryEntry.GetNext() + } + } + } + + return protoBalance +} + +func (balance *Balance) FromProto(proto proto.Message) { + balance.mutex.Lock() + defer balance.mutex.Unlock() + + balance.balanceHistory = &datastructure.DoublyLinkedList{} + for _, balanceHistoryEntryProto := range proto.(*manaproto.Balance).BalanceHistory { + var balanceHistoryEntry BalanceHistoryEntry + balanceHistoryEntry.FromProto(balanceHistoryEntryProto) + + balance.balanceHistory.AddLast(&balanceHistoryEntry) + } +} + +func (balance *Balance) MarshalBinary() ([]byte, errors.IdentifiableError) { + return marshaling.Marshal(balance) +} + +func (balance *Balance) UnmarshalBinary(data []byte) (err errors.IdentifiableError) { + return marshaling.Unmarshal(balance, data, &manaproto.Balance{}) +} + +func (balance *Balance) Equals(other *Balance) bool { + if balance == other { + return true + } + + if balance == nil || other == nil { + return false + } + + balance.mutex.RLock() + other.mutex.RLock() + defer balance.mutex.RUnlock() + defer other.mutex.RUnlock() + + if balance.balanceHistory == other.balanceHistory { + return true + } + + if balance.balanceHistory == nil || other.balanceHistory == nil { + return false + } + + if balance.balanceHistory.GetSize() != other.balanceHistory.GetSize() { + return false + } + + if balance.balanceHistory.GetSize() != 0 { + ownTransferHistoryEntry, err := balance.balanceHistory.GetFirstEntry() + if err != nil { + // should never happen as we check the size before + panic(err) + } + otherTransferHistoryEntry, err := other.balanceHistory.GetFirstEntry() + if err != nil { + // should never happen as we check the size before + panic(err) + } + + for ownTransferHistoryEntry != nil { + if !ownTransferHistoryEntry.GetValue().(*BalanceHistoryEntry).Equals(otherTransferHistoryEntry.GetValue().(*BalanceHistoryEntry)) { + return false + } + + ownTransferHistoryEntry = ownTransferHistoryEntry.GetNext() + otherTransferHistoryEntry = otherTransferHistoryEntry.GetNext() + } + } + + return true +} + // Rolls back transfers that have their spentTime after the given referenceTime and returns a slice containing the // rolled back transfers. // @@ -119,7 +222,7 @@ func (balance *Balance) rollbackTransfers(referenceTime uint64) (result []*Trans result = make([]*Transfer, 0) for { - if lastListEntry, err := balance.transferHistory.GetLast(); err != nil { + if lastListEntry, err := balance.balanceHistory.GetLast(); err != nil { if !datastructure.ErrNoSuchElement.Equals(err) { panic(err) } @@ -130,7 +233,7 @@ func (balance *Balance) rollbackTransfers(referenceTime uint64) (result []*Trans } else { result = append(result, lastTransfer) - if _, err := balance.transferHistory.RemoveLast(); err != nil { + if _, err := balance.balanceHistory.RemoveLast(); err != nil { panic(err) } } @@ -141,7 +244,7 @@ func (balance *Balance) rollbackTransfers(referenceTime uint64) (result []*Trans func (balance *Balance) applyTransfer(transfer *Transfer) { // retrieve current values var currentBalance, lastErosion uint64 - if lastListEntry, err := balance.transferHistory.GetLastEntry(); err != nil { + if lastListEntry, err := balance.balanceHistory.GetLastEntry(); err != nil { if !datastructure.ErrNoSuchElement.Equals(err) { panic(err) } @@ -169,9 +272,8 @@ func (balance *Balance) applyTransfer(transfer *Transfer) { } // store results - balance.transferHistory.AddLast(&BalanceHistoryEntry{ - transfer: transfer, - balance: currentBalance + gainedMana - transfer.burnedMana, - accumulatedRoundingError: 0, // TODO: remove + balance.balanceHistory.AddLast(&BalanceHistoryEntry{ + transfer: transfer, + balance: currentBalance + gainedMana - transfer.burnedMana, }) } diff --git a/packages/mana/balance_history_entry.go b/packages/mana/balance_history_entry.go index 7d4d6d439f74607f4cce10081f8a58d0a9f5538e..05544f248abac2b62c697e037394f5c497276b7e 100644 --- a/packages/mana/balance_history_entry.go +++ b/packages/mana/balance_history_entry.go @@ -1,7 +1,100 @@ package mana +import ( + "sync" + + "github.com/golang/protobuf/proto" + "github.com/iotaledger/goshimmer/packages/errors" + manaproto "github.com/iotaledger/goshimmer/packages/mana/proto" + "github.com/iotaledger/goshimmer/packages/marshaling" +) + type BalanceHistoryEntry struct { - transfer *Transfer - balance uint64 - accumulatedRoundingError float64 + transfer *Transfer + transferMutex sync.RWMutex + balance uint64 + balanceMutex sync.RWMutex +} + +func (balanceHistoryEntry *BalanceHistoryEntry) GetTransfer() *Transfer { + balanceHistoryEntry.transferMutex.RLock() + defer balanceHistoryEntry.transferMutex.RUnlock() + + return balanceHistoryEntry.transfer +} + +func (balanceHistoryEntry *BalanceHistoryEntry) SetTransfer(transfer *Transfer) { + balanceHistoryEntry.transferMutex.Lock() + defer balanceHistoryEntry.transferMutex.Unlock() + + balanceHistoryEntry.transfer = transfer +} + +func (balanceHistoryEntry *BalanceHistoryEntry) GetBalance() uint64 { + balanceHistoryEntry.balanceMutex.RLock() + defer balanceHistoryEntry.balanceMutex.RUnlock() + + return balanceHistoryEntry.balance +} + +func (balanceHistoryEntry *BalanceHistoryEntry) SetBalance(balance uint64) { + balanceHistoryEntry.balanceMutex.Lock() + defer balanceHistoryEntry.balanceMutex.Unlock() + + balanceHistoryEntry.balance = balance +} + +func (balanceHistoryEntry *BalanceHistoryEntry) ToProto() proto.Message { + balanceHistoryEntry.transferMutex.RLock() + balanceHistoryEntry.balanceMutex.RLock() + defer balanceHistoryEntry.transferMutex.RUnlock() + defer balanceHistoryEntry.balanceMutex.RUnlock() + + return &manaproto.BalanceHistoryEntry{ + Transfer: balanceHistoryEntry.transfer.ToProto().(*manaproto.Transfer), + Balance: balanceHistoryEntry.balance, + } +} + +func (balanceHistoryEntry *BalanceHistoryEntry) FromProto(proto proto.Message) { + balanceHistoryEntry.transferMutex.Lock() + balanceHistoryEntry.balanceMutex.Lock() + defer balanceHistoryEntry.transferMutex.Unlock() + defer balanceHistoryEntry.balanceMutex.Unlock() + + protobufBalanceHistoryEntry := proto.(*manaproto.BalanceHistoryEntry) + + balanceHistoryEntry.balance = protobufBalanceHistoryEntry.Balance + balanceHistoryEntry.transfer = &Transfer{} + + balanceHistoryEntry.transfer.FromProto(protobufBalanceHistoryEntry.Transfer) +} + +func (balanceHistoryEntry *BalanceHistoryEntry) MarshalBinary() ([]byte, errors.IdentifiableError) { + return marshaling.Marshal(balanceHistoryEntry) +} + +func (balanceHistoryEntry *BalanceHistoryEntry) UnmarshalBinary(data []byte) (err errors.IdentifiableError) { + return marshaling.Unmarshal(balanceHistoryEntry, data, &manaproto.BalanceHistoryEntry{}) +} + +func (balanceHistoryEntry *BalanceHistoryEntry) Equals(other *BalanceHistoryEntry) bool { + if balanceHistoryEntry == other { + return true + } + + if balanceHistoryEntry == nil || other == nil { + return false + } + + balanceHistoryEntry.balanceMutex.RLock() + balanceHistoryEntry.transferMutex.RLock() + other.balanceMutex.RLock() + other.transferMutex.RLock() + defer balanceHistoryEntry.balanceMutex.RUnlock() + defer balanceHistoryEntry.transferMutex.RUnlock() + defer other.balanceMutex.RUnlock() + defer other.transferMutex.RUnlock() + + return balanceHistoryEntry.balance == other.balance && balanceHistoryEntry.transfer.Equals(other.transfer) } diff --git a/packages/mana/balance_history_entry_test.go b/packages/mana/balance_history_entry_test.go new file mode 100644 index 0000000000000000000000000000000000000000..40c4d4e6d6ad8079213a770cc277e510df5882a7 --- /dev/null +++ b/packages/mana/balance_history_entry_test.go @@ -0,0 +1,26 @@ +package mana + +import ( + "testing" + + "github.com/magiconair/properties/assert" +) + +func TestBalanceHistoryEntry_MarshalUnmarshalBinary(t *testing.T) { + balanceHistoryEntry := &BalanceHistoryEntry{ + transfer: NewTransfer([]*Input{NewInput(10, 100)}, 150, 10), + balance: 100, + } + + marshaledBalanceHistoryEntry, err := balanceHistoryEntry.MarshalBinary() + if err != nil { + t.Error(err) + + return + } + + var unmarshaledBalanceHistoryEntry BalanceHistoryEntry + unmarshaledBalanceHistoryEntry.UnmarshalBinary(marshaledBalanceHistoryEntry) + + assert.Equal(t, unmarshaledBalanceHistoryEntry.Equals(balanceHistoryEntry), true) +} diff --git a/packages/mana/balance_test.go b/packages/mana/balance_test.go index f7c384b04f1f018a7f8f284757f9f71cd00f9213..19319336719af0f36ef0120ec450f8406e47d7a1 100644 --- a/packages/mana/balance_test.go +++ b/packages/mana/balance_test.go @@ -6,6 +6,94 @@ import ( "github.com/magiconair/properties/assert" ) +func TestBalance_MarshalUnmarshalBinary(t *testing.T) { + // initialize calculator + calculator := NewCalculator(500, 0.1) + + // fill transfer history + balance1 := NewBalance(calculator) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 1000)}, + spentTime: 1700, + burnedMana: 10, + }) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 700)}, + spentTime: 1000, + burnedMana: 0, + }) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 0)}, + spentTime: 700, + burnedMana: 0, + }) + + marshaledBalance, err := balance1.MarshalBinary() + if err != nil { + t.Error(err) + + return + } + + var balance2 Balance + if err := balance2.UnmarshalBinary(marshaledBalance); err != nil { + t.Error(err) + + return + } + + assert.Equal(t, balance1.Equals(&balance2), true) +} + +func TestBalance_Equals(t *testing.T) { + // initialize calculator + calculator := NewCalculator(500, 0.1) + + // fill transfer history + balance1 := NewBalance(calculator) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 1000)}, + spentTime: 1700, + burnedMana: 10, + }) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 700)}, + spentTime: 1000, + burnedMana: 0, + }) + balance1.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 0)}, + spentTime: 700, + burnedMana: 0, + }) + + // fill transfer history + balance2 := NewBalance(calculator) + balance2.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 1000)}, + spentTime: 1700, + burnedMana: 10, + }) + balance2.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 700)}, + spentTime: 1000, + burnedMana: 0, + }) + balance2.BookTransfer(&Transfer{ + inputs: []*Input{NewInput(1000, 0)}, + spentTime: 700, + burnedMana: 0, + }) + + assert.Equal(t, balance1.Equals(balance2), true) + + if err := balance2.CleanupTransferHistory(800); err != nil { + t.Error(err) + } + + assert.Equal(t, balance1.Equals(balance2), false) +} + func TestBalance_CleanupTransferHistory(t *testing.T) { // initialize calculator calculator := NewCalculator(500, 0.1) @@ -13,17 +101,17 @@ func TestBalance_CleanupTransferHistory(t *testing.T) { // fill transfer history balance1 := NewBalance(calculator) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 1000)}, + inputs: []*Input{NewInput(1000, 1000)}, spentTime: 1700, burnedMana: 10, }) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 700)}, + inputs: []*Input{NewInput(1000, 700)}, spentTime: 1000, burnedMana: 0, }) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 0)}, + inputs: []*Input{NewInput(1000, 0)}, spentTime: 700, burnedMana: 0, }) @@ -43,7 +131,7 @@ func TestBalance_CleanupTransferHistory(t *testing.T) { } else { assert.Equal(t, val1, uint64(290)) } - assert.Equal(t, balance1.transferHistory.GetSize(), 1) + assert.Equal(t, balance1.balanceHistory.GetSize(), 1) } func TestBalance_AddTransfer(t *testing.T) { @@ -53,17 +141,17 @@ func TestBalance_AddTransfer(t *testing.T) { // spend coins multiple times balance1 := NewBalance(calculator) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 1000)}, + inputs: []*Input{NewInput(1000, 1000)}, spentTime: 1700, burnedMana: 10, }) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 700)}, + inputs: []*Input{NewInput(1000, 700)}, spentTime: 1000, burnedMana: 0, }) balance1.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 0)}, + inputs: []*Input{NewInput(1000, 0)}, spentTime: 700, burnedMana: 0, }) @@ -71,7 +159,7 @@ func TestBalance_AddTransfer(t *testing.T) { // hold coins for the full time balance2 := NewBalance(calculator) balance2.BookTransfer(&Transfer{ - inputs: []*Input{NewInput(1000, 0)}, + inputs: []*Input{NewInput(1000, 0)}, spentTime: 1700, burnedMana: 10, }) diff --git a/packages/mana/calculator_test.go b/packages/mana/calculator_test.go index 6acc03438827baf21dc71db6a185160ea764b7b3..12caa541256850e2842e73076d35fa14338be4be 100644 --- a/packages/mana/calculator_test.go +++ b/packages/mana/calculator_test.go @@ -21,7 +21,7 @@ func TestCalculator_GenerateMana(t *testing.T) { assert.Equal(t, generatedMana, uint64(0)) generatedMana, _ = calculator.GenerateMana(1000, 500) - assert.Equal(t, generatedMana, uint64(200)) + assert.Equal(t, generatedMana, uint64(199)) generatedMana, _ = calculator.GenerateMana(1000, 5000000) assert.Equal(t, generatedMana, uint64(2000)) @@ -42,6 +42,7 @@ func TestCalculator_ManaSymmetry(t *testing.T) { // 2nd case: generate mana by spending only once generatedManaWithoutSpends, _ := calculator.GenerateMana(1000, 1500) - // the two mana values should be equal - assert.Equal(t, generatedManaWithoutSpends, erodedMana1+erodedMana2+generatedManaStep3) + // the two mana values should be roughly the same with multi spends being slightly less optimal + assert.Equal(t, uint64(541), generatedManaWithoutSpends) + assert.Equal(t, uint64(539), erodedMana1+erodedMana2+generatedManaStep3) } diff --git a/packages/mana/errors.go b/packages/mana/errors.go deleted file mode 100644 index b049eeaef8dc9c3f2de9e32f3ef50aa9f8a4241d..0000000000000000000000000000000000000000 --- a/packages/mana/errors.go +++ /dev/null @@ -1,10 +0,0 @@ -package mana - -import ( - "github.com/iotaledger/goshimmer/packages/errors" -) - -var ( - ErrUnmarshalFailed = errors.Wrap(errors.New("unmarshal failed"), "unmarshal failed") - ErrMarshalFailed = errors.Wrap(errors.New("marshal failed"), "marshal failed") -) diff --git a/packages/mana/input.go b/packages/mana/input.go index 320702c4eff35314be7f8da50f4cda9fa23e370e..842d7a6a5420b175ca1ad95c074cb4fdb65df5ba 100644 --- a/packages/mana/input.go +++ b/packages/mana/input.go @@ -5,6 +5,8 @@ import ( "github.com/golang/protobuf/proto" + "github.com/iotaledger/goshimmer/packages/marshaling" + "github.com/iotaledger/goshimmer/packages/errors" manaproto "github.com/iotaledger/goshimmer/packages/mana/proto" ) @@ -51,7 +53,7 @@ func (input *Input) SetReceivedTime(receivedTime uint64) { input.receivedTime = receivedTime } -func (input *Input) ToProto() (result *manaproto.Input) { +func (input *Input) ToProto() (result proto.Message) { input.receivedTimeMutex.RLock() input.coinAmountMutex.RLock() defer input.receivedTimeMutex.RUnlock() @@ -63,33 +65,43 @@ func (input *Input) ToProto() (result *manaproto.Input) { } } -func (input *Input) FromProto(proto *manaproto.Input) { +func (input *Input) FromProto(proto proto.Message) { input.receivedTimeMutex.Lock() input.coinAmountMutex.Lock() defer input.receivedTimeMutex.Unlock() defer input.coinAmountMutex.Unlock() - input.coinAmount = proto.CoinAmount - input.receivedTime = proto.ReceivedTime + inputProto := proto.(*manaproto.Input) + + input.coinAmount = inputProto.CoinAmount + input.receivedTime = inputProto.ReceivedTime } -func (input *Input) MarshalBinary() (result []byte, err errors.IdentifiableError) { - if marshaledData, marshalErr := proto.Marshal(input.ToProto()); marshalErr != nil { - err = ErrMarshalFailed.Derive(marshalErr, "marshal failed") - } else { - result = marshaledData - } +func (input *Input) MarshalBinary() ([]byte, errors.IdentifiableError) { + return marshaling.Marshal(input) +} - return +func (input *Input) UnmarshalBinary(data []byte) errors.IdentifiableError { + return marshaling.Unmarshal(input, data, &manaproto.Input{}) } -func (input *Input) UnmarshalBinary(data []byte) (err errors.IdentifiableError) { - var unmarshaledProto manaproto.Input - if unmarshalError := proto.Unmarshal(data, &unmarshaledProto); unmarshalError != nil { - err = ErrUnmarshalFailed.Derive(unmarshalError, "unmarshal failed") - } else { - input.FromProto(&unmarshaledProto) +func (input *Input) Equals(other *Input) bool { + if input == other { + return true + } + + if input == nil || other == nil { + return false } - return + input.receivedTimeMutex.RLock() + input.coinAmountMutex.RLock() + other.receivedTimeMutex.RLock() + other.coinAmountMutex.RLock() + defer input.receivedTimeMutex.RUnlock() + defer input.coinAmountMutex.RUnlock() + defer other.receivedTimeMutex.RUnlock() + defer other.coinAmountMutex.RUnlock() + + return input.coinAmount == other.coinAmount && input.receivedTime == other.receivedTime } diff --git a/packages/mana/input_test.go b/packages/mana/input_test.go index b78ea6f03b00430deb65f859d67befdf68abc98f..a6cc111c1039b93cd584560243156a91ed00d211 100644 --- a/packages/mana/input_test.go +++ b/packages/mana/input_test.go @@ -1,8 +1,9 @@ package mana import ( - "github.com/magiconair/properties/assert" "testing" + + "github.com/magiconair/properties/assert" ) func TestInput_MarshalUnmarshalBinary(t *testing.T) { @@ -26,6 +27,21 @@ func TestInput_MarshalUnmarshalBinary(t *testing.T) { } // compare result - assert.Equal(t, unmarshaledInput.GetCoinAmount(), originalInput.GetCoinAmount()) - assert.Equal(t, unmarshaledInput.GetReceivedTime(), originalInput.GetReceivedTime()) + assert.Equal(t, unmarshaledInput.Equals(originalInput), true) +} + +func TestInput_Equals(t *testing.T) { + // create test transfers + var input0 *Input + input1 := NewInput(10, 12) + input2 := NewInput(10, 14) + input3 := NewInput(11, 12) + input4 := NewInput(10, 12) + + // check results of Equals + assert.Equal(t, input0.Equals(nil), true) + assert.Equal(t, input1.Equals(nil), false) + assert.Equal(t, input1.Equals(input2), false) + assert.Equal(t, input1.Equals(input3), false) + assert.Equal(t, input1.Equals(input4), true) } diff --git a/packages/mana/proto/balance.pb.go b/packages/mana/proto/balance.pb.go new file mode 100644 index 0000000000000000000000000000000000000000..18b0ffb9fb59cbb34e6293b5eef01fc708a3df76 --- /dev/null +++ b/packages/mana/proto/balance.pb.go @@ -0,0 +1,77 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: balance.proto + +package proto + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +type Balance struct { + BalanceHistory []*BalanceHistoryEntry `protobuf:"bytes,1,rep,name=balanceHistory,proto3" json:"balanceHistory,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Balance) Reset() { *m = Balance{} } +func (m *Balance) String() string { return proto.CompactTextString(m) } +func (*Balance) ProtoMessage() {} +func (*Balance) Descriptor() ([]byte, []int) { + return fileDescriptor_ee25a00b628521b1, []int{0} +} + +func (m *Balance) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Balance.Unmarshal(m, b) +} +func (m *Balance) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Balance.Marshal(b, m, deterministic) +} +func (m *Balance) XXX_Merge(src proto.Message) { + xxx_messageInfo_Balance.Merge(m, src) +} +func (m *Balance) XXX_Size() int { + return xxx_messageInfo_Balance.Size(m) +} +func (m *Balance) XXX_DiscardUnknown() { + xxx_messageInfo_Balance.DiscardUnknown(m) +} + +var xxx_messageInfo_Balance proto.InternalMessageInfo + +func (m *Balance) GetBalanceHistory() []*BalanceHistoryEntry { + if m != nil { + return m.BalanceHistory + } + return nil +} + +func init() { + proto.RegisterType((*Balance)(nil), "proto.Balance") +} + +func init() { proto.RegisterFile("balance.proto", fileDescriptor_ee25a00b628521b1) } + +var fileDescriptor_ee25a00b628521b1 = []byte{ + // 104 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0x4a, 0xcc, 0x49, + 0xcc, 0x4b, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x53, 0x52, 0xd2, 0x50, + 0xd1, 0xf8, 0x8c, 0xcc, 0xe2, 0x92, 0xfc, 0xa2, 0xca, 0xf8, 0xd4, 0xbc, 0x92, 0xa2, 0x4a, 0x88, + 0x1a, 0x25, 0x5f, 0x2e, 0x76, 0x27, 0x88, 0xb4, 0x90, 0x13, 0x17, 0x1f, 0x54, 0xa5, 0x07, 0x44, + 0xa1, 0x04, 0xa3, 0x02, 0xb3, 0x06, 0xb7, 0x91, 0x14, 0x44, 0xa9, 0x9e, 0x13, 0x8a, 0xa4, 0x2b, + 0xc8, 0x90, 0x20, 0x34, 0x1d, 0x49, 0x6c, 0x60, 0xa5, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, + 0x81, 0x7d, 0x7e, 0x22, 0x8a, 0x00, 0x00, 0x00, +} diff --git a/packages/mana/proto/balance.proto b/packages/mana/proto/balance.proto new file mode 100644 index 0000000000000000000000000000000000000000..8a3ce41ee3ef415050652e36fa9edfe2e76e32d5 --- /dev/null +++ b/packages/mana/proto/balance.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +import "balance_history_entry.proto"; + +package proto; + +message Balance { + repeated BalanceHistoryEntry balanceHistory = 1; +}; \ No newline at end of file diff --git a/packages/mana/proto/balance_history_entry.pb.go b/packages/mana/proto/balance_history_entry.pb.go new file mode 100644 index 0000000000000000000000000000000000000000..0a0e05bd4c13669675fb4308a4ef91c2f9aa8f23 --- /dev/null +++ b/packages/mana/proto/balance_history_entry.pb.go @@ -0,0 +1,86 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: balance_history_entry.proto + +package proto + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +type BalanceHistoryEntry struct { + Transfer *Transfer `protobuf:"bytes,1,opt,name=transfer,proto3" json:"transfer,omitempty"` + Balance uint64 `protobuf:"varint,2,opt,name=balance,proto3" json:"balance,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *BalanceHistoryEntry) Reset() { *m = BalanceHistoryEntry{} } +func (m *BalanceHistoryEntry) String() string { return proto.CompactTextString(m) } +func (*BalanceHistoryEntry) ProtoMessage() {} +func (*BalanceHistoryEntry) Descriptor() ([]byte, []int) { + return fileDescriptor_e6eff69733d21190, []int{0} +} + +func (m *BalanceHistoryEntry) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BalanceHistoryEntry.Unmarshal(m, b) +} +func (m *BalanceHistoryEntry) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_BalanceHistoryEntry.Marshal(b, m, deterministic) +} +func (m *BalanceHistoryEntry) XXX_Merge(src proto.Message) { + xxx_messageInfo_BalanceHistoryEntry.Merge(m, src) +} +func (m *BalanceHistoryEntry) XXX_Size() int { + return xxx_messageInfo_BalanceHistoryEntry.Size(m) +} +func (m *BalanceHistoryEntry) XXX_DiscardUnknown() { + xxx_messageInfo_BalanceHistoryEntry.DiscardUnknown(m) +} + +var xxx_messageInfo_BalanceHistoryEntry proto.InternalMessageInfo + +func (m *BalanceHistoryEntry) GetTransfer() *Transfer { + if m != nil { + return m.Transfer + } + return nil +} + +func (m *BalanceHistoryEntry) GetBalance() uint64 { + if m != nil { + return m.Balance + } + return 0 +} + +func init() { + proto.RegisterType((*BalanceHistoryEntry)(nil), "proto.BalanceHistoryEntry") +} + +func init() { proto.RegisterFile("balance_history_entry.proto", fileDescriptor_e6eff69733d21190) } + +var fileDescriptor_e6eff69733d21190 = []byte{ + // 124 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4e, 0x4a, 0xcc, 0x49, + 0xcc, 0x4b, 0x4e, 0x8d, 0xcf, 0xc8, 0x2c, 0x2e, 0xc9, 0x2f, 0xaa, 0x8c, 0x4f, 0xcd, 0x2b, 0x29, + 0xaa, 0xd4, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x53, 0x52, 0x7c, 0x25, 0x45, 0x89, + 0x79, 0xc5, 0x69, 0xa9, 0x45, 0x10, 0x61, 0xa5, 0x18, 0x2e, 0x61, 0x27, 0x88, 0x2e, 0x0f, 0x88, + 0x26, 0x57, 0x90, 0x1e, 0x21, 0x6d, 0x2e, 0x0e, 0x98, 0x42, 0x09, 0x46, 0x05, 0x46, 0x0d, 0x6e, + 0x23, 0x7e, 0x88, 0x06, 0xbd, 0x10, 0xa8, 0x70, 0x10, 0x5c, 0x81, 0x90, 0x04, 0x17, 0x3b, 0xd4, + 0x66, 0x09, 0x26, 0x05, 0x46, 0x0d, 0x96, 0x20, 0x18, 0x37, 0x89, 0x0d, 0xac, 0xc7, 0x18, 0x10, + 0x00, 0x00, 0xff, 0xff, 0xb7, 0x13, 0xaf, 0x10, 0x9a, 0x00, 0x00, 0x00, +} diff --git a/packages/mana/proto/balance_history_entry.proto b/packages/mana/proto/balance_history_entry.proto new file mode 100644 index 0000000000000000000000000000000000000000..52a760db91552528cb88ab79fdfdc3cf34fb945a --- /dev/null +++ b/packages/mana/proto/balance_history_entry.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +import "transfer.proto"; + +package proto; + +message BalanceHistoryEntry { + Transfer transfer = 1; + uint64 balance = 2; +}; \ No newline at end of file diff --git a/packages/mana/transfer.go b/packages/mana/transfer.go index 1597a076e325dcaf31b1bf280ed82a14d4ee709a..bb7459b4764ef5254000d0c0985d02f49ab24c22 100644 --- a/packages/mana/transfer.go +++ b/packages/mana/transfer.go @@ -27,6 +27,48 @@ func NewTransfer(inputs []*Input, spentTime uint64, burnedMana uint64) *Transfer } } +func (transfer *Transfer) GetInputs() []*Input { + transfer.inputsMutex.RLock() + defer transfer.inputsMutex.RUnlock() + + return transfer.inputs +} + +func (transfer *Transfer) SetInputs(inputs []*Input) { + transfer.inputsMutex.Lock() + defer transfer.inputsMutex.Unlock() + + transfer.inputs = inputs +} + +func (transfer *Transfer) GetBurnedMana() uint64 { + transfer.burnedManaMutex.RLock() + defer transfer.burnedManaMutex.RUnlock() + + return transfer.burnedMana +} + +func (transfer *Transfer) SetBurnedMana(burnedMana uint64) { + transfer.burnedManaMutex.Lock() + defer transfer.burnedManaMutex.Unlock() + + transfer.burnedMana = burnedMana +} + +func (transfer *Transfer) GetSpentTime() uint64 { + transfer.spentTimeMutex.RLock() + defer transfer.spentTimeMutex.RUnlock() + + return transfer.spentTime +} + +func (transfer *Transfer) SetSpentTime(spentTime uint64) { + transfer.spentTimeMutex.Lock() + defer transfer.spentTimeMutex.Unlock() + + transfer.spentTime = spentTime +} + // Returns a protobuf representation of this transfer. func (transfer *Transfer) ToProto() (result proto.Message) { transfer.inputsMutex.RLock() @@ -43,7 +85,7 @@ func (transfer *Transfer) ToProto() (result proto.Message) { } for i, input := range transfer.inputs { - protoTransfer.Inputs[i] = input.ToProto() + protoTransfer.Inputs[i] = input.ToProto().(*manaproto.Input) } return protoTransfer @@ -77,5 +119,44 @@ func (transfer *Transfer) MarshalBinary() ([]byte, errors.IdentifiableError) { } func (transfer *Transfer) UnmarshalBinary(data []byte) (err errors.IdentifiableError) { - return marshaling.Unmarshal(transfer, &manaproto.Transfer{}, data) + return marshaling.Unmarshal(transfer, data, &manaproto.Transfer{}) +} + +func (transfer *Transfer) Equals(other *Transfer) bool { + if transfer == other { + return true + } + + if transfer == nil || other == nil { + return false + } + + transfer.inputsMutex.RLock() + transfer.spentTimeMutex.RLock() + transfer.burnedManaMutex.RLock() + other.inputsMutex.RLock() + other.spentTimeMutex.RLock() + other.burnedManaMutex.RLock() + defer transfer.inputsMutex.RUnlock() + defer transfer.spentTimeMutex.RUnlock() + defer transfer.burnedManaMutex.RUnlock() + defer other.inputsMutex.RUnlock() + defer other.spentTimeMutex.RUnlock() + defer other.burnedManaMutex.RUnlock() + + if transfer.spentTime != other.spentTime || transfer.burnedMana != other.burnedMana { + return false + } + + if len(transfer.inputs) != len(other.inputs) { + return false + } + + for i, input := range transfer.inputs { + if !input.Equals(other.inputs[i]) { + return false + } + } + + return true } diff --git a/packages/mana/transfer_test.go b/packages/mana/transfer_test.go index f8b9616b5bb5feea79416cff6b299d81204c4ac3..088fc785c32204c3b783feb32a010d68af746b88 100644 --- a/packages/mana/transfer_test.go +++ b/packages/mana/transfer_test.go @@ -6,8 +6,37 @@ import ( "github.com/magiconair/properties/assert" ) +func TestTransfer_Equals(t *testing.T) { + // create test transfers + transfer1 := NewTransfer([]*Input{}, 1337, 1338) + transfer2 := NewTransfer([]*Input{}, 1337, 1339) + transfer3 := NewTransfer([]*Input{}, 1337, 1338) + transfer4 := NewTransfer([]*Input{}, 1339, 1338) + transfer5 := NewTransfer([]*Input{NewInput(10, 10)}, 1337, 1338) + transfer6 := NewTransfer([]*Input{NewInput(20, 10)}, 1337, 1338) + transfer7 := NewTransfer([]*Input{NewInput(10, 10)}, 1337, 1338) + + // burned mana differs + assert.Equal(t, transfer1.Equals(transfer2), false) + + // transfers are equal + assert.Equal(t, transfer1.Equals(transfer3), true) + + // spentTime differs + assert.Equal(t, transfer1.Equals(transfer4), false) + + // inputs length differs + assert.Equal(t, transfer1.Equals(transfer5), false) + + // inputs differ + assert.Equal(t, transfer5.Equals(transfer6), false) + + // transfers equal + assert.Equal(t, transfer5.Equals(transfer7), true) +} + func TestTransfer_MarshalUnmarshalBinary(t *testing.T) { - // create original input + // create original transfer originalTransfer := NewTransfer([]*Input{}, 1337, 1338) // marshal @@ -27,6 +56,5 @@ func TestTransfer_MarshalUnmarshalBinary(t *testing.T) { } // compare result - assert.Equal(t, unmarshaledTransfer.spentTime, originalTransfer.spentTime) - assert.Equal(t, unmarshaledTransfer.burnedMana, originalTransfer.burnedMana) + assert.Equal(t, unmarshaledTransfer.Equals(originalTransfer), true) } diff --git a/packages/marshaling/marshal.go b/packages/marshaling/marshal.go index 807a4f43bca376bcf5ad2da51b51c14ad1c8a049..04cdf7f2591b066df0e521c034d6bf8e673bf631 100644 --- a/packages/marshaling/marshal.go +++ b/packages/marshaling/marshal.go @@ -5,8 +5,8 @@ import ( "github.com/iotaledger/goshimmer/packages/errors" ) -func Marshal(serializable Serializable) (result []byte, err errors.IdentifiableError) { - if marshaledData, marshalErr := proto.Marshal(serializable.ToProto()); marshalErr != nil { +func Marshal(source ProtocolBufferTarget) (result []byte, err errors.IdentifiableError) { + if marshaledData, marshalErr := proto.Marshal(source.ToProto()); marshalErr != nil { err = ErrMarshalFailed.Derive(marshalErr, "marshal failed") } else { result = marshaledData diff --git a/packages/marshaling/types.go b/packages/marshaling/types.go index 2a56555c2fbf7a68c8580443365dba70e8930721..0d33468134914cf5b866a1a5fb80b6a8b8f7ee43 100644 --- a/packages/marshaling/types.go +++ b/packages/marshaling/types.go @@ -2,7 +2,7 @@ package marshaling import "github.com/golang/protobuf/proto" -type Serializable interface { +type ProtocolBufferTarget interface { ToProto() (result proto.Message) FromProto(proto proto.Message) } diff --git a/packages/marshaling/unmarshal.go b/packages/marshaling/unmarshal.go index e03f1757489bde2fccc8c0232101a6696d6c9d4e..fd2d69aaef2b5b83dcc29ea22cf47aa61353a273 100644 --- a/packages/marshaling/unmarshal.go +++ b/packages/marshaling/unmarshal.go @@ -5,11 +5,12 @@ import ( "github.com/iotaledger/goshimmer/packages/errors" ) -func Unmarshal(serializable Serializable, protobuf proto.Message, data []byte) (err errors.IdentifiableError) { - if unmarshalError := proto.Unmarshal(data, protobuf); unmarshalError != nil { +// Unmarshals the given data into the target using the given protobuf type. +func Unmarshal(target ProtocolBufferTarget, data []byte, messageType proto.Message) (err errors.IdentifiableError) { + if unmarshalError := proto.Unmarshal(data, messageType); unmarshalError != nil { err = ErrUnmarshalFailed.Derive(unmarshalError, "unmarshal failed") } else { - serializable.FromProto(protobuf) + target.FromProto(messageType) } return