diff --git a/pkg/postage/batch.go b/pkg/postage/batch.go index 073df452dd4..c1c9384da0f 100644 --- a/pkg/postage/batch.go +++ b/pkg/postage/batch.go @@ -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 } @@ -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 } diff --git a/pkg/postage/batch_test.go b/pkg/postage/batch_test.go index 617041a14dd..d8c06ff9629 100644 --- a/pkg/postage/batch_test.go +++ b/pkg/postage/batch_test.go @@ -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{} @@ -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()) - } } diff --git a/pkg/postage/batchservice/batchservice.go b/pkg/postage/batchservice/batchservice.go index ad33bccf37b..3705f562d4b 100644 --- a/pkg/postage/batchservice/batchservice.go +++ b/pkg/postage/batchservice/batchservice.go @@ -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) @@ -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 { @@ -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 { diff --git a/pkg/postage/batchservice/batchservice_test.go b/pkg/postage/batchservice/batchservice_test.go index 30d98820da6..f2c7934f440 100644 --- a/pkg/postage/batchservice/batchservice_test.go +++ b/pkg/postage/batchservice/batchservice_test.go @@ -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") @@ -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) @@ -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) { @@ -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") } }) @@ -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") } }) @@ -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) } @@ -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()) - } }) } @@ -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()) - } }) } diff --git a/pkg/postage/interface.go b/pkg/postage/interface.go index 79f313565e5..ac092de2b25 100644 --- a/pkg/postage/interface.go +++ b/pkg/postage/interface.go @@ -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 } diff --git a/pkg/postage/testing/batch.go b/pkg/postage/testing/batch.go index 1676151a221..51bf5b07566 100644 --- a/pkg/postage/testing/batch.go +++ b/pkg/postage/testing/batch.go @@ -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 { @@ -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) - } }