diff --git a/channeldb/channel.go b/channeldb/channel.go index 2df4fcb8..5c8575b6 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -1561,19 +1561,11 @@ func putChanPreimageState(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err // TODO(roasbeef): shouldn't be storing on disk, should re-derive as // needed - data, err := channel.RevocationProducer.ToBytes() - if err != nil { - return err - } - if err := wire.WriteVarBytes(&b, 0, data); err != nil { + if err := channel.RevocationProducer.Encode(&b); err != nil { return err } - data, err = channel.RevocationStore.ToBytes() - if err != nil { - return err - } - if err := wire.WriteVarBytes(&b, 0, data); err != nil { + if err := channel.RevocationStore.Encode(&b); err != nil { return err } @@ -1624,20 +1616,16 @@ func fetchChanPreimageState(nodeChanBucket *bolt.Bucket, channel *OpenChannel) e } // TODO(roasbeef): should be rederiving on fly, or encrypting on disk. - producerBytes, err := wire.ReadVarBytes(reader, 0, 1000, "") - if err != nil { + var root [32]byte + if _, err := io.ReadFull(reader, root[:]); err != nil { return err } - channel.RevocationProducer, err = shachain.NewRevocationProducerFromBytes(producerBytes) + channel.RevocationProducer, err = shachain.NewRevocationProducerFromBytes(root[:]) if err != nil { return err } - storeBytes, err := wire.ReadVarBytes(reader, 0, 1000, "") - if err != nil { - return err - } - channel.RevocationStore, err = shachain.NewRevocationStoreFromBytes(storeBytes) + channel.RevocationStore, err = shachain.NewRevocationStoreFromBytes(reader) if err != nil { return err } diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index b7893bba..d1ab5960 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -119,7 +119,10 @@ func createTestChannelState(cdb *DB) (*OpenChannel, error) { } // Simulate 1000 channel updates. - producer := shachain.NewRevocationProducer((*chainhash.Hash)(&key)) + producer, err := shachain.NewRevocationProducerFromBytes(key[:]) + if err != nil { + return nil, err + } store := shachain.NewRevocationStore() for i := 0; i < 1000; i++ { preImage, err := producer.AtIndex(uint64(i)) @@ -127,7 +130,7 @@ func createTestChannelState(cdb *DB) (*OpenChannel, error) { return nil, err } - if store.Store(preImage); err != nil { + if store.AddNextEntry(preImage); err != nil { return nil, err } } @@ -318,30 +321,36 @@ func TestOpenChannelPutGetDelete(t *testing.T) { } // The local and remote producers should be identical. - oldProducer, err := state.RevocationProducer.ToBytes() + var old bytes.Buffer + err = state.RevocationProducer.Encode(&old) if err != nil { t.Fatalf("can't convert old revocation producer to bytes: %v", err) } - newProducer, err := newState.RevocationProducer.ToBytes() + var new bytes.Buffer + err = newState.RevocationProducer.Encode(&new) if err != nil { t.Fatalf("can't convert new revocation producer to bytes: %v", err) } - if !bytes.Equal(oldProducer, newProducer) { + if !bytes.Equal(old.Bytes(), new.Bytes()) { t.Fatal("local producer don't match") } - oldStore, err := state.RevocationStore.ToBytes() + + old.Reset() + new.Reset() + + err = state.RevocationStore.Encode(&old) if err != nil { t.Fatalf("unable to serialize old remote store: %v", err) } - newStore, err := newState.RevocationStore.ToBytes() + err = newState.RevocationStore.Encode(&new) if err != nil { t.Fatalf("unable to serialize new remote store: %v", err) } - if !bytes.Equal(oldStore, newStore) { + if !bytes.Equal(old.Bytes(), new.Bytes()) { t.Fatal("remote store don't match") } if !newState.TheirCurrentRevocation.IsEqual(state.TheirCurrentRevocation) { diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 21b6a41b..f368c589 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1658,7 +1658,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ([]*P // Ensure that the new pre-image can be placed in preimage store. // TODO(rosbeef): abstract into func store := lc.channelState.RevocationStore - if err := store.Store(&pendingRevocation); err != nil { + if err := store.AddNextEntry(&pendingRevocation); err != nil { return nil, err } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index eca2b7c7..9febf345 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -203,7 +203,7 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan fundingTxIn := wire.NewTxIn(prevOut, nil, nil) bobRoot := deriveRevocationRoot(bobKeyPriv, bobKeyPub, aliceKeyPub) - bobPreimageProducer := shachain.NewRevocationProducer(bobRoot) + bobPreimageProducer := shachain.NewRevocationProducer(*bobRoot) bobFirstRevoke, err := bobPreimageProducer.AtIndex(0) if err != nil { return nil, nil, nil, err @@ -211,7 +211,7 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan bobRevokeKey := DeriveRevocationPubkey(aliceKeyPub, bobFirstRevoke[:]) aliceRoot := deriveRevocationRoot(aliceKeyPriv, aliceKeyPub, bobKeyPub) - alicePreimageProducer := shachain.NewRevocationProducer(aliceRoot) + alicePreimageProducer := shachain.NewRevocationProducer(*aliceRoot) aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0) if err != nil { return nil, nil, nil, err diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 8f255bbd..dfac64d2 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -762,7 +762,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) { // key for the first version of our commitment transaction. To do so, // we'll first create our root, then produce the first pre-image. root := deriveRevocationRoot(masterElkremRoot, ourKey, theirKey) - producer := shachain.NewRevocationProducer(root) + producer := shachain.NewRevocationProducer(*root) pendingReservation.partialState.RevocationProducer = producer firstPreimage, err := producer.AtIndex(0) if err != nil { @@ -901,7 +901,7 @@ func (l *LightningWallet) handleSingleContribution(req *addSingleContributionMsg // Now that we know their commitment key, we can create the revocation // key for our version of the initial commitment transaction. root := deriveRevocationRoot(masterElkremRoot, ourKey, theirKey) - producer := shachain.NewRevocationProducer(root) + producer := shachain.NewRevocationProducer(*root) firstPreimage, err := producer.AtIndex(0) if err != nil { req.err <- err