Skip to content

Commit

Permalink
make value the normalised balance (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler authored and acud committed Jan 27, 2021
1 parent 281d0fa commit 14257f2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 55 deletions.
16 changes: 6 additions & 10 deletions pkg/postage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ import (

// Batch represents a postage batch, a payment on the blockchain.
type Batch struct {
ID []byte // batch ID
Value *big.Int // overall balance of the batch
Start uint64 // block number the batch was created
Owner []byte // owner's ethereum address
Depth uint8 // batch depth, i.e., size = 2^{depth}
NormalisedBalance *big.Int // normalised balance of the batch
ID []byte // batch ID
Value *big.Int // normalised balance of the batch
Start uint64 // block number the batch was created
Owner []byte // owner's ethereum address
Depth uint8 // batch depth, i.e., size = 2^{depth}
}

// MarshalBinary implements BinaryMarshaller. It will attempt to serialize the
// postage batch to a byte slice.
func (b *Batch) MarshalBinary() ([]byte, error) {
out := make([]byte, 157)
out := make([]byte, 93)
copy(out, b.ID)
value := b.Value.Bytes()
copy(out[64-len(value):], value)
binary.BigEndian.PutUint64(out[64:72], b.Start)
copy(out[72:], b.Owner)
out[92] = b.Depth
normalisedBalance := b.NormalisedBalance.Bytes()
copy(out[157-len(normalisedBalance):], normalisedBalance)
return out, nil
}

Expand All @@ -42,6 +39,5 @@ func (b *Batch) UnmarshalBinary(buf []byte) error {
b.Start = binary.BigEndian.Uint64(buf[64:72])
b.Owner = buf[72:92]
b.Depth = buf[92]
b.NormalisedBalance = big.NewInt(0).SetBytes(buf[93:])
return nil
}
5 changes: 1 addition & 4 deletions pkg/postage/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestBatchMarshalling(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(buf) != 157 {
if len(buf) != 93 {
t.Fatalf("invalid length for serialised batch. expected 93, got %d", len(buf))
}
b := &postage.Batch{}
Expand All @@ -43,7 +43,4 @@ func TestBatchMarshalling(t *testing.T) {
if a.Depth != b.Depth {
t.Fatalf("depth mismatch, expected %d, got %d", a.Depth, b.Depth)
}
if a.NormalisedBalance.Uint64() != b.NormalisedBalance.Uint64() {
t.Fatalf("normalised balance mismatch, expected %d, got %d", a.NormalisedBalance.Uint64(), b.NormalisedBalance.Uint64())
}
}
20 changes: 9 additions & 11 deletions pkg/postage/batchservice/batchservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ func New(storer postage.Storer, logger logging.Logger) (postage.EventUpdater, er

// Create will create a new batch with the given ID, owner value and depth and
// stores it in the BatchStore.
func (svc *batchService) Create(id, owner []byte, value *big.Int, normalisedBalance *big.Int, depth uint8) error {
func (svc *batchService) Create(id, owner []byte, normalisedBalance *big.Int, depth uint8) error {
b := &postage.Batch{
ID: id,
Owner: owner,
Value: value,
Start: svc.cs.Block,
Depth: depth,
NormalisedBalance: normalisedBalance,
ID: id,
Owner: owner,
Value: normalisedBalance,
Start: svc.cs.Block,
Depth: depth,
}

err := svc.storer.Put(b)
Expand All @@ -58,14 +57,13 @@ func (svc *batchService) Create(id, owner []byte, value *big.Int, normalisedBala

// TopUp implements the EventUpdater interface. It tops ups a batch with the
// given ID with the given amount.
func (svc *batchService) TopUp(id []byte, amount *big.Int, normalisedBalance *big.Int) error {
func (svc *batchService) TopUp(id []byte, normalisedBalance *big.Int) error {
b, err := svc.storer.Get(id)
if err != nil {
return fmt.Errorf("get: %w", err)
}

b.Value.Add(b.Value, amount)
b.NormalisedBalance.Set(normalisedBalance)
b.Value.Set(normalisedBalance)

err = svc.storer.Put(b)
if err != nil {
Expand All @@ -85,7 +83,7 @@ func (svc *batchService) UpdateDepth(id []byte, depth uint8, normalisedBalance *
}

b.Depth = depth
b.NormalisedBalance.Set(normalisedBalance)
b.Value.Set(normalisedBalance)

err = svc.storer.Put(b)
if err != nil {
Expand Down
24 changes: 4 additions & 20 deletions pkg/postage/batchservice/batchservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestBatchServiceCreate(t *testing.T) {
testBatch.ID,
testBatch.Owner,
testBatch.Value,
testBatch.NormalisedBalance,
testBatch.Depth,
); err == nil {
t.Fatalf("expected error")
Expand All @@ -78,7 +77,6 @@ func TestBatchServiceCreate(t *testing.T) {
testBatch.ID,
testBatch.Owner,
testBatch.Value,
testBatch.NormalisedBalance,
testBatch.Depth,
); err != nil {
t.Fatalf("got error %v", err)
Expand All @@ -104,17 +102,12 @@ func TestBatchServiceCreate(t *testing.T) {
if got.Start != testChainState.Block {
t.Fatalf("batch start block different form chain state: want %v, got %v", got.Start, testChainState.Block)
}
if got.NormalisedBalance.Cmp(testBatch.NormalisedBalance) != 0 {
t.Fatalf("normalised batch value: want %v, got %v", testBatch.NormalisedBalance.String(), got.NormalisedBalance.String())
}

})

}

func TestBatchServiceTopUp(t *testing.T) {
testBatch := postagetesting.MustNewBatch()
testTopUpAmount := big.NewInt(10000000000000)
testNormalisedBalance := big.NewInt(2000000000000)

t.Run("expect get error", func(t *testing.T) {
Expand All @@ -124,7 +117,7 @@ func TestBatchServiceTopUp(t *testing.T) {
mock.WithGetErr(testErr, 1),
)

if err := svc.TopUp(testBatch.ID, testTopUpAmount, testNormalisedBalance); err == nil {
if err := svc.TopUp(testBatch.ID, testNormalisedBalance); err == nil {
t.Fatal("expected error")
}
})
Expand All @@ -136,7 +129,7 @@ func TestBatchServiceTopUp(t *testing.T) {
)
putBatch(t, batchStore, testBatch)

if err := svc.TopUp(testBatch.ID, testTopUpAmount, testNormalisedBalance); err == nil {
if err := svc.TopUp(testBatch.ID, testNormalisedBalance); err == nil {
t.Fatal("expected error")
}
})
Expand All @@ -145,10 +138,9 @@ func TestBatchServiceTopUp(t *testing.T) {
svc, batchStore := newTestStoreAndService(t)
putBatch(t, batchStore, testBatch)

want := testBatch.Value
want.Add(want, testTopUpAmount)
want := testNormalisedBalance

if err := svc.TopUp(testBatch.ID, testTopUpAmount, testNormalisedBalance); err != nil {
if err := svc.TopUp(testBatch.ID, testNormalisedBalance); err != nil {
t.Fatalf("top up: %v", err)
}

Expand All @@ -160,10 +152,6 @@ func TestBatchServiceTopUp(t *testing.T) {
if got.Value.Cmp(want) != 0 {
t.Fatalf("topped up amount: got %v, want %v", got.Value, want)
}

if got.NormalisedBalance.Cmp(testNormalisedBalance) != 0 {
t.Fatalf("normalised batch value: want %v, got %v", testNormalisedBalance.String(), got.NormalisedBalance.String())
}
})
}

Expand Down Expand Up @@ -211,10 +199,6 @@ func TestBatchServiceUpdateDepth(t *testing.T) {
if val.Depth != testNewDepth {
t.Fatalf("wrong batch depth set: want %v, got %v", testNewDepth, val.Depth)
}

if val.NormalisedBalance.Cmp(testNormalisedBalance) != 0 {
t.Fatalf("normalised batch value: want %v, got %v", testNormalisedBalance.String(), val.NormalisedBalance.String())
}
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/postage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// EventUpdater interface definitions reflect the updates triggered by events
// emitted by the postage contract on the blockchain.
type EventUpdater interface {
Create(id []byte, owner []byte, amount *big.Int, normalisedBalance *big.Int, depth uint8) error
TopUp(id []byte, amount *big.Int, normalisedBalance *big.Int) error
Create(id []byte, owner []byte, normalisedBalance *big.Int, depth uint8) error
TopUp(id []byte, normalisedBalance *big.Int) error
UpdateDepth(id []byte, depth uint8, normalisedBalance *big.Int) error
UpdatePrice(price *big.Int) error
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/postage/testing/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ func NewBigInt() *big.Int {
// be filled with random data. Panics on errors.
func MustNewBatch(opts ...BatchOption) *postage.Batch {
b := &postage.Batch{
ID: MustNewID(),
Value: NewBigInt(),
Start: rand.Uint64(),
Depth: defaultDepth,
NormalisedBalance: NewBigInt(),
ID: MustNewID(),
Value: NewBigInt(),
Start: rand.Uint64(),
Depth: defaultDepth,
}

for _, opt := range opts {
Expand Down Expand Up @@ -96,7 +95,4 @@ func CompareBatches(t *testing.T, want, got *postage.Batch) {
if want.Depth != got.Depth {
t.Fatalf("depth: want %v, got %v", want.Depth, got.Depth)
}
if want.NormalisedBalance.Cmp(got.NormalisedBalance) != 0 {
t.Fatalf("normalised balance: want %v, got %v", want.NormalisedBalance, got.NormalisedBalance)
}
}

0 comments on commit 14257f2

Please sign in to comment.