Commit 8ad6ab9c authored by Rusty Russell's avatar Rusty Russell

shachain: add shachain_next_index()

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 2a03b7b3
...@@ -66,10 +66,16 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index, ...@@ -66,10 +66,16 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
derive(0, index, seed, hash); derive(0, index, seed, hash);
} }
shachain_index_t shachain_next_index(const struct shachain *chain)
{
return chain->min_index - 1;
}
void shachain_init(struct shachain *chain) void shachain_init(struct shachain *chain)
{ {
chain->num_valid = 0; chain->num_valid = 0;
chain->min_index = 0; /* This is 0 in the case where SHACHAIN_BITS is 64. */
chain->min_index = (shachain_index_t)((UINT64_MAX >> (64 - SHACHAIN_BITS)) + 1);
} }
bool shachain_add_hash(struct shachain *chain, bool shachain_add_hash(struct shachain *chain,
...@@ -78,9 +84,7 @@ bool shachain_add_hash(struct shachain *chain, ...@@ -78,9 +84,7 @@ bool shachain_add_hash(struct shachain *chain,
unsigned int i, pos; unsigned int i, pos;
/* You have to insert them in order! */ /* You have to insert them in order! */
assert(index == chain->min_index - 1 || assert(index == shachain_next_index(chain));
(index == (shachain_index_t)(UINT64_MAX >> (64 - SHACHAIN_BITS))
&& chain->num_valid == 0));
pos = count_trailing_zeroes(index); pos = count_trailing_zeroes(index);
......
...@@ -71,20 +71,28 @@ struct shachain { ...@@ -71,20 +71,28 @@ struct shachain {
*/ */
void shachain_init(struct shachain *chain); void shachain_init(struct shachain *chain);
/**
* shachain_next_index - what's the next index I can add to the shachain?
* @chain: the chain
*
* This returns 0xFFFFFFFFFFFFFFFF (for a freshly
* initialized chain), or one less than the previously successfully
* added value.
*/
shachain_index_t shachain_next_index(const struct shachain *chain);
/** /**
* shachain_add_hash - record the hash for the next index. * shachain_add_hash - record the hash for the next index.
* @chain: the chain to add to * @chain: the chain to add to
* @index: the index of the hash * @index: the index of the hash
* @hash: the hash value. * @hash: the hash value.
* *
* You can only add index 0xFFFFFFFFFFFFFFFF (for a freshly * You can only add shachain_next_index(@chain).
* initialized chain), or one less than the previously successfully
* added value.
* *
* This can fail (return false without altering @chain) if the hash * This can fail (return false without altering @chain) if the hash
* for this index isn't consistent with previous hashes (ie. wasn't * for this index isn't consistent with previous hashes (ie. wasn't
* generated from the same seed), though it can't always detect that. * generated from the same seed), though it can't always detect that.
* If the hash is inconsistent yet undetected, the next addition will * If the hash is inconsistent yet undetected, a future addition will
* fail. * fail.
* *
* Example: * Example:
......
...@@ -13,7 +13,7 @@ int main(void) ...@@ -13,7 +13,7 @@ int main(void)
uint64_t i, j; uint64_t i, j;
/* This is how many tests you plan to run */ /* This is how many tests you plan to run */
plan_tests(NUM_TESTS * 3 + NUM_TESTS * (NUM_TESTS + 1) - 1); plan_tests(NUM_TESTS * 4 + NUM_TESTS * (NUM_TESTS + 1) - 1);
memset(&seed, 0, sizeof(seed)); memset(&seed, 0, sizeof(seed));
/* Generate a whole heap. */ /* Generate a whole heap. */
...@@ -34,6 +34,7 @@ int main(void) ...@@ -34,6 +34,7 @@ int main(void)
i--) { i--) {
struct sha256 hash; struct sha256 hash;
int expidx = 0xFFFFFFFFFFFFFFFFULL - i; int expidx = 0xFFFFFFFFFFFFFFFFULL - i;
ok1(shachain_next_index(&chain) == i);
ok1(shachain_add_hash(&chain, i, &expect[expidx])); ok1(shachain_add_hash(&chain, i, &expect[expidx]));
for (j = i; j != 0; j++) { for (j = i; j != 0; j++) {
ok1(shachain_get_hash(&chain, j, &hash)); ok1(shachain_get_hash(&chain, j, &hash));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment