-
Notifications
You must be signed in to change notification settings - Fork 338
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
batchstore: reserve #1262
Conversation
59780e7
to
bb01ce9
Compare
is this ready for review? |
bb01ce9
to
059b920
Compare
}) | ||
} | ||
|
||
// |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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.
41f4502
to
fab6980
Compare
fab6980
to
88f49a2
Compare
88f49a2
to
f652df3
Compare
"github.com/ethersphere/bee/pkg/postage" | ||
) | ||
|
||
// ChainStateKey is the localstore key for the chain state. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry yes state store
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
n := rand.Intn(len(batches)) | ||
b, err = bStore.Get(batches[n]) | ||
if err != nil { | ||
if errors.Is(storage.ErrNotFound, err) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use t.TempDir
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
pkg/postage/batchstore/store.go
Outdated
"math/big" | ||
|
||
"github.com/ethersphere/bee/pkg/postage" | ||
"github.com/ethersphere/bee/pkg/storage" | ||
) | ||
|
||
const ( | ||
batchKeyPrefix = "batchKeyPrefix" | ||
stateKey = "stateKey" | ||
batchKeyPrefix = "batch" |
There was a problem hiding this comment.
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?
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
?
pkg/postage/batchstore/reserve.go
Outdated
if b.Value.Cmp(until) >= 0 { | ||
return true, nil | ||
} | ||
// |
There was a problem hiding this comment.
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
02e5ea3
to
261602d
Compare
261602d
to
a572579
Compare
There was a problem hiding this 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. |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
pkg/postage/batchstore/store.go
Outdated
"math/big" | ||
|
||
"github.com/ethersphere/bee/pkg/postage" | ||
"github.com/ethersphere/bee/pkg/storage" | ||
) | ||
|
||
const ( | ||
batchKeyPrefix = "batchKeyPrefix" | ||
stateKey = "stateKey" | ||
batchKeyPrefix = "batch" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
pkg/postage/batchstore/reserve.go
Outdated
if err != nil { | ||
return err | ||
} | ||
// set inner/outer to total if total is greater |
There was a problem hiding this comment.
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.
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>
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>
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>
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>
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>
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