Skip to content

Commit

Permalink
exp/lighthorizon: Add initial support for XDR serialization (#4369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic authored May 5, 2022
1 parent ebf37a7 commit 48e51e9
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 46 deletions.
24 changes: 24 additions & 0 deletions exp/lighthorizon/index/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
XDRS = xdr/LightHorizon-types.x

XDRGEN_COMMIT=3f6808cd161d72474ffbe9eedbd7013de7f92748

.PHONY: xdr clean update

xdr/xdr_generated.go: $(XDRS)
docker run -it --rm -v $$PWD:/wd -w /wd ruby /bin/bash -c '\
gem install specific_install -v 0.3.7 && \
gem specific_install https://github.com/stellar/xdrgen.git -b $(XDRGEN_COMMIT) && \
xdrgen \
--language go \
--namespace xdr \
--output xdr/ \
$(XDRS)'
ls -lAh
go fmt $@

xdr: xdr/xdr_generated.go

clean:
rm ./xdr/xdr_generated.go || true

update: clean xdr
2 changes: 1 addition & 1 deletion exp/lighthorizon/index/cmd/batch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "github.com/stellar/go/support/log"

func main() {
log.SetLevel(log.InfoLevel)
Map()
// Map()
}
2 changes: 1 addition & 1 deletion exp/lighthorizon/index/cmd/batch/reduce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func main() {
if err != nil {
// TODO: in final version this should be critical error, now just skip it
if err == os.ErrNotExist {
log.Errorf("Account %d is unavailable - TODO fix", account)
log.Errorf("Account %s is unavailable - TODO fix", account)
continue
}
panic(err)
Expand Down
1 change: 0 additions & 1 deletion exp/lighthorizon/index/cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func main() {
)
}


if strings.Contains(*modules, "accounts") {
allParticipants, err := participantsForOperations(tx, false)
if err != nil {
Expand Down
60 changes: 18 additions & 42 deletions exp/lighthorizon/index/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package index

import (
"bufio"
"bytes"
"io"
"strconv"
"sync"

"github.com/stellar/go/exp/lighthorizon/index/xdr"
)

const CheckpointIndexVersion = 1
Expand All @@ -18,44 +18,16 @@ type CheckpointIndex struct {
}

func NewCheckpointIndexFromBytes(b []byte) (*CheckpointIndex, error) {
buf := bytes.NewBuffer(b)
r := bufio.NewReader(buf)

firstCheckpointString, err := r.ReadString(0x00)
if err != nil {
return nil, err
}

// Remove trailing 0x00 byte
firstCheckpointString = firstCheckpointString[:len(firstCheckpointString)-1]

firstCheckpoint, err := strconv.ParseUint(firstCheckpointString, 10, 32)
if err != nil {
return nil, err
}

lastCheckpointString, err := r.ReadString(0x00)
if err != nil {
return nil, err
}

// Remove trailing 0x00 byte
lastCheckpointString = lastCheckpointString[:len(lastCheckpointString)-1]

lastCheckpoint, err := strconv.ParseUint(lastCheckpointString, 10, 32)
if err != nil {
return nil, err
}

bitmap, err := io.ReadAll(r)
xdrCheckpoint := xdr.CheckpointIndex{}
err := xdrCheckpoint.UnmarshalBinary(b)
if err != nil {
return nil, err
}

return &CheckpointIndex{
bitmap: bitmap,
firstCheckpoint: uint32(firstCheckpoint),
lastCheckpoint: uint32(lastCheckpoint),
bitmap: xdrCheckpoint.Bitmap,
firstCheckpoint: uint32(xdrCheckpoint.FirstCheckpoint),
lastCheckpoint: uint32(xdrCheckpoint.LastCheckpoint),
}, nil
}

Expand Down Expand Up @@ -243,13 +215,17 @@ func (i *CheckpointIndex) Buffer() *bytes.Buffer {
i.mutex.RLock()
defer i.mutex.RUnlock()

var b bytes.Buffer
b.WriteString(strconv.FormatUint(uint64(i.firstCheckpoint), 10))
b.WriteByte(0)
b.WriteString(strconv.FormatUint(uint64(i.lastCheckpoint), 10))
b.WriteByte(0)
b.Write(i.bitmap)
return &b
xdrCheckpoint := xdr.CheckpointIndex{
FirstCheckpoint: xdr.Uint32(i.firstCheckpoint),
LastCheckpoint: xdr.Uint32(i.lastCheckpoint),
Bitmap: i.bitmap,
}

b, err := xdrCheckpoint.MarshalBinary()
if err != nil {
panic(err)
}
return bytes.NewBuffer(b)
}

// Flush flushes the index data to byte slice in index format.
Expand Down
17 changes: 17 additions & 0 deletions exp/lighthorizon/index/xdr/LightHorizon-types.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

namespace stellar
{

typedef unsigned int uint32;
typedef opaque Value<>;

struct CheckpointIndex {
uint32 firstCheckpoint;
uint32 lastCheckpoint;
Value bitmap;
};

}
Loading

0 comments on commit 48e51e9

Please sign in to comment.