Skip to content

Commit

Permalink
Added some new section handler for Ultra
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Vachon committed Aug 2, 2021
1 parent a6a5bd6 commit e096816
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
25 changes: 18 additions & 7 deletions snapshot/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type Reader struct {

filename string
fl *os.File
buf io.Reader
nextOffset uint64

handlers map[SectionName]sectionHandlerFunc
Expand Down Expand Up @@ -49,6 +48,10 @@ func NewDefaultReader(filename string) (r *Reader, err error) {
reader.RegisterSectionHandler(SectionNameResourceLimitsStateObject, readResourceLimitsStateObject)
reader.RegisterSectionHandler(SectionNameResourceLimitsConfigObject, readResourceLimitsConfigObject)
reader.RegisterSectionHandler(SectionNameGenesisState, readGenesisState)

// Ultra Specific
reader.RegisterSectionHandler(SectionAccountFreeActionsObject, readAccountFreeActionsObject)

return reader, nil
}

Expand All @@ -67,7 +70,7 @@ func NewReader(filename string) (r *Reader, err error) {
return nil, err
}

beginOffset, err := r.fl.Seek(0, os.SEEK_CUR)
beginOffset, err := r.fl.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +91,7 @@ func (r *Reader) readHeader() (*Header, error) {
return nil, err
}

if bytes.Compare(buf[:4], magicNumber) != 0 {
if !bytes.Equal(buf[:4], magicNumber) {
return nil, fmt.Errorf("invalid magic number (first 4 bytes): %v, expected %v", buf[:4], magicNumber)
}

Expand All @@ -99,19 +102,27 @@ func (r *Reader) readHeader() (*Header, error) {
return h, nil
}

var SectionHandlerNotFound = errors.New("section handler not found")
var ErrSectionHandlerNotFound = errors.New("section handler not found")

// Deprecated: Use ErrSectionHandlerNotFound instead
var SectionHandlerNotFound = ErrSectionHandlerNotFound

func (r *Reader) HasSectionHandler(s *Section) bool {
_, found := r.handlers[r.CurrentSection.Name]
return found
}

func (r *Reader) ProcessCurrentSection(f sectionCallbackFunc) error {
h, found := r.handlers[r.CurrentSection.Name]
if !found {
return SectionHandlerNotFound
return ErrSectionHandlerNotFound
}
return h(r.CurrentSection, f)
}

// Next retrieves the next section.
func (r *Reader) NextSection() error {
beginOffset, err := r.fl.Seek(int64(r.nextOffset), os.SEEK_SET)
beginOffset, err := r.fl.Seek(int64(r.nextOffset), io.SeekStart)
if err != nil {
return err
}
Expand All @@ -123,7 +134,7 @@ func (r *Reader) NextSection() error {
}

// end marker
if bytesRead == 8 && bytes.Compare(vals[:8], []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) == 0 {
if bytesRead == 8 && bytes.Equal(vals[:8], []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) {
return io.EOF
}

Expand Down
3 changes: 3 additions & 0 deletions snapshot/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const (
SectionNameResourceLimitsStateObject SectionName = "eosio::chain::resource_limits::resource_limits_state_object"
SectionNameResourceLimitsConfigObject SectionName = "eosio::chain::resource_limits::resource_limits_config_object"
SectionNameGenesisState SectionName = "eosio::chain::genesis_state"

// Ultra Specific
SectionAccountFreeActionsObject SectionName = "eosio::chain::account_free_actions_object"
)

type Section struct {
Expand Down
21 changes: 18 additions & 3 deletions snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestSnapshotRead(t *testing.T) {
testFile string
}{
{name: "Battlefield - b8d703ed1", testFile: "battlefield-snapshot.bin"},
{name: "Ultra - Testnet", testFile: "ultra-testnet-snapshot.bin"},
}

for _, test := range tests {
Expand Down Expand Up @@ -53,8 +54,9 @@ func TestSnapshotRead(t *testing.T) {
logger.Info("new section",
zap.String("section_name", string(section.Name)),
zap.Uint64("row_count", section.RowCount),
zap.Uint64("bytes_count", section.BufferSize),
zap.Uint64("bytes_count", section.Offset),
zap.Uint64("buffer_size", section.BufferSize),
zap.Uint64("offset", section.Offset),
zap.Bool("has_handler", r.HasSectionHandler(section)),
)
switch section.Name {
case SectionNameAccountObject:
Expand Down Expand Up @@ -94,6 +96,19 @@ func TestSnapshotRead(t *testing.T) {
}
return nil
}))

case SectionAccountFreeActionsObject:
require.NoError(t, r.ProcessCurrentSection(func(o interface{}) error {
acc, ok := o.(AccountFreeActionsObject)
if !ok {
return fmt.Errorf("process account free actopms object: unexpected object type: %T", o)
}
logger.Info("new account free actions object",
zap.String("name", string(acc.Name)),
zap.Reflect("object", acc),
)
return nil
}))
}
}
})
Expand All @@ -114,5 +129,5 @@ func fileExists(path string) bool {
}

func testData(filename string) string {
return filepath.Join("test-data", filename)
return filepath.Join("testdata", filename)
}
File renamed without changes.
Binary file added snapshot/testdata/ultra-testnet-snapshot.bin
Binary file not shown.
38 changes: 38 additions & 0 deletions snapshot/typesv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,41 @@ func readTransactionObject(section *Section, f sectionCallbackFunc) error {

return nil
}

/// Ultra Specific

type FreeObjectUsage struct {
UserSize eos.Uint64
UserCount eos.Uint64
UltraSize eos.Uint64
UltraCount eos.Uint64
}

type AccountFreeActionsObject struct {
Name eos.AccountName
PermissionObject FreeObjectUsage
SharedKey FreeObjectUsage
PermissionLevel FreeObjectUsage
Wait FreeObjectUsage
PermissionLinkObject FreeObjectUsage
}

func readAccountFreeActionsObject(section *Section, f sectionCallbackFunc) error {
for i := uint64(0); i < section.RowCount; i++ {
a := AccountFreeActionsObject{}
cnt := make([]byte, 8+(8*4)*5) // fixed size of resource_limits_object
_, err := section.Buffer.Read(cnt)
if err != nil {
return err
}

if err := eos.UnmarshalBinary(cnt, &a); err != nil {
return err
}

if err := f(a); err != nil {
return err
}
}
return nil
}

0 comments on commit e096816

Please sign in to comment.