Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batchstore: reserve #1262

Merged
merged 8 commits into from
Mar 31, 2021
Merged

batchstore: reserve #1262

merged 8 commits into from
Mar 31, 2021

Conversation

zelig
Copy link
Member

@zelig zelig commented Feb 16, 2021

This PR implements the reserve logic for the batchstore.
it is soon to described in AP https://hackmd.io/9ejVZEvSQg69zXG91YsFEQ
complete together with localstore reserve logic

@zelig zelig self-assigned this Feb 16, 2021
@acud
Copy link
Member

acud commented Feb 23, 2021

is this ready for review?

@zelig zelig added the ready for review The PR is ready to be reviewed label Feb 24, 2021
pkg/postage/batchservice/batchservice_test.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/export_test.go Show resolved Hide resolved
})
}

//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can expose a method on store just for the sake for the tests:
func (s *store) GetReserve() (*bit.Int, uint8) { return s.rs.Core, s.rs.Depth}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i cannot use type *store in the test if i go proper package batchstore_test can only use type postage.Storer, so I need to define these this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve_test.go Show resolved Hide resolved
pkg/postage/batchstore/store.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/store.go Show resolved Hide resolved
pkg/postage/batchstore/store.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/store.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/store.go Show resolved Hide resolved
Copy link
Member

@ralph-pichler ralph-pichler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good for the most part. don't fully understand everything in reserve.go yet. will take a look at it again tomorrow.

pkg/postage/batchstore/store.go Outdated Show resolved Hide resolved
pkg/postage/batchservice/batchservice.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
@zelig zelig force-pushed the postage-batchstore-reserve branch 2 times, most recently from 41f4502 to fab6980 Compare March 13, 2021 07:36
@zelig zelig requested a review from acud March 14, 2021 09:26
@zelig zelig force-pushed the postage-batchstore-reserve branch from fab6980 to 88f49a2 Compare March 14, 2021 13:24
pkg/node/node.go Show resolved Hide resolved
pkg/postage/batchservice/batchservice_test.go Show resolved Hide resolved
"github.com/ethersphere/bee/pkg/postage"
)

// ChainStateKey is the localstore key for the chain state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localstore? also, name of variable is wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sry yes state store

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

pkg/postage/batchstore/export_test.go Show resolved Hide resolved
pkg/postage/batchstore/export_test.go Show resolved Hide resolved
n := rand.Intn(len(batches))
b, err = bStore.Get(batches[n])
if err != nil {
if errors.Is(storage.ErrNotFound, err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other way around. errors.Is(err, storage.ErrNotFound). Is this actually hit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

t.Helper()
// we cannot use the mock statestore here since the iterator is not giving the right order
// must use the leveldb statestore
dir, err := ioutil.TempDir("", "batchstore_test")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use t.TempDir

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

}

func nextChainState(bStore postage.Storer) (*postage.ChainState, error) {
cs := bStore.GetChainState()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use t.Helper() and fail directly in the helper, it will make the tests more readable

// the following assumptions are tested on each modification of the batches (top up, depth increase, price change)
// - reserve exceeds capacity
// - value-consistency of unreserved POs
func checkReserve(bStore postage.Storer, unreserved map[string]uint8) (uint8, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

"math/big"

"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/storage"
)

const (
batchKeyPrefix = "batchKeyPrefix"
stateKey = "stateKey"
batchKeyPrefix = "batch"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very generic key names that might conflict with others. can you add a bit more text here?

Copy link
Member

@ralph-pichler ralph-pichler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better. I do find the code to be quite tricky to understand though. More high-level documentation at the beginning might be useful, especially one that uses the same terminology as the code below (e.g. the docs at reserve.go don't even mention inner / outer, what properties should hold about them, etc.). I think this would also be quite useful to the auditors in the future.

if err != nil {
return fmt.Errorf("put: %w", err)
}

svc.logger.Debugf("topped up batch id %s with %v", hex.EncodeToString(b.ID), b.Value)
svc.logger.Debugf("topped up batch id %s from %v to %v", hex.EncodeToString(b.ID), b.Value, normalisedBalance)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this not be %d instead if %v?

if b.Value.Cmp(until) >= 0 {
return true, nil
}
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comments here

pkg/postage/batchstore/reserve.go Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Outdated Show resolved Hide resolved
pkg/postage/batchstore/reserve.go Show resolved Hide resolved
@acud acud force-pushed the postage-batchstore-reserve branch from 02e5ea3 to 261602d Compare March 26, 2021 15:01
@acud acud force-pushed the postage-batchstore-reserve branch from 261602d to a572579 Compare March 26, 2021 15:57
Copy link
Member Author

@zelig zelig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM cannot approve

"github.com/ethersphere/bee/pkg/postage"
)

// ChainStateKey is the localstore key for the chain state.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

t.Helper()
// we cannot use the mock statestore here since the iterator is not giving the right order
// must use the leveldb statestore
dir, err := ioutil.TempDir("", "batchstore_test")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

return depthValueTuple{depth: d, value: v}
}

type batchValueTuple struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just remove 'Tuple' fromall names

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that would force me to rename the function calls to create the instances to be renamed in a different way, since the same name of the type cannot be redeclared as a function (that's the reason why i use this naming convention)

"math/big"

"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/storage"
)

const (
batchKeyPrefix = "batchKeyPrefix"
stateKey = "stateKey"
batchKeyPrefix = "batch"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should still be changed before merge, prefix with postage_, batchstore_ or something

}
// if multiplier == 0 && batch value >= inner
if multiplier == 0 && b.Value.Cmp(s.rs.Inner) >= 0 {
multiplier = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section still would benefit from more comments. Explain why are we setting the multiplier to that.

if err != nil {
return err
}
// set inner/outer to total if total is greater
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not correct. It either keeps inner the same (if inner == 0 || inner > total) or sets it to total+1 otherwise.

@acud acud merged commit 341242a into storage-incentives Mar 31, 2021
@acud acud deleted the postage-batchstore-reserve branch March 31, 2021 10:09
@acud acud restored the postage-batchstore-reserve branch April 8, 2021 16:59
acud added a commit that referenced this pull request Apr 13, 2021
postage: new pkg for postage stamps, uploader stamping (#890)

* postage: new pkg for postage stamps, uploader stamping

* postage: amount->value, blockNumber big.Int-> uint64, stamp only has batch ID, not Batch

* postage: fix godoc and copyright

* postage, swarm:
 - swarm.Stamp as an interface
 - add postage/testing for mock Stamps
 - fix Stamp MarshalBinary to allow nil batch id and signature
 - add StampSize const

* postage: heed review feedback

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

Storage incentives: add stamper putter to api

Postage BatchStore and BatchService (#1070)

Co-authored-by: zelig <viktor.tron@gmail.com>

add normalisedBalance to updater interface (#1108)

make value the normalised balance (#1111)

postage: add event listener (#1099)

Wire up postage stamp syncing (#1114)

localstore, shed: persist stamps (#1116)

add --postage to beeinfra.sh setup

pullsync, pushsync: add postage stamps (#1117)

postage: add create endpoint (#1142)

retrieve erc20 address from postage contract (#1169)

postage: check balance before attempting stamp creation (#1177)

postage: fix bucket depth (#1178)

api: use hex encoding in postage api (#1179)

increase page size (#1182)

postage: handle bucket depth error in api (#1183)

localstore: attach stamp to outgoing chunk (#1192)

update postage stamp contract addresses for new token (#1208)

batchstore: reserve (#1262)

* postage/batchstore: reserve logic

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

stamp support in storage and protocols (#1321)

api: endpoints for stamp issuers (#1535)

retrieval: add stamps (#1552)

localstore reserve logic (#1322)

Co-authored-by: acud <12988138+acud@users.noreply.github.com>
acud added a commit that referenced this pull request Apr 15, 2021
postage: new pkg for postage stamps, uploader stamping (#890)

* postage: new pkg for postage stamps, uploader stamping

* postage: amount->value, blockNumber big.Int-> uint64, stamp only has batch ID, not Batch

* postage: fix godoc and copyright

* postage, swarm:
 - swarm.Stamp as an interface
 - add postage/testing for mock Stamps
 - fix Stamp MarshalBinary to allow nil batch id and signature
 - add StampSize const

* postage: heed review feedback

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

Storage incentives: add stamper putter to api

Postage BatchStore and BatchService (#1070)

Co-authored-by: zelig <viktor.tron@gmail.com>

add normalisedBalance to updater interface (#1108)

make value the normalised balance (#1111)

postage: add event listener (#1099)

Wire up postage stamp syncing (#1114)

localstore, shed: persist stamps (#1116)

add --postage to beeinfra.sh setup

pullsync, pushsync: add postage stamps (#1117)

postage: add create endpoint (#1142)

retrieve erc20 address from postage contract (#1169)

postage: check balance before attempting stamp creation (#1177)

postage: fix bucket depth (#1178)

api: use hex encoding in postage api (#1179)

increase page size (#1182)

postage: handle bucket depth error in api (#1183)

localstore: attach stamp to outgoing chunk (#1192)

update postage stamp contract addresses for new token (#1208)

batchstore: reserve (#1262)

* postage/batchstore: reserve logic

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

stamp support in storage and protocols (#1321)

api: endpoints for stamp issuers (#1535)

retrieval: add stamps (#1552)

localstore reserve logic (#1322)

Co-authored-by: acud <12988138+acud@users.noreply.github.com>
acud added a commit that referenced this pull request Apr 26, 2021
postage: new pkg for postage stamps, uploader stamping (#890)

* postage: new pkg for postage stamps, uploader stamping

* postage: amount->value, blockNumber big.Int-> uint64, stamp only has batch ID, not Batch

* postage: fix godoc and copyright

* postage, swarm:
 - swarm.Stamp as an interface
 - add postage/testing for mock Stamps
 - fix Stamp MarshalBinary to allow nil batch id and signature
 - add StampSize const

* postage: heed review feedback

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

Storage incentives: add stamper putter to api

Postage BatchStore and BatchService (#1070)

Co-authored-by: zelig <viktor.tron@gmail.com>

add normalisedBalance to updater interface (#1108)

make value the normalised balance (#1111)

postage: add event listener (#1099)

Wire up postage stamp syncing (#1114)

localstore, shed: persist stamps (#1116)

add --postage to beeinfra.sh setup

pullsync, pushsync: add postage stamps (#1117)

postage: add create endpoint (#1142)

retrieve erc20 address from postage contract (#1169)

postage: check balance before attempting stamp creation (#1177)

postage: fix bucket depth (#1178)

api: use hex encoding in postage api (#1179)

increase page size (#1182)

postage: handle bucket depth error in api (#1183)

localstore: attach stamp to outgoing chunk (#1192)

update postage stamp contract addresses for new token (#1208)

batchstore: reserve (#1262)

* postage/batchstore: reserve logic

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

stamp support in storage and protocols (#1321)

api: endpoints for stamp issuers (#1535)

retrieval: add stamps (#1552)

localstore reserve logic (#1322)

Co-authored-by: acud <12988138+acud@users.noreply.github.com>
acud added a commit that referenced this pull request Apr 27, 2021
postage: new pkg for postage stamps, uploader stamping (#890)

* postage: new pkg for postage stamps, uploader stamping

* postage: amount->value, blockNumber big.Int-> uint64, stamp only has batch ID, not Batch

* postage: fix godoc and copyright

* postage, swarm:
 - swarm.Stamp as an interface
 - add postage/testing for mock Stamps
 - fix Stamp MarshalBinary to allow nil batch id and signature
 - add StampSize const

* postage: heed review feedback

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

Storage incentives: add stamper putter to api

Postage BatchStore and BatchService (#1070)

Co-authored-by: zelig <viktor.tron@gmail.com>

add normalisedBalance to updater interface (#1108)

make value the normalised balance (#1111)

postage: add event listener (#1099)

Wire up postage stamp syncing (#1114)

localstore, shed: persist stamps (#1116)

add --postage to beeinfra.sh setup

pullsync, pushsync: add postage stamps (#1117)

postage: add create endpoint (#1142)

retrieve erc20 address from postage contract (#1169)

postage: check balance before attempting stamp creation (#1177)

postage: fix bucket depth (#1178)

api: use hex encoding in postage api (#1179)

increase page size (#1182)

postage: handle bucket depth error in api (#1183)

localstore: attach stamp to outgoing chunk (#1192)

update postage stamp contract addresses for new token (#1208)

batchstore: reserve (#1262)

* postage/batchstore: reserve logic

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

stamp support in storage and protocols (#1321)

api: endpoints for stamp issuers (#1535)

retrieval: add stamps (#1552)

localstore reserve logic (#1322)

Co-authored-by: acud <12988138+acud@users.noreply.github.com>
acud added a commit that referenced this pull request Apr 27, 2021
postage: new pkg for postage stamps, uploader stamping (#890)

* postage: new pkg for postage stamps, uploader stamping

* postage: amount->value, blockNumber big.Int-> uint64, stamp only has batch ID, not Batch

* postage: fix godoc and copyright

* postage, swarm:
 - swarm.Stamp as an interface
 - add postage/testing for mock Stamps
 - fix Stamp MarshalBinary to allow nil batch id and signature
 - add StampSize const

* postage: heed review feedback

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

Storage incentives: add stamper putter to api

Postage BatchStore and BatchService (#1070)

Co-authored-by: zelig <viktor.tron@gmail.com>

add normalisedBalance to updater interface (#1108)

make value the normalised balance (#1111)

postage: add event listener (#1099)

Wire up postage stamp syncing (#1114)

localstore, shed: persist stamps (#1116)

add --postage to beeinfra.sh setup

pullsync, pushsync: add postage stamps (#1117)

postage: add create endpoint (#1142)

retrieve erc20 address from postage contract (#1169)

postage: check balance before attempting stamp creation (#1177)

postage: fix bucket depth (#1178)

api: use hex encoding in postage api (#1179)

increase page size (#1182)

postage: handle bucket depth error in api (#1183)

localstore: attach stamp to outgoing chunk (#1192)

update postage stamp contract addresses for new token (#1208)

batchstore: reserve (#1262)

* postage/batchstore: reserve logic

Co-authored-by: acud <12988138+acud@users.noreply.github.com>

stamp support in storage and protocols (#1321)

api: endpoints for stamp issuers (#1535)

retrieval: add stamps (#1552)

localstore reserve logic (#1322)

Co-authored-by: acud <12988138+acud@users.noreply.github.com>
@acud acud deleted the postage-batchstore-reserve branch June 19, 2021 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull-request ready for review The PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants